[译]javascript中的条件语句
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单
源地址在此:
https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b
Javascript的代码是从上到下一条条运行的.如果出于某些原因你想要打乱这个顺序,或者当某些条件满足的时候优先处理某些语句的话.这时候我们就要用到条件语句了
Javascript有如下的条件语句
if
if else
if else if else
switch
ternary operator - 是if..else语句的一种简写
if的例子
var userInput = Number(prompt("Please enter a number", "")); if (userInput == 1) { alert("You number is One"); } if (userInput == 2) { alert("You number is Two"); } if (userInput == 3) { alert("Your number is Three"); } if(userInput != 1 && userInput != 2 && userInput != 3) { alert("Your number is not between 1 and 3"); }
if-else if例子
var userInput = Number(prompt("Please enter a number", "")); if (userInput == 1) { alert("You number is One"); } else if (userInput == 2) { alert("You number is Two"); } else if (userInput == 3) { alert("Your number is Three"); } else { alert("Your number is not between 1 and 3"); }
小白前端学习之路