Navigate back to the homepage

how to add character in string in javascript?

Infinitbility
JavaScript
July 22nd, 2022 · 1 min read
how to add character in string in javascript?

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.

1let name = "";
2
3name += "Infinite";
4name += " ";
5name += "Ability";
6
7console.log(name)
8// 👇️ Output
9// 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. )

1let name = "";
2
3// add string how much you want
4name += "Infinite";
5name += " ";
6name += "Ability";
7
8console.log(name)
9// 👇️ Output
10// 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.

1let name = "";
2
3const names = ["Infinite", "Ability"];
4for(let value of names){
5 name += value;
6 name += " ";
7}
8
9console.log(name)
10// 👇️ Output
11// 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.

1const names = ["Infinite", "Ability"];
2let name = names.join();
3
4console.log(name)
5// 👇️ Output
6// '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.

1let name = "";
2
3const nameObj = {
4 firstName: "Infinite",
5 lastName: "Ability",
6};
7for(let [key, value] of Object.entries(nameObj)){
8 name += value;
9 name += " ";
10}
11
12console.log(name)
13// 👇️ Output
14// 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.

1let name = "";
2
3const nameObj = {
4 firstName: "Infinite",
5 lastName: "Ability",
6};
7name = Object.values(nameObj).join();
8
9console.log(name)
10// 👇️ Output
11// 'Infinite,Ability'

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to delete flow in node red?

how to delete flow in node red?

Example of removing flows from the node-red with the step-by-step guide.

July 21st, 2022 · 1 min read
how to uninstall node in node red?

how to uninstall node in node red?

Example of uninstalling node from the node-red with the step-by-step guide.

July 21st, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]