
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is there is a standard function to check for null, undefined, or blank variables in JavaScript?
No there is not a standard function to check for null, undefined or blank values in JavaScript. However, there is the concept of truthy and falsy values in JavaScript.
Values that coerce to true in conditional statements are called truth values. Those that resolve to false are called falsy.
According to ES specification, the following values will evaluate to false in a conditional context −
- null
- undefined
- NaN
- empty string ("")
- 0
- false
This means that none of the following if statements will get executed −
if (null) if (undefined) if (NaN) if ("") if (0) if (false)
Keywords to verify falsys
But there are some existing Keywords to check whether a variable is null, undefined or blank. They are null and undefined.
Example
Following example verifies for null, undefined and, blank values −
<!DOCTYPE html> <html> <head> <title>To check for null, undefined, or blank variables in JavaScript</title> </head> <body style="text-align: center;"> <p id="output"></p> <script> function checkType(x) { if (x == null) { document.getElementById('output').innerHTML += x+'The variable is null or undefined' + '<br/>'; } else if (x == undefined) { document.getElementById('output').innerHTML += 'The variable is null or undefined' + '<br/>'; } else if (x == "") { document.getElementById('output').innerHTML += 'The variable is blank' + '<br/>'; } else { document.getElementById('output').innerHTML += 'The variable is other than null, undefined and blank' + '<br/>'; } } var x; checkType(null); checkType(undefined); checkType(x); checkType(""); checkType("Hi"); </script> </body> </html>
- Related Articles
- How to check for null, undefined, or blank variables in JavaScript?
- Is there any way to check if there is a null value in an object or array in JavaScript?
- How to check for 'undefined' or 'null' in a JavaScript array and display only non-null values?
- How to check empty/undefined/null strings in JavaScript?
- Replace a value if null or undefined in JavaScript?
- What is a standard for commenting a function in JavaScript?
- How to check for “undefined” variable in JavaScript?
- Is there a “null coalescing” operator in JavaScript?
- What is the difference between null and undefined in JavaScript?
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- How to determine if a variable is 'undefined' or 'null'?
- Check for NULL or NOT NULL values in a column in MySQL
- Check if a String is empty ("") or null in Java
- Check whether a field is empty or null in MySQL?
- Check for specific beginning or ending letters in a JavaScript function

Advertisements