JavaScript `push` array with square brackets instead of parentheses - no error?

I did it by accident ...

var numbers = [1, 2, 3, 4]; numbers.push[5]; 

Why was there no error message?

push needs brackets, not square brackets. It was just a typo. I did not pay enough attention to what I was doing ... but why was there no error message?

As far as I can tell, the array of numbers has not been modified in any way. It's just ... nothing.

+5
source share
1 answer

numbers.push is just a function, but you are trying to find a property located in key 5 of it that will be evaluated as undefined .

 function test() { console.log("test"); } // test[5] evaluates to `undefined` and does nothing console.log(test[5]); // We can even manually set this without messing up the function test[5] = "foo"; // outputs "foo" console.log(test[5]); // outputs our expected value "test" test(); 
+8
source

Source: https://habr.com/ru/post/1274698/


All Articles