Truthy and Falsy value.

JavaScript Truthy and Falsy value explained.

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.