About the problems of congruence and inequality, equality and inequality in JS

About the problems of congruence and inequality, equality and inequality in JS

Congruent and Incongruent

The comparison of the operands on both sides of the symbol is the same as the operator. For example, if the operands are equal, it returns true, otherwise it returns false. The equality and inequality operators do not convert the operands when comparing for equality, that is, they directly compare for equality or inequality without conversion.

congruent

Symbol: ===

let str1="1"
let str2=1
console.log(str1===str2) //false

Not all equal

symbol:! ==

let str1="1"
let str2=1
console.log(str1!==str2) //true

Equal and unequal

The equality and inequality operators also compare two operands. For example, the equality operator returns true if the two operands are equal, otherwise it returns false. However, the difference between the equality and inequality operators is that the equality and inequality operators will first perform forced type conversion on the operands, and then compare the converted operands.

equal

Symbol: ==

let str1="1"
let str2=1
console.log(str1==str2) //true
console.log(str1===str2) //false
let flag=true
let num=1
console.log(flag==num) //true
console.log(flag===num) //false

Not equal

symbol:! =

let str1="1"
let str2=1
let str3="hello"
console.log(str1!=str2) //false First type conversion, then comparison console.log(str1!==str2) //true For the equality operation, the two operands are not necessarily equal without forced type conversion.	
console.log(str2!=str3) //true	  
console.log(str2!==str3) //true

The rules for type conversion of operands for equality and inequality operators are as follows:

Boolean values ​​are directly converted to numeric values ​​for comparison: false: 0; true: 1

When a string encounters a number, it will be converted to a number and then compared.

For objects, if both operands are objects, compare whether they are the same object. If the two operands refer to the same object, return true; otherwise, return false. If one of the operands is an object, the valueOf method is called to get the original value, and then the comparison is performed (according to the above rules)

const p = new Object('zhangsan') // equivalent to const p = new String('zhangsan')
console.log(p.valueOf() == 'zhangsan') //true
console.log(p == 'zhangsan') //true
const p1={name:'zhangsan'}
let p2=p1
console.log(p1==p2) //true
p2={}
console.log(p1==p2) //false

For null and undefined, null and undefined are equal, but they will not be coerced no matter who they are compared to.

console.log(null==undefined) //true
console.log(null===undefined) //false For all equals, the return result is still false

For NaN, NaN is not equal to any value including itself. Comparison involving operators that are NaNs returns false if they are equal, and true if they are not equal.

console.log(NaN==NaN) //false
console.log(NaN!='') //true
console.log(NaN!=1) //true
console.log(NaN!={}) //true

This is the end of this article about congruence and inequality, equality and inequality in JS. For more relevant JS content about congruence and inequality, equality and inequality, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Briefly explain the difference between = = (equal to) and = = = (entirely equal to) in Javascript
  • Are the Event objects between js event processing functions identical?
  • The difference between equals (==) and congruent (===) in JavaScript
  • JavaScript equality operator usage instructions
  • Solve the problem that 0.1+0.2 is not equal to 0.3 in JavaScript
  • What is the code for not equal to in javascript?

<<:  Usage of Linux userdel command

>>:  How to quickly add columns in MySQL 8.0

Recommend

The latest popular script Autojs source code sharing

Today I will share with you a source code contain...

Docker file storage path, modify port mapping operation mode

How to get the container startup command The cont...

Solution to the docker command exception "permission denied"

In Linux system, newly install docker and enter t...

Implementation of multi-environment configuration (.env) of vue project

Table of contents What is multi-environment confi...

How to use CSS to achieve data hotspot effect

The effect is as follows: analyze 1. Here you can...

Sample code for implementing multi-application deployment using tomcat+nginx

Table of contents Multi-application deployment 1-...

CentOS 8 officially released based on Red Hat Enterprise Linux 8

The CentOS Project, a 100% compatible rebuild of ...

Docker installs and runs the rabbitmq example code

Pull the image: [mall@VM_0_7_centos ~]$ sudo dock...

How to deploy HTTPS for free on Tencent Cloud

Recently, when I was writing a WeChat applet, the...

Detailed installation and configuration tutorial of PostgreSQL 11 under CentOS7

1. Official website address The official website ...

Detailed explanation of this pointing in JS arrow function

Arrow function is a new feature in ES6. It does n...

Detailed Analysis of the Differences between break and last in Nginx

Let's talk about the difference first last, t...

Basic knowledge of HTML: a preliminary understanding of web pages

HTML is the abbreviation of Hypertext Markup Langu...