JavaScript's typeof operator

Source: Internet
Author: User
Tags object object

First, the TypeOf operator function:

Is the data type used to detect variables. Using the typeof operator for a value or variable returns the following string.

Ii. judgment of various data types

1, undefined: variable defined but not initialized, is undefined

var box;alert (box);                     // undefinedalert (typeof box);            // box is the undefined type, the value is undefined, and the string returned by the type is also undefined

2. Boolean:

var true ; alert (Boxboolean);                 // truealert (typeof Boxboolean);        // Boxboolean is a Boolean, the value is true, and the string returned by the type is also a Boolean

3. String:

var boxstring = ' Obama '; alert (boxstring); // Obama alert (typeof boxstring); // boxstring is a string type, the value is ' Obama ', and the type returned

4. Number:

var boxnumber = +; alert (boxnumber);                 // alert (typeof Boxnumber);        // Boxnumber is the number type, the value is 100, and the string returned by the type is

5. Object:

//An empty object that indicates that the object was created, but there is nothing inside .varBoxobject = {};//an empty object was createdalert (boxobject);//[Object Object]AlerttypeofBoxobject);//Boxobject is an object type, the value is [Object Object], and the string returned by the type is also an object    varBoxobject =NewObject ();//an empty object was createdalert (boxobject);//[Object Object]AlerttypeofBoxobject);//Boxobject is an object type, the value is [Object Object], and the string returned by the type is also an object//null object, which means no creation, is a nullvarBoxnull =NULL; alert (boxnull); //null or nothing there's only one windowAlerttypeofBoxnull);//Boxnull is a null type, the value is null or nothing, the string returned by the type is also an object, and null is derived from Object

6. Function:

function boxfunction () {        }alert (boxfunction);                 // print out the main body alert (typeof boxfunction);        // Boxfunction is the function value is the Boxfunction () {}, and the string returned by the type is function

The function is an object in ECMAScript, not a data type. Therefore, it is necessary to use TypeOf to distinguish between function and object.

  7. The typeof operator can manipulate variables, and can also manipulate literal values. While this can also be used: typeof (Box), typeof is an operator rather than a built-in function.

// can be directly on the value (literal) with typeofalert (typeof ' Obama '); // string

typeof operator for JavaScript

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.