String concatination without +

# Cedric neuland (9 years ago)

I hope this is the right place for this..

I was wondering why this: 1+'b' + "c" can't be written like this: 1'b'"c"

Perhapse this can be a new feature of ES?

# kdex (9 years ago)

No one says you have to use the "+" operator. Why don't you use template literals?

# Isiah Meadows (9 years ago)

They don't trim the leading indent, but that's easy enough to implement in a template tag.

# Cedric neuland (9 years ago)

It would be less to write like example below.

a='a'
console.log(`b${a}`); //7 chars
console.log('b'+a); //5 chars
console.log('b'a); //4 chars -> SyntaxError
# kdex (9 years ago)

@Cedric: Your proposed change would break template literals. Consider this example:

const tag = () => 1;

console.log(tag`test`); // Valid syntax, but no way to keep apart tagged template literals from your syntax syntactically

Further, with your approach, I don't see a way to take care of proper spacing between the injected variables. How would you concatenate just two variables, a and b, without a space in between, without accidentally referencing ab?

Lastly, is there any considerable benefit from your proposal, apart from maybe saving a space character in certain cases? In most real-world use cases, template literals are usually shorter than their + equivalent, even if you uglify your whitespace:

const message1 = `Good job, ${user.name}! You've earned ${user.points} with your comment.`;
const message2 = "Good job, " + user.name + "! You've earned " + user.points + " with your comment.";
const message3 = "Good job, "+user.name+"! You've earned "+user.points+" with your comment.";
# Bob Myers (9 years ago)

According to Brian Kernighan in "Masterminds of Programming":

"A more dubious design decision in AWK is that concatenation was expressed by adjacency, without an explicit operator; a sequence of adjacent values is just concatenated....I think that's an example of stupid design." Bob

# Andrea Giammarchi (9 years ago)

concatenation was expressed by adjacency

I've seen worst in few PL, using adjacency to justify function invocation ( you know ... bash, ES.next @decorators ... )

Although I agree with all the thing we could discuss here, this about strings is honestly, IMO, the least.

String templates + tags solved them all.

Best