The typeof
operator will check if the variable is really undefined.
if (typeof variable === 'undefined') { // variable is undefined }
The typeof
operator, unlike the other operators, doesn’t throw a ReferenceError exception when used with an undeclared variable.
However, do note that typeof null
will return "object"
. We have to be careful to avoid the mistake of initializing a variable to null
. To be safe, this is what we could use instead:
if (typeof variable === 'undefined' || variable === null) { // variable is undefined or null }
For more info on using strict comparison ===
instead of simple equality ==
, see:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
Related Posts:
- How can I determine if a variable is ‘undefined’ or ‘null’?
- Define a global variable in a JavaScript function
- How can I check for “undefined” in JavaScript?
- How can I check for “undefined” in JavaScript? [duplicate]
- How do you check if a variable is an array in JavaScript?
- JavaScript null check
- JavaScript null check
- How to resolve TypeError: Cannot convert undefined or null to object
- Static variables in JavaScript
- What does ${} (dollar sign and curly braces) mean in a string in Javascript?
- Declaring multiple variables in JavaScript
- How can I check if an element exists in the visible DOM?
- Check if object exists in JavaScript
- What is the difference between null and undefined in JavaScript?
- How to interpolate variables in strings in JavaScript, without concatenation?
- How do I use this JavaScript variable in HTML?
- How to check ‘undefined’ value in jQuery
- Multiple left-hand assignment with JavaScript
- Is there a standard function to check for null, undefined, or blank variables in JavaScript?
- How to check for an undefined or null variable in JavaScript?
- How can I unset a JavaScript variable?
- wp_localize_script with boolean and init
- Setting HTML properties in a Gutenberg plugin using WordPress settings
- Use one javascript variable into another javascript file
- addEventListener vs onclick
- How do I check if an array includes a value in JavaScript?
- JavaScript document.getElementById().value logs empty value
- What is console.log?
- Fetch: POST JSON data
- JavaScript hide/show element
- Copy array by value
- Compare two dates with JavaScript
- ReferenceError: fetch is not defined
- In reactJS, how to copy text to clipboard?
- SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag
- Uncaught SyntaxError: Invalid or unexpected token
- JavaScript TypeError: Cannot read property ‘style’ of null
- How to import image (.svg, .png ) in a React Component
- How do I embed PHP code in JavaScript?
- Make javascript alert Yes/No Instead of Ok/Cancel
- How to create a dialog with “yes” and “no” options
- Adding images to a Bootstrap Carousel with AJAX
- XMLHttpRequest cannot load XXX No ‘Access-Control-Allow-Origin’ header
- Uncaught TypeError: Cannot read property ‘props’ of null
- How to use iMonkey in an iOS app
- How to handle the `onKeyPress` event in ReactJS?
- ES6: Create Strings using Template Literals – Freecodecamp
- Youtube api – stop video
- Uncaught TypeError: Cannot read property ‘msie’ of undefined – jQuery tools
- Angular JS Uncaught Error: [$injector:modulerr]
- Nested objects in javascript, best practices
- How do I convert an integer to binary in JavaScript?
- What does window.jQuery and window.$ mean?
- How to change date format in JavaScript
- OnChange event handler for radio button (INPUT type=”radio”) doesn’t work as one value
- InvalidValueError: not an instance of HTMLInputElement
- Can not solve: Geocode was not successful for the following reason: OVER_QUERY_LIMIT
- this vs $(this)
- How to open a URL in a new Tab using JavaScript or jQuery?
- how to call a onclick function in tag?
- How to sort an array of integers correctly
- Failed to execute ‘postMessage’ on ‘DOMWindow’: https://www.youtube.com !== http://localhost:9000
- How can I check whether a radio button is selected with JavaScript?
- JavaScript button onclick not working
- typescript – cloning object
- Ajax success function
- What is the equivalent of Java’s System.out.println() in Javascript?
- How can I exit from a javascript function? [duplicate]
- Object doesn’t support property or method ‘addEventListener’
- How to fix javascript .toFixed is not a Function error
- Implementing Isomorphic JavaScript (React JS) in WordPress?
- Any advantage of using wp_scripts and is_IE when enqueuing scripts
- TinyMCE in a div / textarea on frontend?
- Adding wp_ajax to a theme outside functions.php (on specific template page)
- WordPress problem with htmlentities
- Provide specific example for block variation
- VB.net and WordPress
- Gutenberg passing block attributes to component in ES6/ESNext
- How to add a custom.js file for a child-theme in a swift way?
- WP REST API Post Status Using JavaScript
- var is undefined in a Gutenberg block
- Custom wp.editor.initialize settings ignored
- Why my javascript code not working for the dashboard of wordpress admin?
- Got a TypeError after update and need to adapt found solution
- How do you filter the list of states when country is selected? [closed]
- Prevent update_checkout from firing mid-typing
- How to move core js files into the footer
- Suddenly extra hex string in tags
- Retrieving data about comments and likes
- Including Styles and JS files to work ON my plugin interface
- Linking wp_enqueue can’t find the javascript file (adds “?ver=x.x.x” to the src)
- Gutenberg – useEffect manipulate DOM after block re-renders DOM
- Javascript working on jsfiddle but not wordpress site?
- Drodown list return issue
- Spans in gutenberg
- Get a default customizeAction text for a section using Customizer JS API
- Put dynamic Javascript in header after doing operations
- custom page url slug needs illegal ?id=1 for javascript
- Why is this script not working?
- Astra Mobile Cart Drawer is blank on home page