Why variable hoisting after return works on some browsers, and some not?

前端 未结 5 1234
执笔经年
执笔经年 2020-11-22 12:11
alert(myVar1);
return false;
var myVar1;

Above code throws error in IE, FF and Opera stating that return statement has to come in the function. But

5条回答
  •  孤独总比滥情好
    2020-11-22 13:08

    This code makes little sense:

    • The var myVar1 will never be ran.
    • The return false; will not return a thing since you're not in a function

    Opera, IE and FF are right to throw an error because this code is really not valid since you're not able to return unless you are in a function.

    If it works in Safari and Chrome it must be because the javascript engine they use is ready to handle buggy code. My guess would be that they see the "return" and drop or replace it with some kind of break.

    More information about functions: http://www.w3schools.com/js/js_functions.asp

提交回复
热议问题