JavaScript operators--relational operators

Source: Internet
Author: User
Tags object object

xTable of Contents [1] identity [2] equal [3] greater than [4] less than the preceding words

The relational operator is used to test the relationship between two values, and returns TRUE or false depending on whether the relationship exists, and the relational expression always returns a Boolean value, usually using a relational expression in an if, while, or for statement to control the execution process of the program

JavaScript provides = = =,!==, = =,! =, <, <=, >, >=8 relational operators, this article is divided into 4 classes to introduce relational operators

Identity operator

The identity operator ' = = = ', also called the strict equality operator, evaluates the value of its operand first, then compares the two values, and the comparison process does not have any type conversions, as follows:

"1" returns false if two values have different types

Console.log (1 = = = ' 1 '); // falseconsole.log (1 = = = [1]); // false

"2" returns true if all two values are undefined, Null, Boolean, number, string the same as the original type, or False if the value is the same.

Console.log (undefined = = = undefined); // trueconsole.log (nullnull); // true
Console.log (truetrue); // trueConsole.log (false); // true
Console.log (1 = = 1); // trueconsole.log (2.5 = = = 2.5); // true

[note] No matter what the numbers are, they are converted to decimal when the relationship is compared.

Console.log (= = = 0xa); // true

In the numeric number type, there is a value that is special, Nan (not a number), which is not equal to any value, and that there are +0 and 0 in the numeric number type, although the symbols are different but the values are equal

Console.log (nan = = = Nan); // falseconsole.log (+0 = = = 0); // true

Two identical string values are as follows: the same length and the same character correspond to the same position

Console.log (' abc ' = = = ' abc '); // trueconsole.log (' abc ' = = = ' ACB '); // false

"3" returns true if two values refer to the same object, otherwise, false

[note] A more detailed explanation is that the comparison of JavaScript objects is a reference comparison, not a comparison of values. The object and itself are equal, but not equal to any other object. If two different objects have the same number of attributes, the same property names and values, they are still unequal

Console.log ([] = = = []); // falseconsole.log ({} = = = {}); // false     Console.log (functionfunction() {}); // false
var a === = = b); // true

"Constant inequality operator"

The constant inequality operator (!==) is also called the strict non-equal operator, and the operands are compared in the same way as the identity operator, and the result is reversed. If the comparison result of ' = = = ' is true, then '!== ' compares the result to false; ' = = = ' Compares the result is false, then the '!== ' comparison result is true

Console.log (1!== ' 1 '); // trueconsole.log (1!== 1); // falseConsole.log (truefalse); // trueconsole.log ({}!== {}); // true

Equality operators

The equality operator ' = = ' is similar to the identity operator, but the equality operator is not strictly comparable, and if the two operands are not of the same type, the equality operator attempts some type conversion before comparing

The comparison rule and the identity operator rule are the same when the two operands are of the same type

Console.log (undefined = = undefined); // trueconsole.log (ten = = 0xa); // trueconsole.log (nan = = Nan); // falseconsole.log ([] = = []); // false

When the two operand types are different, the equality operator ' = = ' adheres to the following rules:

"1" If one value is an object type and the other value is the original type, the object type is first converted to the original value using valueof (), and if the result is not yet the original value, then the ToString () method is used to convert and then compare

[Note] The date class only allows the use of the ToString () method to convert to a string. Similarly, the time date object is converted to a string using ToString () when adding operations, and in other mathematical operations, including subtraction, multiplication, division, and redundancy, use the number () conversion function to convert a time Date object using valueof () to a numeric

"2" after the object is converted to the original value, if the two operands are strings, the string is compared

Console.log (new Date) = = ' Sat June 11:07:20 gmt+0800 (China Standard Time) '); // true

"3" After the object is converted to the original value, if at least one operand is not a string, then two operands are converted to numbers by the number () transformation function to numeric comparison

Console.log (true= = 1);//trueConsole.log (true= = 0);//falseConsole.log (false= = ' 1 ');//falseConsole.log (false= = ' 0 ');//trueConsole.log (true= = ' true ');//false, equivalent to 1 = = NaNConsole.log ([1] = = 1);//true, equivalent to 1 = = 1Console.log ([1] = = ' 1 ');//true, equivalent to ' 1 ' = = ' 1 'Console.log ([] = = 0);//true, equivalent to 0 = = 0Console.log ([] = = ' 0 ');//false, equivalent to ' = = ' 0 'Console.log ([]==true);//false, equivalent to 0 = = 1Console.log ([1] = =true);//true, equivalent to 1 = = 1
varA ={valueOf:function(){        return1; }, ToString:function(){        return' 2 '; }} console.log (a= = ' 1 ');//true, equivalent to 1 = = 1varA ={valueOf:function(){        return {}; }, ToString:function(){        return' 1 '; }} console.log (a= = 1);//true, equivalent to 1 = = 1

  [note] Returns true if one value is null and the other value is undefined. Although number (null) is 0, null and 0 are not equal

Console.log (null = = undefined); // trueconsole.log (null = = 0); // false

[note] empty string or space string will be converted to 0

Console.log (null = = []); // falseConsole.log (null = = "); // falseconsole.log ([] = = "); // false, equivalent to ' = =' Console.log ([] = = "); // true, equivalent to ' = =' ' console.log (0 = = '); // true

"Inequality operator"

The operand comparison procedure for the inequality operator (! =) is the same as the equality operator, and the result is reversed. If the comparison of ' = = ' results in true, then '! = ' results in a comparison of false; if ' = = ' compares the result is false, the comparison result of '! = ' is True

Console.log (1! = ' 1 '); // false, equivalent to 1! = 1console.log (true ! = ' 1 '); // false, equivalent to 1! = 1console.log (' true '! = 1); // true, equivalent to Nan! = 1console.log ([1]! = ' 1 '); // false, equivalent to ' 1 '! = ' 1 ' true); // false, equivalent to 1! = 1

Greater than operator

The greater-than operator (>) is used to compare two operands, and if the first operand is greater than the second operand, the greater-than operator evaluates to true, otherwise false

Operands that are greater than operators can be of any type, however, only numbers and strings can actually perform comparison operations, so those operands that are not numbers and strings are type-cast, and the type conversion rules are as follows:

"1" If the operand is an object, the object will first be converted to the original value using valueof (), and if the result is not yet the original value, then the ToString () method is used to convert

[note] In fact, in a native object, the valueof () method is converted to the original value, only the time date object that is converted to the numeric number type, and the other objects are string through the ToString () method

"2" after the object is converted to the original value, if the two operands are strings, the two strings are compared in alphabetical order, and the alphabetical order mentioned here refers to the index order of the 16-bit Unicode characters that make up the string

 Console.log (' B ' > ' a '); // true  console.log (' B ' > ' a '); // false  console.log ({}  > ' [a] '); // true, equivalent to ' [Object Object] ' > ' [a] '  console.log ({} > ' [P] '); // false, equivalent to ' [Object Object] ' > ' [P] '  console.log ([a]  > [b]); // false, equivalent to ' [a] ' > ' [b] '  console.log ([2] > [11]); // true, equivalent to ' [2] ' > ' [one] '  

[note] Although uppercase letters in the alphabet are in front of lowercase letters, uppercase < lowercase letters; The string object has a method of string comparison the Localecompare () method takes into account the natural language ordering and puts ' B ' behind ' a '. The method returns a negative number when the string precedes its argument in the alphabet, and returns a positive number when the string is in the alphabet after its argument

Console.log (' B '. Localecompare (' a ')); // 1console.log (' B ' > ' a '); // falseConsole.log (' B '. Localecompare (' a ')); // 1console.log (' B ' > ' a '); // true

"3" After the object is converted to the original value, if at least one operand is not a string, then two operands are converted to numbers for comparison

[note] in the Equals operator, the time date () object only allows conversion to a string through the ToString () method, not to a number through the valueof () method, whereas in the greater operator, the time date () object allows the priority use of the valueof () method to convert to a number

Console.log (NewDate () > 100);//true, equivalent to 1466826928667 >Console.log (true> [0]);//true, equivalent to 1 > 0Console.log (2 > 1);//trueConsole.log (> ' 2 ');//true, equivalent to > 2Console.log (NaN> 1);//falseConsole.log (1 > NaN);//falseConsole.log ({} >true);//false, equivalent to NaN > 1

[note] Although the result of NULL = = 0 is false, this is because JavaScript sets the result of NULL = = undefined to True. In the greater-than operation, null and undefined perform number () transformation function conversions to 0 and Nan respectively

Console.log (Undefined >-1); // false, equivalent to Nan >-1console.log (null >-1); // true, equivalent to 0 > 1console.log (undefined > 0); // false, equivalent to Nan > 0console.log (null > 0); // false, equivalent to 0 >    0

The plus operator and the comparison operator behave differently for numbers and strings, and the plus operator prefers strings, and strings are concatenated if one of its operands is a string. Comparison operators prefer numbers, and only strings are compared when two operands are strings.

Console.log (1 + 2); // 3console.log (' 1 ' + ' 2 '); // 'console.log ' (' 1 ' + 2); // ' 12 ', equivalent to ' 1 ' + ' 2 ' Console.log (2 > 1); // trueconsole.log (' 2 ' > ' 1 '); // trueconsole.log (' 2 ' > 1); // true, equivalent to 2 > 1

"Less than equals operator"

The less-than-equals operator (<=) does not rely on comparison rules that are less than or equal to the operator, but instead follows a comparison rule that is greater than the operator. If the comparison result of ' > ' is true, then ' <= ' compares the result to false; if ' > ' compares to False, then ' <= ' compares to True

Console.log (1 <= ' 0 '); // false, equivalent to 1 <= 0console.log (true <= ' 0 '); // false, equivalent to 1 <= 0console.log (' true ' <= 0); // false, equivalent to Nan <= 0console.log ([1] <= ' 0 '); // false, equivalent to ' 1 ' <= ' 0 ' true); // true, equivalent to 0 <= 1console.log (1 <= 1); // true

Less operator

The less-than operator (<) is used to compare two operands, and if the first operand is less than the second operand, the less-than operator evaluates to true, otherwise false

A less-than operator is similar to a type conversion rule that is greater than an operator.

"Greater than equals operator"

Similarly, the greater-than-equal operator (>=) does not rely on comparison rules that are greater than or equal to the operator, but rather follows the comparison rule of the less-than operator, with the result reversed. If the comparison of ' < ' is true, the result of ' >= ' is false; if ' < ' is false, the result of ' >= ' is true

Resources

"1" es5/relational operator Https://www.w3.org/html/ig/zh/wiki/ES5/expressions#.E5.85.B3.E7.B3.BB.E8.BF.90.E7.AE.97.E7.AC.A6
"2" Ruan one peak JavaScript standard reference Tutorial--syntax--comparison operator HTTP://JAVASCRIPT.RUANYIFENG.COM/GRAMMAR/OPERATOR.HTML#TOC6
"3" W3school-javascript advanced tutorial--ecmascript relational operator http://www.w3school.com.cn/js/pro_js_operators_relational.asp
"4" JavaScript authoritative Guide (6th edition), chapter 4th expressions and operators
"5" JavaScript Advanced Programming (3rd Edition), chapter 3rd Basic Concepts
"6" JavaScript DOM Programming Art (2nd Edition), chapter 2nd JavaScript syntax

JavaScript operators--relational operators

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.