FAQ: Conditional Statements - Else If Statements

There’s no limit to the number of else if statements, but the logic can become unwieldy, and difficult to follow if you get too carried away. Same goes for a switch statement with lots of case:'s. It may be that your code could be refactored to handle the task differently in many situations to avoid this predicament.

1 Like

The escape character is first introduced when the user fails to use it in the solution. It should be introduced to the student before their statement (which was written by the program and not the student) is determined to be wrong.

The definition of Ternary Operator is not clearly outlined in the lesson and must be referenced in a third-party program. The definition of the object we are learning to use should be in the opening of the lesson where its use is discussed.
In the final portion of the lesson where conditionals are reviewed, this sentence appears: “The ternary operator is shorthand to simplify concise if...else statements.” However the operator ? does not appear on the page. It should be added to the definition just as <, >, ||, etc also appear on the page.

1 Like

I cant understand what i did wrong. can anyone take a look please.

let season = 'summer';

if (season === 'spring') {
  console.log('It\'s spring! The trees are budding!');
} else if(season ==='winter'){
  console.log('It\'s winter! Everything is covered in snow.');
}

Hello, @domwebster. Welcome to the forum.
I can’t tell for sure from your code, but it appears that you have used an apostrophe twice inside of single quotes. Whenever you need to include code in a post, please use the </> button in the menu bar. If you click it first, you can then paste your code in the space indicated, and formatting, special characters, etc. will be preserved.

Try using the escape character \ (backslash) before the apostrophe marks inside your string literals like so:

console.log('It\'s spring! The trees are budding!');

I believe this technique is included in the instructions for the exercise. As you progress through the lessons, you’ll learn other techniques that would eliminate the need for the \.

where is the else clause? It should have been there, you are suppose to add the else if statement, which means you end up with: if statement, else if statement and an else statement.

3 Likes

Is there a way to use something like Ternary Operator for Else If Statements?

i suppose you could nest ternary operators, but that will very quickly becomes very unreadable.

there is switch, have you been taught switch yet?

I just did!!! :slight_smile: I just wanted to know if we can do the same thing as Ternary Operator with Else If Statements!!!

ternary operator is a useful shorthand for simple if and else statement, once you involve else if you should generally stay clear of ternary operator.

3 Likes

Can anyone please take a look at this. I can’t understand what is wrong. Thank you for your time.

let season = 'summer';

if (season === 'spring') {
  console.log('It\'s spring! The trees are budding!');
} else if(season === 'winter') {
  console.log('It\'s winter! Everything is covered in snow.');
} else if(season === 'fall') {
  console.log("It's fall! Leaves are falling!");
} else if(season === 'summer') {
  console.log("It's sunny and warm because it's summer!");
} else {
  console.log('Invalid season.');
}
VM278:1 Uncaught SyntaxError: Identifier 'season' has already been declared
    at <anonymous>:1:1```

Hello, @romeob9267349247.

Welcome to the forums.

Are you by chance running this code in a browser console? If so, in the current browser session, season has already been declared when you ran your code the first time. You can comment out or delete the let season = 'summer'; line, and run the remaining code, or just delete let, and season will be re-assigned the value 'summer' when you run your code again.

Thank you for the reply, yes I did run it on Chrome, I should have mentioned. I wanted to compare the results because on the codecademy console wasn’t giving me the right results either. Thanks again for your help.

Regards

1 Like

In the previous section I have learnt about tenerary operator ? if : else

is this only for simple if else or can we do a else if also…

We can’t do this with else if. Well, i suppose you could nest a ternary operator, but that is absolute not preferable.

I am doing the first part of the assignment and it gives me the error:

/home/ccuser/workspace/learn-javascript-conditionals-else-if-statements/main.js:7
} else
^^^^
SyntaxError: Unexpected token else

This what I have, what am I doing wrong?

let season = ‘summer’;

if (season === ‘spring’) {
console.log(‘It’s spring! The trees are budding!’);
} else if(season === ‘winter’); {
console.log(‘It’s winter! Everything is covered in snow.’);
} else
console.log(‘Invalid season.’);
}

I’ve looked at the completed assignment and I don’t see the error.

here:

 else if(season === ‘winter’); 

the semi-colon interrupts the the clauses, causing an error

Why does this course(so far) always use === instead of just ==? Is there a reason to always teach to compare types as well, or does the course later explain ==?

The backslash is an “escape” from the quotes, so, like you said, the next character will be part of the string instead of ending it. It can also be used for " in a string encased in ", but like stetim94 said, it’s much easier to just use the other type to encase the string if you want to use one inside.

console.log('I told Dave, "Eat your vegetables!" but he still refused');
console.log("I'm king of the world!!!");

But if you plan on having both, or only like encasing in one type, you’ll need to “escape” the string.

console.log('I heard him say "I\'m starving.", but he wouldn\'t eat what was in the house.');
console.log("He kept screaming \"I'm starving!\", but he wouldn't even pour his own cereal!");

I know the examples are strange (just something quick, lol), but hopefully that’s somewhat clear.

not checking for type can give unexpected behavior/type coercien. Its preferable to use ===.

A bug or did I forget something?