How to join two strings in JavaScript
4 Ways to join two strings in JavaScript
In this article, we will explain to you how to join two strings in JavaScript. here we will give you four simple examples of how to join two strings in JavaScript.
Way 1: Plus operator
We can easily combine two strings using the plus operator. so you can see the below example,
1 2 3 4 5 6 | const first = "Hello"; const second = "World"; const join = first + second; console.log(join); // "HelloWorld" |
Way 2: Es6 Template literals
We can easily join two strings using Es6 Template literals. so you can see the below example.
1 2 3 4 5 6 | const first = "Hello"; const second = "World"; const join = `${first} ${second}`; console.log(join); // "Hello World" |
Way 3: += operator
We can easily combine the first string with the second string into the third variable using += operator. so you can see the below example.
1 2 3 4 5 6 | let first = "Hello"; let second = "World"; second += first; // combined second with first console.log(second); // "Pack World" |
Way 4: concat() method
We can easily combine two strings using the javascript string concat() method. which is take one string argument parameter and it returns the concat string. so you can see the below example.
1 2 3 4 5 | const first = "Hello"; const greet = first.concat(", World"); console.log(greet); // "Hello, World" |
Please follow and like us: