FAQ: Conditional Statements - Else If Statements

This community-built FAQ covers the “Else If Statements” exercise from the lesson “Conditional Statements”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Else If Statements

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I don’t remember being taught this. If you place a backslash before a single apostrophe does it indicate that it is part of the string and not an indicator of where a string starts and stops?

2 Likes

you seem to understand it correctly, even though it might or might have been taught.

personally, i would then just enclose the string in quotation marks:

console.log("i'm clever");

even simpler

2 Likes

Is there a way to change the variable season (in the code below) using a particular time interval, from summer to spring to fall and back to summer?

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.’);
}

you could setTimeOut:

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout

combined with a loop, you might be able to achieve what you want.

1 Like

ok let me give it a try

Does anybody know what I have done wrong here. 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.’);
}

strings printed/logged need to be an exact match

thanks, missed a dot after ‘snow’.

my point exactly. Good you spotted it

ONE. Why is there a backslash inside the word “It’s”?
TWO. How do I write possessive words like “It’s” in JavaScript? Every time I start typing a string, I start with the ’ single quotation mark then start typing my string. But then if a word is possessive and contains the ’ single quotation mark, it closes my string and I can’t add more single quotation marks because they force me to add 2 single quotation marks at the same time. Just impossible.

Question one is the answer to question two, by escaping (the backslash) the apostrophe, you can insert apostrophes in a string enclosed in apostrophes.

of course, the easier solution in javascript is to enclose your string in quotation marks, then you don’t need the backslash

1 Like

What if I want to log the same string if either one or the other of two things are true? For example, I want to say
if {season === "fall’ || ‘autumn’) { console.log(‘It’s fall!’); }
But using the || doesn’t work, because then it just logs this even when season === ‘summer’. Or maybe I’m just using it all wrong? 29%20AM

'autumn' doesn’t have a comparison value, so JS will just check if the string is truthy, which it is

Then why does it have the triple === ? I thought that that meant that we were looking for an exact match. Because ‘autumn’ isn’t the variable, it’s the (potential) value of the variable, and we’re checking if the string is, indeed, the same as the string assigned to the variable “season.”

And how would one go about trying to log the same string for both ‘autumn’ and ‘fall’ without creating a whole new “else if” statement?

it does, but that is only for season === 'fall' (the left hand side of your or (||) operator. On the right hand side of your or operator you don’t do any comparison.

on the right hand side of your or operator, you also need to check for equality.

Ok, I got it now! I just said season === on both sides of the || and it worked. Thanks for the help!

is there’s any limit of how many times we can use that else if? or it’s unlimited?

if () {
  
} else if () {
} else if () {
} else if () {
} else if () {
} else if () {

} else {
}

now here’s the

switch

chapter, okay :smiley: