Skip to content

Instantly share code, notes, and snippets.

@miwillhite
Created April 5, 2012 02:54
Show Gist options
  • Save miwillhite/2307640 to your computer and use it in GitHub Desktop.
Save miwillhite/2307640 to your computer and use it in GitHub Desktop.
Mind yo commas!
// This snippet constructs an array of objects...via some server-side templating thing
// Now can you tell me what's wrong with this code?
//
// ...
//
<script language="javascript">
var nouns = [
{% for noun in nouns['people']: -%}
{ 'noun': '{{ noun.name }}', 'category':'People', 'slug':'{{ noun.slug }}' },
{% endfor -%}
{% for noun in nouns['places']: -%}
{ 'noun': '{{ noun.name }}', 'category':'Places', 'slug':'{{ noun.slug }}' },
{% endfor -%}
{% for noun in nouns['things']: -%}
{ 'noun': '{{ noun.name }}', 'category':'Things', 'slug':'{{ noun.slug }}' },
{% endfor -%}
];
</script>
// Take a look at line 21 -- that NASTY LITTLE COMMA
//
// Chrome:
[1,2,3,].length //=> 3
//
// IE:
[1,2,3,].length //=> 4
JSON.stringify([1,2,3]) //=> [1,2,3]
JSON.stringify([1,2,3,]) //=> [1,2,3,null]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment