Js operator summary, js Operator

Source: Internet
Author: User

Js operator summary, js Operator

Logic or (|)

Var result = true | false;
Similar to logic and operation, if an operand is not a Boolean value, the logic may not necessarily return a Boolean value. In this case, it follows the following rules:
□If the first operand is an object, the first operand is returned.

□If the first operand is evaluated as false, the second operand is returned.

□If both operands are objects, the first operand is returned.

□If both operands are null, null is returned.

□If both are undefined, return undefined.

□If both are NaN, return NaN

The difference between "=" and "="

Equal operator (=)

The equal operator implicitly converts the operation values and then compares them:

If an operation value is a Boolean value, convert it to a value before comparison.
If one operation value is a string and the other operation value is a numerical value, the Number () function is used to convert the string to a numerical value.
If one operation value is an object and the other is not, call the valueOf () method of the object and compare the result according to the preceding rules.
Null and undefined are equal.
If an operation value is NaN, equal comparison returns false.
If both operation values are objects, compare whether they point to the same object. If both operands point to the same object, the equal operator returns true. Otherwise, false is returned.
The descriptions are as follows:
=, Which 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 the values are the same, [equal]; (! If at least one of them is NaN, [not equal]. (You can only use isNaN () to determine whether a value is NaN)
3. If both are strings and the characters at each position are the same, [equal]; otherwise [not equal].
4. If both values are true or false, [equal].
5. If both values reference the same object or function, [equal]; otherwise [not equal].
6. If both values are null or undefined, [equal].

Then, according to the following rules:
1. If the two values are of the same type, compare them with =.
2. If the two values have different types, they may be equal. Perform type conversion and comparison based on the following rules:
A. If one is null and the other is undefined, [equal].
B. if one is a string and the other is a numerical value, convert the string to a numerical value before comparison.
C. If any value is true, convert it to 1 for comparison. If any value is false, convert it to 0 for comparison.
D. If one is an object and the other is a value or string, convert the object to a base type value before comparison. Converts an object to a base type and uses its toString or valueOf method. Js core built-in class, will try valueOf prior to toString; the exception is Date, Date uses toString conversion. Non-js core objects (I don't know much about it)
E. Any other combinations are [not equal].

Expression value
Null = undefined true = 1 true
"NaN" = NaN false $ null = 0 false
False = 0 true NaN! = NaN true
In short, "=" only requires equal values. "=" Requires that the values and types are equal.

The materials are from the javascript advanced program design, sorted out, and updated later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.