WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

JavaScript · Basic · question 18 of 100

How do you concatenate strings in JavaScript?

📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

In JavaScript, you can concatenate strings by using the + operator or the concat() method. Both methods allow you to combine two or more strings into a single string.

Here’s an example using the + operator:

    var firstName = "John";
    var lastName = "Doe";
    var fullName = firstName + " " + lastName;
    console.log(fullName); // Outputs "John Doe"

In this example, the + operator is used to concatenate the firstName, a space, and the lastName to create the fullName.

You can also use the concat() method to concatenate strings. Here’s an example:

    var firstName = "John";
    var lastName = "Doe";
    var fullName = firstName.concat(" ", lastName);
    console.log(fullName); // Outputs "John Doe"

In this example, the concat() method is used to concatenate the firstName, a space, and the lastName to create the fullName.

In addition to these methods, you can also use template literals (introduced in ES6) to concatenate strings. Here’s an example:

    var firstName = "John";
    var lastName = "Doe";
    var fullName = `${firstName} ${lastName}`;
    console.log(fullName); // Outputs "John Doe"

In this example, the template literal syntax is used to create the fullName. The variables firstName and lastName are wrapped in $ to create a single string.

In summary, you can concatenate strings in JavaScript using the + operator, the concat() method, or template literals. All three methods allow you to combine two or more strings into a single string.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic JavaScript interview — then scores it.
📞 Practice JavaScript — free 15 min
📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

All 100 JavaScript questions · All topics