Here is a quick tip for JavaScript falsy conditions for programmers or for people who come from Java or C#.
In Java and C# when we thinks of using a compare conditions in a if statements, we tend to use something along the line of:
We tend to bring this baggage of knowledge to JavaScript also, but it is not necessary. In JavaScript one can simply use the conditionals as below.
Here is a table that will show you what could be the values of val that would translate to false.
Type | Result |
---|---|
Undefined | false |
Null | False |
String | if length == 0, false |
Object | true |
Number | if NAN or 0, false |
Here is some sample code if you wish to test out falsy in JavaScript.
Learning these fundamental idioms in JavaScript allows one to write cleaner Javascript code.
Leave A Comment