AppDividend
Latest Code Tutorials

Javascript String Concat: How to Concat String in JavaScript

Javascript String concat() is an inbuilt function that concatenates string arguments to a calling string and returns a new string. The concat() method is used for one or more strings. Javascript concat() method does not change existing strings but returns a new string containing the text of the joined strings.

Javascript string concat()

Javascript String concat() function concatenates the string arguments to a calling string and returns the new string. The changes to an original string or the returned string don’t affect the other. 

Concatenation is an operation that forms the basis of joining the two strings.

Merging strings is the inevitable aspect of programming.

Before getting into “String Concatenation in JavaScript”, let’s clear out the basics first.

When the interpreter executes an operation, the new string is created.

Every programming language has a different syntax for concatenation operation.

 The syntax is javascript string concat() syntax is following.

Syntax

string.concat(string1, string2, ..., stringX)

Parameters

A minimum one string parameter is required to concat a string with another string. The number of arguments to this function is equal to the number of strings to be joined together. Let us take an example. 

Return Value

Javascript concat() function returns a new string that is the combination of all the different strings passed to it as the argument.

Example

// app.js

let hello = 'Good Morning, ';
console.log(hello.concat('MJ', '. Have a nice day.'));

Javascript String Concat Example | String.prototype.concat() Tutorial

If the arguments are not of a type string, they are converted to string values before concatenating.

One test concludes that the assignment operators (+) can be used in string concatenation instead of using the string concat() method.

Javascript concat multiple strings

We can write the following code to concat three strings.

// app.js

let str1 = 'Harry, ';
let str2 = "Hermione, and";
let str3 = " Ronald";
let res = str1.concat(str2, str3);
console.log(res);

We can also do the same thing with the + operator.

// app.js

let resplus = str1 + str2 + str3;
console.log(resplus);

The output will be the same for this example, but we have used the + operator instead of the concat method.

Concat three strings

Also, concatenation is applicable for other data types like character, integer, binary, floating, etc.

But for this to happen, these data types are first converted into the strings.

Also, when we deal with objects, the string concatenation is only possible if either or both the objects belong to the same class.

Javascript Concat Two Strings with Comma

Let’s say we have been given the two strings “str1” and “str2” and we need to join them into the single string.

The final result should be something like this “str1, str 2”.

The “str1” and “str2” variables, however, does not have the “, “.

So now for the question: How do I concat these strings while having them separated by a comma?

See the following code.

const test = (str1, str2) => {
  let res = str2 + ',' + str1;
  return res;
}

console.log(test('venon', 'carnage'));

 See the output.

➜  es git:(master) ✗ node app
carnage,venon
➜  es git:(master) ✗

Finally, Javascript String concat() function example is over.

Recommended Posts

Javascript String split()

Javascript String substring()

Javascript String lastIndexOf()

Javascript String toUpperCase()

Javascript String toLowerCase()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.