What would this PHP array look like in JavaScript?

What would be the closest translation?

$nursery_rhyme = array(“mary”, “had”, “a”, “little”, “lamb”);
link|flag
3  
This is not even valid PHP (there are no smart quotes in PHP). Wild guess: copied from your homework? – Tomalak Dec 18 at 17:21

5 Answers

var nursery_rhyme=new Array("mary", "had", "a", "little", "lamb");
link|flag

As others mentioned, the curly/smart quotes are invalid.

You can get the JavaScript representation of most simple PHP structures by running the data through json_encode():

php> echo json_encode(array("mary", "had", "a", "little", "lamb"))
["mary","had","a","little","lamb"]
link|flag
var nursery_rhyme = ["mary", "had", "a", "little", "lamb"];
link|flag
var nursery_ryhme = ["mary", "had", "a", "little", "lamb"];
link|flag
1  
There are no smart quotes in JavaScript. ;-) – Tomalak Dec 18 at 17:22
Haha true. I apparently fail at copy+paste. I wonder if I should even bother fixing the answer, because off all the other 100% equivalent answers. – Jonathan Dec 18 at 17:32
Just for the record: The downvote is not mine. – Tomalak Dec 18 at 17:55

Closest JS translation:

$nursery_rhyme = new Array("mary", "had", "a", "little", "lamb");

Notice:

  • no var is needed
  • you can still use the $
link|flag
cheers to the quote-fix pts, I didn't notice that – vol7ron Dec 18 at 17:52

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.