String Concatenation using '' operator
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In JavaScript, the + operator can join strings, but it also performs numeric addition and type coercion. That makes it convenient for simple cases and error-prone in larger expressions, so it is worth understanding both what it does well and where other string-building tools are clearer.
How + Concatenation Works
When at least one operand is a string, JavaScript converts the other operand to a string and joins the two values.
This is the most direct use case. You have two or three values, you want one final string, and the expression is easy to read.
The same rule applies to numbers:
JavaScript converts 42 to "42" because the expression already contains a string.
Why Type Coercion Can Surprise You
The catch is that + is overloaded. With numbers, it adds. With strings, it concatenates. In mixed expressions, evaluation order matters.
In the first line, 1 + 2 is computed first as numeric addition, giving 3, then "3" is appended. In the second line, the first operand is already a string, so the rest of the expression becomes concatenation.
This is why + is fine for clear string expressions but dangerous when arithmetic and formatting are mixed together casually.
When + Is Fine
Use + when:
- the expression is short
- all values are already strings or obvious primitives
- readability is still high
That is completely reasonable. The trouble begins when the number of pieces grows or when conditions and formatting rules make the expression hard to scan.
When to Prefer Template Literals
For most modern JavaScript, template literals are clearer than repeated + concatenation.
Template literals are easier to read because the final string shape is visible directly. They also reduce bugs caused by missing spaces, operator precedence, or accidental coercion.
For multiline output, they are much cleaner:
That is much more maintainable than a long chain of "..." + "\n" + "...".
Building Many Strings Efficiently
If you are joining many items, Array.prototype.join() is often a better mental model than repeated concatenation in a loop.
This is especially useful when the content already exists as a collection. It expresses intent better than incremental result += value logic scattered through a loop body.
There is also a maintainability benefit. When separators become conditional, join() lets you build the list of meaningful parts first and format it afterward. That is often easier to test than a loop that mutates one growing string across several branches.
Common Pitfalls
- Mixing numbers and strings in one
+expression and expecting purely numeric addition. - Building long, unreadable concatenation chains when template literals would show the final string more clearly.
- Forgetting spaces or separators between concatenated values.
- Using repeated concatenation in collection-building code where
join()would express the intent better. - Assuming
+is harmless type conversion even in expressions where coercion changes the result unexpectedly.
Summary
- In JavaScript,
+concatenates when at least one operand is a string. - The same operator also does numeric addition, so mixed expressions can surprise you.
- Short concatenations with
+are fine when they stay obvious. - Template literals are usually clearer for dynamic or multiline strings.
- '
join()is often the right choice when combining many string parts from an array.'
Related reading
- String interpolation in JavaScript?
- Strip all non-numeric characters from string in JavaScript
- Strip HTML from strings in Python
- Strip HTML tags from text using plain JavaScript
- Stripping out HTML tags from a string
- Stripping out HTML tags from a string
- Stylesheet not loaded because of MIME type
- Styling an input type="file" button
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.