How to use the JavaScript String concat() method

The JavaScript string concat() method is used to join two or more strings together.

Let’s say you have two variables for storing your name, and you want to join them into one string. Here’s how to do that with the concat() method:

const firstName = "David"
const lastName = "Tromholt"

var fullName = firstName.concat(lastName)

console.log(fullName)
// "DavidTromholt"

Pretty simple, huh?

But what if you want space between the two strings? There are several ways to do that.

I would either just add a space in one of the string values, e.g. "David ":

const firstName = "David " // add space
const lastName = "Tromholt"

var fullName = firstName.concat(lastName)

console.log(fullName)
// "David Tromholt"

Or you can add the space directly in the concat() method:

const firstName = "David"
const lastName = "Tromholt"

const fullName = firstName.concat(" ", lastName) // add space between variables

console.log(fullName)
// David Tromholt

Has this been helpful to you?

You can support my work by sharing this article with others, or perhaps buy me a cup of coffee 😊

Kofi

Share & Discuss on