Reputation: 430
I'm going to sort an array using merge sort. I built a function for it like this and executed it using Node.js.
arr = [57, 23, 45, 12, 49, 59];
//sort array using merge sort
function mergeSort(arr) {
//nested function to merge decomposed arrays
function mergeArrays(leftArr, rightArr) {
sortedArr = [];
while (leftArr.length && rightArr.length) {
if (leftArr[0] > rightArr[0]) {
sortedArr.push(rightArr.shift());
} else {
sortedArr.push(leftArr.shift());
}
}
sortedArr.push(...leftArr, ...rightArr);
return sortedArr;
}
//end line of the nested array
if (arr.length > 1) {
m = Math.floor(arr.length / 2);
// console.log(arr.splice(m))
leftArr = mergeSort(arr.splice(0, m));//this line makes the defect if there is no "var or let" in front of it.
rightArr = mergeSort(arr);
return mergeArrays(leftArr, rightArr);
} else {
return arr;
}
}
console.log(mergeSort(arr));
Above code is supposed to sort the initial array of the code.
arr = [57, 23, 45, 12, 49, 59]//this array
So it is supposed to give a result like this.
[12, 23, 45, 49, 57, 59]
Instead it gives me a result like this.
[49, 59]
But when I put "var" or "let" in front of "leftArr" variable (please look inside the bottom "if" condition, I have mentioned the relevant line by commenting on it) the code gives the correct result. I know we have to declare variables using var, let or const. But even though I didn't do it javascript had never gave me any errors until now. So someone please help me to understand why this defect is occurring when "var" is not presented. Thanks in advance to anyone who would like to help me.
Upvotes: 0
Views: 40
Reputation: 33929
From var
on MDN:
Unqualified identifier assignments
The global object sits at the top of the scope chain. When attempting to resolve a name to a value, the scope chain is searched. This means that properties on the global object are conveniently visible from every scope, without having to qualify the names with
globalThis.
orwindow.
orglobal.
.Because the global object has a
String
property (Object.hasOwn(globalThis, 'String')
), you can use the following code:function foo() { String('s') // Note the function `String` is implicitly visible }
So the global object will ultimately be searched for unqualified identifiers. You don't have to type
globalThis.String
, you can just type the unqualifiedString
. The corollary, in non-strict mode, is that assignment to unqualified identifiers will, if there is no variable of the same name declared in the scope chain, assume you want to create a property with that name on the global object.foo = 'f' // In non-strict mode, assumes you want to create a property named `foo` on the global object Object.hasOwn(globalThis, 'foo') // true
In ECMAScript 5, this behavior was changed for strict mode. Assignment to an unqualified identifier in strict mode will result in a
ReferenceError
, to avoid the accidental creation of properties on the global object.Note that the implication of the above, is that, contrary to popular misinformation, JavaScript does not have implicit or undeclared variables, it merely has a syntax that looks like it does.
Read more on var
, let
, and const
Upvotes: 1