typeof Operator
The typeof operator tells the data type of a given variable. The typeof operator returns one of the following strings:
Returned Value | Meanings |
---|---|
"undefined" | if the value is undefined |
"boolean" | if the value is a Boolean |
"string" | if the value is a string |
"number" | if the value is a number |
"object" | if the value is an object or null |
"function" | if the value is a function |
<!DOCTYPE html>
<html>
<head>
<title>typeof Example</title>
<script type="text/javascript">
var aString = "string";
//The typeof is an operator and not a function, no parentheses are required.
document.writeln(typeof aString); //"string"
document.writeln(typeof(aString)); //"string"
document.writeln(typeof 10); //"number"
</script>
</head>
<body>
</body>
</html>
Calling typeof null returns a value of "object"
<!DOCTYPE html>
<html>
<head>
<title>typeof Example</title>
<script type="text/javascript">
var aString = null;
document.writeln(typeof aString); //"object"
</script>
</head>
<body>
</body>
</html>
The following code calls typeof on an undefined variable.
<!DOCTYPE html>
<html>
<head>
<title>typeof Example</title>
<script type="text/javascript">
var aString;
document.writeln(typeof aString); //"undefined"
</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