Check undefined, null, empty or blank variable in JavaScript
- By Preneesh AV --
- 20-Oct-2018 --
- 82 Comments
If you are already sure or can make sure as shown in code above, that a variable is declared at least (no matter if it has a value or not), you should directly check if it has a truthy value using the code written below:
0 1 2 3 |
if(myVar){ // myVar has truthy value } |
This will evaluate to true if myVar's value is not:
- null
- empty string (“”)
- undefined
- NaN
- false
- 0
