Home  >  Article  >  Web Front-end  >  js operator summary_basic knowledge

js operator summary_basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:31:531233browse

Logical OR (||)

var result = true || false;
Similar to the logical AND operation, logical OR does not necessarily return a Boolean value if one of the operands is not a Boolean value; in this case, it follows the following rules:
□ If the first operand is an object, return the first operand.

□ If the first operand evaluates to false, the second operand is returned.

□ If both operands are objects, return the first operand.

□ If both operands are null, return null

□ If both are undefined, return undefined.

□ If both are NaN, return NaN

The difference between “===” and “==”

Equality operator (==)

The equality operator performs implicit conversion on the operation values ​​before comparison:

If an operand value is a boolean, convert it to a numeric value before comparing
If one operation value is a string and the other operation value is a numeric value, the string is converted to a numeric value through the Number() function
If one operation value is an object and the other is not, the valueOf() method of the object is called, and the results are compared according to the previous rules
null and undefined are equal
If an operation value is NaN, the equality comparison returns false
If both operand values ​​are objects, compare whether they point to the same object. The equality operator returns true if both operands point to the same object, otherwise, returns false
The following are respectively explained:
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 numeric values ​​and are the same value, then [equal]; (!Exception) is that 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 at 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 comparison 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 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, Ling Shuo (it’s more troublesome, I don’t quite understand it)
e. Any other combination is [not equal].

expression value expression value
null==undefined true true==1 true
"NaN" ==NaN false $null==0 false
false==0 true NaN!=NaN true
In short, "==" only requires equal values. "===" requires both value and type to be equal.

The information comes from javascript advanced programming, organized and updated later

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn