String concatenation with variables - quotation marks

The exercise says create a variable named favoriteAnimal and set it equal to your favorite animal. I was wondering why the solution uses double quotation marks instead of single quotation marks. I copied the solution below.

let favoriteAnimal = "giraffe";

console.log("My favorite animal: " + favoriteAnimal);

In the example they used single quotation marks. That’s why I’m confused. What is the difference please?

let myPet = 'armadillo';
console.log('I own a pet ' + myPet + '.'); 
// Output: 'I own a pet armadillo.'

Link to exercise

In the examples shown, there is no difference. If, however, you wanted to include an apostrophe in your string literal, you would have the following options.

console.log("I'm learning JavaScript.");
console.log('I\'m learning JavaScript.'); // have to use the escape character (\) since the outer quotes are the same as the apostrophe
console.log(`I'm learning JavaScript.`); // either single or double quotes could be used inside the back ticks

All three ouput, I'm learning JavaScript.

1 Like