How to Concatenate Strings in JavaScript ?
Last Updated :
13 Sep, 2023
In this article, we will see different methods to Concatenate Strings in JavaScript. The concat means joining two or more strings and returning a new string.
Approaches to Concate String in JavaScript:
- Using String concat() Method
- Using + Operator
- Using Array join() Method
The concat() method is used to join two or more strings without changing the original strings and returning a new string.
Syntax:
str1.concat( str2, str3, . . . , strN )
Example: In this example, we will concat the given string using the strings concat() method.
Javascript
let str = 'Geeks' ;
let value = str.concat( 'for' , 'Geeks' );
console.log(value);
|
The + operator adds strings and returns the concatenated string. It is the easiest method for the concatenation of two strings.
Syntax:
let str = str1 + str2
Example: In this example, we will concate two string using + operator.
Javascript
let str1 = 'Geeks' ;
let str2 = 'for' ;
let str3 = 'Geeks' ;
let str = str1 + str2 + str3;
console.log(str);
|
The JavaScript Array join() Method is used to join the elements of an array into a string. The elements of the string will be separated by a specified separator and its default value is a comma ( , ).
Syntax:
array.join( separator )
Example: In this article we will concate strings present in an array.
Javascript
let arr = [ 'Geeks' , 'for' , 'Geeks' ];
let str = arr.join( '' );
console.log(str);
|
Share your thoughts in the comments
Please Login to comment...