首页 > 解决方案 > If, else if, statements : Uncaught ReferenceError: Invalid left-hand side in assignment

问题描述

我刚开始做 Javascript,我不断收到语法错误,但没有设法找出它的来源。这是我写的一个片段:

let num1 = 5;
let num2 = 8;
let num3 = 10;

if (num1 === num2) {
  console.log("the comparison shows");
} else if (num1 > num2 = true); {
  console.log("Number 1 is greater than number 2. The value for num 1 is " + num1);
} else(num2 > num1 = true); {
  console.log("Number 2 is greater and the value is " + num2);

标签: javascript

解决方案


在 else if 和 else 块之后删除;,不需要在 else-if/else 语句中将表达式等同于 true。不需要 else 语句中的条件。如果以上都没有通过,则控制将只转到 else

let num1 = 5;
let num2 = 8;
let num3 = 10;
if (num1 === num2) {
  console.log("the comparison shows");
} 
else if (num1 > num2) {
  console.log("Number 1 is greater than number 2. The value for num 1 is " + num1);
} 
else
  console.log("Number 2 is greater and the value is " + num2);


推荐阅读