The concat() method in JavaScript is used to concatenate (join together) two or more strings and returns a new string.
Syntax: string.concat(string1, string2, ..., stringN)
- string: The initial string to which other strings will be concatenated.
- string1, string2, ..., stringN: Strings to be concatenated with the initial string. We can specify multiple strings separated by commas.
Example:
let str1 = "Hello";
let str2 = "World";
let concatenatedStr = str1.concat(", ", str2);
console.log(concatenatedStr); // Output: Hello, World
Explanation:
- We have two strings str1 and str2 containing "Hello" and "World" respectively.
- We use concat() to concatenate str1, a comma and space, and str2.
- The resulting string, "Hello, World", is stored in the variable concatenatedStr and logged to the console.
Code Compiler Link: https://app.singhteekam.in/codecompiler/