Logo

JavaScript: Concatenation

Often, strings need to be assembled from several parts, for example, to combine a first and last name, add a unit of measurement, or compose text from a template. The concatenation operation, that is, gluing strings together, is used for this.

How to combine strings

In JavaScript, strings are combined using the + operator. Even though this operator is also used to add numbers, in the case of strings it means combining, that is, gluing the contents together.

console.log('Dragon' + 'stone');
// => Dragonstone

Order matters. First comes the left part ('Dragon'), then the right part ('stone'). The result comes out in the order in which the operands are specified.

Here is how combining several strings works:

'Hello' + ', ' + 'World!'
└──┬──┘   └┬┘   └──┬───┘
   └────┬───┘       │
  'Hello, '    +  'World!'
     └──────┬───────┘
      'Hello, World!'

Examples.

console.log('Kings' + 'wood');       // => Kingswood
console.log('Kings' + 'road');       // => Kingsroad
// Here we use double quotes on the outside because there is a single quote inside
console.log("King's" + 'Landing');   // => King'sLanding

JavaScript lets you combine strings even if they are written with different quotes. The main thing is that both parts are strings.

A space is also a character

When combining, JavaScript does not insert spaces automatically. If there should be a space between the parts, it must be specified manually.

// Space at the end of the first string
console.log("King's " + 'Landing');  // => King's Landing

// Space at the beginning of the second string
console.log("King's" + ' Landing');  // => King's Landing

The result will be the same. But if you do not add a space, the words will glue together.

Escape sequences

In strings, you can use escape sequences, for example \n for a line break or \t for a tab. During concatenation, they work the same as any other characters.

console.log('Hello,' + '\n' + 'World!');
// Hello,
// World!

In the same way, you can use the tab \t to align output.

console.log('A' + '\t' + 'B'); // => A	B

Conclusion

Concatenation is the combining of strings via +, and strings can be combined regardless of the type of quotes.

  • Gluing happens strictly in order from left to right.
  • Spaces are not added automatically; they must be included in the strings manually.

Instructions

The site automatically generates links to repository pages by assembling them from separate parts. Assemble the link using concatenation and print it to the screen:

https://github.com/hexlet/exercises-javascript

Each URL component is a separate string: the protocol, the domain, the owner name, and the repository name.

Tips

  • If the editor contains // BEGIN and // END, then the code must be written between these lines.

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!