Hi Friends 👋,

Welcome To Infinitbility! ❤️

To add a character in a string in javascript, just use the + operator it will concat two strings in one string and for short syntax, you can also use the += operator.

let’s take an example of adding two characters in a single string.

let name = "";

name += "Infinite";
name += " ";
name += "Ability";

console.log(name)
// 👇️ Output
//  Infinite Ability

Today, I’m going to show you How do I add a character in a string in javascript, as above mentioned here, I’m going to use the + operator to add a string to another string.

Let’s start today’s tutorial how do you add character in string in javascript?

Here we will take the example of

  1. add multiple strings to the string
  2. add an array of string to the string
  3. add an object of string to string

Let’s write code…

Add multiple strings to string example

To add multiple strings to a string, use the += operator to add n numbers of strings in a variable. (it works in case of static. )

let name = "";

// add string how much you want
name += "Infinite";
name += " ";
name += "Ability";

console.log(name)
// 👇️ Output
//  Infinite Ability

Add an array of string to the string

To add an array of strings to a string, use the for...of loop with the += operator it will concat n number array elements to a single string.

let name = "";

const names = ["Infinite", "Ability"];
for(let value of names){
  name += value;
  name += " ";
}

console.log(name)
// 👇️ Output
//  Infinite Ability

If you need an array of strings as a comma-separated string just use the join() method. it will convert your array of strings to comma-separated strings.

const names = ["Infinite", "Ability"];
let name = names.join();

console.log(name)
// 👇️ Output
//  'Infinite,Ability'

Add object of string to string

To add an object of string to string, use for...of loop and Object.entries() with the += operator it will concat n number object elements to a single string.

let name = "";

const nameObj = {
    firstName: "Infinite",
    lastName: "Ability",
};
for(let [key, value] of Object.entries(nameObj)){
  name += value;
  name += " ";
}

console.log(name)
// 👇️ Output
//  Infinite Ability

If you need the object of the string as a comma-separated string just uses with Object.values() with join() method. it will convert your array of strings to comma-separated strings.

let name = "";

const nameObj = {
    firstName: "Infinite",
    lastName: "Ability",
};
name = Object.values(nameObj).join();

console.log(name)
// 👇️ Output
//  'Infinite,Ability'

I hope it helps you, All the best 👍.