Example of typeof operator usage in JavaScript

Source: Internet
Author: User
Tags numeric

  in Web front-end development, we often need to judge the data type of a variable. Since ECMAScript is loosely typed, there is a need to have a means to detect the data type of a given variable--typeof is the operator responsible for providing this convenient information.

Using the typeof operator on a value may return one of the following strings: "Undefined"-if the value is not defined "boolean"-if the value is a Boolean "string"-if the value is a string "number"-if the value is a numeric " Object "-If this is an objects or null" function "-if this value is a function   The return value of a commonly used typeof operator includes number, String, Boolean, Undefined, Object and function. such as:     code as follows: Var n; Console.log (typeof N); "Undefined"   n = 1; Console.log (typeof N); "Number"   n = "1"; Console.log (typeof N); "string"   n = false; Console.log (typeof N); "Boolean"   N = {name: "obj"}; Console.log (typeof N); "Object"   N = new number (5); Console.log (typeof N); "Object"   N = function () {return;}; Console.log (typeof N); The "function"     examples show that the operands of the typeof operator can be either a variable (message) or a literal number. Note that TypeOf is an operator rather than a function, so the parentheses in the example are not required (although they can be used).     From the above example, we found that numbers created with number () are also returned with the value "Object" by TypeOf, because constructors return objects, so if we want to distinguish between numeric objects (number), What if you have a String object (string), an array object (array), a function object, a Day object (date), a Boolean object (Boolean), and a JavaScript built-in object such as an Error object (Error)? Here you can call the object's ToString method, such as:     code as follows: VarN, Res;   N = new number (66); res = Object.prototype.toString.call (n); Console.log (RES); "[Object number]"   n = new String ("string"); res = Object.prototype.toString.call (n); Console.log (RES); "[Object String]"   n = []; res = Object.prototype.toString.call (n); Console.log (RES); [Object Array]  //...  

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.