Summary of methods for judging Data Types in JavaScript

Source: Internet
Author: User
For example, to determine whether a variable is of the array type, the is_array () function in PHP can be used to directly determine whether a variable is of the array type. However, in js, we need... -- well, next let's take a detailed look at the method for judging the Data Type in JavaScript. Summary: typeof
When typeof is used for many times, it is used to determine whether a global variable is absent. If a page defines a global variable. If you make the following judgment:

// Haorooms is the global variable if (haorooms! = Undefined) {} // js will report an error, saying "Uncaught ReferenceError: haorooms is not defined"

The solution is as follows:

if(typeof haorooms!=undefined){}

If typeof is used, no error will be reported! This is one of the typeof applications!

In addition, typeof can also be used to determine the data type! As follows:

Var haorooms = "string"; console. log (haorooms); // stringvar haorooms = 1; console. log (haorooms); // numbervar haorooms = false; console. log (haorooms); // booleanvar haorooms; console. log (typeof haorooms); // undfined var haorooms = null; console. log (typeof haorooms); // objectvar haorooms = document; console. log (typeof haorooms); // objectvar haorooms = []; console. log (haorooms); // objectvar haorooms = function () {}; console. log (typeof haorooms) // function can be used to determine the data type and function type.

Obviously, for typeof, except for the first four types, null, objects, and arrays all return object types;

Instanceof
You can use it to determine whether it is an array.

Var haorooms = []; console. log (haorooms instanceof Array) // return true

Constructor
Constructor is the constructor corresponding to the returned object.
Methods for judging 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 outputting haorooms. constructor, we can see that constructor is the constructor corresponding to the returned object.

Object. prototype. toString
We mentioned earlier that the constructor attribute can be used to determine the Object type. Let's talk about the Object. protype. toString method.

Object.prototype.toString.apply({}) // "[object Object]"Object.prototype.toString.apply([]) // "[object Array]"Object.prototype.toString.apply(NaN)// "[object Number]"Object.prototype.toString.apply(function(){}) // "[object Function]"

Using this method, we can correctly judge the basic state of a variable, but if it is a custom type, we cannot know the actual type, because the result will still be [object Object]

Others
JQuery also has a type determination method. The following is an example.

$.isWindow(window) // true

How can this problem be solved?

core.js#479isWindow: function( obj ) {  return obj != null && obj == obj.window;}

So open an Object like this:

var fakeWindow;fakeWindow = {};fakeWindow.window = fakeWindow;$.isWindow(fakeWindow) // true

You cheated him.

Summary
In JavaScript, You need to correctly judge the type. It is really troublesome to study it carefully. It is very important to design your attention style according to different situations, we must also think about how to judge the correct type in the most concise way. Of course, this article is not introduced in many places. For example, the isPrototypeOf method, JavaScript is a language with many historical burdens, but it is constantly improving. When using it, you should note that there are too many ways to use dual-sided blades. Remember to use them with caution.

For more information about how to judge the Data Type in JavaScript, refer to PHP!

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.