JavaScript concatenate strings

 JavaScript concatenate strings

In JavaScript we can use the + operator to concatenate or connect two strings.
This is pretty useful when you want to add something to the existing string.

Let's see this in action. Suppose we have three strings.
var firstName = "Santosh";
var lastName = "J";
var occupation = "Web consultant";

We can display this as per our liking in a single line. We can even add symbols to it when needed.

Suppose we want to display the persons first and last name in single line followed by the occupation we can do this:


Please note, it did not add spaces, you will have to add them manually in the strings or handle them using the empty quoted string like this +" ". Other wise it will show as SantoshJWeb consultant. I will leave this as an assignment for you, show me how you added spaces to this using the comments below. 

You could also do something like: Santosh works as Web consultant like so.
console.log(firstName+"works as"+occupation);

We could even do this using the compound assignment operator. Read about them here if you want to revise. It also supports addition to strings.
Suppose we have a statement and want to add to it. It would say "Hey kitty you go meow".

let statement = "Hey kitty";
statement += " you go meow";

Tutorials take time to write, please share with developers or learners thanks.

Post a Comment