Skip to main content
fix undefined behavior example
Source Link
Austin
  • 716
  • 1
  • 5
  • 7

The main practical difference is hoisting. For example:

foo(); // alerts 'hello'
function foo() {alert('hello');}

vs

foo(); // throws an error since foo is undefined
var foo = function() {alert('hello');}

Also, I think thatthis is undefined behavior

function foo(){
  if (true) {
    function bar(){}
  }
  bar();
}

Is technically invalid/undefined, but all browsers support so it really doesn't matterwhile this is ok.

function foo(){
  if (true) {
    var bar = function(){}
  }
  bar();
}

The main practical difference is hoisting. For example:

foo(); // alerts 'hello'
function foo() {alert('hello');}

vs

foo(); // throws an error since foo is undefined
var foo = function() {alert('hello');}

Also, I think that

function foo(){
  function bar(){}
}

Is technically invalid/undefined, but all browsers support so it really doesn't matter.

The main practical difference is hoisting. For example:

foo(); // alerts 'hello'
function foo() {alert('hello');}

vs

foo(); // throws an error since foo is undefined
var foo = function() {alert('hello');}

Also, this is undefined behavior

function foo(){
  if (true) {
    function bar(){}
  }
  bar();
}

while this is ok.

function foo(){
  if (true) {
    var bar = function(){}
  }
  bar();
}
Source Link
Austin
  • 716
  • 1
  • 5
  • 7

The main practical difference is hoisting. For example:

foo(); // alerts 'hello'
function foo() {alert('hello');}

vs

foo(); // throws an error since foo is undefined
var foo = function() {alert('hello');}

Also, I think that

function foo(){
  function bar(){}
}

Is technically invalid/undefined, but all browsers support so it really doesn't matter.