Add string to string in javascript

JavaScript add strings

JavaScript add strings tutorial shows how to concatenate strings in JavaScript.

In JavaScript, string is an object used to represent and manipulate a sequence of characters.

JavaScript add strings with + operator

The easiest way of concatenating strings is to use the + or the += operator. The + operator is used both for adding numbers and strings; in programming we say that the operator is overloaded.

let a = 'old'; let b = ' tree'; let c = a + b; console.log(c);

In the example, we add two strings with the + opeartor.

$ node add_string.js old tree

In the second example, we use the compound addition operator.

let msg = 'There are'; msg += ' three falcons'; msg += ' in the sky'; console.log(msg);

The example builds a message with the += operator.

$ node add_string2.js There are three falcons in the sky

JavaScript add strings with join

The join method creates and returns a new string by concatenating all of the elements of an array.

let words = ['There', 'are', 'three', 'falcons', 'in', 'the', 'sky']; let msg = words.join(' '); console.log(msg);

The example forms a message by concatenating words of an array.

$ node joining.js There are three falcons in the sky

JavaScript add strings with concat

The concat method concatenates the string arguments to the calling string and returns a new string.

Because the concat method is less efficient than the + operator, it is recommended to use the latter instead.

let a = 'old'; let c = a.concat(' tree'); console.log(c);

The example concatenates two strings with the built-in concat method.

JavaScript add strings with string formatting

We can build JavaScript strings with string formatting, which is essentially another form of string addition.

let w1 = 'two'; let w2 = 'eagles'; let msg = `There are $ $ in the sky`; console.log(msg);

The example builds a message using template literals.

$ node formatting.js There are two eagles in the sky

In this article we have presented several ways of concatenating strings in JavaScript.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

Источник

How to Concatenate Two Strings in JavaScript

Your Guide to String Concatenation in JavaScript.

When working on a project as a programmer, you will likely need to update a string in one of the codes by adding other strings to it. This is possible with the help of concatenation, a programming term used for combining several values into one value. In this article, we will look at string concatenation in JavaScript and how to merge strings using different methods.

Table of Contents

What is string concatenation in JavaScript?

String concatenation means the same thing in all programming languages. It simply adds one or more strings to another string, usually to the end of that string. For example, if you want to concatenate “Hello” and “Joshua”, it becomes “Hello Joshua” by adding Joshua to the end of Hello.

Methods to concatenate strings

Methods for string concatenation in JavaScript.webp

Let’s discuss the various ways to combine strings in JS.

Using the “+” operator

In JavaScript, the “+” operator can be used to add strings together. You can add as many strings as you want.

let greeting = "hello"; let name = "Joshua"; let aboutMe = "programmer"; console.log(greeting + name + "is a" + aboutMe); Output: //helloJoshuais aprogrammer 

You can see that there is a flaw in the output. There is no space between the variables that were concatenated. This can be corrected by adding a space between each concatenated variable.

 let greeting = "hello"; let name = "Joshua"; let aboutMe = "programmer"; console.log(greeting + " " + name + " " + "is a" + " " + aboutMe); Output: // hello Joshua is a programmer Other examples: let socialMedia1 = "Facebook" let socialMedia2 = "Twitter" let socialMedia3 = "Linkedln" console.log("As a Programmer, it is good to be available on" + " " + socialMedia1 + " " + socialMedia2 + " " + socialMedia3 ) Output: // As a Programmer, it is good to be available on Facebook Twitter Linkedln 

Although the output is now correct (by adding a space (“ “) between each variable that was concatenated), the method used can be laborious when you want to concatenate up to 10 variables that were declared and initialized. This can be addressed with the next method.

Using the template literals ($)

The template literals is a special character in JavaScript that serves various purposes, such as multiline strings, string concatenation, etc. An ES6 feature, it was introduced in 2015 and is supported in all modern browsers except Internet Explorer. One major feature, “variable substitution”, allows JavaScript string concatenation.

let greeting = "hello"; let name = "Joshua"; let aboutMe = "programmer"; console.log(`$ $ is a $`); The example shows that you don’t need to put a space like with the “+” operator. It is a template literal, so it takes care of it. More examples: let nameOfCompany = "Turing"; let stageOfCompany = "Startup"; console.log( `$ named one of America's Best $ Employers for 2022 by Forbes` ); Output: //Turing named one of America's Best Startup Employers for 2022 by Forbes 

The variable substitution method of template literals is one of the best ways to concatenate strings in JavaScript.

let part1 = "learning"; let part2 = "JavaScript"; let part3 = "missing"; console.log(`if you are not $ $, you are $`); 

//if you are not learning JavaScript, you are missing

Using the concat() method

concat() is a built-in method that combines strings in JS. It adds the new string to the old string and returns the overall string.

const myName = "Joshua"; console.log(myName.concat(" said that he loves programming")); Output: // Joshua said that he loves programming let a = "string1"; console.log(a.concat("string2, string3")); Output: string1string2, string3 

a – This is the existing string that you want to combine with other strings.

b – This is the new string that is to be added to a (the old string).

let a = "programming"; let b = "excellently well"; console.log(a.concat("help a programmer think", b)); Output: //programminghelp a programmer thinkexcellently well 

In the above example, you can see that the error made in the + operator is also made here. This can be resolved the same way, i.e., by adding a space to separate each concatenation.

console.log(a.concat(» help a programmer think «, b));

Putting a space at the beginning of the concatenation will separate “programming” and “help”. It will also separate “think” and “excellently”.

Note: When using the concat() method, each concatenation is separated by a comma otherwise it will display an error.

let a = "programming"; let b = "excellently well"; console.log(a.concat(" help a programmer think" b)); Output: // Uncaught SyntaxError: missing ) after argument list 

An error is displayed when there is no comma between “programmer” and “b” because what you console.log cannot be evaluated correctly. You need to make sure that it is formatted properly by adding a comma. This tells JavaScript that it is undergoing concatenation, that b is a variable, and that you want to concatenate with the existing string (“programming help a programmer think”).

console.log(a.concat(» help a programmer think», b));

Using array.join() method

This will concatenate all the values in an array and return a new string from an array, which by default is separated by a comma. You can use other string parameters or separators to join the string together.

const sport = ["Football", "Tennis", "Cricket", "Golf"]; console.log(sport.join()); Output: //Football,Tennis,Cricket,Golf 

In the first example, the default string parameter or separator is a comma but you can use others to separate them, such as space, dash, or asterisk.

const sport = ["Football", "Tennis", "Cricket", "Golf"]; console.log(sport.join(" ")) Output: Football Tennis Cricket Golf 
const sport = ["Football", "Tennis", "Cricket", "Golf"]; console.log(sport.join("-")); Output: Football-Tennis-Cricket-Golf 
const sport = ["Football", "Tennis", "Cricket", "Golf"]; console.log(sport.join("*")); Output: Football*Tennis*Cricket*Golf 

Conclusion

We’ve explored JavaScript concatenation, what it is, and the various methods used to concatenate strings, including the “+” operator, template literals, the concat method, and the array.join method. Use them in your projects whenever you need to update strings in your code.

Author

Onwuemene Joshua

Joshua is a frontend developer, a WordPress developer and a Technical writer. He has collaborated on projects which required his Html, CSS/SASS, TailwindCSS and JavaScript skills. He writes on frontend development explaining difficult concepts in a beginner-friendly manner.

Источник

Читайте также:  Bitrix24 доступ запрещен просмотр файла auth index php запрещен
Оцените статью