DEV Community

Cover image for Day 88/100 Truthy and Falsy
Rio Cantre
Rio Cantre

Posted on • Originally published at dev.to

Day 88/100 Truthy and Falsy

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.

banner

Overview

let myVariable = 'I Exist!';

if (myVariable) {
   console.log(myVariable)
} else {
   console.log('The variable does not exist.')
}
Enter fullscreen mode Exit fullscreen mode
  • The code block in the if statement will run because myVariable has a truthy value; even though the value of myVariable is not explicitly the value true, when used in a boolean or conditional context, it evaluates to true 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';
}
Enter fullscreen mode Exit fullscreen mode

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.

resources

Discussion (0)