HTML and CSS Reference
In-Depth Information
answer1 =
true
;
or
if (answer2 ==
false
) { do something; }
The
typeof
Operator.
The
typeof
operator returns a string to identify the type of its
operand (i.e., a variable, string, keyword, or object). The values returned can be “num-
ber”, “string”, “boolean”, “object”, “null”, and “undefined”. You can use the
typeof
oper-
ator to check whether a variable has been defined because if there is no value associated
with the variable, the
typeof
operator returns
undefined
.
FORMAT
typeof operand
typeof (operand)
EXAMPLE
typeof(54.6)
typeof("yes")
EXAMPLE
3.3
<html>
<head>
<title>The typeof Operator</title>
</head>
<body bgcolor="gold">
<big>
<script type="text/javascript">
1 document.write(
typeof(55)
,"<br />"); // Number
2 document.write(
typeof("hello there")
,"<br />"); // String
3 document.write(
typeof(true)
,"<br />"); // Boolean
</script>
</big>
</body>
</html>
EXPLANATION
1
The integer,
55
, is a
number
type.
2
The text
“hello there”
is a
string
type.
3
The
true
or
false
keyword represent a
boolean
type. See Figure 3.2.