JavaScript array length is weird

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ต.ค. 2024

ความคิดเห็น • 435

  • @siamekanto
    @siamekanto 2 ปีที่แล้ว +1074

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

    • @Meister256
      @Meister256 2 ปีที่แล้ว +139

      No, its not. The length property shouldn't work that way, it should be a read property, never a write property. Any other type of behavior on this property, which alters its content, is a language malfunction and should be corrected in the future. There are too many cases like this that have not been fixed because too many programmers have gotten used to these bad practices, and as a result we have an inconsistent language with countless bugs, exceptions, and inconsistent behaviors.

    • @ky3ow
      @ky3ow 2 ปีที่แล้ว +29

      @@Meister256 most bad practices kept in language for backward compatibility, so new versions dont break older

    • @Meister256
      @Meister256 2 ปีที่แล้ว +7

      @@ky3ow of course, that is why there are migration processes from obsolete to current functionalities. Every language goes through that process and that's why there are different versions.

    • @evendiloctum7856
      @evendiloctum7856 2 ปีที่แล้ว +31

      @@Meister256 " it should be a read property, never a write property" says who?

    • @widevader
      @widevader 2 ปีที่แล้ว +1

      .

  • @shubhamsehgal2336
    @shubhamsehgal2336 2 ปีที่แล้ว +539

    That's purely logical

    • @rahff99
      @rahff99 2 ปีที่แล้ว +13

      no is not logical at all, it violate encapsulation principle and make a indesirable or not intentional side effect on state of the instance.

    • @poopfartlord9695
      @poopfartlord9695 2 ปีที่แล้ว +18

      I mean the effects make sense, and could even be useful, however you should not be able to change the value of arr.length. It should obviously be read only.

    • @AndrewTSq
      @AndrewTSq ปีที่แล้ว +3

      @@poopfartlord9695Its probably unlogical for people used to say C where it works different. But in JS a array is not really the same thing as an array in C. one position in the array in Javascript takes up like tenths of bytes of memory. Already there we should understand that its not a normal array. Index 0 and Index 1 does not need to be saved next to eachother in Javascript memory. Its more of a linked list / hashtable behind the scenes. Thats why you can do things like this. I think its brilliant, and I dont care if some old law from 70ies programming think its wrong :)

    • @IllusionEDM
      @IllusionEDM ปีที่แล้ว +1

      Length should be immutable

    • @arjix8738
      @arjix8738 ปีที่แล้ว +1

      @@rahff99 an array is just an object where the keys are the indexes and the values are the items...
      so the length is just a getter/setter

  • @NHCH
    @NHCH 2 ปีที่แล้ว +1015

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

    • @camiscooked
      @camiscooked 2 ปีที่แล้ว +92

      Not many languages let you fuck around with an arrays length after initialization

    • @saytdiabloxsayt_family1925
      @saytdiabloxsayt_family1925 2 ปีที่แล้ว +7

      @@camiscooked probably not but certainly not like that lol, there's some ways to extend an array but not fuck it up with the length attribute

    • @8koi245
      @8koi245 2 ปีที่แล้ว

      Bruh true

    • @victorpimentel3072
      @victorpimentel3072 2 ปีที่แล้ว

      Fax

    • @raianmr2843
      @raianmr2843 2 ปีที่แล้ว +33

      lmao bro coded in js so much his programming standards turned undefined

  • @VideoDarslar
    @VideoDarslar 11 หลายเดือนก่อน +1

    This glitch shows we are living in the matrix 😊

  • @lejames5987
    @lejames5987 ปีที่แล้ว +79

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

  • @tibebetech4867
    @tibebetech4867 2 ปีที่แล้ว +35

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

  • @muneeburrehman4690
    @muneeburrehman4690 ปีที่แล้ว +75

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

    • @herosova
      @herosova ปีที่แล้ว +4

      yup

    • @kumarraj197
      @kumarraj197 ปีที่แล้ว +3

      Why?

    • @muneeburrehman4690
      @muneeburrehman4690 ปีที่แล้ว +6

      @@kumarraj197 We are actually too much used to the restrictions provided in C/C++. There's too much leniency in datatypes or array length in Javascript.

    • @user-0xDEEDBEEF
      @user-0xDEEDBEEF ปีที่แล้ว +1

      not C++. we have a vector with similar behavior.

  • @loctite417
    @loctite417 ปีที่แล้ว +2

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

  • @electricengine8407
    @electricengine8407 ปีที่แล้ว +3

    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

  • @wesosdequeso8360
    @wesosdequeso8360 ปีที่แล้ว

    I never faced the use case where I had to set an array's length and fill it with empty values.
    Usually length is a trivial value that reflects how many items are left.

  • @dave6012
    @dave6012 ปีที่แล้ว +31

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

    • @ales--channel
      @ales--channel ปีที่แล้ว +2

      Lmao no way, js is really it's on thing

    • @mridulpj
      @mridulpj ปีที่แล้ว +15

      Here's a secret. Javascript doesn't actually have arrays. They're just objects pretending to be arrays.

    • @electricengine8407
      @electricengine8407 ปีที่แล้ว +4

      its because arrays are objects duh
      have you tried assigning string keys to arrays?

    • @bhavishshetty5332
      @bhavishshetty5332 ปีที่แล้ว

      Imposter

    • @ko-Daegu
      @ko-Daegu ปีที่แล้ว

      @@electricengine8407yeah but ….

  • @01dom
    @01dom ปีที่แล้ว

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

  • @nalbertcerqueira6079
    @nalbertcerqueira6079 ปีที่แล้ว

    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

  • @austinmajeski9427
    @austinmajeski9427 11 หลายเดือนก่อน

    Turns out, this is a documented and supported feature. Wow.

    • @aschmelyun
      @aschmelyun  11 หลายเดือนก่อน

      JS never fails to surprise lol

  • @dog4ik
    @dog4ik 2 ปีที่แล้ว +34

    Didn’t know that length is mutable 😮

  • @AvenDonn
    @AvenDonn 2 ปีที่แล้ว +1

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

  • @devvy8343
    @devvy8343 ปีที่แล้ว

    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

  • @gokhanburhan382
    @gokhanburhan382 ปีที่แล้ว +1

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

  • @nothingisreal6345
    @nothingisreal6345 2 ปีที่แล้ว +4

    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).

    • @ko-Daegu
      @ko-Daegu ปีที่แล้ว

      How does this really explain why some items are undefined instead of adding to the last possible item location or simply length is read property and there’s another write property like all other languages almost ??
      Thus being said if I have an array of undefined how to get rid of all the undefined in an efficient way ?
      Let’s say I have
      [ “a”, undefined, …punch stuff…, “c”, undefined, undefined, “z”, undefined…]
      Want an array with just stuff without undefined how to actually do that in most optimized way? I thought not being in this situation is the best thing but hey what’s the next best thing then

  • @codenerd7823
    @codenerd7823 ปีที่แล้ว +1

    Now that's what I call, amazing 🤩

  • @adamandsteve13
    @adamandsteve13 ปีที่แล้ว

    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.

  • @mihaiyoo6
    @mihaiyoo6 ปีที่แล้ว

    heya, can you share the name of the extension you're using to run the conde in the VS-code console? thanks

  • @GottZ
    @GottZ ปีที่แล้ว

    holey arrays have insane performance drops tho.

  • @AlexAnie-uz2nr
    @AlexAnie-uz2nr ปีที่แล้ว

    This video is beautiful. I enjoyed watching it.
    Thanks

  • @basboerboom9328
    @basboerboom9328 ปีที่แล้ว +1

    Isn't this super nice to set a constant array length?

  • @AdnanShaikh-zh5zd
    @AdnanShaikh-zh5zd ปีที่แล้ว

    That's the most sane thing in javascript

  • @gilgameshgaming4210
    @gilgameshgaming4210 ปีที่แล้ว

    that's because length is a setter function

  • @fernando9175
    @fernando9175 ปีที่แล้ว

    Js is so flexible that even their developers haven't yet discover the limit.
    Webs, apps, games, data storage, backend, soon AI can be programmed with Js.

    • @julianmcmillan2867
      @julianmcmillan2867 ปีที่แล้ว +1

      You're not wrong, but you have to admit, JavaScript's limits are being stretched. It has the potential to be a very volatile language, like Python, but it was specifically designed on the fly to handle and create behaviour on webpages.
      It wasn't designed to work on embedded systems, desktop apps, A.I or gaming.
      The way JS is able to enter into all these other fields of application is with alot of environments, libraries, frameworks and API's being engineered which allow it to be able to work in those environments, but that means that these environments have to be moulded to accommodate JavaScript which obviously calls for limitations in what you're trying to accomplish.
      JS may become more and more volatile and flexible and might eventually veer towards applications for a general-purpose language, but I think it will be a very long time before it becomes that and even when it does, I don't think it will ever outcompete languages that were specifically designed for those purposes. JavaScript becomes complex and very abstract rather quickly and was designed to be the ideal language for the web.
      Taking JavaScript and applying it to other things doesn't mean it won't perform, it just won't perform nearly as well or be able to do as much as you'd like. It's like taking a dog, which can swim well, and forcing it to live in the ocean: just because it can swim, doesn't mean it should live in the ocean.
      Being a jack of all trades and a master of none may be better than being a master of one in most things, but not when it comes to programming a software engineering. There is always going to be a set of tools and an environment which is niched for specific applications. The more complex and robust you get, the more specialisation and precision matters as opposed to just being able to get the job done.

    • @fernando9175
      @fernando9175 ปีที่แล้ว

      @@julianmcmillan2867 you are right sir.
      And yes.
      Java, Python, C++, Scala will always be the dominant for serious programing.

    • @julianmcmillan2867
      @julianmcmillan2867 ปีที่แล้ว

      @@fernando9175 I wouldn't call it "serious". They're niched for their respective environments and usages, where some are specifically designed to work for a specific context where no other language can work there. That's all I meant.

  • @PanDiaxik
    @PanDiaxik ปีที่แล้ว

    What's weird about it is that changing a variable can remove elements from an array. It would be more logical to have a resize method. I wasn't sure if increasing the size of array after it was decreased would bring back the removed element or not.

  • @chr13
    @chr13 ปีที่แล้ว

    What's the purpose of setting the length to a greater number if the output remains the same as it the elements were undefined?

  • @devarshihazarika4871
    @devarshihazarika4871 ปีที่แล้ว

    Javascript is a people pleaser until "use strict"

  • @rahulr9539
    @rahulr9539 ปีที่แล้ว +2

    That's expected behaviour

    • @jakubswistak8003
      @jakubswistak8003 ปีที่แล้ว

      You shouldnt be able to change the size of the array. It should be read only. You can add elements to it if you want be because its an arraylist really but manually changing the size of the array under the hood is just dumb

  • @triplezgames3882
    @triplezgames3882 ปีที่แล้ว

    It really said: "❤️ empty items>"

  • @davidsousaRJ
    @davidsousaRJ ปีที่แล้ว

    I didn't know this feature. I find it very cool.

  • @Alanimdeo
    @Alanimdeo ปีที่แล้ว

    I didn't ever think I can mutate Array.length value.

  • @bpinkston21
    @bpinkston21 2 ปีที่แล้ว

    Why is Banana no longer included in the array after the length of array is increased to 5? Is it because it’s being compiled as length being 2 so banana is cut off > then length is 5 so add three blank item > then input new word at array.[3]?

    • @AndrewTSq
      @AndrewTSq ปีที่แล้ว

      It would be a catastroph if you get an item in an array when you allocate more memory for it. Just imagine having a loop that goes thru the length of your array, and it is in your pacemaker and suddenly you allocate more memory and gets some garbage data in the array, and stops your heart. When he sets the length to 2, you erase everything beyond index 2. But since JS lets you dynamicly allocate memory for arrays, when you increase to 5 you get 3 empty items which you should expect. If you know what hashtable is, that is how Arrays work in JS

  • @sulivan4873
    @sulivan4873 2 ปีที่แล้ว +1

    just can’t imagine a use case to mutate the length so maybe it shouldn’t accept it

    • @sharanoth
      @sharanoth 2 ปีที่แล้ว

      Performant way to return a subarray

    • @tratbagd4500
      @tratbagd4500 ปีที่แล้ว +1

      @@sharanoth still could have been done through a method rather then modifying the length attribute. Also, this only allows you to return a subarray starting at index 0 (a prefix), so not very useful tbh

    • @AndrewTSq
      @AndrewTSq ปีที่แล้ว

      In C say you allocate memory for an array with 255 items. But the software can get up to 300 items, making the software crash. Javascript just dynamicly allocate more memory and nothing happens.

  • @petevenuti7355
    @petevenuti7355 ปีที่แล้ว

    So if you specify a length , you can't overrun? It dismisses extra input? How?

    • @julianmcmillan2867
      @julianmcmillan2867 ปีที่แล้ว +1

      Because JS arrays aren't like arrays in C or C++. At their base level, they are but JS has a very high level of abstraction which takes care of alot of the things you'd have to do to work on or create arrays. Everything in JS is an object and has an object prototype for their specific type. Number has a prototype object, string has a prototype object and array has a prototype object called Array. Arrays have .length property which defines the length of the array. Because this is a property on an object, it can be mutated. When it is mutated, it "syncs" up with the properties in the array object, which are what we call elements in the array. The .length property is defined by the number of elements + 1. You can see how this is different to a lower level language like C wherein you have to explicitly allocate memory space for an array by defining the length of the array at declaration.
      You can also declare an array in JS similar to how you would in C, but again, the length property is mutable. You can change this behaviour so that .length is not mutable, but yeah. Hope that helped. Go check out JS array and Array prototype on MDN.

  • @goranjosic
    @goranjosic 2 ปีที่แล้ว +2

    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

    • @AndrewTSq
      @AndrewTSq 2 ปีที่แล้ว

      Ive programmes a lot of different languages since 80ies and python is one of those. Why? Wierd syntax slow as hell

  • @whirled_peas
    @whirled_peas ปีที่แล้ว

    That was surprisingly normal behaviour for JS

  • @collab802
    @collab802 2 ปีที่แล้ว +69

    Not weird at all.

  • @davidvernon3119
    @davidvernon3119 ปีที่แล้ว

    It’s almost like the developers of JavaScript didn’t know much about programming

    • @rockfox5
      @rockfox5 ปีที่แล้ว

      98% of webs are powered by Javascript but yeah they don’t know what they are doing.

    • @0x5D
      @0x5D ปีที่แล้ว

      It's almost like Javascript was designed by one guy in only 10 days

    • @davidvernon3119
      @davidvernon3119 ปีที่แล้ว

      @@0x5D both can be true simultaneously

    • @AndrewTSq
      @AndrewTSq ปีที่แล้ว

      I think he is probably a good coder. He is also one if the persons behind the rust language. He co-founded mozilla, he made the brave browser to name a few things. He is also around in the comments on youtube sometimes. Brandon Eich is his name. Lex Fridman did a good interview with him also.

  • @noraddinhassan7590
    @noraddinhassan7590 ปีที่แล้ว

    that is why you should go with typescript

  • @lilyydotdev
    @lilyydotdev 2 ปีที่แล้ว

    That seems like intended behaviour, and pretty sensical behaviour at that

  • @Kelz_codes
    @Kelz_codes ปีที่แล้ว

    Love it when people don't understand something they say it is weird or magical (like Ruby on rails) 😂

  • @haven252
    @haven252 ปีที่แล้ว

    How is it weird though ?

  • @ИванВелинов-у5п
    @ИванВелинов-у5п ปีที่แล้ว

    That's how arrays are supposed to work, not weird at all.

  • @edeath12
    @edeath12 ปีที่แล้ว

    That's certainly quite a behavior

  • @jonaslamprecht9169
    @jonaslamprecht9169 ปีที่แล้ว

    JS is the only language I use, so for me it's how it should work and I would find it weird if it did otherwise.

  • @toopkarcher
    @toopkarcher ปีที่แล้ว

    What's weirder is the amount of people saying this isn't weird

  • @bioblade87
    @bioblade87 ปีที่แล้ว

    this is exactly what a non programmer would intuitively expect.

  • @aljon5947
    @aljon5947 ปีที่แล้ว

    surprise surprise object oriented language has objects instead of actual arrays

  • @mohitpreet9690
    @mohitpreet9690 ปีที่แล้ว

    Wow it doesn’t throw errors amazing

  • @milantornier1553
    @milantornier1553 ปีที่แล้ว +57

    That’s the behavior I would expect tbh.

  • @wlockuz4467
    @wlockuz4467 ปีที่แล้ว +1

    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.

  • @noob_4986
    @noob_4986 ปีที่แล้ว

    Most logical thing in javascript :

  • @braven3754
    @braven3754 ปีที่แล้ว

    should i delete the last item with this arr.length = arr.length -1?

  • @rohinchopra9812
    @rohinchopra9812 2 ปีที่แล้ว

    JavaScript has a lot of weird magic, but this is not one of them.

  • @ares106
    @ares106 2 ปีที่แล้ว

    Weird? It seems like it was beautifully designed to me.

  • @Aziqfajar
    @Aziqfajar 2 ปีที่แล้ว

    'banana' still existing and had accessed the backroom

  • @rahuldange185
    @rahuldange185 ปีที่แล้ว

    It's all object and an object is emulated as arrays so that's why except primitive everything in js is object

  • @fraysa
    @fraysa 2 ปีที่แล้ว +2

    How about using const and not let?

    • @rockfox5
      @rockfox5 ปีที่แล้ว

      Doesn’t matter. Don’t confuse yourself with reassignment and mutation.
      Example: const arr = [1,2,3]
      arr = true;

    • @fraysa
      @fraysa ปีที่แล้ว

      @@rockfox5 I know in this particular video it doesn’t matter. However it’s best practice to use const when possible, like in this video example.

  • @isrealsixojeifo4517
    @isrealsixojeifo4517 ปีที่แล้ว

    I think it actually makes sense

  • @mikeandrewfernandez9797
    @mikeandrewfernandez9797 ปีที่แล้ว

    The fact that it's purely logical makes it weird. JS is the weirdest shit I've been into

    • @herosova
      @herosova ปีที่แล้ว +1

      it mean you arent that much logical irl

  • @itsmefrancois6825
    @itsmefrancois6825 2 ปีที่แล้ว

    What exactly was the weird part now?

  • @shamsiddinavezov5045
    @shamsiddinavezov5045 ปีที่แล้ว

    You can set item to 6th item when your item length is 3. Your array length will be 7 then😊

    • @aschmelyun
      @aschmelyun  ปีที่แล้ว +1

      Isn't this language great?

  • @umairiqbal5172
    @umairiqbal5172 2 ปีที่แล้ว

    Now what’s weird is when you add non-integer key(index) value pairs it doesn’t contribute to the length of the arr.

    • @AndrewTSq
      @AndrewTSq ปีที่แล้ว

      how do you mean? it adds upp for me

    • @umairiqbal5172
      @umairiqbal5172 ปีที่แล้ว

      @@AndrewTSq try const arr = []
      arr['ab'] = 2
      console.log(arr.length)
      it should print 0

    • @AndrewTSq
      @AndrewTSq ปีที่แล้ว

      @@umairiqbal5172 ah, you are not adding to the array. When you write ['ab'] next to an array (edit: the object that is the array that is), its the same as you try to access ab in an object. So you add an object called ab to the array object. but you have not added anything to the array. so that is how it should be. its the same thing as arr.ab = 2

    • @umairiqbal5172
      @umairiqbal5172 ปีที่แล้ว

      @@AndrewTSqSo JavaScript arrays are essentially objects , so when you do something like arr[0] = 1 you are giving it a property of 0 with a value of 1 and since this property is an integer it increments the length property. So when we do ['ab'] or ['0'] or [0] we are doing the same thing that is assigning properties to the object but the difference is that when that property is integer it adds to the length. Since I’m fairly new to JavaScript coming from Java some of these things feel weird like how you can slap properties on an object any time.

    • @AndrewTSq
      @AndrewTSq ปีที่แล้ว

      @@umairiqbal5172 I might missunderstand you, but arr[0] = 1 puts the value 1 in the array arr at index 0. It does not make a new property called 0. In Javascript if you have an array with objects (like arr = [{a:1, b:2},{a:4,b:4}] and you want to get to b:2 in the first array, you can do it like arr[0].b, or you can do it like arr[0]['b'] to access 2 . the last way is great since you then can use a variable to access different named objectkeys. so if i do b = 'b', i can then do arr[0][b] and get the same answer as arr[0].b or arr[0]['b'] . Arrays are like linked lists or hasttable in javascript.

  • @d0cx
    @d0cx 2 ปีที่แล้ว

    I personally can’t see a reason allocating space in an array in Javascript is remotely useful, so I understand why this can be weird. Being able to just set the length down an arbitrary amount is cool though. Definitely not the weirdest function Javascript has.

  • @mrs8683
    @mrs8683 ปีที่แล้ว +1

    I don't think typescript will let you play with this xD

  • @power_bgmi
    @power_bgmi ปีที่แล้ว +15

    So students a array cannot change its size..
    JS : What? I didn't hear you!

  • @jamzykimani
    @jamzykimani ปีที่แล้ว

    Whoever is saying this is logical... Length cuts data??? Also Which item should push function act on 6, 5 or 4?

  • @georgecop9538
    @georgecop9538 ปีที่แล้ว

    Js wasn't strange enough, they need to add this

  • @alexdago5412
    @alexdago5412 ปีที่แล้ว

    Tell me you you have never managed raw memory without telling me you have never managed raw memory

  • @Jiyoon02
    @Jiyoon02 ปีที่แล้ว

    It's not only array length that is weird in JS.

  • @genechristiansomoza4931
    @genechristiansomoza4931 2 ปีที่แล้ว

    The item exist and its value is undefined

  • @amityadav-np1rk
    @amityadav-np1rk ปีที่แล้ว

    More of this please!!

  • @pratiksuthar8055
    @pratiksuthar8055 2 ปีที่แล้ว

    Which theme you are using?

  • @DainSPb
    @DainSPb ปีที่แล้ว

    There are a lot of weird things in our world, son

  • @razor-4eg
    @razor-4eg ปีที่แล้ว

    and how it’s wired? Looks absolutely logical.

  • @scharpmeister
    @scharpmeister ปีที่แล้ว

    so setting the length to 2 actually removed banana?

  • @rafaelmramblas
    @rafaelmramblas ปีที่แล้ว

    If you think arrays as memory positions this behavior is very logic.

  • @johncarrick4639
    @johncarrick4639 ปีที่แล้ว

    Java Scripts seems to be a mix between C++ and Python.

  • @saroj3483
    @saroj3483 ปีที่แล้ว

    That's because everything is object in javascript

  • @giviko1709
    @giviko1709 ปีที่แล้ว

    JavaScript arrays>>

  • @lofiandchill6062
    @lofiandchill6062 ปีที่แล้ว

    People saying its normal or some shit has only coded in JS. It's not normal to be able to do this in any other language.

  • @Hugos68
    @Hugos68 2 ปีที่แล้ว

    Im so confused how length is a mutuable property

  • @victorajayi9056
    @victorajayi9056 2 ปีที่แล้ว

    Still trying to find what's weird about all that

    • @julianmcmillan2867
      @julianmcmillan2867 ปีที่แล้ว

      Lower level languages don't do this. JS is object orientated, thus you can dynamically mutate the size of an array with the .length property which is really just a property on the Array object. In a language like C, you can't do this. You'd have to define a new array with the new size and then copy the original array's values into that array.

  • @ItzShinePlayz
    @ItzShinePlayz ปีที่แล้ว

    oh wait I see something here, js actually write it this way for non-computer person to define.

  • @mirsahib596
    @mirsahib596 ปีที่แล้ว

    In js world everything is an object even undefined and that's why I hated it

  • @punkerts7711
    @punkerts7711 ปีที่แล้ว

    You should define arrays with const. That's actually more weird.

  • @tyres9
    @tyres9 2 ปีที่แล้ว

    arr[2] = undefined? Index is start at 0 while length starts at 1 in counting. Im not sure.

    • @julianmcmillan2867
      @julianmcmillan2867 ปีที่แล้ว +1

      Yes, that's correct because setting the length to 2 removed the third item which would be index 2. Thus if you try to acces index 2, it will return undefined because the third value, at index 2 doesn't exist anymore.

  • @trackpadpro
    @trackpadpro 2 ปีที่แล้ว

    peekaboo is weird. when i move my hands from my face, my face is visible again.

  • @ashrude1071
    @ashrude1071 2 ปีที่แล้ว

    This is common in compiled languages

  • @evolvingtrident
    @evolvingtrident ปีที่แล้ว

    What if you make th le default length of an array to 5?

  • @codemaniadev
    @codemaniadev ปีที่แล้ว

    That's how dynamic arrays work bro 🤌

  • @oscar7557
    @oscar7557 ปีที่แล้ว

    Should have demonstrated by it a bit larger so people can see where this can go terribly wrong lol. The comments denote a naïveté around why this is weird. That scares me for the future of web development lol.

    • @ronaldgipa8731
      @ronaldgipa8731 ปีที่แล้ว

      You just need to learn how to use the tool before using it to avoid common mistakes

  • @maximillianusforrest
    @maximillianusforrest 2 ปีที่แล้ว

    Length should be designed as a function method, so it can't be modified .

    • @AndrewTSq
      @AndrewTSq ปีที่แล้ว

      how do you handle a situation where you need more places in your array than you allocated then? Just let the software crash? JS makes this which is great.

    • @michawielgus9827
      @michawielgus9827 ปีที่แล้ว

      @@AndrewTSq provide a method allowing you to add additional elements? Like .append in python or .add in java

    • @AndrewTSq
      @AndrewTSq ปีที่แล้ว

      @@michawielgus9827 and.. what do these languages do when you call those functions? It allocates memory and sets the pointer to new location. Just like JS does. An array in js can be a fast array or slow array. The slow array is more of a linked list. If people understood this then this .length makes sense

    • @michawielgus9827
      @michawielgus9827 ปีที่แล้ว

      @@AndrewTSq than make it implicit and not some whacky shenanigans. No other language does it this way

    • @AndrewTSq
      @AndrewTSq ปีที่แล้ว

      @@michawielgus9827 I have never seen anyone adjust the array by writing to the.length property so its a no problem. In modern javascript you dont need to use array.length. only people watching tutorials from 2015 does that

  • @sremagamers
    @sremagamers ปีที่แล้ว

    This smells like a leaky abstraction.

  • @doltramir
    @doltramir 2 ปีที่แล้ว

    Also, you were getting length property, not calling. That is a two very different things.

  • @moxxy3565
    @moxxy3565 2 ปีที่แล้ว

    How is this weird? I'm not even that good at programming and this is how I would expect it to work...