JavaScript data type judgment
JavaScript data types are divided into two types: the original type (basic type) and the object type (reference type ). The primitive types include numbers, strings, and boolean values. There are two special primitive values: null and undefined, which are all objects. Objects also include two types of special objects: arrays and functions. If the return value of all the following code expressions is true, the judgment is true. Otherwise, the variable obj indicates the value to be determined. For general methods, the typeof operator can be used to determine the data type. The return value is a string representing the data type (note that it is a string and it is in lower case ): typeof 1 // 'number' typeof 'abc' // 'string' typeof false // 'boolean' typeof undefined // 'undefined' typeof null // 'object' typeof {x: 1} // 'object' typeof [1, 2, 3] // 'object' typeof function () {} // The 'function' typeof operator can effectively judge numbers, strings, Boolean values, undefined, and other primitive types. However, it is powerless when facing arrays and objects. Borrow an Object. prototype. the toString () method borrows the Object. prototype. the toString () method returns a string that represents the Object type: var toString = Object. prototype. toString; toString. call ('abc') // '[object String]' toString. call (true) // '[object Boolean]' toString. call ([]) // '[object Array]' toString. call ({}) // '[object Object]' toString. call (/. /) // '[object RegExp]' toString. call (new Date) // '[object Date]' toString. call (Math) // '[object Math] 'This method can be used to effectively determine the object types (reference types) such as arrays, functions, dates, and regular expressions ). In ECMAScript 5, you can use this method to determine null and undefined: toString. call (null) // '[object Null]' toString. call (undefined) // '[object Undefined]' is a special case. Use the typeof operator to determine any number, NaN, or Infinity for the original (basic) Number: typeof NaN // 'number' typeof Infinity // 'number' if you want to exclude NaN and Infinity, you can use the isFinite () method, but isFinite () this method is used to convert some non-numeric types to numbers. Therefore, double insurance is required: typeof obj = 'number' & isFinite (obj) if the above expression returns true, it ensures that the variable obj is numeric and not NaN or Infinity. After all, we do not want these two special values to be involved in mathematical operations. The Number. isFinite () method added by ECMAScript 6 has the same effect. An integer is used to determine whether a number is an integer and is in a safe range. After an integer is obtained, it is equal to itself: typeof obj = 'number' & isFinite (obj) & obj>-9007199254740992 & obj <9007199254740992 & Math. floor (obj) = objNaN the global isNaN () method also tries to implicitly convert some non-numeric types to numbers. If the conversion is successful, it will regard this value as a number, otherwise, the result of dividing NaN: isNaN (NaN) // trueisNaN (0/0) // true 0 by 0 is NaNisNaN ('1 ') // The false string '1' can be implicitly converted to the number 1 isNaN (true) // the false Boolean value true can be implicitly converted to the number 1 isNaN ([]) // false an empty array can be implicitly converted to the number 0 isNaN (Infinity) // falseisNaN ('abc') // trueisNaN (undefined) // The true string 'abc' and undefined cannot be implicitly converted to a number, therefore, it is regarded as a NaN. NaN is a special value. It is not equal to or even not equal to itself. Therefore, the best way to judge a value of NaN is to judge whether it is a numeric type and not equal to itself: typeof obj = 'number' & obj! = + Number added by objECMAScript 6. the isNaN () method solves this problem better. true: Number is returned only when the value is NaN. isNaN (NaN) // trueNumber. isNaN (Number. naN) // trueNumber. isNaN (0/0) // trueNumber. isNaN (Infinity) // falseNumber. isNaN ('abc') // falseNumber. isNaN (undefined) // false indicates the Number. the isNaN () method is different from the isNaN () method. The Number. isNaN () method is only used to determine whether it is a special value NaN. If the Boolean value is neither true nor false, you can use the typeof operator or determine the value as follows: obj = true | obj = falseUndefined in ECMAScript 3, undefined can be read and written. Therefore, it is not necessarily accurate to directly compare the returned results with undefined, in this way, var undefined = 1 can change its value in some implementations. At this time, the comparison result is a little unexpected. Generally, we use the typeof operator to judge the result: typeof obj = 'undefined'. However, when using the typeof operator, the undefined variables and undefined variables cannot be distinguished (the two are different ), another method is to use the void operator, because its operation result always returns undefined: obj = void 0Null using the typeof operator. Null will return 'object'. This is obviously not what you want, so the best way is to directly compare it with the null value: obj = null. = must be used here, because undefined = null will return true. When an attribute is read from a variable whose value is null or undefined, an error is thrown. Therefore, you may need to make a judgment and simply use the if statement to judge the attribute, all values that can be implicitly converted to false are excluded, such as numbers 0, empty strings, and empty arrays. They can be implicitly converted to false, since undefined = null will return true, and false will be returned if it is compared with any other non-null or undefined value, it is better to directly compare it with the null value: if (obj! = Null) {obj. property;} can be used to determine whether the variable obj is neither undefined nor null, or, as follows: typeof obj! = 'Undefined' & obj! = Null can be safely obtained by using the. syntax to obtain attributes through judgment. Object Type (reference type) objects must be different. null and other objects can be judged as follows, because the null value can be implicitly converted to false: obj & typeof obj = 'object' to remove null. The variable obj is an object, an array, or another object (excluding a function ). The following is another method: obj = Object (obj) use the Object () method. If the input parameter is not an Object, it will be converted to an Object. Otherwise, simply return the input parameters. This method can be used to determine all objects, including functions. Array Method: Object. prototype. toString. call (obj) = '[object Array]' ECMAScript 5 adds the native method for Array detection: Array. although the typeof operator treats the isArray (obj) function specially, the 'function' is returned if the typeof operator is not a function in some implementations ', therefore, the following method is used to determine the function: Object. prototype. toString. call (obj) = '[object Function]' regular expressions use the typeof operator to determine the regular expression. Generally, 'object' is returned, but some implementations still return 'function ', therefore, the Object is used. prototype. toString Method to Determine: Object. prototype. toString. call (obj) = '[object RegExp]'