javascript
Truthy and Falsy in JavaScript
/*In JavaScript, truthy are expressions which evaluates to boolean true value and falsy evaluates to boolean false value. Unlike other languages, true and false values are not limited to boolean data types and comparisons. It can have many other forms. */
//Falsy Values
false
0 Both positive and negative
0n BigInt, when used as a boolean, follows the same rule as a Number
''
null
undefined
NaN
let a = false
let b = 0
let c = -0
let d = 0n
let e = ''
let f = null
let g = undefined
let h = NaN
console.log(Boolean (a))
console.log(Boolean (b))
console.log(Boolean (c))
console.log(Boolean (d))
console.log(Boolean (e))
console.log(Boolean (f))
console.log(Boolean (g))
console.log(Boolean (h))
//Truthy Values
true
{} An object (whether empty or not)
[] An array (whether empty or not)
25 Numbers (whether positive or negative)
'true' Non empty strings
'false' Non empty strings
new Date() Date object
12n BigInt, when used as a boolean, follows the same rule as a Number
Infinity
Was this helpful?
Similar Posts
- Javascript code to remove falsy value like null, undefined, false from array
- indexOf and lastIndexOf a string in javascript
- Get Top MAX and MIN itens inside array Javascript
- Javascript Map and Filter together
- Remove new line separator from start and end positions of string in Javascript
- Join array items and convert to string in javascript
- Join two arrays and remove duplicate elements using Javascript