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.
Table of Contents
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:
1 2 3 4 5 |
var str='avascript'; var result= 'j'+str; console.log(result) |
Output:
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:
1 2 3 4 5 6 |
var str='avascript'; var chr='j' var result= chr.concat(str); console.log(result) |
Output:
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:
1 2 3 4 5 |
var str='javascrip'; var result= str+ 't'; console.log(result) |
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:
1 2 3 4 5 6 |
var str='javascrip'; var chr='t' var result= str.concat(chr); console.log(result) |
Output:
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:
1 2 3 4 5 6 7 8 |
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:
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.
1 2 3 4 5 6 7 8 |
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:
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.
1 2 3 4 5 6 7 8 |
var arr = ['B', 'L', 'O','G']; var str = 'JAVA2'; for (var c in arr){ str = str + arr[c]; } console.log(str); |
Output:
Using concat() method
We can iterate through array using for loop and add each char of array to string using concat method.
1 2 3 4 5 6 7 8 |
var arr = ['B', 'L', 'O','G']; var str = 'JAVA2'; for (var c in arr){ str = str.concat(arr[c]); } console.log(str); |
Output:
That’s all about how to add character to String in javascript.