Home > Web Front-end > Front-end Q&A > How to determine whether two strings are equal in es6

How to determine whether two strings are equal in es6

青灯夜游
Release: 2022-04-19 17:09:04
Original
3072 people have browsed it

Judgment method: 1. Use the "==" operator to compare whether the values ​​on both sides of the equation are equal. The syntax is "String 1 == String 2"; 2. Use "Object.is() ", syntax "Object.is(String 1, String 2)", if the two strings have the same length and the same characters are arranged in the same order, the two strings are equal.

How to determine whether two strings are equal in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

es6 Determine whether two strings are equal

Method 1: Use the == operator

==Operator, you can compare whether the values ​​on both sides of the equation are equal

Example: Determine whether two strings are equal

var x = "hello";
var y = "HELLO";
var z = "hello";
console.log(x == y);  // 输出: false
console.log(x == z);  // 输出: true
Copy after login

How to determine whether two strings are equal in es6

Method 2: Use the Object.is() method

Object.is() method to determine whether two values ​​are the same value. The two values ​​are equal if the following conditions are met:

  • are both undefined

  • are null

  • are all true or false

  • are all strings of the same length and the same characters are arranged in the same order

  • are all the same object ( means that each object has the same reference)

  • is a number and

    • is 0

    • are all -0

    • are all NaN

    • or are both non-zero and non-NaN and are the same value

Example: Determine whether two strings are equal

var x = "hello";
var y = "HELLO";
var z = "hello";
console.log(Object.is(x,z));  // 输出: true
console.log(Object.is(x,y));  // 输出: false
Copy after login

How to determine whether two strings are equal in es6

[Related recommendations: javascript video tutorialweb front end

The above is the detailed content of How to determine whether two strings are equal in es6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
es6
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