Home > Web Front-end > JS Tutorial > Parsing relational operators in javascript operators

Parsing relational operators in javascript operators

怪我咯
Release: 2017-03-31 09:53:48
Original
2087 people have browsed it

Directory

[1]Identity[2]Equal[3]Greater than[4]Less than

Previous words

 RelationshipOperator is used to test the relationship between two values. According to the relationship Returns true or false whether it exists or not. Relationship expression always returns a boolean value, usually in if, while or for statement Use relational expressions to control the execution flow of the program

javascript provides ===,!==,==,!=,<,<=,> ;, >= 8 relational operators, this article will be divided into 4 categories to introduce the relational operators

Identity operator

Identity operator '== =', also called the strict equality operator, first calculates the value of its operand, and then compares the two values. The comparison process does not have any type conversion. The comparison process is as follows:

 [1 】If the two values ​​are of different types, return false

console.log(1 === '1');//false
console.log(1 === [1]);//false
Copy after login

【2】If both values ​​are Undefined, Null, Boolean, Number, String If the values ​​of the same primitive type are the same, true will be returned, otherwise, false will be returned

console.log(undefined === undefined);//true
console.log(null === null);//true
Copy after login


console.log(true === true);//true
console.log(false === false);//true
Copy after login


console.log(1 === 1);//true
console.log(2.5 === 2.5);//true
Copy after login

[Note] No matter what base the number is in, when performing relational comparisons, it will eventually be converted to decimal for calculation

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


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

console.log(NaN === NaN);//false
console.log(+0 === -0);//true
Copy after login


The same string value is expressed as: the same length and the same character corresponding to the same position

console.log('abc' === 'abc');//true
console.log('abc' === 'acb');//false
Copy after login


 【3】If two valuesRefer to the same object , then return true, otherwise, return false

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

console.log([] === []);//false
console.log({} === {});//false    
console.log(function(){} === function(){});//false
Copy after login


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


【Identity inequality operator】

Identity inequality operator (!==) is also called the strict inequality operator. The comparison process of the operands is the same as the identity operator, and the result is negated. If the comparison result of '===' is true, then the comparison result of '!==' is false; if the comparison result of '===' is false, then the comparison result of '!==' is true

console.log(1 !== '1');//true
console.log(1 !== 1);//false
console.log(true !== false);//true
console.log({} !== {});//true
Copy after login

Equality operator

The equality operator '==' is similar to the identity operator, but the comparison of the equality operator is not strict. If the two operands are not the same Type, the equality operator will try to perform some type conversions before comparing

When the two operand types are the same, the comparison rules are the same as the identity operator rules

console.log(undefined == undefined);//true
console.log(10 == 0xa);//true
console.log(NaN == NaN);//false
console.log([] == []);//false
Copy after login


When the two operand types are different, the equality operator '==' will comply with the following rules:

[1] If one value is an object type and the other value is a primitive type, Then the object type will first be converted into the original value using valueOf(). If the result is not the original value, then it will be converted using the toString() method and then compared.

[Note] Date classes only allow the use of toString() Method to convert to string. Similarly, when adding timeDate objects, use toString() to convert them to strings, while in other mathematical operations, including subtraction, multiplication, division, remainder, etc., Number() is used ConversionFunctionConvert the time Date object into a number using valueOf()

【2】After the object is converted to the original value, if both operands are strings, perform a string Comparison

console.log(new Date() == 'Sat Jun 25 2016 11:07:20 GMT+0800 (中国标准时间)');//true
Copy after login


【3】After the object is converted to a primitive value, if at least one operand is not a string, both operands will be passed through Number() The transformation function is converted into a number for numerical comparison

console.log(true == 1);//true
console.log(true == 0);//false
console.log(false == '1');//false
console.log(false == '0');//true
console.log(true == 'true');//false,相当于1 == NaN

console.log([1] == 1);//true,相当于1 == 1
console.log([1] == '1');//true,相当于'1' == '1'
console.log([] == 0);//true,相当于0 == 0
console.log([] == '0');//false,相当于'' == '0'

console.log([] == true);//false,相当于0 == 1
console.log([1] == true);//true,相当于1 == 1
Copy after login


var a = {
    valueOf:function(){
        return 1;
    },
    toString:function(){
        return '2';
    }
} 
console.log( a == '1');//true,相当于1 == 1

var a = {
    valueOf:function(){
        return {};
    },
    toString:function(){
        return '1';
    }
} 
console.log( a == 1);//true,相当于1 == 1
Copy after login


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

console.log(null == undefined);//true
console.log(null == 0);//false
Copy after login


[Note] Empty strings or space strings will be converted to 0

console.log(null == []);//false
console.log(null == '');//false
console.log([] == ' ');//false,相当于'' == ' '
console.log([] == '');//true,相当于'' == ''
console.log(0 == '');//true
Copy after login


【不相等运算符】

  不相等运算符(!=)的操作数比较过程与相等运算符相同,结果取反。如果'=='的比较结果是true,则'!='的比较结果是false;如果'=='的比较结果是false,则'!='的比较结果是true

console.log(1 != '1');//false,相当于1 != 1
console.log(true != '1');//false,相当于1 != 1
console.log('true' != 1);//true,相当于NaN != 1
console.log([1] != '1');//false,相当于'1' != '1'
console.log([1] != true);//false,相当于1 != 1
Copy after login


大于运算符

  大于运算符(>)用于比较两个操作数,如果第一个操作数大于第二个操作数,则大于运算符的计算结果为true,否则为false

  大于运算符的操作数可能是任意类型,然而,只有数字和字符串才能真正执行比较操作,因此那些不是数字和字符串的操作数都将进行类型转换,类型转换规则如下:

  【1】如果操作数是对象,则这个对象将先使用valueOf()转换成原始值,如果结果还不是原始值,则再使用toString()方法转换

  [注意]实际上,在原生对象中,使用valueOf()方法转换为原始值的,只有转换为数字Number类型的时间Date对象,其他对象都通过toString()方法成了字符串

  【2】在对象转换为原始值之后,如果两个操作数都是字符串,则按照字母表的顺序对两个字符串进行比较,这里提到的字母表顺序是指组成这个字符串的16位unicode字符的索引顺序

Parsing relational operators in javascript operators

console.log('b' > 'a');//true
console.log('B' > 'a');//false

console.log({} > '[a]');//true,相当于'[object Object]' > '[a]'
console.log({} > '[p]');//false,相当于'[object Object]' > '[p]'

console.log([a] > [b]);//false,相当于'[a]' > '[b]'
console.log([2] > [11]);//true,相当于'[2]' > '[11]'
Copy after login


  [注意]虽然在字母表中大写字母在小写字母的前面,所以大写字母 < 小写字母;但字符串String对象有一个字符串比较的方法localeCompare()方法会考虑自然语言的排序情况,把'B'排在'a'的后面,该方法在字符串在字母表中排在其参数之前时,返回一个负数;字符串在字母表中排在其参数之后时,返回一个正数

console.log(&#39;B&#39;.localeCompare(&#39;a&#39;));//1
console.log(&#39;B&#39; > 'a');//false
console.log('b'.localeCompare('a'));//1
console.log('b' > 'a');//true</p>
<p><br>  【3】在对象转换为原始值之后,如果至少有一个操作数不是字符串,则两个操作数都转换成数字进行比较<br><br>  [注意]在等于<a href="http://www.php.cn/wiki/996.html" target="_blank">操作符</a>中,时间Date()对象只允许通过toString()方法转换为字符串,而不允许通过valueOf()方法转换为数字;而在大于操作符中,时间Date()对象允许优先使用valueOf()方法转换为数字 <br><br></p>
<pre class="brush:php;toolbar:false">console.log(new Date() > 100);//true,相当于1466826928667 > 100
console.log(true > [0]);//true,相当于 1 > 0

console.log(2 > 1);//true
console.log(11 > '2');//true,相当于11 > 2

console.log(NaN > 1);//false
console.log(1 > NaN);//false
console.log({} > true);//false,相当于 NaN > 1
Copy after login


  [注意]虽然null == 0的结果为false,这是因为javascript将null == undefined的结果设为true。在大于运算中,null和undefined进行Number()转型函数转换分别转换为0和NaN

console.log(undefined > -1);//false,相当于NaN > -1
console.log(null > -1);//true,相当于0 > -1
console.log(undefined > 0);//false,相当于NaN > 0
console.log(null > 0);//false,相当于0 > 0
Copy after login


  对于数字和字符串来说,加号运算符和比较运算符行为有所不同,加号运算符更偏爱字符串,如果它的一个操作数是字符串,就进行字符串连接。而比较运算符则更偏爱数字,只有在两个操作数都是字符串时,才进行字符串的比较

console.log(1 + 2);//3
console.log('1' + '2');//'12'
console.log('1' + 2);//'12',相当于 '1' + '2'

console.log(2 > 1);//true
console.log('2' > '1');//true
console.log('2' > 1);//true,相当于 2 > 1
Copy after login


【小于等于运算符】

  小于等于运算符(<=)并不依赖于小于或等于运算符的比较规则,而是遵循大于运算符的比较规则,结果取反。如果'>'的比较结果是true,则'<='的比较结果是false;如果'>'的比较结果是false,则'<='的比较结果是true

console.log(1 <= &#39;0&#39;);//false,相当于1 <= 0
console.log(true <= &#39;0&#39;);//false,相当于1 <= 0
console.log(&#39;true&#39; <= 0);//false,相当于NaN <= 0
console.log([1] <= &#39;0&#39;);//false,相当于&#39;1&#39; <= &#39;0&#39;
console.log([0] <= true);//true,相当于0 <= 1
console.log(1 <= 1);//true
Copy after login


小于运算符

  小于运算符(<)用于比较两个操作数,如果第一个操作数小于第二个操作数,则小于运算符的计算结果为true,否则为false

  小于运算符与大于运算符的类型转换规则类似,就不再赘述

【大于等于运算符】

  同样地,大于等于运算符(>=)并不依赖于大于或等于运算符的比较规则,而是遵循小于运算符的比较规则,结果取反。如果'<'的比较结果是true,则'>='的结果是false;如果'<'的比较结果是false,则'>='的结果是true

The above is the detailed content of Parsing relational operators in javascript operators. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template