JavaScript checking null | Using equality operator

In JS null value means no value or absence of a value. Checking null in Javascript is easy, you can use the equality operator == or strict equality operator === (also called identity operator).

Examples of JavaScript checking null

Is the variable null? A code of Using equality operator to check null in JS.

if (a === null)

Note:

See the MDN article on Equality comparisons and sameness for more detail.

Complete Example

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

    var name_first= null;

    if (name_first === null){
    	alert("name is null")
    }

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

Output:

JavaScript checking null

Q: Is there a standard function to check for null, undefined, or blank variables in JavaScript?

Answer: You can just check if the variable has a truthy value or not.

if( value ) {
}

will evaluate to true if value is not:

  • null
  • undefined
  • NaN
  • empty string (“”)
  • 0
  • false

Q: What is the Type of null in JavaScript?

Answer: null has the 0 type tag, which corresponds to an object. The value null represents the intentional absence of any object value

typeof null // "object"

Do comment if you have any doubts and suggestions on this question.

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.