JS determines whether the array and common type judgments

Source: Internet
Author: User
Tags array object constructor log return string variable

Objective

This article is the basic knowledge of the popular article, JS judge data types. We all know that PHP has the Is_array () function, but not in JS, we determine whether the data type is an array, usually for the sake of insurance, you can write a function to judge. Today, popularize some basic data type judgment method, hope to be helpful to everybody. typeof articletypeof use more time, is to judge a global variable in the absence, if a page defines a global variable. If you make the following judgment://haorooms is a global variable if (haorooms!=undefined) {}//js will complain that "uncaught referenceerror:haorooms is not defined" solution is I We write as follows: if (typeof haorooms!=undefined) {} After using typeof, there will be no error!   This is one of the TypeOf applications! In addition, typeof can also be judged by the data type! as follows: Var haorooms= "string"; Console.log (haorooms); string Var Haorooms=1; Console.log (haorooms); Number Var Haorooms=false; Console.log (haorooms); boolean var haorooms; Console.log (typeof haorooms); undfined var haorooms= null; Console.log (typeof haorooms); Object var haorooms = document; Console.log (typeof haorooms); object var haorooms = []; Console.log (haorooms); object var haorooms = function () {}; Console.log (typeof Haorooms)//function In addition to judging the data type can also determine the function type is obvious, for TypeOf, except the first four types, null, object, array return is the object type; instanceof ArticleYou can use it to determine if it is an array. var haorooms=[]; Console.log (haorooms instanceof Array)//Return True constructor chapter constructor is the constructor that returns the corresponding object. ways to determine the various data types:Console.log ([].constructor = = Array); Console.log ({}.constructor = = Object); Console.log ("string". Constructor = string); Console.log ((123). constructor = number);   Console.log (True.constructor = = Boolean);     function Employee (name,job,born) {this.name=name;     This.job=job; This.born=born;  } var haorooms=new employee ("Bill Gates", "Engineer", 1985); Console.log (Haorooms.constructor); Output function employee (name, JobTitle, born) {this.name = name; this.jobtitle = Job; This.born = born;} by Output Haorooms.constru ctor, you can see that constructor is the constructor that returns the object.

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.