Skip to content

Instantly share code, notes, and snippets.

@imparvez
Created July 18, 2020 17:24
Show Gist options
  • Save imparvez/b57173aa94ddfe001c4f94a9f77e9a49 to your computer and use it in GitHub Desktop.
Save imparvez/b57173aa94ddfe001c4f94a9f77e9a49 to your computer and use it in GitHub Desktop.
What are falsy values in JavaScript?
// Falsy values in Javascript
// The falsy values in JavaScript are 0, 0n, null, undefined, false, NaN, and the empty string "".
// A false value is a value that is false when encountered in Boolean context.
Boolean(0)
> false
Boolean(0n)
> false
Boolean(null)
> false
Boolean(undefined)
> false
Boolean(false)
> false
Boolean(NaN)
> false
Boolean('')
> false
// Truthy values is going to be everything else - anything that is not falsy
// The 7 falsy values
0 ? console.log('Truthy') : console.log('Falsy') // Falsy
0n ? console.log('Truthy') : console.log('Falsy') // Falsy
null ? console.log('Truthy') : console.log('Falsy') // Falsy
undefined ? console.log('Truthy') : console.log('Falsy') // Falsy
'' ? console.log('Truthy') : console.log('Falsy') // Falsy
false ? console.log('Truthy') : console.log('Falsy') // Falsy
NaN ? console.log('Truthy') : console.log('Falsy') // Falsy
// Some examples of Truthy values.
37 ? console.log('Truthy') : console.log('Falsy') // Truthy
37n ? console.log('Truthy') : console.log('Falsy') // Truthy
true ? console.log('Truthy') : console.log('Falsy') // Truthy
let empty = []
empty ? console.log('Truthy') : console.log('Falsy') // Truthy
empty = {}
empty ? console.log('Truthy') : console.log('Falsy') // Truthy
// Reference: https://medium.com/coding-at-dawn/what-are-falsy-values-in-javascript-ca0faa34feb4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment