Use typeof operator to check if a variable is a primitive type or undefined or object in JavaScript
Description
The following code shows how to use typeof operator to check if a variable is a primitive type or undefined or object.
Example
<!-- w ww. j a v a2 s. co m-->
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var s = "JavaScript";
var b = true;
var i = 22;
var u;
var n = null;
var o = new Object();
document.writeln(typeof s); //string
document.writeln(typeof b); //boolean
document.writeln(typeof i); //number
document.writeln(typeof u); //undefined
document.writeln(typeof n); //object
document.writeln(typeof o); //object
</script>
</head>
<body>
</body>
</html>
The code above generates the following result.
Javascript Tutorial Variable
Copy reference during assignment for object...
Copy value from one primitive type to anoth...
Create a code with three level of execution...
Define a variable outside a function and ca...
Demonstrate that there are no Block-Level S...
Demonstrate that variable defined with var ...
Demonstrate that variable defined without v...
Demonstrate that variables created by the v...
Find out the difference between primitive w...
Use eval to evaluate dynamic expressions in...
Copy reference during assignment for object...
Copy value from one primitive type to anoth...
Create a code with three level of execution...
Define a variable outside a function and ca...
Demonstrate that there are no Block-Level S...
Demonstrate that variable defined with var ...
Demonstrate that variable defined without v...
Demonstrate that variables created by the v...
Find out the difference between primitive w...
Use eval to evaluate dynamic expressions in...