Reputation: 5619
In CoffeeScript, the following statement evaluates to a JavaScript statement that is prefixed by an empty string.
I feel like there is an edge case with regards to type safety, but I can't think of it off the top of my head. In what case does the prefix make a difference?
CoffeeScript:
x = "#{foo} bar"
JavaScript:
x = "" + foo + " bar";
Upvotes: 0
Views: 150
Reputation: 1845
It ensures that the expression is always evaluated as a string, preventing e.g. numerical addition instead of concatenation. In the case where a string only contains a single interpolated expression, it also effectively converts that expression to a string. A couple of examples:
x = 2
y = 3
typeof "#{x}" is string # true since this compiles to "" + x
str2 = "#{x}#{y}" # We want the string "23" here, not the number 5
Upvotes: 1