Variable Practice

We've already covered basic mathematical operations:

4 + 5 // returns 9

Variables can be used in place of values when performing mathematical operations:

var firstNumber = 4;
var secondNumber = 5;
firstNumber + secondNumber // returns 9

Strings can also be combined with the + operator. This is called string concatenation:

"Bart" + " Simpson" // returns "Bart Simpson"

Multiple strings can be concatenated:

"Bart " + "JoJo" + " Simpson" // returns "Bart JoJo Simpson"

Variables can also be used with string concatenation:

var first = "Homer";
var last = "Simpson";
first + " " + last // "Homer Simpson"
Question Click to View Answer

Assign the variable yy to the value "i like ". Assign the variable zz to the value "whales". Concatenate the two variables and print the result to the console.

var yy = "i like ";
var zz = "whales";
console.log(yy + zz);

Assign the variable birthYear to the value 1970. Assign the variable singularityYear to 2032. Subtract birthYear from singularityYear and print the result to the console.

var birthYear = 1970;
var singularityYear = 2032;
console.log(singularityYear - birthYear);
<-- Previous (Variable Reassignment) Next (More Variable Reassignment) -->