JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to append a string to the end of a string in JavaScript?

In JavaScript, there are three ways of adding a string to the end of a string:

1. Using the addition operator: The addition operator (+) enables you to concatenate two strings. The following example shows you how to add a string to the end of a string:

let str = "I am learning";

console.log(str + " JavaScript."); //"I am learning JavaScript."

2. Using the concat() method: String provides the concat() method to append a string to the end of a string. The concat() method accepts one or more strings and returns a concatenated string.

Note: The concat() method does not modify the original string.

let str = "I";

console.log(str.concat(" am", " learning", " JavaScript.")); //"I am learning JavaScript."

3. Using template literal: ES6's template literal provides a new way of adding a string to the end of a string. Instead of single or double quotation marks, you enclose a string in backticks. In template literal, you use a dollar sign and curly braces for evaluating variables.

let str = "Mohit";

console.log(`${str} Natani`); //"Mohit Natani"

Recommended Posts