Home >Web Front-end >JS Tutorial >How to compare two strings in JS
String comparison in JavaScript
Greater than (>), less than (<) operator
javascript string in When performing a greater than (less than) comparison, the comparison will be based on the ASCII value code of the first different character. When the number (number) is compared with the string (string), the number (number) will be forcibly converted into a character. string(string) and then compare.
Code:
(function(){ console.log('13'>'3'); // 输出:false console.log(5>'6'); // 输出: false console.log('d'>'ABDC') // 输出: true console.log(19>'ssf') // 输出 false console.log('A'>'abcdef') // 输出 false })()
Equality (==), strict equality (===) operator
When performing equality (==) operation comparison, if one side is a character and the other side is a number, the string will be converted into a number first and then compared; for strict equality (===), it will not be performed. Type conversion will compare types for equality. Note that NaN is false when compared with any value
(function(){ console.log('6'==6) // true console.log('6'===6) // false console.log(6===6) // true console.log('abc'==2) // false console.log('abc'=='abc') // true console.log('abc'==='abc') // true })()
3. Equality and strict equality comparison of some special values
(function(){ console.log(null==undefined) // 输出:true console.log(null===undefined) // 输出:false console.log(null===null) // 输出:true console.log(undefined===undefined) // 输出:true console.log(NaN==undefined) // 输出:false console.log(NaN==null) // 输出:false console.log(NaN==NaN) // 输出:false console.log(NaN===NaN) // 输出:false })()
The above is the detailed content of How to compare two strings in JS. For more information, please follow other related articles on the PHP Chinese website!