String Concatenation with Variables

 VARIABLES

In previous exercises, we assigned strings to variables. Now, let’s go over how to connect, or concatenate, strings in variables.

The + operator can be used to combine two string values even if those values are being stored in variables:

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

In the example above, we assigned the value 'armadillo' to the myPet variable. On the second line, the + operator is used to combine three strings: 'I own a pet', the value saved to myPet, and '.'. We log the result of this concatenation to the console as:

I own a pet armadillo.


Instructions:
1.Create a variable named favoriteAnimal and set it equal to your favorite animal.
2.Use console.log() to print 'My favorite animal: ANIMAL' to the console. Use string concatenation so that ANIMAL is replaced with the value in your favoriteAnimal variable.

Solutions:
let favoriteAnimal = 'Cat';
console.log('My favorite animal: '+ favoriteAnimal);

In the console:
My favorite animal: Cat


Comments