简体   繁体   中英

Why doesn't "else if" work in console.log and keeps popping syntax error?

I have a problem with a console. I wanted to make if n<10 else if n<100, and else with console comment. Seems like it won't get me through else if. I tried to find an answer, but nothing worked.

let n = 14;
undefined
if (n<10){
  console.log("grats");}
undefined
else if (n<100){console.log("grats2");}
SyntaxError: expected expression, got keyword 'else'

In the browser's console, every line seems to be interpreted and executed separately. When you close your if 's curly brace if you stop there, then the expression is executed and on the next line you can't go else because the if statement is finished.

What you can do is

if (condition) {
  // ... operations
} else {

by writing else right after closing the curly brace you're telling the console than you're not done yet with the if statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM