Search This Blog

2023/07/26

Truthy & Falsy Values in javascript

 In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context.

below are truthy values.if evaluated in if clause it will return true.

true
{}
[]
42
"0"
"false"
new Date()
-42
12n
3.14
-3.14
Infinity
-Infinity

e.g.

if("0"){
console.log("True")
}
else{
console.log("False")
}
output:
True

A falsy (sometimes written falsey) value is a
value that is considered false when encountered in a Boolean context.

Following are falsy values in javascript

null
undefined
false
NaN
0
-0
0n
""

e.g.

if ("") {
console.log("True")
}else{
console.log("False")
}

output:
False

No comments:

Post a Comment