Home  >  Article  >  Web Front-end  >  How to determine whether strings are equal in js

How to determine whether strings are equal in js

不言
不言Original
2018-08-15 17:43:3719443browse

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("不等");

②"==="match:

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

③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)?'相等':'不等');

④equals method, such as:

if(pwd1.equals(pwd2)) {
...}

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!

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