Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does CoffeeScript support interpolation in single quotation string?

I come to this question:

 f = (param) ->
      console.info '#{param}'
 f(1)

The outcome is #{param}

When I enclose the string with double quotation marks, this just print 1. I have also tested it in Ruby, its behaviour is the same. But that just contradicts the rule in CoffeeScript.org:

The golden rule of CoffeeScript is: "It's just JavaScript".

Because I think in Javascript, single quotes and double quotes are treated equally. And I do not use Ruby often. Can anyone explain why?

Thanks a lot.

like image 535
Chao Zhang Avatar asked Aug 23 '12 02:08

Chao Zhang


People also ask

What is CoffeeScript used for?

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.

Are single quotes more pythonic?

In Python, such sequence of characters is included inside single or double quotes. As far as language syntax is concerned, there is no difference in single or double quoted string. Both representations can be used interchangeably.

How do you pass a single quote in a string?

Enclosing Quotation Marks That means strings containing single quotes need to use double quotes and strings containing double quotes need to use single quotes. "It's six o'clock."; 'Remember to say "please" and "thank you."'; Alternatively, you can use a backslash \ to escape the quotation marks.

Can you use single quotes for Strings in JavaScript?

In JavaScript, there are three ways to write a string — they can be written inside single quotes ( ' ' ), double quotes ( " " ), or backticks ( ` ` ). The type of quote used must match on both sides, however it is possible that all three styles can be used throughout the same script.


2 Answers

From the CoffeeScript documentation:

Ruby-style string interpolation is included in CoffeeScript. Double-quoted strings allow for interpolated values, using #{ ... }, and single-quoted strings are literal.

like image 130
Justin Niessner Avatar answered Sep 30 '22 17:09

Justin Niessner


"It's just javascript" means it fundamentally compiles to ordinary JavaScript and doesn't attempt to take a radically different programming paradigm and compile it to JavaScript. CoffeeScript is primarily concerned with avoiding "the bad parts", boilerplate, and unnecessary syntax as opposed to introducing radically different basic constructs such as data types, etc.

JavaScript has no string interpolation. CoffeeScript brings this over from Ruby as a convenience. Disabling it for single quotes just gives you a clean way to get a string without the interpolation interpreted.

Don't take It's just JavaScript to mean It IS JavaScript. It's a flavor/variant/sibling.

like image 21
Peter Lyons Avatar answered Sep 30 '22 17:09

Peter Lyons