2.0 KiB
2.0 KiB
title |
---|
Falsy Values |
Description
A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsy values in JavaScript: undefined
, null
, NaN
, 0
, ""
or ''
(empty string), and false
of course.
Checking for falsy values on variables
It is possible to check for a falsy value in a variable with a simple conditional:
if (!variable) {
// When the variable has a falsy value the condition is true.
}
General Examples
var string = ""; // <-- falsy
var filledString = "some string in here"; // <-- truthy
var zero = 0; // <-- falsy
var numberGreaterThanZero; // <-- falsy
var emptyArray = []; // <-- truthy, we'll explore more about this next
var emptyObject = {}; // <-- truthy
Fun With Arrays
if ([] == false) // <-- truthy, will run code in if-block
if ([]) // <-- truthy, will also run code in if-block
if ([] == true) // <-- falsy, will NOT run code in if-block
if (![]) // <-- falsy, will also NOT run code in if-block
Caveat
Be aware of the data type when evaluating a value in a Boolean context. If the data type of the value is meant to be a number, the truthy/falsy evalution can result in an unexpected outcome:
const match = { teamA: 0, teamB: 1 }
if (match.teamA)
// The following won't run due to the falsy evaluation
console.log('Team A: ' + match.teamA);
}
An alternative to the use case above is to evaluate the value using typeof
:
const match = { teamA: 0, teamB: 1 }
if (typeof match.teamA === 'number')
console.log('Team A: ' + match.teamA);
}