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 th…


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer

js-test-36

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!' won’t raise any errors and is perfectly fine from the language standpoint.

Once it’s executed, the array will have an extra property greeting.

The length of an array won’t change as Hello, world! isn’t considered one of the elements of the array.

ANSWER: the length of an array won’t change and will remain equal to 4. The second console.log prints out Hello, world! to the screen.

Learn Full-Stack JavaScript


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer


Print Share Comment Cite Upload Translate Updates
APA

Coderslang: Become a Software Engineer | Sciencx (2021-04-29T15:25:22+00:00) JS Interview #36: Can you add a new property to the JS array?. Retrieved from https://www.scien.cx/2021/04/29/js-interview-36-can-you-add-a-new-property-to-the-js-array/

MLA
" » JS Interview #36: Can you add a new property to the JS array?." Coderslang: Become a Software Engineer | Sciencx - Thursday April 29, 2021, https://www.scien.cx/2021/04/29/js-interview-36-can-you-add-a-new-property-to-the-js-array/
HARVARD
Coderslang: Become a Software Engineer | Sciencx Thursday April 29, 2021 » JS Interview #36: Can you add a new property to the JS array?., viewed ,<https://www.scien.cx/2021/04/29/js-interview-36-can-you-add-a-new-property-to-the-js-array/>
VANCOUVER
Coderslang: Become a Software Engineer | Sciencx - » JS Interview #36: Can you add a new property to the JS array?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/29/js-interview-36-can-you-add-a-new-property-to-the-js-array/
CHICAGO
" » JS Interview #36: Can you add a new property to the JS array?." Coderslang: Become a Software Engineer | Sciencx - Accessed . https://www.scien.cx/2021/04/29/js-interview-36-can-you-add-a-new-property-to-the-js-array/
IEEE
" » JS Interview #36: Can you add a new property to the JS array?." Coderslang: Become a Software Engineer | Sciencx [Online]. Available: https://www.scien.cx/2021/04/29/js-interview-36-can-you-add-a-new-property-to-the-js-array/. [Accessed: ]
rf:citation
» JS Interview #36: Can you add a new property to the JS array? | Coderslang: Become a Software Engineer | Sciencx | https://www.scien.cx/2021/04/29/js-interview-36-can-you-add-a-new-property-to-the-js-array/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.