Sitemap
Rogerio Santos

Rogerio Santos

Truthy a Falsey values javascript

Jul 31, 2023

--

Truthy values and falsey values in a boolean context.

many codes to check if an variable has a value is like this.

let someVar;

if(someVar !== nil && someVar !== undefined && someVar !== '') {
console.log('variable has a value');
}

you can refactor it to like this

  let someVar;

if(someVar) {
console.log('Variable has a value');
}

but you must be aware of what evalute to to fase and what is evaluated to true

Get Rogerio Santos’s stories in your inbox

Join Medium for free to get updates from this writer.

Evaluate to fase

  • null
  • undefined
  • 0
  • Empty String
  • false
  • NaN

Evalute to true

  • Empty Object {}
  • Empty array []
  • Negative number -5

you must be sure what kind of value the variable will store.

--

--