Description
The JavaScript method concat() concatenates the provided arguments to the string, in the same order.
- Recommended to use the assignment operators
+and+=for string concatenation, as they are faster than theconcat()method.
The below table summarizes its usage.
| Usage Details | |
| Syntax | str.concat(str1, str2, ..., strN)
|
| Parameters | Takes an arbitrary number of strings to be concatenated with the string str. |
| Result | Returns the concatenated string containing all the strings combined. |
| Category | String Methods |
Example: Using the method
For a valid integer index value, the method returns the character from the specified index.
var str1 = "Hello";
var str2 = "World";
// Concatenating the strings
var newStr = str1.concat(" ", str2, "!");
document.write(newStr); // Prints: Hello World!
// Other examples
document.write("".concat(2, 3)); // Prints: 23
document.write("".concat(true, false)); // Prints: truefalse
document.write("".concat(null, null)); // Prints: nullnull
document.write("".concat({}, {})); // Prints: [object object][object object]
Output:
Hello World!
23
truefalse
nullnull
[object object][object object]
Overall
The JavaScript method concat() returns the concatenated string character at a specified index in a string.