The concat() method is used in JavaScript to merge two or more strings, returning a new string that is the union of the strings it operates on. This method does not change the existing strings but rather combines them, preserving their content.
Syntax: The basic syntax of the concat() method is as follows:
string.concat(string1, string2, ..., stringN)
string1, string2, …, stringN: These are the strings to concatenate to the calling string.
Examples and Execution: The following examples can be executed in any JavaScript environment, such as browser consoles, online JavaScript editors, or a Node.js setup.
Basic concatenation:
let hello = 'Hello, ';
let world = 'World!';
let helloWorld = hello.concat(world);
console.log(helloWorld); // Outputs: "Hello, World!"
let str1 = 'I ';
let str2 = 'am ';
let str3 = 'learning ';
let str4 = 'JavaScript!';
let sentence = str1.concat(str2, str3, str4);
console.log(sentence); // Outputs: "I am learning JavaScript!"
let value = 100;
let unit = ' kg';
let weight = 'Weight is: '.concat(value, unit);
console.log(weight); // Outputs: "Weight is: 100 kg"
In this example, the concat() method implicitly converts non-string arguments to their string representations.
function createFullName(firstName, lastName) {
return firstName.concat(' ', lastName);
}
let fullName = createFullName('John', 'Doe');
console.log(fullName); // Outputs: "John Doe"
createFullName is a function that takes a first name and a last name, then uses concat() to merge them into a full name.
Get more articles on Java Script
Read more on Shell and Linux related articles
Refer more on python here : Python