Typeof operator
·
It is a unary operator that is placed before its
single operand, which can be of any type. Its value is a string indicating the
data type of the operand.
·
The typeof operator evaluates to “number”,
“string”, or “Boolean” if its operand is a number, string, or Boolean value and
returns true or false based on the evaluation.
Type
|
STRING returned by type of
|
String
|
“number”
|
Boolean
|
“Boolean”
|
Object
|
“object”
|
Function
|
“function”
|
Undefined
|
“undefined”
|
Null
|
“object”
|
Example:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x = 123; // x is a number
var y = new Number(123); // y is an object
var txt = typeof(x) + " " +
typeof(y);
document.getElementById("demo").innerHTML=txt;
</script>
</body>
</html>
No comments:
Post a Comment