Truthy and Falsy value.
To understand the truthy and falsy values in Javascript first, let's understand what is Null, Undefined and NAN-
Undefined:
let user;
console.log(user);
It means value does not exist in compiler or accessing value that is not defined. it's value for initialize variables.
consider falsy in boolean.
Null:
Value is empty but not undefined it means that the object is empty and is not pointing to any memory address.
Null value represent the intentionally absence of any object value. It is one of the javascript primitive and treated as falsy for boolean operation.
funtion getval (str) {
const s = str.m.match(/[abi/gi]);
if (s === null) {
return 0;
}
return s.length;
}
console.log(get val('earth'));
Example-
typeof Null = object
typeof undefined = undefined
Null === undefined = false
Null == true
Null == Null = true
!Null = true
NAN:
The global NAN property is a value representing Not-A-Number .
Javascript return this value when number we are supposed to get is nor a number.
"five"
12/"25"
Note:
There are some situation in which you could expect to get this value but you won't
- When you are adding something to the string if javascript sees + sign and a string, it automatically converts second element of addition into string as well.
2000 + "miles"
//2000miles
true = "false"
//truefalse
Truthy & Falsy
false
undefined
null
0, -0
" "
NAN
Falsy value = falsy value is in Javascript built-in-type coercion convert to false. Variable that do not have value or do not exist but it's more then that..
let user = null; / ""
if (user) {
console.log("condition true");
}
//no output (falsy value)only true val run
coercion= ( === ), ( ?? )
Anything asides the falsy value are referred to truth values.
True - {}object,[ ]array, 26.
Subscribe to my newsletter
Read articles from Vikas Taliyan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vikas Taliyan
Vikas Taliyan
Hello, As a Frontend Web Developer(Fresher), I bring a unique blend of technical skills and creativity to the table. With an eye for detail and a passion for user experience, I am dedicated to creating visually stunning and engaging websites and applications. My skills in HTML, CSS, JavaScript, and various frontend frameworks such as React and Angular allow me to design and implement innovative solutions that meet the needs of modern businesses. I love reading and writing Technical blogs and helping out the community.