Type comparison in javascript

1. Six values ​​in js

There are six false values ​​in js, which are 0, "", null, undefined, NaN, false , and others (including {}, [], infinity) are true.

You can use the Boolean() function or negate twice to get the Boolean value of the object. For example, Boolean(undefined) and !!undefined can also get the Boolean value false


Two, js comparison rules

If there is a boolean in the two compared, the boolean will be converted to the corresponding number first, that is, 0 and 1

If one of the two sides of the comparison is a number and the other is a string, the string will be converted to a number

When converting string directly to boolean, an empty string is converted to false, and all other strings are true

1. if ('0') alert("'0' is true");
2. if ('0' == false) alert("'0' is false");
3.  
4. 运行结果:两次都会alert

Ⅰ.NaN

NaN is not equal to any value, including NaN itself.

Ⅱ. Equality operator

Equality and inequality operators follow these basic rules when converting between different data types:

1. If one of the operands is a Boolean value, it is converted to a numeric value before comparing for equality—false is converted to 0, and true is converted to 1;

2. If one operand is a string and the other operand is a number, convert the string to a number before comparing for equality;

3. If one operand is an object and the other operand is not, call the valueOf() method of the object, and use the obtained basic type value to compare according to the previous rules;

Ⅲ.Number type

The conversion rules of the Number() function are as follows:

1. If it is a Boolean value, true and false will be converted to 1 and 0 respectively

2. If it is a numeric value, it is simply passed in and returned.

3. If it is a null value, return 0.

4. If it is undefined, return NaN.

5. If it is a string, follow the following rules:

5.1 If the string contains only numbers (including cases preceded by a plus or minus sign), convert it to a decimal value, that is, "1" will become 1, "123" will become 123, and "011" will become 11 (note: leading zeros are ignored);

5.2 If the string contains a valid floating-point format, such as "1.1", convert it to the corresponding floating-point value (again, leading zeros are also ignored)

5.3 If the string contains a valid hexadecimal format, such as "0xf", convert it to a decimal integer value of the same size;

5.4 If the string is empty (contains no characters), convert it to 0;

5.5 If the string contains characters other than the above format, convert it to NaN.


Ⅳ.'true'==true; why return false

First, 'true' == true conforms to rule 1, which translates into evaluating 'true' == 1

At this point, the expression conforms to rule 2, and the Number function is used to convert 'true' into a value. According to rule 5 and rule 5.5, the value of Number('true') is NaN, which means that the current problem becomes NaN == 1 is evaluated.

Then, according to the NaN rule, the value of NaN == 1 is false.

Finally, stroke it again, 'true' == true --> 'true' == 1 --> NaN == 1 --> false


3. The difference and usage of ! and !!

1. ! Variables can be converted to boolean type, null, undefined and empty string inversion are all true

2. ! ! It is often used for type judgment, after the first step! (variable), do logical negation


 let a;
  
 if(a!=null&&typeof(a)!=undefined&&a!=''){
  
 //a有内容才执行的代码
  
 }
  
 if(!!a){
  
 //a有内容才执行的代码...
  
 }

 //上面两种写法实现的功能一样,下面一种明显更简单

Fourth, the difference between == and ===

1. ===: It is called an equivalence symbol. When the types of the values ​​on both sides are the same, compare the values ​​directly. If the types are not the same, return false directly;

2. ==: It is called an equivalence symbol. When the types on both sides of the equal sign are the same, directly compare whether the values ​​are equal. If they are not the same, first convert to a value of the same type, and then compare;

Type conversion rules:

 1) If any two of boolean, string, or number are on both sides of the equal sign for comparison, convert to numbers first for comparison.

2) If null or undefined appears on both sides of the equal sign, null and undefined are equal to each other except that they are equal to themselves

Note: NaN==NaN //returns false, NaN is not equal to all values ​​including itself.


Added: There are some interesting properties about null and undefined:

If the typeof operator is used on a variable whose value is null, the result is object;

And use typeof on the value of undefined, the result is undefined


Five, typeof and instanceof usage

1. typeof is used to obtain the type of a variable or expression, typeof generally can only return the following results:

number, boolean, string, function (function), object (NULL, array, object), undefined.

Note: We can use typeof to get whether a variable exists, such as if(typeof a!="undefined"){}, instead of using if(a) because if a does not exist (undeclared), an error will occur.


Just because typeof will return the object type when encountering null, array, and object, so when we want to judge whether an object is an array, or whether a variable is an instance of an object, we must choose to use another key syntax instanceof


2. instanceof is used to determine whether a variable is an instance of an object

var a=new Array();
 alert(a instanceof Array); 
//会返回true,
alert(a instanceof Object)
//也会返回true;这是因为Array是object的子类。
function test(){};
var a=new test();
alert(a instanceof test)
//会返回true

console.log(true instanceof Boolean); 返回false
console.log(new Boolean(true) instanceof Boolean);  返回true

Guess you like

Origin blog.csdn.net/Primary_Insist/article/details/129275695