Data type of 01.javascript

Source: Internet
Author: User
Tags object object

1. Data type

JavaScript has a total of six data types. (ES6 added a value for the seventh symbol type)

    • Value (number)
    • Strings (String)
    • Boolean Value (Boolean)
    • Undefined
    • Null
    • Objects (object)
2. Data type determination

JavaScript has three ways to determine the type of a value

    • typeof operator
    • instanceof operator
    • Object.prototype.toString () method
typeof operator

typeofThe operator can return the data type of a value.
Values, strings, and Boolean values are returned number , respectively string boolean .

typeof 123  //"number"typeof ‘hello‘  //"string"typeof true  //"boolean"

function to return function .

function f(){} typeof f  //"function"

undefinedReturn undefined .

typeof undefined  // "undefined"

object is returned object .

typeof {} // "object"typeof [] // "object"

Null 返回 object '.

typeof null // "object"
instanceof operator

instanceofThe operator returns a Boolean value that indicates whether the object is an instance of a constructor.
Because the instanceof entire prototype chain is checked, the same instance object may be returned to more than one constructor true .
instanceofOne use of the operator is to determine the type of the value.

var x = []var f={}x instanceof Array //truef instanceof Object //true

instanceofThe operator can only be used with objects, not values of the original type.

With the instanceof operator, you can also resolve the problem of adding a command when calling new the constructor.

function Fn (f1, f2) {  if (this instanceof Fn) {    this._foo = f1;    this._bar = b2;  } else {    return new Fn(f1, f2);  }}
Object.prototype.toString ()

toStringThe function of a method is to return a string form of an object, returning a type string by default.

var o1 = new Object();o1.toString() //"[object Object]"

Application of ToString (): Judging data type
Object.prototype.toStringmethod returns the type string of an object, so it can be used to determine the type of a value.

var obj = {};obj.toString() // "[object Object]"

The above code calls a method of an empty object, and the toString result returns a string object Object with the second Object constructor representing the value. This is a very useful method of judging the data type.

Because instance objects may have custom toString methods that override methods, Object.prototype.toString it is best to use the method directly in order to get the type string Object.prototype.toString . By means of a function call , you can call this method on any value to determine the type of the value.

Object.prototype.toString.call(value)

The above code indicates that the value method is called on this value Object.prototype.toString .
The method return values for different data types Object.prototype.toString are as follows.

    • Value: Returns [object Number] .
Object.prototype.toString.call(12) //"[object Number]"
    • String: Returns [object String] .
Object.prototype.toString.call(‘ab‘)  //"[object String]"
    • Boolean value: Returns [object Boolean] .
Object.prototype.toString.call(true)  //"[object Boolean]"
    • Undefined: return [object Undefined] .
Object.prototype.toString.call(undefined)  //"[object Undefined]"
    • Null: Returns [object Null] .
Object.prototype.toString.call(null)  //"[object Null]"
    • Array: Returns [object Array] .
Object.prototype.toString.call([])  //"[object Array]"
    • Function: Returns [object Function] .
var f = function (){}Object.prototype.toString.call(f)  //"[object Function]"

Using this feature, you can write a typeof type-judging function that is more accurate than the operator.

var type = function (o){  var s = Object.prototype.toString.call(o);  return s.match(/\[object (.*?)\]/)[1].toLowerCase();};type({}); // "object"type([]); // "array"type(3); // "number"type(null); // "null"type(); // "undefined"type(/abcd/); // "regex"type(new Date()); // "date"

Not to be continued

Data type of 01.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.