Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript if expression evaluation

Tags:

javascript

I was wondering when the Javascript if expression actually evaluates to false and when to true. When is the if statement false, and is that true for all JS interpreters?

I guess the condition is false on

  • false
  • undefined
  • null
  • 0

otherwise true. Is that correct for all implementations (tested in Safari/WebKit console), or am I better off with explicit checking like (typeof a === "undefined")?

like image 247
Era Avatar asked Mar 19 '12 09:03

Era


1 Answers

The following values will evaluate to false:

  • false
  • undefined
  • null
  • 0
  • NaN
  • the empty string ("")

https://developer.mozilla.org/en/JavaScript/Guide/Statements#if...else_Statement

like image 187
Jakub Konecki Avatar answered Oct 26 '22 23:10

Jakub Konecki