Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the equality operator in ECMAScript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what is the use of equality operators in ECMAScript, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

The equivalence operator is generally used to determine whether two variables are equal. This operation is fairly simple when dealing with raw values, but the task is slightly more complex when it comes to objects.

Gender operator points:

1. Equal and unequal signs are used to process original values

2. All equal signs and incomplete equal signs are used to process objects

I. Equal and non-equal signs (comparative values)

In ECMAScript,

An equal sign is represented by a double equal sign (==), which returns true if and only if the two operands are equal.

2, non-equal sign by exclamation mark plus equal sign (!=) Indicates that it returns true if and only if the two operands are not equal. To determine whether two operators are equal, both operators are type-cast.

The rules for performing type conversions are as follows:

If an operand is a Boolean value, convert it to a numeric value before checking for equality. false is converted to 0, true is 1

If one operand is a string and the other is a number, try converting the string to a number before checking for equality.

If one operand is an object and the other is a string, try converting the object to a string before checking for equality.

If one operand is an object and the other is a number, try converting the object to a number before checking for equality.

Chestnuts to say something:

If an operand is a Boolean value, convert it to a numeric value before checking for equality. false is converted to 0, true is 1

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

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

console.log(false == 1);//false

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

console.log(false != 0);//false

console.log(true != 1);//false

console.log(false != 1);//true

console.log(true != 0);//true

If one operand is a string and the other is a number, try converting the string to a number before checking for equality.

console.log("12" == 12);//true

console.log("12" != 12);//false

If one operand is an object and the other is a string, try converting the object to a string before checking for equality.

var obj = ['a']

var str = "a"

console.log(obj.toString());//a -> Convert to string and become a

console.log(obj == str);//true

console.log(obj != str);//fasle

If one operand is an object and the other is a number, try converting the object to a number before checking for equality.

var obj = ['12']

var Num = 12;

console.log(Number(obj));//12 -> Convert to number and become 12

console.log(obj == Num);//true

The operator also obeys the following rules when comparing:

1. null and undefined are equal

When checking equality, null and undefined cannot be converted to other values.

If an operand is NaN, an equal sign returns false, and a non-equal sign returns true.

If both operands are objects, their reference values are compared. The equal sign returns true if both operands point to the same object, otherwise the operands are unequal.

Here comes our good friend, Comrade Chestnut:

The values null and undefined are equal.

var a = '';

console.log(a[xss_clean]);//undefined

console.log(null == a[xss_clean]);//true

console.log(null != a[xss_clean]);//fasle

When checking equality, null and undefined cannot be converted to other values.

This is understood literally on the line, I do not know how to give you an example, CGL sorry everyone.

If an operand is NaN, the equal sign returns false, and the non-equal sign returns true.

console.log(NaN == null);//false

console.log(NaN == undefined);//false

console.log(NaN == NaN);//false

console.log(NaN == 0);//false

console.log(NaN != 0);//true

If both operands are objects, then their reference values are compared. The equal sign returns true if both operands point to the same object, otherwise the operands are unequal.

The example is a bit inappropriate. Let's just take a look. It probably means this.

var fn1 = function(){console.log ('I am fn 1')};

var fn2 = fn1

var fn3 = function(){fn1()}function(){ //Copy: www.gendan5.com

fn1()

fn2()

fn3()

console.log(fn1 == fn2);

console.log(fn2 == fn3);

var fn4 = [1,2,3];

var fn5 = [1,2,3];

console.log(fn4 == fn5);

II. Complete and incomplete equal signs (compare values and types)

Equal and non-equal operators are all equal and non-all equal. These two operators do the same thing as equals and non-equals except that they do not perform type conversions before checking for equality.

A full equal sign is represented by three equal signs (===) and returns true only if the operands are equal without type conversion.

2, not all equal signs by exclamation mark plus two equal signs (!==) Indicates that true is returned only if the type conversion operands are not equal without type conversion.

var num1 = 30;

var num2 = 30;

var str = "30";

console.log(num1 == num2);//true -> Same value, types are numbers

console.log(num1 == str);//false -> Same value, type is number and string

console.log(NaN === NaN);//false -> directly false, hard to remember

console.log(num1 != str);//false -> Different types, but same values

console.log(NaN != NaN);//true -> direct true, hard to remember

console.log(NaN !== NaN);//true -> direct true, hard to remember

console.log(num1 !== num2);//false -> Same value and type

Thank you for reading this article carefully. I hope the article "What is the use of equality operators in ECMAScript" shared by Xiaobian is helpful to everyone. At the same time, I hope you will support it a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report