String Concatenation In JavaScript
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Amruta
In this article we will show you the solution of string concatenation in javascript, in JavaScript, string concatenation simply refers to adding a string or string towards the ending of another string.
Concatenating strings in JavaScript can be done in four different ways: utilising the concat() method, the "+" operator, a array join() method, and template literals.
In any programming language, string concatenation simply refers to the act of appending one or even more string to the ends of another string. Now move to the concept of string concatenation in javascript.
Step By Step Guide On String Concatenation In JavaScript :-
The concat() method in JavaScript is used to combine two or even more strings without altering the source strings and returns a new string.
The method String.concat() takes the list of strings for parameters and produces a new string that combines all the strings after concatenation.
Concatenation takes place after the parameters, if they are not strings, are first converted via the concat() method.
Due to the fact that JavaScript strings are immutable, the concat() method creates a new string by concatenating all of the input strings together instead of altering the original strings.
Some strings that are required to be combined together are the argument to this function.
As many parameters are required for this function to merge all the strings are required.
You can combine strings that are stored in multiple variables by using the concat() function.
Moreover, the concat() function can be used to join strings together with spaces.
Syntax:
str.concat(string2, string3, string4,......, stringN)
let str1 = 'Talkerscode' let str2 = 'Talkerstech' let str3 = 'Talkersmoney' let result1 = str1.concat(str2,str3) console.log('Result without spaces: ' +result1) let result2 = str1.concat(' ',str2,' ',str3) console.log('Result with spaces: ' +result2)
- To begin, we create three strings called str1, str2, and str3 for Talkerscode, Talkerstech, and Talkersmoney, respectively.
- After that, we combine all the strings empty of spaces using the concat function.
- Next, we use the JavaScript method console.log, which can output any type of variable that has been defined previously or can simply publish a message to the user. This function is being used to print the outcome in result1 form here without any spaces.
- Then, we combine all of the strings with spaces once more using the concat method.
- Next, we print the second result in result2 form using console.log and add spaces.
Conclusion :-
We were able to understand the JavaScript concept of string concatenation as a result.
Also, we discovered that the JavaScript concat() technique can combine two or more strings into one new string without changing the original valuesThe list of strings can be passed as inputs to the String.concat() function, which will concatenate the strings to produce a new string.
If the parameters are not strings, they must first be transformed using the concat() method before concatenation can take place.
Owing to the immutability of JavaScript strings, the concat() method concatenates all input strings into a single new string rather than modifying the original strings.
I hope this article on string concatenation in javascript helps you and the steps and method mentioned above are easy to follow and implement.