I tried executing the code in Chrome Developer Console and i got this odd result which am not able to understand
var arr = [[2,2]];console.log('Array is',arr);arr[0] = [3,3]
The output i got after executing this is
Array is [[3,3]]
The assignment should happen after the console.log
had been executed.But it magically happened before that.
To clarify i tried running the same code in JsBin.However in JSBin i got the expected out which is
Array is [[2,2]]
However this code yields expected result in chrome
var arr = [2,2];console.log('Array is',arr);arr[0] = 3;console.log(arr)
Output
Array is [2,2] [3,2]
Can someone help me understand this.
{} is shorthand for creating an empty object. You can consider this as the base for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array . [] is shorthand for creating an empty array.
It is shorthand for empty array. Same as new Array(). Also {} is an empty object. Objects are like hashtables in Js so you can use it as a dictionary. Copy link CC BY-SA 2.5.
JavaScript arrays are zero-indexed: the first element of an array is at index 0 , the second is at index 1 , and so on — and the last element is at the value of the array's length property minus 1 . JavaScript array-copy operations create shallow copies.
This is because chrome puts the value of the variable assignment in the console, when you initialize / declare a variable. This is an expected behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With