JavaScript array length is weird

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

That's what it's supposed to do, isn't it?

siamekanto
Автор

Weird? It's the most logical thing js has lmao

NHCH
Автор

That's not weird. What's weird would be setting the length to 5, then the program adds 'pear' and 'strawberry' to the array.

lejames
Автор

this is actually a very good feature helping manage array length dynamic

tibebetech
Автор

Under the hood the array has to reserve memory. You can either reallocate the memory at ever add OR right aways allocate memory for e.g. 10 elements and then simply set them, which is supposing to be faster. The problem with script "kiddies" is: they never learn how this is ACTUALLY being implemented under the hood. If you learn programming in C or PASCAL you would understand that a collection either needs to dynamically allocate and move memory or use something like linked lists. Linked lists don't require reallocation but are SLOW. Continuous memory (array) is very fast but if you if you want an element that will require to grow the allocated memory this is costly. So typically a strategy to implement this to allocate more memory right away to avoid reallocation. In C#: arrays are fixed size. List has an option to specify a capacity but the size is dynamic (and internally a list is implemented as an array).

nothingisreal
Автор

Ugh, I just found out recently you can assign values to negative indices and JS is just like, “sure thing, buddy 👍”

dave
Автор

the empty items are just properties on the array that dont even exist, thats why theyre not undefined but return undefined when you index them
just like an object key

electricengine
Автор

C/C++ developers having a stroke rn. 🤣

muneeburrehman
Автор

This isn’t weird, but it’s something I’ve never thought of. I will keep this in mind. I’ve been coding in js for 4 years lol

devvy
Автор

I tried to learn javascript after languages like c#, c++. It just doesn't make sense. It makes! But when I found out TypeScript it became so much better.

adamandsteve
Автор

its like CHAR, the length is fixed but if you doesnt use all spaces in the array, they will show empty fields but with memory allocated

nalbertcerqueira
Автор

Whole JS is weird! I started learning it after learn Python quite well, and it was funny to me how much illogic there is in this language

goranjosic
Автор

If you give static array number it does allocate the memory but doesnt put any value in there

gokhanburhan
Автор

Whats weird? Was expecting something unlogical, but this was incredibly easy to understand

dom
Автор

This is a certainly an unconventional feature, its there for backwards compatibility, but I never had any issues with it because in my many years of coding with js I never had a scenario where I needed to assign a value to .length

If you want something actually weird, you should look at how js spec handles the regex with global flag. Its one of the worst pitfalls of working with regex on js.

wlockuz
Автор

This video is beautiful. I enjoyed watching it.

Thanks

AlexAnie-uznr
Автор

That’s the behavior I would expect tbh.

milantornier
Автор

Imagine that they turned .length into a read-only and every single js framework dont work anymore

loctite
Автор

This glitch shows we are living in the matrix 😊

VideoDarslar
Автор

In the grim dark of the 3rd millennium, there is only javascript

AvenDonn
join shbcf.ru