Treat undefined the same as null, you can use the equality operator (==) in JavaScript

Description

The following code shows how to treat undefined the same as null, you can use the equality operator (==).

Example


<!DOCTYPE HTML>
<html>
<body>
<script type="text/javascript">
var firstVal = null;<!--  w w  w.  j  av  a 2  s  .  c  o m-->
var secondVal;
var equality = firstVal == secondVal;
var identity = firstVal === secondVal;
document.writeln("Equality: " + equality);
document.writeln("Identity: " + identity);
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Treat undefined the same as null, you can use the equality operator (==) in JavaScript
Home »
  Javascript Tutorial »
    Data Type »
      Null
Javascript Tutorial Null
Assign undefined value to a variable in Jav...
Assign variable to NULL value in JavaScript
Check Whether a Variable or Property Is nul...
Check if a value is undefined in JavaScript
Check if a variable has been assigned a val...
Create a null value with null in JavaScript
Create undefined value by defining a variab...
Differentiate between null and undefined, y...
Find out the difference between undefined v...
Treat undefined the same as null, you can u...
Use typeof operator on null in JavaScript