Here is the code
var numberArray = [0, 1]
(function() {
numberArray.push(2)
function nestedFunction() {
numberArray.push(3)
function anotherNestedFunction() {
numberArray.push(4)
}
console.log(numberArray)
}
})()
I am expecting numberArray
with value [0,1,2,3,4]
but it is giving this error:
TypeError
:[0,1]
is not a function
var numberArray = [0, 1]
(function() {
is equivalent to
var numberArray = [0, 1] (function() {
That is where the error rise.
To resolve the issue place ;
after the array declaration which JavaScript engine will consider both the line as separate statement:
var numberArray = [0, 1];
(function() {
numberArray.push(2);
function nestedFunction() {
numberArray.push(3);
function anotherNestedFunction() {
numberArray.push(4);
}
anotherNestedFunction();
console.log(numberArray);
}
nestedFunction();
})();
To ignore all these unexpected issue, it is always good practice to use semicolons (;
) after every statement in JavaScript.