0

I'm learning about switches in Javascript, and whenever I add a case, I'm not sure whether to use single quotation marks or double quotation marks.

switch(answer)
{
    case 'hello':
        console.log("Hello there!");
        break;
}

vs

switch(answer)
{
    case "hello":
        console.log("Hello there!");
        break;
}

Note that in the line with the case, I put double quotation marks instead of single quotation marks on the second example. Which one is correct?

Jason Chen
  • 312
  • 1
  • 4
  • 12
  • 1
    Doesn't matter imho. Both are correct. More a matter of preference than anything else. – techfoobar Jul 20 '14 at 19:37
  • In many languages, Javascript may be excluded, single quotes are usually used to denote characters, not strings. In some languages like Perl, single quoted strings will take everything inside the quotes as literal, in that you cannot expand variables. I would say use double-quotes just to keep in line with what they are generally used to represent; strings. – Ryan J Jul 20 '14 at 19:39
  • 1
    if you have single quotes in your string use double, and vise versa, otherwise there is no other difference – Patrick Evans Jul 20 '14 at 19:40
  • 1
    Does not matter in Javascript as long as you don't mix and match. ``"a string"`` is same as ``'a string'`` but ``"a 'string'"`` is not same as ``'a "string"'`` – Sarbbottam Jul 20 '14 at 19:41
  • http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript – Hamid Alaei Jul 20 '14 at 19:41
  • It´s not because it is like that in javascript that it´s the right way. A string always should have double quotes, e.g. "string". Single quotes are used for characters, e.g. 'A'. And yea Javascript doesnt obey these rules, but that is because javascript is not a programming language. – fonZ Jul 20 '14 at 19:43

1 Answers1

2

You do whatever you like. String literals in JavaScript are written with either single quotes or double qoutes. It doesn't matter.

case 'hello': will be the same as case "hello":, just as console.log('Hello there!') will be the same as console.log("Hello there!")

Arjan Einbu
  • 13,543
  • 2
  • 56
  • 59