2

Possible Duplicate:
What is the difference between ‘ and “ in JavaScript?

Should i use ' or " char, to define a string in javascript?

Is it just a developer's choice? I think, strings more offten contains ' char, so i prefer using of "(with inner " translated to \").

What do you think?

Community
  • 1
  • 1
Feryt
  • 2,262
  • 2
  • 22
  • 32

7 Answers7

2

I prefer to use ", so after a full day of coding in JavaScript, I don't try to use ' on strings in C#

Allen Rice
  • 19,068
  • 14
  • 83
  • 115
  • 1
    When I see single quotes in JavaScript it makes me cringe, unless they are using it for something regarding proper English grammar...I would rather just escape double quotes than use singles. I guess I spent too much time in C, C++ and C# using single quotes always means the char data type to me, not a string. – Jason Bunting Jul 09 '09 at 17:07
2

I normally use the single quote simply because in ASPX it doesn't conflict with double quotes inside serverside scripts (<%= %>). Other than that it is a developer's choice.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
2

Most of the time I use html elements like that :

<input type="button" onclick="alert('test')" />

because of the attribute value has double quotes, I use single quote to represent javascript strings.

Canavar
  • 47,715
  • 17
  • 91
  • 122
2

It's up to you. Double quotes are more common than single quotes in general, but I'll use single quotes if I have a double quote in my string. I don't force myself to only use double quotes.

var name = "John Doe";
alert('Hello there, "'+name+'", if that is your real name.');
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

I also prefer ", but tend to switch to ' if I have strings that contain HTML or XML code. But it's more a matter of taste since, if I remember correctly, ' is allowed in HTML and XML for attributes as well, so you could also do it the other way round.

OregonGhost
  • 23,359
  • 7
  • 71
  • 108
1

My personal preference for Python (where, like JS, single and double quotes are equivalent) is for single quotes, just because that way there are "fewer pixels showing" (OK, a somewhat arbitrary criterion;-). But, it IS a matter for a "style guide" (either a personal one or one shared by a group of programmers working on the same codebase) rather than any objective rule.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
0

There are two choices in Javascript solely for the developer to choose whichever is convenient at the time. There is no semantic or syntactic difference, unlike in other languages. So, use both to wild abandon!

Adam Luter
  • 2,193
  • 1
  • 15
  • 20