JavaScript - String concat() method
In this post, we will discuss about concat() method in JavaScript. concat() is a method used to append the parent string with given string.
Let us create the String for demonstration.
let str='student who gets first marks that student will be eligible for scholarship';
console.log(str);
Output:
student who gets first marks that student will be eligible for scholarship
String concat():
concat() used to append the given string with its parent string but it will return the new string by appending but will not change the parent string.
concat() syntax:
String.concat('stringtoappend')
Where, String is a parent string and 'stringtoappend' is the given string to append with parent string.
concat() example 1:
create a string declaration with sentence and append the declared string with the given string.
let str='student who gets first marks that student will be eligible for scholarship';
console.log('parent string = '+str);
let appendString = ' with scholarship less worried about money'
let concatenatedString = str.concat(appendString);
console.log('parent string = '+str);
console.log('concatenated string = '+ concatenatedString);
Execution Steps:
- Copy the above example code.
- Open the browser(chrome or Microsoft edge) and press(ctrl+shift+I).
- Go to the console tab.
- Paste the copied code into the console and press enter button.
Output:
parent string = student who gets first marks that student will be eligible for scholarship
parent string = student who gets first marks that student will be eligible for scholarship
concatenated string = student who gets first marks that student will be eligible for scholarship with scholarship less worried about money
concat string with '+' example:
In this example, we will append the string with parent string using '+' operator.
let str='student who gets first marks that student will be eligible for scholarship';
console.log('parent string = '+str);
let appendString = ' with scholarship less worried about money'
let concatenatedString = str + appendString;
console.log('parent string = '+str);
console.log('concatenated string = '+concatenatedString);
Execution Steps:
- Copy the above example code.
- Open the browser(chrome or Microsoft edge) and press(ctrl+shift+I).
- Go to the console tab.
- Paste the copied code into the console and press enter button.
Output:
parent string = student who gets first marks that student will be eligible for scholarship
parent string = student who gets first marks that student will be eligible for scholarship
concatenated string = student who gets first marks that student will be eligible for scholarship with scholarship less worried about money