js中的两个if?

问题描述 投票:0回答:1

我的网站上有一个密码,我怎样才能做两个如果?

喜欢,我有

if (password==pass1) {
  one()
}
else {
  three()
}

我如何添加第二个接受的答案?

喜欢,

if (password==pass1) {
  one()
}
if (password==pass2) {
  two()
}
else {
  three()
}

我尝试过的一切都打破了它。

var密码是一个提示。

我希望 pass1 做一件事,pass2 做另一件事,以及其他任何事情做第三件事。

javascript if-statement logic
1个回答
-1
投票

您可以使用

else if
语法,或者,为了获得更“清晰”的代码,您可以使用
switch case

else if
:

if (password==pass1) {
  one()
}
else if (password==pass2) {
  two()
}
else {
  three()
}

switch case
:

switch(password) {
    case pass1:
        one();
        break;
    case pass2:
        two();
        break;
    case pass3:
        three();
        break;
    default:
        console.log(password)
        password;
}
© www.soinside.com 2019 - 2024. All rights reserved.