Skip to content

JavaScript isNull | How to Check for null

The value null represents the object value or variable value is don’t have any value(“No value”). There is no isNull function in JavaScript script to find without value objects.

However you can build your own isNull() function with some logics.

Examples of Check null in JavaScript

Let’s see the example of it:-

Code JavaScript check if null

You can use the qualities of the abstract equality operator to do this:

Strict equality operator:-

if (variable === null){
    // your code here.
}

Because null == undefined is true, the above code will catch both null and undefined.

Just by using if

if( value ) {

}

JavaScript isNull function

You can create function isNull like:-

<!DOCTYPE html> 
<html>
  
<body> 
    <script type="text/javascript"> 

    function isNull(value) {
    if ( value === null ){
    	return true;
		}
    }
    
	var abc = null;
	alert(isNull(abc))

    </script> 
      
    
</body> 
  
</html> 

Output:

JavaScript isNull function

Q: How to JavaScript check not null?

Answer: Try to check with ‘null’ as below code: 3 ways to check for “not null”. My recommendation is to use the Strict Not Version.

Strict Not Version

if (val !== null) { ... }

Non-strict Not Version

if (val != 'null') { ... }

Double Not Version

if (!!val) { ... }

Q: What is the Type of null in JavaScrip?

Answer: Type of null is an object in JS, you can check the type of any variable with typeof keyword. for example

typeof null

Do comment if you have any questions or suggestion on this topic.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

1 thought on “JavaScript isNull | How to Check for null”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading