Freshblood
Freshblood

Reputation: 6431

Using return is valid on global scope?

Using return is valid if code execution is not inside of a function or in the other i am totally on global scope.

var x = y + 1
if(x == 10)
  return;//visual studio gives warnings and says that "return keyword is not inside a function"

Upvotes: 1

Views: 66

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382177

No it's not. This makes a syntax error if it isn't inside a function.

From the ECMAScript spec :

An ECMAScript program is considered syntactically incorrect if it contains a return statement that is not within a FunctionBody.

Note that some browsers don't follow the spec rigorously :

  • Firefox is correct in refusing to execute the code
  • Chrome, incorrectly, accepts to run the code until it has to execute that faulty statement

Upvotes: 3

Related Questions