I know arrays in javascript are a bit special, compared to other languages, but I don\'t really get this behaviour, or what\'s going on here.
I\'d like to know why i
arr
is a parameter to your function already, and it is not undefined. var
gets hoisted, so to the interpreter, your current code actually looks something like:
function setupWindowProgBar(settings, window, palette, arr){
var arr; // **does not reassign the parameter**
console.log('start');
console.log(arr);
if(typeof arr == 'undefined'){
arr = [];
}
console.log(arr);
console.log('stop');
}
You should never be declaring a variable whose name is already scoped to the block/function you're using it in.
If arr
actually isn't defined in the argument, then it will be assigned to an array on the line arr = [];
, but at least on Chrome, that empty array will not necessarily be printed at the time you console.log
it.
console.log(arr);
does not necessarily immediately display the arr
- depending on your browser, it may only be populated in your console after you open your console, after this line has run:
arr[arr.length] = createProgBar('master', 'bg', window, ...