1

In Ruby, if you use single quotes to make a string, the program parses it so that the output is literally what you wrote.

For example, if you create a string the following way:

variable_a = 'my\nname\nis\nOliver\nQueen'

the output of puts variable_a is

>>my\nname\nis\nOliver\nQueen

However, if you instead use double quotes when building the string, like so:

variable_b = "my\nname\nis\nOliver\nQueen"

the output of puts variable_b would be

>>my
>>name
>>is
>>Oliver
>>Queen

I am looking for a way in Javascript that does just what the single quotes do in Ruby, so that there will be less mistakes when trying to properly build a string that contains backslashes, and other characters that would 'break' the intended string.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Sammy I.
  • 2,523
  • 4
  • 29
  • 49

1 Answers1

2

Single and double quotes in Javascript are equivalent. The only real reason to use one over the other is preference, and avoiding escaping embedded quotes, e.g.

"Don't need to escape this apostrophe."

or

'No need to escape this "quoted" word.'

Unless you are talking about JSON. In JSON you must use double quotes or it is considered a syntax error by many parsers.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112