Home  >  Article  >  Web Front-end  >  How to use the Object.is() method in JavaScript? (code example)

How to use the Object.is() method in JavaScript? (code example)

青灯夜游
青灯夜游Original
2019-04-22 15:23:168867browse

In JavaScript, the Object.is() method can be used to determine whether two values ​​are the same. The following article will introduce to you how to use the JavaScript Object.is() method and understand the difference between the Object.is() method and the == operator. I hope it will be helpful to you. [Video tutorial recommendation: JavaScript tutorial]

How to use the Object.is() method in JavaScript? (code example)

JavaScript Object.is() method

The Object.is() method is used to determine whether two values ​​are the same; it accepts two parameters, which are the values ​​to be compared, and returns a Boolean value indicating whether the two parameters are the same. Syntax:

Object.is(value1, value2)

Two values ​​can be the same if they have one of the following properties:

●If both values ​​are undefined .

● If both values ​​are null.

● If both values ​​are true or false.

● If two strings have the same length, the same characters and the same order.

● If both values ​​are numbers and both are "0".

● If both values ​​are numbers and both are "-0".

● If both values ​​are numbers and both are "NaN"; or neither is NaN, both are non-zero, and both have the same value.

Object.is() method can be applied to:

●Object.is() is used to compare two strings.

●Object.is() is used to compare two numbers.

●Object.is() is used to compare two objects.

Example 1:

Object.is('PHP中文网', 'PHP中文网');

Output:

How to use the Object.is() method in JavaScript? (code example)

Example 2:

Object.is(0,-0);

Output:

How to use the Object.is() method in JavaScript? (code example)

Difference between Object.is() method and "==" operator

1. The "==" and "===" operators treat the numeric values ​​"0" and "-0" as equal, while the Object.is() method treats them as equal. is not equal to.

2. If both values ​​are numbers and both are "NaN", the "==" and "===" operators will not treat them as equal; and the Object.is() method are considered equal.

var a=NaN;
var b=NaN;
console.log(a==b);

Output:

How to use the Object.is() method in JavaScript? (code example)

The above is the detailed content of How to use the Object.is() method in JavaScript? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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