I’m having trouble with global variables in JavaScript. From every article I’ve read a variable declared out of a function has a complete scope. But below alert(leftMargin); results in ‘undefined’ when inside a function;
var leftMargin = 36;
alert(leftMargin); /* '36' */
function position(direction) {
alert(leftMargin); /* 'undefined' */
}
Are you positive it’s undefined?
It alerts 36 twice for me, as expected. It might be unset between defining the
leftMarginvariable and actually callingposition().