Truthy and Falsy value.

Vikas KumarVikas Kumar
2 min read

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.

0
Subscribe to my newsletter

Read articles from Vikas Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vikas Kumar
Vikas Kumar

As a Frontend Web Developer, I bring a unique blend of technical expertise and creative vision to design and implement robust web applications. My proficiency in core web technologies including HTML, CSS, and JavaScript, combined with hands-on experience in React and NodeJs, allows me to deliver high-performance, responsive, and intuitive user interfaces. I am also dedicated to continuous learning and community engagement, regularly publishing technical blogs to share insights and foster collaborative growth.