Add character to String in Javascript

Add character to String in Javascript

In this post, we will see how to add character to String in Javascript.

There are multiple ways to add character to String in Javascript. Let’s go through them one by one.

Add character to String at start of String in Javascript

Using + operator

You can simply use + operator to add character to String in Javascript.

Here is an example:

Output:

javascript

As you can see, we have added char j to var str using + operator.

Using concat() method

You can also use predefined method concat() to add char to String in Javascript at start.

concat() method joins two strings and returns new String. It does not change existing String.

If you need to change existing String, then you need to assign result of concat() method to same variable.

Here is an example:

Output:

javascript

As you can see, we have added char j to var str using concat() method.

Add character to String at end of String in Javascript

Using + operator

You can simply use + operator to add character to String in Javascript.

Here is an example:

javascript

As you can see, we have added char ‘t’ to var str using + operator at the end.

Using concat() method

You can also use predefined method concat to add char to String in Javascript at end.

Here is an example:

Output:

javascript

As you can see, we have added char ‘t’ to var str using concat() method at the end.

Add character to String at given postion

Using substring() method

You can use substring function to add char to String at particular position in javascript.
Here is an example:

Output:

java2blog

Here, we have concated string before the position, added character and then again concated String.

Using slice() method

You can also use slice() function with join() to add character to String in Javascript at particular position.

Output:

java2blog

This method is similar to substring() method one.

Add character from array to String in Javascript

Using + operator

We can iterate through array using for loop and add each character of array to string using + operator.

Output:

JAVA2BLOG

Using concat() method

We can iterate through array using for loop and add each char of array to string using concat method.

Output:

JAVA2BLOG

That’s all about how to add character to String in javascript.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *