I see my global variable which is an object is getting modified inside a function.
Below is the example I created:
var globalVarForTest = ["dfbsdfbsdfb", "sfgb", "gtsgt", "ttt"];
function testingError() {
console.log("BEFORE")
console.log(globalVarForTest);
for (var i = 0; i < globalVarForTest.length; i++) {
console.log(globalVarForTest[i]);
}
//Modifying Local Variable
var localVarforTest = globalVarForTest;
for (var i = 0; i < localVarforTest.length; i++) {
localVarforTest[i] = localVarforTest[i].length;
}
console.log("AFTER")
console.log(globalVarForTest);
for (var i = 0; i < globalVarForTest.length; i++) {
console.log(globalVarForTest[i]);
}
}
testingError();
The function prints BEFORE and AFTER different values for a global variable.
How to set a local variable inside a function equal to a global array so that the global array is not modified in the function?