TypeOf can tell us whether its operand is a string (string), a numeric value (number), a function, a Boolean value (Boolean), or an object.
1. Strings (String)
Alert (typeof ("Asss")), Alert (typeof ("123")), and so on.
The warning box now displays a string.
Note: strings need double quotes.
2. Value (number)
Alert (typeof (123)).
The warning box now displays number.
Note: typeof can contain only numbers, no letters and other special symbols, and no double quotes.
3. Functions (function)
Function Union () {
var num1=1,num2=2;
var total=num1+num2;
}
Alert (typeof (union));
The warning box now shows the function.
4. Boolean Value (Boolean)
Alert (typeof (True)), or alert (typeof (false));
The warning box now displays a Boolean.
5. Objects (object)
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "Author" content= "Hehe" >
<title>shopping list</title>
<link rel= "stylesheet" type= "Text/css" href= "My_css.css" >
<body>
<p title= "A gentle reminder" >don ' t forgrt to buy this stuff.</p>
<ul id= "Purchases" >
<li>a Tin of Beans</li>
<li class= "Sale" >Cheese</li>
<li class= "Sale Important" >Milk</li>
</ul>
<script type= "Text/javascript" src= "My_script.js" ></script>
</body>
Alert (typeof document.getElementById ("purchases"));
The warning box now displays object.
Note: getElementsByTagName and Getelementsbyclassname return an array that is different from getElementById.
Pass
var itsems=document.getelementsbytagname ("Li");
for (Var i=0;i<itsems.length;i++) {
Alert (typeof Itsems[i]);
}
Changes to the output object.
The I Warning box displays 3 object.
---reading javascript DOM programming Art
The typeof operator is used in JavaScript to display the operands on the page