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
ifstatement will run becausemyVariablehas a truthy value; even though the value ofmyVariableis not explicitly the valuetrue, when used in a boolean or conditional context, it evaluates totruebecause it has been assigned a non-falsy value. - So which values are falsy— or evaluate to
falsewhen checked as a condition? The list of falsy values includes:0- Empty strings like
""or'' -
nullwhich represent when there is no value at all -
undefinedwhich 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)