Data type judgment of JavaScript

1. Data type of JS

Simple data types: String, Number, Boolean, null, undefined, Symbol

Complex data type: Object

2,typeof

typeof is an operator, not a function, which can judge the data type of variables. It is generally used to judge the data of basic data types. The principle of judging the data type is: the data of JavaScript is stored in binary form at the bottom, and the last three bits of binary represent the data type. typeof judges the data type through this feature. typeof determines the data type. The returned values include number, string, boolean, object, function, undefined, and symbol, all of which are returned in the form of strings.

Common: 000 object, 100 string, 110 Boolean, 1xx integer, 010 floating point number. All binary machine codes of special null are 0, so null will be recognized as object by typeof. The last three machine codes of array are also 000, so it will also be recognized as object. The defect of typeof lies in the inaccurate judgment of null and array data types.

Case code:
// Number type
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Although NaN means non numeric
typeof Number(1) === 'number'; // But not recommended!

// String type
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof returns a string
typeof String("abc") === 'string'; // But not recommended!

// Boolean type
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // But not recommended!

// Symbol type
typeof Symbol() === 'symbol';
typeof Symbol('foo') === 'symbol';
typeof Symbol.iterator === 'symbol';

// Undefined
typeof undefined === 'undefined';
var a // Declared but not assigned
typeof a === 'undefined';
typeof b === 'undefined'; // Undeclared, unassigned

// object type
typeof {a:1} === 'object';
typeof [1, 2, 4] === 'object'; // The array is judged as an object
typeof new Date() === 'object';
typeof null === 'object'; // null is also judged as an object

// function
typeof function(){} === 'function';
typeof Math.sin === 'function';

3,instanceof

Instanceof is an operator, which is A commonly used way to judge the specific data type of reference type data. Its principle is to judge through the prototype chain. If the prototype of the right variable is on the prototype chain of the left variable, the left variable is an example of the right variable. For example, A instanceof B is to look up the prototype chain layer by layer from the proto of the current A to see if B.prototype can be found. If it is found, it returns true, indicating that A is A class B data type.

However, it should be noted that instanceof is only used to compare custom objects from the same JavaScript context. If it is used to compare objects belonging to different JavaScript contexts (for example, different document structures in the browser), an error will occur because their constructor functions will not be the same object.

In addition, instanceof can also be used to judge whether an instance belongs to its parent type in the inheritance relationship.

instanceof cannot judge null and undefined data. Moreover, it is also true that it judges that data types such as arrays and functions belong to Object. Therefore, if you want to judge specific data types, you must judge them accurately, not by exclusion.

// array
var a = [1,2,3]
console.log(a instanceof Array)  // true
console.log(a instanceof Object) // True, both are true
var a2 = new Array
console.log(a2 instanceof Array)  // true
console.log(a2 instanceof Object) // true

// Functions (including arrow functions)
var b = function() {}
console.log(b instanceof Function) // true
console.log((()=>{}) instanceof Function) // true 
console.log(b instanceof Object)  // true

// object
var c = {}
console.log(c instanceof Object) // true
console.log(Date instanceof Object) // true

// Built in object
var str = 'aaa'
str instanceof String // false, you can only judge the built-in objects created through new
var str = new String()
str instanceof String // true
var myDate = new Date()
myDate instanceof Date;     // true
myDate instanceof Object;   // true

// Judge inheritance
// Is Foo an instance of the Foo class and an instance of its parent type
 function Aoo(){} 
 function Foo(){} 
 Foo.prototype = new Aoo(); //JavaScript prototype inheritance

 var foo = new Foo(); 
 console.log(foo instanceof Foo) // true 
 console.log(foo instanceof Aoo) // true

4,Array.isArray()

This method is used to judge whether a variable is of array type, and the return value is true/false.

// Judgment array   
var arr = []
console.log(Array.isArray(arr)) // true

5,isNaN()

This method is used to judge whether a variable is of non numeric type, and the return value is true/false.

// Judge non numeric
var usrAge = 21;
console.log(isNaN(userAge)); // false, 21 is not a non number
var usrName = "andy";
console.log(isNaN(userName));// true, "andy" is a non number

6,===

We can judge null and undefined by = = =.

// null
console.log(null === null) // true
// undefined
console.log(undefined === undefined) // true

7,toString()

toString() is the prototype method of the Object. Calling this method returns [[Class]] of the current Object by default. This is an internal attribute in the form of [object Xxx], where Xxx is the type of Object.

Object.prototype.toString.call('') ;   // [object String]
Object.prototype.toString.call(1) ;    // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(Symbol()); //[object Symbol]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
Object.prototype.toString.call(new Error()) ; // [object Error]
Object.prototype.toString.call(document) ; // [object HTMLDocument]
Object.prototype.toString.call(window) ; //[object global] window is a reference to the global object global

Keywords: Javascript Front-end

Added by osram on Tue, 07 Dec 2021 22:43:54 +0200