Javascript String concat()

Introduction

Javascript String concat() can concatenate one or more strings and return the concatenated string.

str.concat(str2 [, ...strN])
  • str2[,...strN] - strings to concatenate to str.

The following example combines strings into a new string.

let hello = 'Hello, '
console.log(hello.concat('Javascript', '. Have a nice day.'));

let greetList = ['Hello', ' ', 'Javascript', '!'];
let a = "".concat(...greetList);
console.log(a);/*from  w w w .  j  ava2  s.  c  o m*/
a = "".concat({});console.log(a);
// [object Object]
a = "".concat([]);
console.log(a);// ""
a = "".concat(null);
console.log(a);// "null"
a = "".concat(true);
console.log(a);  // "true"
a = "".concat(4, 5);
console.log(a);  // "45"
let stringValue = "hello "; 
let result = stringValue.concat("world"); 
              /*from w  w  w .ja  v a  2s .  c o m*/
console.log(result);       // "hello world" 
console.log(stringValue);  // "hello" 

The result of calling the concat() method on stringValue is "hello world".

The value of stringValue remains unchanged.

The concat() method accepts any number of arguments.

let stringValue = "hello "; 
let result = stringValue.concat("world", "!"); 
              // w ww  . ja va2s  .c o  m
console.log(result);       // "hello world!" 
console.log(stringValue);  // "hello" 

The concat() method works for string concatenation.

The addition operator (+) is used more often.




PreviousNext

Related