Created
November 26, 2013 01:33
-
-
Save nola/7652041 to your computer and use it in GitHub Desktop.
Null, Undefined, Empty Checks Shorthand
When creating new variables sometimes you want to check if the variable your referencing for it’s value isn’t null or undefined. I would say this is a very common check for JavaScript coders.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (variable1 !== null || variable1 !== undefined || variable1 !== '') { | |
| var variable2 = variable1; | |
| } | |
| //short hand | |
| var variable2 = variable1 || ''; |
you should declare variable1 first
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, i tried this example, but both if-conditions throws an ReferenceError: "variable1 is not defined".
Does this still works?