# String concatenation like in PHP

**URL:** <https://es.discourse.group/t/string-concatenation-like-in-php/1076>\
**Category:** 💡 Ideas\
**Created:** [November 10, 2021, 12:18pm UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076 "2021-11-10T12:18:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaroslav.tavgen](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jaroslav.tavgen/32/2732_2.png) [@jaroslav.tavgen](https://es.discourse.group/u/jaroslav.tavgen)\
**Post date:** [November 10, 2021, 12:18pm UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/1 "2021-11-10T12:18:58Z")

</div>

Absolutely obvious idea, and I don't understand why other programming languages have not implemented that thousand years ago:

"string" . " concatenation" // "string concatenation"

This is the way to remove confustion between + sign for strings and + sign for numbers.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [November 10, 2021, 2:30pm UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/2 "2021-11-10T14:30:29Z")

</div>

So, what would happen if you tried to use a number with this "." operator? Would it throw a type error? Or try to coerce it into a string?

And what about other operators that fall into a similar boat? Like using `obj[attr]` for both property access and array entry lookup? Should those be separated out as well, to make it more obvious what you're trying to do. (I know that technically array entry lookup _is_ property access, but on a high level, the two actions are done for different purposes, and a similar argument can easily be made to have a different array lookup operator).

(Also, as a minor note, we would need to pick some other operator, as `.` already means property access. You wouldn't be able to concatenate two string variables.)

```javascript
const x = 'hello '
const y = 'world!'
console.log(x . y) // Today this would print undefined, we can't break this behavior.

```

I'm a little skeptical about how much value this would actually add. I usually don't feel like I get that confused about what the "+" is doing when reading other people's code, it's usually easy to tell from the context. but as I actually think about this more, I do recall an area in my code where I've wanted to concatenate a bunch of items together as a string, some of which were not strings. I had ended up writing it like this:

```javascript
return '' + a + b + c + d + e

```

which feels a little icky. Though, I guess an alternative solution would have been to simply coerce the non-string values into strings first.

```javascript
return String(a) + String(b) + c + String(d) + e

```

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [November 10, 2021, 3:31pm UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/3 "2021-11-10T15:31:42Z")

</div>

```
return '' + a + b + c + d + e

```

...

```
return String(a) + String(b) + c + String(d) + e

```

Though these two are also not necessarily equivalent ;)

```javascript
const o = v => ({
  [Symbol.toPrimitive]([d]) {
    return d === 'd' ? `<${v}>` : `[${v}]`
  }
})
const a = o('a')
const b = o('b')
const c = ' "c" '
const d = o('d')
const e = ' "e" '

console.log('' + a + b + c + d + e) // <a><b> "c" <d> "e" 
console.log(String(a) + String(b) + c + String(d) + e) // [a][b] "c" [d] "e" 

```

---

<div class="post-metadata">

**Author:** ![claudepache](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudepache/32/236_2.png) [@claudepache](https://es.discourse.group/u/claudepache)\
**Post date:** [November 10, 2021, 5:02pm UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/4 "2021-11-10T17:02:14Z")

</div>

If we cannot use “`.`” (as does Perl or PHP), we can use “`~`” (as does Raku) or whatever else. This is not a problem.

The real problem is the legacy “`+`” operator, for which the legacy semantics will remain unchanged because of BC constraints, as no browser vendor would want to break the web. As a consequence, we are stuck with `"1" + 1 == 11`.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [November 10, 2021, 5:21pm UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/5 "2021-11-10T17:21:34Z")

</div>

There's also that "template literals exist", which do all the right things, and just don't happen to involve a binary operator.

---

<div class="post-metadata">

**Author:** ![Rudxain](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rudxain/32/2272_2.png) [@Rudxain](https://es.discourse.group/u/Rudxain)\
**Post date:** [September 14, 2022, 10:06pm UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/6 "2022-09-14T22:06:51Z")

</div>

I would suggest another operator, like `##`, for arbitrary concatenation of literals and variables. For concat of literals, we can omit the operator:

```javascript
const x = 'hello' ' ' 'world';
const y = "hello " ## "world";
console.log(x === y) //true

```

---

<div class="post-metadata">

**Author:** ![DiriectorDoc](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/diriectordoc/32/987_2.png) [@DiriectorDoc](https://es.discourse.group/u/DiriectorDoc)\
**Post date:** [September 17, 2022, 1:23am UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/7 "2022-09-17T01:23:17Z")

</div>

We don't need a system for concatenating two literals. Just make it one literal.

---

<div class="post-metadata">

**Author:** ![Rudxain](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rudxain/32/2272_2.png) [@Rudxain](https://es.discourse.group/u/Rudxain)\
**Post date:** [September 17, 2022, 6:10am UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/8 "2022-09-17T06:10:29Z")

</div>

> [@DiriectorDoc](#):
>
> We don't need a system for concatenating two literals. Just make it one literal.

But what if each literal is too long and needs to be split between lines?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [September 17, 2022, 6:27am UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/9 "2022-09-17T06:27:13Z")

</div>

It doesn’t.

Also you can use a template tag, or backslashes in a string literal.

---

<div class="post-metadata">

**Author:** ![Rudxain](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rudxain/32/2272_2.png) [@Rudxain](https://es.discourse.group/u/Rudxain)\
**Post date:** [September 17, 2022, 7:28am UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/10 "2022-09-17T07:28:01Z")

</div>

> [@ljharb](#):
>
> Also you can use a template tag, or backslashes in a string literal.

True. I forgot about tagged template literals.

But I didn't know about the backslash thing. I thought it only worked in some langs, but not JS. It looks kind of ugly:

```javascript
'\
example\
 text\
 string\
' == "example text string"

```

While a "cleaner" version looks like:

```javascript
'example'
' text'
' string' == "example text string"

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [September 17, 2022, 2:34pm UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/11 "2022-09-17T14:34:01Z")

</div>

[https://www.npmjs.com/package/deline](https://www.npmjs.com/package/deline)

---

<div class="post-metadata">

**Author:** ![Rudxain](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rudxain/32/2272_2.png) [@Rudxain](https://es.discourse.group/u/Rudxain)\
**Post date:** [September 18, 2022, 4:45am UTC](https://es.discourse.group/t/string-concatenation-like-in-php/1076/12 "2022-09-18T04:45:59Z")

</div>

I have to admit `deline` is a better alternative. Now I feel kind of stupid, lol
