The typeof operator is used in JavaScript to display the operands on the page

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.