Brendan Eich didn’t have a type system in JavaScript when he designed it, so JavaScript is a dynamic language, and the type is only determined when it comes into the execution context, so you might have a JavaScript variable that was a String at the last moment, The next moment it’s Number, and in the process, the JavaScript variable is typed. When using Javascript for equality, we might use “==”, “===” and object. is to compare two values for equality.
- When we use ===, identity, strictly compare operators, no type conversions, different types are unequal, NaN is not equal to NaN.
- When object.is () is used, the behavior is basically the same as “===”, except for two differences:
- Plus 0 is not equal to minus 0.
- NaN is equal to itself.
- Different types of values can also be treated as equal when == is used. This can be confusing for many JavaScript developers. What type changes happen to different data types when “==” is used?
Javascript data types
The latest ECMAScript standard defines eight primitive data types, which are divided into seven primitive data types and one reference type.
- Basic types of
Undefined, Null, String, Number, Boolean, Symbol and BigInt are all primitive types. Primitive types are immutable. Immutable refers to the difference between the primitive type itself and a variable assigned as a primitive type. Variables are assigned a new value, and the original value cannot be changed like arrays, objects, and functions.
- Reference types: Object, including Array, Function, RegExp, and Date.
Conversion rules for different types of values using the “==” operator
Type (x) | Type (y) | Type (y) |
---|---|---|
null | undefined | true |
undefined | null | true |
digital | string | x == toNumber(y) |
string | digital | y== toNumber(x) |
Boolean value | Any type | toNumber(x) == y |
Any type | Boolean value | x == toNumber(y) |
String or number | object | x == toPrimitive(y) |
object | String or number | toPrimitive(x)== y |
ToNumber’s rules for converting different types of values:
- undefined: NaN
- null: +0
- Boolean: true: 1, flase: 0
- Value Returns the corresponding value
ToPrimitive does not convert primitive types; the output is equal to the input, and for objects, returns the original value if the valueOf method of the object returns the original value, if the toString method of the object returns the original value, and otherwise returns an error.
ToNumber and toPrimitive are among the internal methods described by ECMA.
Example:
1 console.log('juejin' ? true : false); // true
2 console.log('juejin' == true); // false
Copy the code
The first line of code executes true and the second line executes false. Why is this?
- According to the rules given above,true is converted to 1 by the toNumber method, that is, ‘juejin’ == 1,
- ‘juejin’ was converted to NaN by the toNumber method, that is, the final expression becomes NaN == 1, so return false.
Does ‘juejin’ == false return true? The answer is false again. Why is that?
- According to the rules given above,false is converted to 0 by the toNumber method, that is, ‘juejin’ == 0,
- ‘juejin’ was converted to NaN by the toNumber method, that is, the final expression becomes NaN == 0, so return false.
Extension – Data type determination
- Typeof, a basic data type, can be checked by the typeof operator in addition to null:
- Undefined: typeof instance === “undefined”
- Boolean: typeof instance === “Boolean”
- Number: typeof Instance === “Number”
- String: typeof Instance === “String
- BigInt: typeof Instance === “BigInt”
- Symbol :typeof instance === “symbol”
Null is also a basic type, typeof null === “object”, this is a bug in the JS language itself. In js, the first three bits of the binary are all zeros, so the first three bits of the binary are all zeros, so the first three bits of the binary are all zeros, so the first three bits of the binary are all zeros, so the typeof returns “object”. In addition, functions within reference types can also be determined using typeof:
- Function :typeof instance === “function”
All other reference types return “object”.
- instanceof
The instanceof operator is used to test whether constructor. Prototype exists on the prototype chain of an instance object.
function Class1() {
console.log('Class1');
}
function Class2() {
console.log('Class2');
}
const obj1 = new Class1();
obj1 instanceof Class1; // true
obj1 instanceof Class2; // false
Class1.prototype = {};
obj1 instanceof Class1; // false
Copy the code
. In addition, the Object prototype. ToString. Call and the construct can also carries on the data type of judgment.