Home > Article > Web Front-end > !=, ==, !==, === usage summary
This time I will bring you a summary of the use of !=, ==, !==, ===, what are the precautions when using !=, ==, !==, === , the following is a practical case, let’s take a look.
var num = 1; var str = '1'; var test = 1; test == num //true 相同类型 相同值 test === num //true 相同类型 相同值 test !== num //false test与num类型相同,其值也相同, 非运算肯定是false num == str //true 把str转换为数字,检查其是否相等。 num != str //false == 的 非运算 num === str //false 类型不同,直接返回false num !== str //true num 与 str类型不同 意味着其两者不等 非运算自然是true啦
== and != If the types are different, try converting the type first, then perform value comparison, and finally return the value comparison result.
And
=== and !== will only compare their values if they are of the same type.
First of all, == equality is equal, === identity is equal.
==, when the value types on both sides are different, type conversion must be performed first, and then compared.
===, no type conversion is performed, and different types must not be equal.
The following are explained separately:
Let’s talk about === first, this is relatively simple. The following rules are used to determine whether two values are === equal:
1. If the types are different, [not equal]
2. If both are numerical values and are the same A value, then [equal]; (except!) if at least one of them is NaN, then [not equal]. (To determine whether a value is NaN, you can only use isNaN() to determine)
3. If both are strings, and the characters in each position are the same, then [equal ]; otherwise [not equal].
4. If both values are true, or both are false, then [equal].
5. If both values refer to the same object or function, then [equal]; otherwise [not equal].
6. If both values are null, or both are undefined, then [equal].
Let’s talk about ==, according to the following rules:
1. If the two value types are the same, perform === comparison.
2. If two value types are different, they may be equal. Perform type conversion and then compare according to the following rules:
a. If one is null and the other is undefined, then [equal].
b. If one is a string and the other is a numerical value, convert the string into a numerical value and then compare.
c. If any value is true, convert it to 1 and then compare; if any value is false, convert it to 0 and compare.
d. If one is an object and the other is a numerical value or string, convert the object into a value of the basic type and then compare. The object is converted to the base type using its toString or valueOf method. JS core built-in classes will try valueOf before toString; the exception is Date, which uses toString conversion. Non-js core objects, such as
e or any other combination (it’s more troublesome, I don’t quite understand it), are [not equal].
Example:
"1" == true
The types are not equal, true will be converted to the value 1 first, and now it becomes "1" == 1, then convert "1" into 1, compare 1 == 1, equal.
= Assignment operator
== equals
=== strictly equals
Example:
var a = 3;
var b = "3";
a==b returns true
a===b returns false
Because the types of a and b are different
=== Used for strict comparison judgment
var data = ({"val":"7","flag":"true"}); <FONT face=Verdana></FONT>
How to judge the value of flag below?
Because true with double quotes == is presumed to be a string true
If without double quotes==== it is a Boolean value true
This is very important, I have never had it before Figure this out
Writing method 1
if(data.flag=true){…}else{..}
No matter how you write it, it is correct. You cannot get the value of else at all. The reason is this This way of writing is equivalent to
if(true){…}
Writing 2
if(data.flag==true){…}else{..}
There is no such way of writing
##Writing 3
if(data.flag='true'){…}else{..}is correct no matter how you write it, you can’t get the value of else at all. The reason is that this way of writing is equivalent to the way of writing
if(true){…}
4
if(data.flag=='true'){…}else{..}
这个才是正确的写法
“=”:这个表示赋值,不是表示运算符
“==”:表示等于(值)
“===”:表示全等于(类型和值)
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of !=, ==, !==, === usage summary. For more information, please follow other related articles on the PHP Chinese website!