Bird
Raised Fist0
Javascriptprogramming~20 mins

String concatenation in output in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Concatenation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of concatenated strings with numbers
What is the output of this JavaScript code?
console.log('5' + 3 + 2);
Javascript
console.log('5' + 3 + 2);
A'7'
B'55'
C'10'
D'532'
Attempts:
2 left
💡 Hint
Remember that + with a string converts numbers to strings and concatenates.
❓ Predict Output
intermediate
2:00remaining
Concatenation with template literals
What will this code output?
const a = 4;
const b = 5;
console.log(`${a} + ${b} = ${a + b}`);
Javascript
const a = 4;
const b = 5;
console.log(`${a} + ${b} = ${a + b}`);
Aa + b = 9
B4 + 5 = 45
C4 + 5 = 9
D4 + 5 = a + b
Attempts:
2 left
💡 Hint
Template literals evaluate expressions inside ${}.
❓ Predict Output
advanced
2:00remaining
Output of mixed concatenation and addition
What is the output of this code?
console.log('3' + 2 * 2);
Javascript
console.log('3' + 2 * 2);
A'7'
B'34'
C'32'
D7
Attempts:
2 left
💡 Hint
Remember operator precedence: * before +.
❓ Predict Output
advanced
2:00remaining
Concatenation with null and undefined
What will this code print?
console.log('Value: ' + null + undefined);
Javascript
console.log('Value: ' + null + undefined);
A'Value: nullundefined'
B'Value: nullundefinedundefined'
C'Value: nullundefinednull'
DTypeError
Attempts:
2 left
💡 Hint
null and undefined convert to strings when concatenated.
🧠 Conceptual
expert
3:00remaining
Why does this concatenation produce unexpected output?
Consider this code:
console.log(1 + 2 + '3' + 4 + 5);

Why does it output '3345' instead of '12345' or a number?
ABecause + is left to right, 1+2=3, then '3' + '3' = '33', then '33' + 4 + 5 = '3345'
BBecause JavaScript converts all numbers to strings before adding
CBecause the + operator always performs string concatenation
DBecause the expression is evaluated right to left
Attempts:
2 left
💡 Hint
Think about how + works left to right and when it switches from addition to concatenation.

Practice

(1/5)
1. What does the + operator do when used between two strings in JavaScript?
easy
A. Divides the first string by the second
B. Joins the two strings together into one string
C. Multiplies the two strings
D. Subtracts the second string from the first

Solution

  1. Step 1: Understand the + operator with strings

    In JavaScript, the + operator combines two strings into one continuous string.
  2. Step 2: Recognize string concatenation

    When you write "Hello" + " World", it becomes "Hello World".
  3. Final Answer:

    Joins the two strings together into one string -> Option B
  4. Quick Check:

    String + String = Concatenation [OK]
Hint: Remember + joins strings like glue [OK]
Common Mistakes:
  • Thinking + subtracts or divides strings
  • Confusing + with arithmetic on strings
  • Forgetting to add spaces inside quotes
2. Which of the following is the correct way to concatenate the strings "Hello" and "World" with a space between them in JavaScript?
easy
A. "Hello" - " " - "World"
B. "Hello" + "World"
C. "Hello" + " " + "World"
D. "Hello" * "World"

Solution

  1. Step 1: Identify how to add space between words

    To add a space, include " " (space inside quotes) between the two strings.
  2. Step 2: Use + operator to join all parts

    Use + to join "Hello", " ", and "World" to get "Hello World".
  3. Final Answer:

    "Hello" + " " + "World" -> Option C
  4. Quick Check:

    Use + and " " for spaces [OK]
Hint: Add spaces inside quotes when joining words [OK]
Common Mistakes:
  • Forgetting the space string " "
  • Using - or * instead of +
  • Not putting quotes around the space
3. What will be the output of the following code?
let name = "Anna";
console.log("Hello, " + name + "!");
medium
A. Hello, + name + !
B. Hello, name!
C. Hello Anna
D. Hello, Anna!

Solution

  1. Step 1: Understand variable substitution in concatenation

    The variable name holds the string "Anna" and is concatenated with other strings.
  2. Step 2: Combine strings and variable value

    "Hello, " + name + "!" becomes "Hello, Anna!" when printed.
  3. Final Answer:

    Hello, Anna! -> Option D
  4. Quick Check:

    Variable value joins strings correctly [OK]
Hint: Variables inside + join as their values [OK]
Common Mistakes:
  • Printing variable name instead of value
  • Missing punctuation or spaces
  • Using commas instead of + for concatenation
4. Find the error in this code snippet:
let greeting = "Hi";
let name = "Sam";
console.log(greeting + name);

What is missing if the output should be Hi Sam?
medium
A. A space string between greeting and name
B. Quotes around the variable name
C. A comma instead of + operator
D. Semicolon after console.log

Solution

  1. Step 1: Check the concatenation result

    Currently, greeting + name joins "Hi" and "Sam" without space, resulting in "HiSam".
  2. Step 2: Add a space string between variables

    Insert " " between greeting and name: greeting + " " + name to get "Hi Sam".
  3. Final Answer:

    A space string between greeting and name -> Option A
  4. Quick Check:

    Add " " to separate words [OK]
Hint: Add " " between words to separate them [OK]
Common Mistakes:
  • Forgetting spaces in concatenation
  • Using commas instead of +
  • Thinking semicolon affects output
5. You want to create a greeting message that says Hello, John Doe! You have 5 new messages. using variables firstName, lastName, and messageCount. Which code correctly concatenates these values with spaces and punctuation?
hard
A. console.log("Hello, " + firstName + " " + lastName + "! You have " + messageCount + " new messages.");
B. console.log("Hello, " + firstName + lastName + "! You have " + messageCount + " new messages.");
C. console.log("Hello, " + firstName + " " + lastName + "! You have " + messageCount + "new messages.");
D. console.log("Hello, " + firstName + " " + lastName + "! You have " + messageCount + " new messages")

Solution

  1. Step 1: Check spacing between first and last names

    console.log("Hello, " + firstName + " " + lastName + "! You have " + messageCount + " new messages."); adds a space string " " between firstName and lastName, correctly spacing the full name.
  2. Step 2: Verify punctuation and spaces in message

    console.log("Hello, " + firstName + " " + lastName + "! You have " + messageCount + " new messages."); includes "! You have " and " new messages." with correct spaces and punctuation.
  3. Final Answer:

    console.log("Hello, " + firstName + " " + lastName + "! You have " + messageCount + " new messages."); -> Option A
  4. Quick Check:

    Use " " to separate names and punctuation correctly [OK]
Hint: Add spaces inside quotes between variables [OK]
Common Mistakes:
  • Missing space between first and last name
  • Missing space before 'new messages'
  • Omitting punctuation at the end
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard