Skip to content

Commit

Permalink
Fix #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 3, 2016
1 parent fbf27c4 commit 8a32559
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions string/concat.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
# Interpolating variables

Instead of [String.prototype.concat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat).
Creating an `String` interpolating variables always was a pain.

The reason is that this way to concat is more primitive and need less wrapping around the `String` object.
The solution that have [more](https://jsperf.com/concat-vs-plus-vs-join) [perfomance](https://jsperf.com/string-concat-fast/17) is using `+=` operator:

Check [test#1](https://jsperf.com/concat-vs-plus-vs-join) and [test#2](https://jsperf.com/string-concat-fast/17) benchmarks.
```js

function greetings(name) {
var message = ''
message += 'Hello '
message += name
message += ', how are you?'
return message
}

var sayHello = greetings('Kiko')
console.log(sayHello) // => 'Hello Kiko, how are you?'
```

However, nowadays is totally recommended (less code and good perfomance) use template string for this purpose:

```js
function greetings(name) {
return `Hello ${name}, how are you?`
}

var sayHello = greetings('Kiko')
console.log(sayHello) // => 'Hello Kiko, how are you?'
```

0 comments on commit 8a32559

Please sign in to comment.