Parse Relational operators in javascript Operators

Source: Internet
Author: User
Directory [1] constant [2] equal [3] greater than [4] less than the preceding value Relational operators are used to test the relationship between two values. true or false is returned based on whether the relationship exists, A relational expression always returns a Boolean value. It is usually used in an if, while, or for statement to control the execution flow of the program. javascript provides ,! ,,! , & Lt;, & lt;, & gt;, & gt; Directory

[1] constant [2] equal [3] greater than [4] less

Previous

Link # wiki/119.html "target =" _ blank "> the operator is used to test the relationship between two values. true or false is returned based on whether the relationship exists. The relational expression always returns a Boolean value, relational expressions are usually used in if, while, or for statements to control the execution process of a program.

Javascript provides = ,! =, = ,! =, <, <=,>,> = Eight Relational operators. This article introduces four types of Relational operators.

Constant equal Operator

The constant operator '=' is also called the strict equality operator. First, the value of the operand is calculated, and then the two values are compared. The comparison process does not involve any type conversion. The comparison process is as follows:

[1] If the two values have different types, false is returned.

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

[2] If both values are Undefined, Null, Boolean, Number, and String values of the same original type, true is returned if the values are the same. Otherwise, false is returned.

console.log(undefined === undefined);//trueconsole.log(null === null);//true


console.log(true === true);//trueconsole.log(false === false);//true


console.log(1 === 1);//trueconsole.log(2.5 === 2.5);//true

[Note] No matter what hexadecimal number, during relational comparison, it is eventually converted to decimal for calculation.

console.log(10 === 0xa);//true


In the Number type, a special value is NaN (not a number), which is not equal to any value. In addition, there are + 0 and-0 in the Number type. Although their symbols are different, their values are equal.

console.log(NaN === NaN);//falseconsole.log(+0 === -0);//true


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

console.log('abc' === 'abc');//trueconsole.log('abc' === 'acb');//false


[3] If two values reference the same object, true is returned. Otherwise, false is returned.

[Note] A more detailed explanation is that comparing javascript objects is a reference comparison, not a value comparison. An object is equal to itself, but not to any other object. If two different objects have the same number of attributes and the same attribute names and values, they are still not equal

console.log([] === []);//falseconsole.log({} === {});//false    console.log(function(){} === function(){});//false


var a = {};b = a;console.log(a === b);//true


[Constant unequal operator]

Constant unequal operator (! =) Strictly not equal to the operator. The comparison process of the operands is the same as that of the constant operator, and the result is reversed. If the comparison result of '=' is true '! = ', The comparison result is false. If' = ', the comparison result is false, then '! = 'The comparison result is true.

console.log(1 !== '1');//trueconsole.log(1 !== 1);//falseconsole.log(true !== false);//trueconsole.log({} !== {});//true
Equal Operators

The equal operator '=' is similar to the constant operator, but the comparison of equal operators is not strict. If the two operands are not of the same type, the equal operator will try to perform some type conversion before comparison.

When the two operands are of the same type, the comparison rule is the same as the constant operator rule.

console.log(undefined == undefined);//trueconsole.log(10 == 0xa);//trueconsole.log(NaN == NaN);//falseconsole.log([] == []);//false


When the two operands are of different types, the equal operator '=' follows the following rules:

[1] If one value is of the object type and the other value is of the original type, the object type is first converted to the original value using valueOf (). If the result is not of the original value, the toString () method is used for conversion and comparison.

[Note] the date class can only be converted to a string using the toString () method. Similarly, the time Date object is converted to a string by using toString () during addition operations, while in other mathematical operations, including subtraction, multiplication, division, and remainder operations, the Number () conversion function is used to convert the time Date object to a Number using valueOf ().

[2] After the object is converted to the original value, if both operands are strings, the string is compared.

Console. log (new Date () = 'sat Jun 25 2016 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, both operands will be converted to numbers through the Number () transformation function for numerical 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


Var a = {valueOf: function () {return 1 ;}, toString: function () {return '2' ;}} console. log (a = '1'); // true, equivalent to 1 = 1var a = {valueOf: function () {return {};}, toString: function () {return '1';} console. log (a = 1); // true, equivalent to 1 = 1


[Note] If one value is null and the other value is undefined, true is returned. Although Number (null) is 0, null and 0 are not equal.

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


[Note] An empty string or space string is converted to 0.

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


[Unequal operator]

Unequal operator (! =. If the comparison result of '=' is true '! The comparison result of = 'is false; if the comparison result of' = 'is false, then '! = 'The comparison result 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' console. log ([1]! = True); // false, equivalent to 1! = 1


Greater than Operator

The greater than operator (>) is used to compare two operands. If the first operand is greater than the second operand, the calculation result greater than the operator is true. Otherwise, the value is false.

Operations greater than the operator may be of any type. However, only numbers and strings can be used for comparison. Therefore, operations other than numbers and strings will be converted. The type conversion rules are as follows:

[1] If the operand is an object, the object will first use valueOf () to convert it to the original value. If the result is not the original value, then use toString () to convert it.

[Note] In fact, in a native object, the valueOf () method is used to convert to the original value. Only the time Date object of the numeric Number type is converted () the method is a string.

[2] After the object is converted to the original value, if both operands are strings, the two strings are compared in alphabetical order, the alphabetic order mentioned here refers to the index order of the 16-bit unicode characters that constitute the string.

Console. log ('B'> 'A'); // trueconsole. log ('B'> 'A'); // falseconsole. 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]'> '[11]'


[Note] Although uppercase letters are written before lowercase letters, uppercase letters <lowercase letters are used. However, the String object has a String comparison method: localeCompare () the method sorts 'B' after 'A' based on natural language sorting. This method returns a negative number when the string is placed before its parameter in the alphabet; returns a positive number when a string is placed after its parameter in the alphabet.

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, both operands are converted to numbers for comparison.

[Note] In the equals operator, the time Date () object can only be converted to a string using the toString () method, but cannot be converted to a number using the valueOf () method; in the greater than operator, the time Date () object allows conversion to numbers using the valueOf () method first.

Console. log (new Date ()> 100); // true, equivalent to 1466826928667> console. log (true> [0]); // true, equivalent to 1> 0console. log (2> 1); // trueconsole. log (11> '2'); // true, equivalent to 11> 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, the Number () transformation functions of null and undefined are converted 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


For numbers and strings, the behavior of the plus sign operator and the comparison operator is different. The plus sign operator prefers a string. If one of its operands is a string, the string is connected. Comparison operators prefer numbers. String comparison is performed only when both operands are strings.

Console. log (1 + 2); // 3console. log ('1' + '2'); // '12' 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


Operator less than or equal]

The less than or equal operator (<=) does not depend on the comparison rules of the less than or equal operator, but follows the comparison rules of the greater than operator. The result is reversed. If the comparison result of '>' is true, the comparison result of '<=' is false. If '>' is false, the comparison result of '<=' is 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' console. log ([0] <= true); // true, equivalent to 0 <= 1console. log (1 <= 1); // true


Less than Operator

The less than operator (<) is used to compare two operands. If the first operand is smaller than the second operand, the calculation result smaller than the operator is true. Otherwise, the value is false.

The less than operator is similar to the type conversion rule that is greater than the operator.

Operator greater than or equal]

Similarly, the greater than or equal operator (> =) does not depend on the comparison rules of the greater than or equal operator, but follows the comparison rules of the less than operator and returns the inverse result. If '<' is true, '> =' is false. If '<' is false, '> =' is true.

The preceding section describes how to parse Relational operators in javascript operators. For more information, see other related articles in the first PHP community!

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.