Home > Web Front-end > JS Tutorial > body text

How to determine whether strings are equal in js

不言
Release: 2018-08-15 17:43:37
Original
19456 people have browsed it

The content of this article is about how to determine whether strings are equal in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Generally, "==" or "===" is used to judge the matching of two strings. The difference is:
1.== equality is the same, === identity is the same.
2.==, When the value types on both sides are different, type conversion must be performed first and then compared.
3.==, no type conversion is performed, and different types must not be equal.
①"=="match:

不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等var str1="a";
var str2="b";
var str1="a";
if(str1 == str2)alert("相等");
else alert("不等");
Copy after login

②"==="match:

===如果类型不同,其结果就是不等var str2="1";
var str2="b";
var str1="a";
if(str1 === str2)alert("相等");
else alert("不等");
Copy after login

③mach object comparison:

var str1='1;2;3';
var str2='231';
function M(str1,str2){
function sort(s){return s.match(/\d/g).sort()+'' }
return sort(str1)==sort(str2)
}
alert( M(str1,str2)?'相等':'不等');
Copy after login

④equals method, such as:

if(pwd1.equals(pwd2)) {
...}
Copy after login

Related recommendations:

JS string to remove duplicate characters

js intercepts strings Common methods for intercepting strings

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

Related labels:
js s
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