JavaScript comparison operators, strictly compare = = =

Source: Internet
Author: User

There are two ways to compare JavaScript: the strict comparison operator and the conversion type comparison operator. For strict comparison operators (three =), the case for Ture is that only two operands have the same type, and for the widely used comparison operator (two =), two operands are converted to the same type before being compared. For relational operators (two = =), the operands are first converted to their original values, and then compared by the same type.

For strings, use Unicode values based on the standard dictionary order

Characteristics of the comparison operation:

    • A strict comparison operator should be used for two strings that have the same character order, the same length, and the position of each character.
    • For numbers with the same number two, the strict comparison operator should be used, and Nan and any values are not equal, including themselves, and positive 0 equals negative zeros.
    • Strict comparison operators should be used for two Boolean operands with the same true or false.
    • Do not use a strict comparison operator or comparison operator to compare two unequal objects.
    • When comparing an expression and an object, only two operands refer to the same object (the pointer points to the same object).
    • For null and Undefined types, you should use strict comparison operators to compare themselves, using comparison operators to compare each other.
"Two-equal" comparison operatorsEDITEqual (= =)

The comparison operator converts a type to two operands of different types, and then makes a strict comparison. When two operands are objects, JavaScript compares their internal references and is equal only if their references point to the same object (region) in memory, that is, they have the same reference address in the stack memory.

Grammar
x = = y
Example
1   ==  1     // true"1" == 1 // true 1 == ‘1‘ // true 0 == false // true
Unequal (! =)

The inequality operator returns true only if the operands are not equal, and if the two operands are not of the same type, JavaScript attempts to convert it to a suitable type and then compare it. If the two operands are object types, JavaScript compares their internal reference addresses, only if they are not equal when referencing different objects in memory.

Grammar
X! = y
Example
1 !=   2     // true1 != "1" // false1 != ‘1‘ // false1 != true // false0 != false // false
Consistent/strict Equality (= = =)

The consistent operators do not type conversions and return true only if the operands are strictly equal

Grammar
x = = y
Example
3 === 3   // true3 === ‘3‘ // false
Inconsistent/Strictly unequal (!==)

Inconsistent operators return True when operands are unequal or different types

Grammar
X!== y
Example
3 !== ‘3‘ // true4 !== 3 // true
Relational operatorsEDITGreater than operator (>)

The greater than operator returns true only if the left operand is greater than the right operand

Grammar
X > Y
Example
4 > 3 // true
Greater than equals operator (>=)

Greater than equals operator returns True when the left operand is greater than or equal to the right operand

Grammar
X >= y
Example
4 >= 3 // true3 >= 3 // true
Less operator (<)

The less operator returns true only if the left operand is small less right operand the operand

Grammar
X < y
Example
3 < 4 // true
Less than equals operator (<=)

Less than equals operator returns True when the left operand is less than or equal to the right operand

Grammar
X <= y
Example
3 <= 4 // true
Using comparison operatorsEDIT

The standard equality operator (and == != ) uses the Abstract equality Comparison algorithm to compare the two operands. When the two operand types are unequal, attempts to convert them to the same type before the comparison. e.g. for an expression 5 == ‘5‘ , the operand 5 of the right string type is converted to a number before comparison.

The strict equality operator === ( !== and) uses Strict equality Comparison algorithm and attempts to compare two identical operands equally, and if they are of unequal type, they will always return false so5 !== ‘5‘。

Strict equality operators should be used when it is necessary to specify the type and value of the operand, or when the exact type of the operand is important. Otherwise, you can use the standard equality operator to compare when you allow the operands to be type-converted before the comparison.

When comparing the design type conversions (i.e., non–strict comparison), JavaScript operates on the operands of a string, number, Boolean, or object type as follows:

    • When you compare numbers and strings, the strings are converted to numeric values. JavaScript attempts to convert numeric literals to values of numeric types. First, a mathematical value is derived from the numeric literal, and then the value of the number type is rounded.
    • If one of the operands is a Boolean type, the Boolean operand, if true, is converted to 1 and, if False, to an integer 0, or 0.
    • If an object is compared to a number or string, JavaScript attempts to return the object's default value. The valueof operator attempts to convert an object to its original value (a string or numeric value) by means of the method, and ToString. If the attempt to convert fails, a run-time error is generated.
    • Note: When and only when compared to the original value, the object is converted to the original value. When the two operands are all objects, they are compared as objects, only if they refer to the same object that returns True.
Note:The type of the string object is an object, not a string! String objects are rarely used, so the following results may make you duang~duang~duang~~~
True as both operands is Type string (i.e. String primitives):' Foo '===' Foo 'var a=NewString( ' foo ' ) ;var b = new  String ( ' foo ' //false as A and B are Type Object and reference different Objectsa == b //false as A and B are Type Object and reference different Objectsa === b //true as A and ' foo ' are of different type and, the Object (a) //is converted to String ' foo ' before Comparisona ==  ' foo '               
Specifications

JavaScript comparison operators, strictly compare = = =

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.