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

Syntax error resulting from invalid syntax in if / else statements

本文关键字:语法 无效 错误 if else 语句      更新时间:2023-09-26
// 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 位置的某个地方。

使用<运算符而不是is less than

// 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");
}

您也可以使用三元运算符减少代码行数

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