Skip to content

Instantly share code, notes, and snippets.

@getify
Created August 22, 2012 03:01
Show Gist options
  • Select an option

  • Save getify/3421868 to your computer and use it in GitHub Desktop.

Select an option

Save getify/3421868 to your computer and use it in GitHub Desktop.
`return` syntax errors
// i understand THAT javascript doesn't assume a `;` before `return` in
// these examples. but i don't understand WHY it can't? why didn't they
// say that the grammar for `return` always assumes the previous statement
// is complete?
// NOTE: david baron pointed out that `if(a)return;` definitely wouldn't want
// to have this assumption. good point. but the others?
// NOTE: assume all these are inside a function, so `return` is safe.
// illegal
var a=""return;
var a;a=""return;
var a;a={}return;
var a;(a="")return;
var a;[a=""]return;
var a;a=[]return;
// legal
var a;{a=""}return;
if(a){}return;
for(var a=0; a<1; a++){}return;
function a(){}return;
switch(a){}return;
@rwaldron
Copy link
Copy Markdown

Looks like all statements:

(but that still doesn't answer why)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment