Concatenate strings in JavaScript with String.concat()

base.concat(value1, value2) returns a string with the arguments appended to base in order. The original string stays the same. Add any spaces or separators yourself. Numbers and other non-string arguments are converted to strings.

Join several values in order

Run the code in the console. A space in the first argument separates the two words. The number 3 is converted to text and placed after #. With no arguments, the returned string has the same contents. See ECMAScript: String.prototype.concat().

const base = 'Hello';
const result = base.concat(' ', 'World', ' #', 3);
console.log(result); // 'Hello World #3'
console.log(base);   // 'Hello'
console.log(typeof result); // 'string'

Convert null, undefined, and arrays to strings

null becomes 'null' and undefined becomes 'undefined'. An ordinary array converts to comma-separated text by default. For a different separator, call join() on the array first. String concat() returns a string, while array concat() returns a new array. Passing a Symbol directly throws a TypeError during conversion.

Use value ?? '' to append nothing only when the value is missing. The fallback applies to null and undefined, while numbers such as 0 and booleans such as false are kept. Compare the output below.

console.log('> '.concat(null));      // '> null'
console.log('> '.concat(undefined)); // '> undefined'
console.log('> '.concat(['A', 'B'])); // '> A,B'

const maybe = null;
console.log('> '.concat(maybe ?? '')); // '> '
console.log('> '.concat(0 ?? ''));     // '> 0'
console.log('> '.concat(false ?? '')); // '> false'

Choose between + and template literals

Use concat() or + to join a few string pieces. When inserting values into a fixed message, a template literal keeps the structure visible. The first three lines below print the same text.

const name = 'Mina';
console.log('Hello, '.concat(name, '!'));
console.log('Hello, ' + name + '!');
console.log(`Hello, ${name}!`);

console.log((1 + 2) + '3');       // '33'
console.log(''.concat(1, 2, 3)); // '123'

+ can also perform numeric addition. (1 + 2) + '3' computes 3 first and then produces '33'. ''.concat(1, 2, 3) converts each argument and produces '123'. See ECMAScript: +.

Complete example: enter your own separator

Save the HTML in UTF-8 as string-concat.html and open it. With the defaults, Join strings produces "Hello World #3". The separator starts with one space. Clear it or enter / and compare the results.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Practice string concat()</title>
</head>
<body>
<label for="first">First string</label>
<input id="first" type="text" value="Hello">
<label for="second">Second string</label>
<input id="second" type="text" value="World">
<label for="count">Number</label>
<input id="count" type="number" step="any" value="3">
<label for="separator">Separator between the strings</label>
<input id="separator" type="text" value=" ">
<button id="join" type="button">Join strings</button>
<pre id="output" aria-live="polite"></pre>
<script>
const first = document.querySelector('#first');
const second = document.querySelector('#second');
const count = document.querySelector('#count');
const separator = document.querySelector('#separator');
const output = document.querySelector('#output');

function show() {
  const base = first.value;
  const word = second.value;
  const number = count.valueAsNumber;
  if (!Number.isFinite(number)) {
    output.textContent = "Enter a valid number.";
    return;
  }
  const result = base.concat(separator.value, word, ' #', number);
  output.textContent = `concat() result: ${JSON.stringify(result)}\n` +
    `Original string: ${JSON.stringify(base)}\n` +
    `Result type: ${typeof result}`;
}

document.querySelector('#join').addEventListener('click', show);
show();
</script>
</body>
</html>

Change the number to 0 to append #0. Clear it to see a message. The separator keeps its surrounding spaces, and the output includes the original first string and the result type.