null Type

The null type has only one value: null. A null value is an empty object pointer. typeof returns "object" for typeof null.

 
<!DOCTYPE html>
<html>
<head>
    <title>Null Example</title>
    <script type="text/javascript">
          
        var aString = null;
        document.writeln(typeof aString);   //"object"
        document.writeln(typeof null);      //"object"
    </script>

</head>
<body>
  
</body>
</html>
  
Click to view the demo

The value undefined is a derivative of null, so JavaScript defines them to be equal:

 
<!DOCTYPE html>
<html>
<head>
    <title>Null Example</title>
    <script type="text/javascript">
          
        document.writeln(null == undefined);   
              
    </script>

</head>
<body>
  
</body>
</html>
  
Click to view the demo

We can check for null to see if the variable has been assigned a value:

 
<!DOCTYPE html>
<html>
<head>
    <title>Null Example</title>
    <script type="text/javascript">
          
        var aString = null;
        if(aString == null){
           document.writeln("Not assigned");
        }
        aString = "some value";
        if(aString == null){
           document.writeln("Not assigned");
        }else{
           document.writeln("Assigned");
        }
        
    </script>

</head>
<body>
  
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Data Types:
  1. JavaScript Data Types
  2. typeof Operator
  3. The Undefined Type
  4. null Type
  5. null vs undefined
  6. Boolean Type
  7. Boolean() casting function
  8. The Literials of Number Type
  9. Octal Integer
  10. Hexadecimal
  11. Floating-Point Values
  12. Value range
  13. NaN
  14. Number Conversions:Number(), parseInt() and parseFloat()
  15. Number() function
  16. parseInt()
  17. parseFloat()
  18. The String Type
  19. String Literals and Escapes
  20. Get the String Length
  21. Converting to a String with toString() method
  22. Convert Number to String with radix
  23. Convert to String with String() casting function