How to judge variable type in js

js There are many ways to determine the type of a variable.

  • 1. typeofOperator, which can return a string to describe the type of variable, such as:
console.log(typeof "hello");  // string
console.log(typeof 123);  // number
console.log(typeof true);  // boolean
console.log(typeof {});  // object
console.log(typeof []);  // object
console.log(typeof undefined);  // undefined
console.log(typeof null);  // object
  • 2. instanceofOperators can be used to determine whether a variable is an instance of a certain class, such as:
console.log("hello" instanceof String);  // false
console.log(new String("hello") instanceof String);  // true
console.log([1, 2, 3] instanceof Array);  // true
console.log({} instanceof Object);  // false
  • 3. Object.prototype.toString.call()The method can return the type of the variable, such as:
console.log(Object.prototype.toString.call("hello"));  // [object String]
console.log(Object.prototype.toString.call(123));  // [object Number]
console.log(Object.prototype.toString.call(true));  // [object Boolean]
console.log(Object.prototype.toString.call({}));  // [object Object]
console.log(Object.prototype.toString.call([]));  // [object Array]
console.log(Object.prototype.toString.call(undefined));  // [object Undefined]
console.log(Object.prototype.toString.call(null));  // [object Null]

It is worth noting that when judging null, both typeof and Object.prototype.toString.call() will return 'object', while instanceof will return false. At this time, you can use x === nullto whether it is null.

  • 4. Use JavaScript's native constructorproperty . Every object has a constructorproperty that points to the function that created the object. You can use this attribute to determine the type of a variable.
console.log("hello".constructor === String);   // true
console.log((123).constructor === Number);     // true
console.log([].constructor === Array);         // true
console.log({}.constructor === Object);        // true
console.log(true.constructor === Boolean);     // true
console.log(undefined.constructor === undefined);   // true
console.log(null.constructor === null);        // Uncaught TypeError: Cannot read property 'constructor' of null

But this method has a flaw, when the variable is nullor undefined, accessing constructorthe property will throw an error.

It should be noted that all the above methods will return 'function' when judging the function. If you need a precise function type, you can use Object.prototype.toString.call() to get the type "[object Function]" or use Function.name to get the function name.

  • 5. Use the new ES6 Symbol.toStringTagattribute, which can return a string to describe the type of the variable. This method is similar to using Object.prototype.toString.call()
console.log("hello"[Symbol.toStringTag]);  // "String"
console.log(123[Symbol.toStringTag]);  // "Number"
console.log(true[Symbol.toStringTag]);  // "Boolean"
console.log({}[Symbol.toStringTag]);  // "Object"
console.log([] [Symbol.toStringTag]);  // "Array"
console.log(undefined[Symbol.toStringTag]);  // "Undefined"
console.log(null[Symbol.toStringTag]);  // "Null"
console.log(()=>{}[Symbol.toStringTag]);  // "Function"

Note that when using this method, you need to ensure that the target browser supports the ES6 Symbol type, or use tools such as babel to convert.

  • 6. Using a third-party library, such lodashas isXXXthe series of functions, can be used to determine the type of the variable. The use of these functions is very simple, just call the corresponding function directly, such as:
console.log(_.isString("hello"));  // true
console.log(_.isNumber(123));  // true
console.log(_.isBoolean(true));  // true
console.log(_.isObject({}));  // true
console.log(_.isArray([]));  // true
console.log(_.isUndefined(undefined));  // true
console.log(_.isNull(null));  // true
console.log(_.isFunction(() => {}));  // true

Using these functions can avoid problems in type judgment in different environments and be more rigorous.

To sum up, there are many ways to judge the type of variables in JavaScript, such as third-party libraries such as typeof, instanceof, Object.prototype.toString.call(), constructorattributes, Symbol.toStringTagattributes, lodashand which need to be selected according to actual needs.

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/131147130