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.
[toc]

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:

var str='avascript';
var result= 'j'+str;
console.log(result)

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:

var str='avascript';
var chr='j'
var result= chr.concat(str);
console.log(result)

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:

var str='javascrip';
var result= str+ 't';
console.log(result)
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:

var str='javascrip';
var chr='t'
var result= str.concat(chr);
console.log(result)

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:

var str='javablog';
var chr='2'
var pos=4
// Add character 2 at position 4 in str
str = str.substring(0, pos) + chr + str.substring(pos, str.length);
console.log(str)

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.

var str='javablog';
var chr='2'
var pos=4
// Add character 2 at position 4 in str
var output = [str.slice(0, pos), chr, str.slice(pos)].join('');
console.log(output)

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.

var arr = ['B', 'L', 'O','G'];
var str = 'JAVA2';
for (var c in arr){
    str =  str + arr[c];
}
console.log(str);

Output:

JAVA2BLOG

Using concat() method

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

var arr = ['B', 'L', 'O','G'];
var str = 'JAVA2';
for (var c in arr){
    str =  str.concat(arr[c]);
}
console.log(str);

Output:

JAVA2BLOG

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

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.