How to Build Strings in JavaScript

  1. Build Strings in JavaScript
  2. Build Strings Using + and concat() in JavaScript
  3. Build Strings Using push() and join() in JavaScript
How to Build Strings in JavaScript

This article will discuss generating or building a string using the concatenation operator and some built-in methods in JavaScript with different code examples.

Build Strings in JavaScript

To build a string in JavaScript, we can say that if we need to combine small portions of the string, like words, to make a complete sentence, we must require attaching all the given portions and generate a new resultant string.

Here is the desired solution we need:

a = "Delft"

b = "Stack"

result = "DelftStack"

In JavaScript, we can achieve this task using a custom logic like simple concatenation of strings or using default methods.

Build Strings Using + and concat() in JavaScript

In JavaScript, we have an operator +. Using this operator between integers will create an addition, but it will concatenate and join the string if we use this with string values.

We also have a default string method, concat(), to join strings. We must pass the comma-separated string values in that method as an argument.

The concat() method will not change the original string. It will return a new string.

Syntax (using the + operator):

let finalResult = stringA + stringB

Syntax (using the concat() method):

let finalResult = myString.concate(stringA,stringB)

Example code:

<script>

let first = "Delft";
let second = "Stack";
let result1 = first + second ; // it will combine "DelftStack" together and create single string

console.log("concatenation using + operator : "+result1)

first = "hello";

let result2 = first.concat(" ","Delft","stack"); // it will combine "hello DelftStack" together and create single string

console.log("concatenation using concat() method : "+result2)

</script>

Output:

  "concatenation using `+` operator : DelftStack"

  "concatenation using `concat()` method : hello Delftstack"

Example code explanation:

  1. We created a string in assigned variables in the above JavaScript source code.
  2. Then, we have created the result1 variable and assigned the concatenated string using the + operator.
  3. We printed the result using console.log().
  4. We have reinitialized the string variable with hello.
  5. Using that variable, we have called the concat() method and passed comma-separated string values as an argument.
  6. We then printed the result using console.log() again.

Build Strings Using push() and join() in JavaScript

In JavaScript, we have the default push() method of an array to insert an array of elements. The join() method of an array generates a string of all array elements with commas.

We can use both methods to build a string.

Syntax:

array.push("stringA","stringB")

let finalResult = array.join()

Example code:

<script>

let array = [];

array.push("hello","delft","stack"); //inserting string into arrays

let result = array.join();

console.log("Using default method push and join : "+result)

</script>

Output:

  "Using default method push and join : hello,delft,stack"

Example code explanation:

  1. Again, we have created an array variable in the above JavaScript source code.
  2. Then, we have pushed the string elements into an array using the push() method.
  3. We used the join() method to create a string from an array.
  4. Lastly, we printed the result using console.log().

Related Article - JavaScript String