FAQ: Variables - String Concatenation with Variables

This community-built FAQ covers the “String Concatenation with Variables” exercise from the lesson "Variables ".

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

Web Development

Introduction To JavaScript

FAQs on the exercise String Concatenation with Variables

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!

3 posts were split to a new topic: How can a space be added with string concatenation?

Does anyone know what program(for the actual console) they use for the lessons, I have been looking for a good one?

Shouldn’t the correct solution be:

console.log('My favorite animal: ' + favoriteAnimal.toUpperCase())
1 Like

The all-caps in the instructions indicates a placeholder. We might infer the result should be the same uppercase, but it is not specified in the instructions.

2 Likes

I thing, it is better use that:

console.log(`some text ${variable}`)
1 Like

3 posts were split to a new topic: Lesson didn’t pass my code?

why is it “” in the solution but ‘’ in the example ?

You can use single quotes ' or double quotes " to create string literals.


An advantage using one over the other could be that you have a single quote in your string:

const exampleString = "I'm using a single quote as an apostrophe.";

Here you can use double quotes so that the single quote used for the apostrophe in I’m does not affect your string.


You can use single quotes to surround your string to use double quotes within your string.

const secondExample = 'He explained, "This is one way to quote someone."';


There are also a few other ways to go about it. This documentation about javascript strings shows you how to use escape characters (look for the Escape Notations header) and has more info on strings. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
You could also use a string template literal which is covered later in this course.

Edit: Mozilla documentation link

@mtf Should I had not used that link because it was from another learning platform? Is it okay to link the Mozilla documentation to learn about escape characters?

1 Like

Yes, that would be the one to use, please and thanks.

We treat links from that other site as spam.

1 Like

ok that makes sense now thank so much :grinning:

Can someone please explain to me how this is wrong?

Be sure to use the exact phrase given in the instructions. Compare the two.

Please try to lowcase the word animal.

Thanks for illustrating this, someone mentioned string literals and interpolation in another post, just what I was looking for!

Nice observation though @mtf explained the reason very well

I was confused and thought I had to do let ANIMAL = favoriteAnimal for the second line and then console.log('My favorite animal: " + ANIMAL) but it ended up being simpler than that. I was a little mad.

1 Like

Any reason why someone is required to specify a non-empty string? Why is the following not a valid answer?

let favoriteAnimal = '';

I’m not too keen on animals myself. This was my first attempt before I was forced to provide a value.

The exercise is not expecting an empty string, and then why would it? Sometimes we need to keep our personal preferences out of it and just play along. Don’t make it about yourself, that will only interfere with learning.

1 Like