JS native data type judgment

Source: Internet
Author: User

The data type of jquery used previously was judged, such as: IsArray (), and so on, today we see a simple native method of judging the data type and share it with everyone

Object.prototype.toString method returns the type string of an object, so it can be used to determine the type of a value

var val =
  object. Prototype. Tostring. Call ({}) //"[Object Object]" object. Prototype. Tostring. Call ([]) //"[Object Array]" 
Object.prototype.toString.call(2) // "[object Number]"Object.prototype.toString.call(‘‘) // "[object String]"
var type = Val.match (/\[object (. *?) \]/) [1].tolowercase ();//Type of data

On the basis of this, you can also add a method that specifically judges some type of data.

var Typearr = [' Null ',                    ' Undefined ', '                    Object ', '                    Array ',                    ' String ',                    ' number ', '                    Boolean ', '                    Function ',                    ' RegExp '                ];                var type = {};                Typearr.foreach (function (t) {                    type[' is ' + t] = function (params) {                        var val = Object.prototype.toString.call (PA Rams);                        var type = Val.match (/\[object (. *?) \]/) [1].tolowercase ();                        return type = = = T.tolowercase ();                    }                })

From the JavaScript standard reference tutorial (Alpha), by Ruan Feng

JS native data type judgment

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.