7

For string literals, are any differences between using apostrophes or using quotes? I'm thinking in regard to performance, functionality, conventions, etc.

To clarify:

'Hello!'

vs.

"Hello!"

(for want of a more imaginative string)

Jay
  • 558
  • 1
  • 7
  • 23
  • 4
    *Exact duplicate:* http://stackoverflow.com/q/3149192/1563422 – Danny Beckett Mar 16 '13 at 04:26
  • Can you explain what are you worried about in this question ? – Vedant Terkar Mar 16 '13 at 04:28
  • 1
    The only difference: You have to hold `shift` to get `"` – Derek 朕會功夫 Mar 16 '13 at 04:39
  • The accepted answer to the above referenced question is pretty much what I would have said here: attributes in HTML using double quotes tends to make me want to use single quotes as the delimiters in javascript, but old habits die hard, and often I've used double quotes unless there's HTML involved. – JayC Mar 16 '13 at 04:40
  • I I tried to search for this question but I don't think I got results because I typed "apostrophes" instead of " single quotes". Thanks for the link. @Vedant Terkar Like I said, I'm asking if there are functional differences or performance differences or best practices. – Jay Mar 16 '13 at 05:08

3 Answers3

4

For me i use it depending on the contents of the string - will the string itself have double quotes or single quotes? if the string has one type of quote, i'll use the other so i don't have to escape it: e.g.:

my string is: <div class="someclass" >hello</div>

for the above i'd use single quote so i don't have to escape the single ones:

var str = '<div class="someclass" >hello</div>';

otherwise id need

var str = "<div class=\"someclass\" >hello</div>";

both are perfectly valid, but ones more easy on the coder...

Tucker
  • 7,017
  • 9
  • 37
  • 55
3

I wouldn't say there is a preferred method, you can use either. However If you are using one form of quote in the string, you might want to use the other as the literal.

alert('Say "Hello"');
alert("Say 'Hello'");

The most likely reason is programmer preference / API consistency.

The difference is that you don't need to escape single quotes in double quotes, or double quotes in single quotes. That is the only difference, if you do not count the fact that you must hold the Shift key to type "

Vitthal
  • 546
  • 3
  • 18
1

I don't think there may be any performance reason to use one or the other . Its just that sometimes you want to use double quotes inside the string so you will wrap the string in single quotes and vice versa .

Eg : var new = 'Abraham "Lincoln"';

AllTooSir
  • 48,828
  • 16
  • 130
  • 164