Concatenation Operator
The concatenation operator in JavaScript is the plus sign +
. When used with strings
, the +
operator concatenates, or combines, the two strings together into a single string.
For example:
In this example, we're using the concatenation operator to combine the strings Hello,
and !
with the value of the firstName
variable.
When used with numbers
, the +
operator adds the two numbers together.
For example:
In this example, we're using the +
operator to add the values of the num1
and num2
variables together.
When used with strings
and numbers
, the +
operator converts the number to a string and then concatenates the two strings together.
For example:
In this example, we're using the +
operator to convert the value of the num
variable to a string and then concatenate it with the string The number is
.
In the exercise below, you'll be using the +
operator to combine strings and numbers.
You'll be given a variable called x
and a variable called y
with random numbers. You'll also be given a variable called message
. Your job is to use the +
operator to combine the x
and y
variables together and assign the result to the message
variable with a string that says "The sum of x and y is z"
.
Example: