To know the value of a function and use a proper boolean may conclude that it is easier to distinguish which one is not real to begin with.
Overview
let myVariable = 'I Exist!';
if (myVariable) {
console.log(myVariable)
} else {
console.log('The variable does not exist.')
}
- The code block in the
if
statement will run becausemyVariable
has a truthy value; even though the value ofmyVariable
is not explicitly the valuetrue
, when used in a boolean or conditional context, it evaluates totrue
because it has been assigned a non-falsy value. - So which values are falsy— or evaluate to
false
when checked as a condition? The list of falsy values includes:0
- Empty strings like
""
or''
-
null
which represent when there is no value at all -
undefined
which represent when a declared variable lacks a value -
NaN
, or Not a Number
Code Snippets
let username = '';
let defaultName;
if (username) {
defaultName = username;
} else {
defaultName = 'Stranger';
}
a11y myths
Accessibility can only be tested by disabled people
Well, usually disabled people are the best testers - they use assistive technologies full time. However, everyone can learn how to test websites for accessibility.
Top comments (0)