JS Judging type

Source: Internet
Author: User

1. Simple judgment, TypeOf and constructor

(1) typeof

typeof (1);  Numbertypeof ("s");  Stringtypeof (a);  Undefinedtypeof (TRUE);  Booleantypeof (a);  Functiontypeof (a); Object (objects, NULL, array)

(2) constructor

Console.log ([].constructor = = Array); Console.log ({}.constructor = = Object); Console.log ("string". Constructor = = String); Console.log ((123). constructor = = number); Console.log (true.constructor = = Boolean);

2, the judgment of the array

(1) rigorous type judgment

function IsArray (object) {    return object && typeof object=== ' object ' &&            Array = = Object.constructor;}

(2) Characteristic judgment

function IsArray (object) {    return  object && typeof object=== ' object ' &&                typeof object.length=== ' number ' &&              typeof object.splice=== ' function ' &&                 //Determine if the length property is enumerable for arrays Will get false              ! ( Object.propertyisenumerable (' length '));}

(3) The simplest method of judging

function IsArray (o) {    return Object.prototype.toString.call (o) = = = ' [Object Array] ';}

  

JS Judging type

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.