An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
JS Interview 36: Can you add a new property to the JS array?
Does the new array property affect its length? Is such an assignment valid at all?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
JavaScript arrays are used to store values in an orderly fashion. Normally, to add a new element, you should use the built-in push()
function.
But, in JS, all arrays are objects in the first place. Which means you can add new fields to them.
The assignment arr.greeting = 'Hello, world!'
wont raise any errors and is perfectly fine from the language standpoint.
Once its executed, the array will have an extra property greeting
.
The length of an array wont change as Hello, world!
isnt considered one of the elements of the array.
ANSWER: the length of an array wont change and will remain equal to 4
. The second console.log
prints out Hello, world!
to the screen.
Original Link: https://dev.to/coderslang/js-interview-36-can-you-add-a-new-property-to-the-js-array-137b
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To