-3

I do not have a problem, simply a question.

I am doing a Codecademy Tutorial using arrays and objects.

In the array, I have the various parts of an address including:

address: ["236 Apple Lane", "San Jose", "California", "95119"]

But if I change the code to a single quote like:

address: ['236 Apple Lane', 'San Jose', 'California', '95119']

It still works, AND, is actually part of the "Tip" in sorts that you should follow. I'm confused that both work. Are both correct? Or is one more preferred than the other in arrays?

Codecademy is flawed in sorts and I'd like to get a professionals opinion. I love this forum and it's inhabitants so I look forward to hearing your response. Thank you for taking the time to read this message and have a wonderful Tuesday!

danteMingione
  • 75
  • 1
  • 2
  • 7
  • 3
    No difference in JavaScript, they're interchangeable. No convention either, use what you like most. – elclanrs Mar 11 '14 at 23:15
  • 1
    The only time it makes a difference is if the string itself is likely to contain a single or double quote, then the other would be preferred. I see a whole sidebar full of this question duplicated. – Will P. Mar 11 '14 at 23:16
  • Yep, definitely a dup. – jfriend00 Mar 11 '14 at 23:41

1 Answers1

1

Javascript strings can be delineated with either single quotes or double quotes. Whichever one is not used at the end of the string can be used in the middle of the string without having to escape it.

So all of these are proper javascript syntax:

var str1 = "The quick brown fox";
var str2 = 'The quick brown fox';
var str3 = "The girl's long hair";
var str4 = 'I told him to just take a "little" bit.';

But, you can't use the character you choose for delineating the string in the middle of the string without escaping it. So these won't work for that reason:

var str5 = 'I told him that he'd get in trouble';
var str6 = "He said: "Don't ever say that to me again."";
jfriend00
  • 683,504
  • 96
  • 985
  • 979