educative.io

Educative

Quiz Question: How to avoid the answer in a string from coming altogether instead of spaced out?

I have a question in regards to this code. At first, I thought the asnwer was going to be Hello there! with a space in between. However, the answer is Hellothere!
Why is that the correct answer?
How can we avoid string answers that are written altogether instead of in a space as shown?
Please feel free to use a clear, detailed example and explanation and thank you in advance. :smiley:

let str1 = 'Hello';
let str2 = 'there!';
console.log(str1 + str2);

Course: Introduction to JavaScript: First Steps - Free Interactive Course
Lesson: Using Operators with Different Types - Introduction to JavaScript: First Steps

Hi @Ashley_R

In this line console.log(str1 + str2);, the + operator in the concatenation operator, which concatenates the first one with the second one in our case str1 and str2, in short, it merges them.
If you look at the example in the lesson thereโ€™s a space after the Hello as let str1 = 'Hello ';.
If you want to avoid the space you have to use the concatenate operator +.
If you want to have a space between them or others that should come after a space you can use the comma , instead of the + operator as:

let str1 = 'Hello';
let str2 = 'there!';
console.log(str1 , str2);

The output would be

Hello there!

with a space between.

If you still face any confusion, feel free to ask.