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>
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>
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>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Data Types:
- JavaScript Data Types
- typeof Operator
- The Undefined Type
- null Type
- null vs undefined
- Boolean Type
- Boolean() casting function
- The Literials of Number Type
- Octal Integer
- Hexadecimal
- Floating-Point Values
- Value range
- NaN
- Number Conversions:Number(), parseInt() and parseFloat()
- Number() function
- parseInt()
- parseFloat()
- The String Type
- String Literals and Escapes
- Get the String Length
- Converting to a String with toString() method
- Convert Number to String with radix
- Convert to String with String() casting function