要比较JavaScript中的两个字符串,请使用localeCompare()方法。如果两个字符串相等,则该方法返回0;如果字符串1在字符串2之前排序,则返回-1;如果字符串2在字符串1之前排序,则返回1。
您可以尝试运行以下代码来比较两个字符串
<!DOCTYPE html>
<html>
<body>
<button onclick="compareStr()">Compare Strings</button>
<p id="test"></p>
<script>
function compareStr() {
var string1 = "World";
var string2 = "World";
var result = string1.localeCompare(string2);
document.getElementById("test").innerHTML = result;
}
</script>
</body>
</html>