Javascript 如果条件,如何在javascript中比较两个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42319247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:03:43  来源:igfitidea点击:

how to compare two strings in javascript if condition

javascriptif-statementstring-comparison

提问by Michael Schwartz

I'm having trouble recalling how to compare these two strings in an if statement. What I'm string to do is check if my variable compareequals page1or page2if not, go to the else statement.

我无法回忆如何在 if 语句中比较这两个字符串。我要做的是检查我的变量是否compare等于page1page2如果不是,转到 else 语句。

var compare = "page3";

if (compare === "page1" || "page2") {
  document.body.innerHTML = "github url";
} else {
  document.body.innerHTML = "non-github url";
}

回答by Nina Scholz

You could check every option.

您可以检查每个选项。

if (compare === "page1" || compare === "page2") {

Or you could use an array and check with an existential quantifier like Array#someagainst, like

或者你可以使用一个数组并检查一个存在量词,比如Array#some反对,比如

if (["page1", "page2"].some(a => a === compare)) {

var compare = "page3";

if (compare === "page1" || compare === "page2") {
    document.body.innerHTML = "github url";
} else {
    document.body.innerHTML = "non-github url";
}

回答by Scott Marcus

Anytime you have multiple things to check in an ifcondition, you must write each condition separate from the other. So, the test must be written as:

任何时候您有多项要检查一个if条件时,您必须将每个条件分别编写。所以,测试必须写成:

// If compare equals "page1" OR compare equals "page2"
if (compare === "page1" || compare === "page2") {

When you have a single variable that may contain many different values, using a switchstatement can be more logical and more efficient since it only has to look up the value of the variable one time.

当您有一个可能包含许多不同值的单个变量时,使用switch语句会更合乎逻辑且更高效,因为它只需查找变量的值一次。

Also, remember that strings are literals and "page1" does not equal "Page1". To make a comparison that is case-insensitive, you can force all the values to lower (or upper) case first and then check them against each other (as shown here):

另外,请记住,字符串是文字,而 "page1" 不等于 "Page1"。要进行不区分大小写的比较,您可以先将所有值强制为小写(或大写),然后相互检查(如下所示):

switch (compare.toLowerCase()) {
    case "page1" :
        // Do work here
        break;
    case "page2" :
        // Do work here
        break;
    case "page3" :
        // Do work here
        break;
    default :
        // Do work here
        break;
}

回答by Nagibaba

a.localeCompare(b)is another cool way of comparing larger strings

a.localeCompare(b)是另一种比较大字符串的很酷的方法

function areEqual(a, b){


  if (a.length !== b.length) {
         return false;
  }

  return a.localeCompare(b) === 0;
}


if(areEqual(a,b)){...}

回答by Kamil Kie?czewski

Try

尝试

if( ["page1", "page2"].includes(compare) ){...}

var compare = "page3";

if( ["page1", "page2"].includes(compare) ) {
  document.body.innerHTML = "github url";
} else {
  document.body.innerHTML = "non-github url";
}