if / else语句中的语法无效导致语法错误

时间:2016-04-30 05:13:13

标签: javascript

// Check if the user is ready to play!

confirm("Are you ready to play?");

var age = prompt("What's your age");

if ( age is less than 13)
{
    console.log("You are allowed to play,but we take no responsibility");
}

else {
console.log("Go on! you can play");
} 

执行此JavaScript代码时出现语法错误,前两行(确认和变量)正确,此错误位于if / else语句中。

2 个答案:

答案 0 :(得分:2)

使用<运算符代替is less than

&#13;
&#13;
// Check if the user is ready to play!

confirm("Are you ready to play?");

var age = prompt("What's your age");
if (age < 13) {
  alert("You are allowed to play, but we take no responsibility");
} else {
  alert("Go on! you can play");
}
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

您也可以使用三元运算符

来减少代码行数
 age < 13 ?  console.log("You are allowed to play, but we take no      responsibility"):console.log("Go on! you can play");