Table of Contents
Previous words
Identity operator
Equality operator
大于运算符
小于运算符
Home Web Front-end JS Tutorial Parsing relational operators in javascript operators

Parsing relational operators in javascript operators

Mar 31, 2017 am 09:53 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles