JavaScript - 获得第一个条件的价值

时间:2017-09-06 15:38:28

标签: javascript if-statement

不确定如何标题。尽管如此 - 请考虑您有以下声明

if (messageOne || messageTwo || messageThree || messageFour) {
  // do something here ...
 }

你会如何抓住并使用得到满足的信息?例如

if (messageOne || messageTwo || messageThree || messageFour) {
  console.log(messageOne) // <-- but rather than hardcoding messageOne I want to be able to grab whichever one is executed
 }

如果问题措辞不当,请道歉,如有必要可以澄清。

谢谢!

1 个答案:

答案 0 :(得分:6)

你可以使用

let result = messageOne || messageTwo || messageThree || messageFour;

&#13;
&#13;
let messageOne = null;
let messageTwo = null;
let messageThree = null;
let messageFour = "4";

let result = messageOne || messageTwo || messageThree || messageFour;

console.log(result);
&#13;
&#13;
&#13;