Home > Web Front-end > JS Tutorial > body text

How to determine value type in JavaScript

巴扎黑
Release: 2017-09-16 09:29:39
Original
1608 people have browsed it

There is an operator in js that can help us determine the type of a value, it is the typeof operator. Through this article, I will share with you how to determine the type of a value in JavaScript. Friends who need it can refer to it

We know that there is an operator in js that can help us determine the type of a value. It is the typeof operator. .


console.log(typeof 123);  //number
console.log(typeof '123'); //string
console.log(typeof true);  //boolean
console.log(typeof undefined); //undefined
console.log(typeof null); //object
console.log(typeof []);  //object
console.log(typeof {}); //object
console.log(typeof function() {}); //function
Copy after login

We can see the shortcomings of typeof from the above results. It returns number, string, and boolean for numerical values, strings, and Boolean values, and the function returns function. undefined returns undefined. Otherwise, object is returned in other cases.

So if the return value is object, we cannot know whether the type of the value is an array, an object or other values. In order to accurately get the type of each value, we must use another operator instanceof in js. Let’s briefly talk about the usage of instanceof.

The instanceof operator returns a Boolean value indicating whether the specified object is an instance of a certain constructor.

The left side of the instanceof operator is the instance object, and the right side is the constructor. It checks whether the ptototype attribute of the right constructor is on the prototype chain of the left object.


var b = [];
b instanceof Array //true
b instanceof Object //true
Copy after login

Note that the instanceof operator can only be used for objects, not for primitive type values.

So we can combine the characteristics of typeof and instanceof operators to make a more accurate judgment on the type of a value.


//得到一个值的类型
function getValueType(value) {
  var type = '';
  if (typeof value != 'object') {
    type = typeof value;
  } else {
    if (value instanceof Array) {
      type = 'array';
    } else {
      if (value instanceof Object) {
        type = 'object';
      } else {
        type = 'null';
      }
    }
  }
  return type;
}
getValueType(123);  //number
getValueType('123'); //string
getValueType(true);  //boolean
getValueType(undefined); //undefined
getValueType(null); //null
getValueType([]);   //array
getValueType({});  //object
getValueType(function(){}); //function
Copy after login

The above is the detailed content of How to determine value type in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template