typeof
is safer as it allows the identifier to never have been declared before:
if(typeof neverDeclared === "undefined") // no errors
if(neverDeclared === null) // throws ReferenceError: neverDeclared is not defined
More Related Contents:
- When is JavaScript’s eval() not evil?
- What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
- What’s the best way to convert a number to a string in JavaScript?
- Declaring multiple variables in JavaScript
- JavaScript: What dangers are in extending Array.prototype?
- How can I set multiple CSS styles in JavaScript?
- style.display=’none’ doesn’t work on option tags in chrome, but it does in firefox
- Unobtrusive JavaScript: at the top or the bottom of the HTML code?
- What does this “(function(){});”, a function inside brackets, mean in javascript? [duplicate]
- JavaScript braces on new line or not? [closed]
- Is there a better way of writing v = (v == 0 ? 1 : 0); [closed]
- Javascript document write overwriting page?
- Why use semicolon? [duplicate]
- What is “?:” notation in JavaScript?
- setTimeout() with string or (anonymous) function reference? speedwise [closed]
- Are there any coding standards for JavaScript? [closed]
- JavaScript style for optional callbacks
- JavaScript: inline functions vs predefined functions
- XSS attacks and style attributes
- How can I programmatically copy all of the style attributes from one DOM element to another
- How to use JSON from instagram API?
- How to update nested state properties in React
- Accessing JPEG EXIF rotation data in JavaScript on the client side
- THREE.js Ray Intersect fails by adding div
- Vanilla JavaScript equivalent of jQuery’s $.ready() – how to call a function when the page/DOM is ready for it [duplicate]
- jQuery equivalent of JavaScript’s addEventListener method
- Strange Unexpected token ILLEGAL in JavaScript [closed]
- What is the difference between “event loop queue” and “job queue”?
- appendChild in for loop only adds 1 child
- detect whether ES Module is run from command line in Node