Iterable vs Enumerable in JavaScript

Поділитися
Вставка
  • Опубліковано 26 вер 2024
  • Iterable and Enumerable are different things in JavaScript.
    When you add properties to an object they can be defined as enumerable or not.
    Some object types like Arrays, Strings, Maps, Sets, and NodeLists also have Iterators which are objects that let you access certain values from the object.
    This video discusses the difference between the two and how the for...in and for...of loops take advantage of the difference.

КОМЕНТАРІ • 67

  • @RogerontheKeys
    @RogerontheKeys Рік тому +2

    Thank you! I kept running into these terms while studying objects, and no one seemed to really explain it.

  • @rotrose7531
    @rotrose7531 4 роки тому +6

    These fine details can not be found elsewhere except in your channel. Your tutorials give me great power to face my real world problems. Thank you so much!

  • @ctfrancia
    @ctfrancia 6 років тому +8

    I really really like this! I mean it's still something that I will need to rewatch but this is by far the best and clearly articulated explanation I've found thus far!

  • @sachinjaveri1
    @sachinjaveri1 2 роки тому +1

    Why are you not famous yet.. ? Best Prof !

  • @tseponkalai6528
    @tseponkalai6528 5 років тому +2

    Thank you so much, you just helped me understand in less than 10 minutes whtat took me the last 2 hours NOT to understand. Video liked and subscribed

  • @koki1721
    @koki1721 2 роки тому

    easy and clear english and expalination. as a second language user, can understand everything thanks!

  • @amansaxena4446
    @amansaxena4446 Рік тому +1

    Gold mine content available for free. Thanks you so much

  • @emilewamsteker3412
    @emilewamsteker3412 5 років тому +3

    This is an excellent explanation. Thanks.

  • @MohammedAli-p7e9d
    @MohammedAli-p7e9d 20 днів тому

    Thanks alot Sir!
    It's the first video I see on this channel which make me interested in looking at other videos.
    My understanding is:
    Enumerable is a propery of objects elements allowing to enumerate over using for...in.
    Iterable is not completely clear yet, but my initial understanding is
    It is an object that tells how we can iterate over an object. (I will check your channel or any other resources to clear it for me).

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  14 днів тому +1

      For iterable, think Array. A numbered list. It is also possible to create your own custom objects and give them an Iterator, which will control how you loop through specific values in the object.

    • @MohammedAli-p7e9d
      @MohammedAli-p7e9d 14 днів тому

      @@SteveGriffith-Prof3ssorSt3v3 Sir, I really appreciate your efforts.
      You are one of a kind.
      🤍🤍

  • @ShreyasKumar
    @ShreyasKumar 3 роки тому

    Such an confusing concept got cleared after watching your video.. thank you..

  • @ramusesandasari8451
    @ramusesandasari8451 3 роки тому

    Wow great explained 🙂

  • @bridgetfitzgerald7656
    @bridgetfitzgerald7656 6 місяців тому

    Thank you for this video 🙌

  • @aparnaiyer5873
    @aparnaiyer5873 3 роки тому

    Thank you , this was extremely concise and helpful!

  • @ataddata
    @ataddata 4 роки тому

    Great Video, very insightful and clear

  • @devenpatil6
    @devenpatil6 5 років тому

    Awesome...Really nice and clear explanation

  • @shirodeepdahal3772
    @shirodeepdahal3772 3 роки тому

    It helped to understand something more than my guru

  • @codea8901
    @codea8901 2 роки тому

    So glad I came across your channel Steve, only regret is that I didn't find it few years ago! Is there a way users can make a donation to your channel?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 роки тому +1

      Thanks. Yes, there is the Super Thanks - look below the video for the heart icon with the dollar sign inside it.

    • @codea8901
      @codea8901 2 роки тому

      @@SteveGriffith-Prof3ssorSt3v3 Hmm, I cannot see this option, only like, dislike, share, save and three dots at the end.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 роки тому

      @@codea8901 When you click on the three dots what additional options are you shown?

    • @codea8901
      @codea8901 2 роки тому

      @@SteveGriffith-Prof3ssorSt3v3 Report and Show Transcript links. I looked up on UA-cam help centre but couldn't find any helpful information to work out what's going on.

  • @elnurshabanov1608
    @elnurshabanov1608 4 роки тому

    Great and brief explanation!

  • @chesterxp508
    @chesterxp508 3 роки тому

    Another very cool tutorial!

  • @zakariaelaissaoui9990
    @zakariaelaissaoui9990 4 роки тому

    Brilliant explanation! Thanks

  • @codea8901
    @codea8901 2 роки тому

    Hi Steve, My understanding of iteration is the ability to loop over. So since we can iterate an Object using for...in, how come Objects are classed as not iterable by default? Just struggling to get my head around that bit. Many thanks.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 роки тому +3

      Iterable means specifically that there is an order to looping over the object. The items are numbered. With a standard object the properties do not have a specific order.
      The for...in loop relies on the enumerable property descriptor.

    • @codea8901
      @codea8901 2 роки тому +1

      @@SteveGriffith-Prof3ssorSt3v3 Thanks Steve, it makes sense now.

  • @eugene1207
    @eugene1207 4 роки тому

    Thanks! Awesome explanation!

  • @saumyagaur3500
    @saumyagaur3500 Рік тому

    Where does one learn all this? I would love to learn all these in depth concepts by myself. Can you please refer me some resources where you learned all this from.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  Рік тому +1

      There's not one or even just a few resources where I have learned all the things that I have learned about JavaScript over the last 25 years. That's one of the reasons that I make these videos - to try and bring these topics together in one place as much as I can.

  • @santhoseaghilsanthose3317
    @santhoseaghilsanthose3317 5 років тому

    Awesome!! clear explanation

  • @lavdixit2267
    @lavdixit2267 4 роки тому

    Thanks , For Good Explanation

  • @inetmiguel
    @inetmiguel 4 роки тому

    Very clear, thanks so much!

  • @darkfrozen1860
    @darkfrozen1860 2 роки тому +1

    Practically it was really well defined but the main question still remains what is enumerable?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 роки тому

      An object can have properties that are enumerable or not. With a for...in loop you step through the properties that are enumerable only.
      An object that is iterable has an iterator which controls which properties can be seen and in what order, when you use a for..of loop.

    • @MohammedAli-p7e9d
      @MohammedAli-p7e9d 20 днів тому

      What I understood is arrays and objects keys are enumerable because their elements enumerable attribute is true by default.
      So enumerable is an attribute of elements of objects that can be enumerated over using for...in.
      That's my understanding.

  • @blank-vw2sb
    @blank-vw2sb 4 роки тому

    Thanks. You are awesome

  • @ordaniel7699
    @ordaniel7699 2 роки тому

    I am confused. You compare in the title of the video between two things that aren't the opposite, because 'iterables' describe (objects!!) that are iterable, while 'enumarables' describe the (properties!!) of an object.
    My unresolved questions:
    1. Are the properties or the values called 'iterables' as well? Because you once said this and once that.
    2. I think the topic of the video should have been for...in loops VS for...of loops, and not iterables vs enumarables.
    3. If I want to sum up this video, would it be right to say:
    Iterables describe objects that are iterable. Enumarables describe the properties of an object, therefore they're called 'property descriptors'.
    **for ... in loop**, loops over the properties that are enumerable. In other words, we can access them and write them out.
    **for ... of loop**, loops of the values there are returned by an iterator object built in the object we're iterating. Arrays, maps, sets, strings all have built-in iterators.
    When you add properties to an object they can be defined as enumerable or not.
    Iterable object means that the object has a specialized function called an Iterator which decides which properties and in which order are available from the object.

  • @nigelbrie5693
    @nigelbrie5693 4 роки тому

    So in summary, iterable object will have access to only values and enumerable will only show properties?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 роки тому +3

      Nope. iterable means that the object (whatever it is) has a specialized function called an Iterator which decides which properties and in which order are available from the object.
      Enumerable is a property descriptor on every property of every object. The for in loop uses this to loop through the available properties.
      Remember in JS that Arrays are Objects too.

  • @Sharukkhan-kx9rw
    @Sharukkhan-kx9rw 4 роки тому

    Your Videos Are Legendary Man!!!!!!!!
    Can you help Me?
    Use Plotly for Android Apps is permitted?
    I wanna use plotly for my analysis in my android app and publish in Google Play Store and IOS AppStore, Is that Acceptable to use plotly inside my Android App as well as IOS App, Or Is there any subscription to use Plotly in Android App,I didn't get into my mind by reading the license, Please answer in layman terms and answer will be very useful for me.
    Thank you in advance!

  • @leannec6821
    @leannec6821 5 років тому

    Great tutorial, thanks a lot!

  • @ateshrampersaud2664
    @ateshrampersaud2664 4 роки тому

    So, iterable refers to the properties (i.e. index), while enumerable refers to the values assigned to those properties?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 роки тому

      Not quite. Both are properties that describe how and when you can loop over the properties in an Object. They are different ways of controlling the ability to loop in different ways. You could have an Object whose properties are both iterable and enumerable and another object whose properties are neither.

  • @virajjay
    @virajjay 3 роки тому

    Many thanks for making these awesome videos, Steve - much appreciated!
    Just wondered what's the difference between using 'Object.defineProperty' vs 'names.elf' as per this example, please? What you've done using 'Object.defineProperty' can also be achieved by writing 'names.ent = {value: 'Treebeard', enumerable: false}, is that correct?
    Thanks in advance.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 роки тому

      You need to use defineProperty or defineProperties to set the values for the property descriptors. Your example would not work, it would just set names.ent's value as an object that contains two properties - value and enumerable. They would not be seen as descriptors.

    • @virajjay
      @virajjay 3 роки тому +1

      @@SteveGriffith-Prof3ssorSt3v3 Many thanks for the explanation.

  • @zachdelorenzo1903
    @zachdelorenzo1903 4 роки тому

    What are some situations in which you'd want to purposely make a property non-enumerable?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 роки тому

      When you don't want the property to appear in a loop. Maybe you have internal properties that maintain state or measure the other properties. Some properties are the data and others are for your reference when working with the object. You might want to loop and display all the data but not a property that refers to the total number of properties.

  • @kalishamalama5805
    @kalishamalama5805 5 років тому

    pheew...! I finally got it well explained thankx....

  • @AshishGupta-wk4ts
    @AshishGupta-wk4ts Рік тому

    Thanks !

  • @1polyron1
    @1polyron1 5 років тому

    You are a king.

  • @treea5063
    @treea5063 4 роки тому

    great

  • @fs17792
    @fs17792 2 роки тому

    Danke!

  • @lycan2494
    @lycan2494 5 років тому

    ty

  • @rafaelanataly2192
    @rafaelanataly2192 4 роки тому

    I'm sorry but I'm a really really dumb one. So there's no way to, let's say, get middleEarth[0], right? Or to iterate with a regular for loop?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 роки тому +1

      middleEarth is an object, so we can't use numbers to get to the properties, we have to use the property names.
      You can iterate through an array with a for loop. However, you can't use a for loop with an Object, again because it doesn't use numbers to access the values.

    • @rafaelanataly2192
      @rafaelanataly2192 4 роки тому +1

      @@SteveGriffith-Prof3ssorSt3v3 Thank you!

  • @SimonSirumba
    @SimonSirumba Рік тому +1

    didnt understand a thing

  • @DmitriyMalayevProfile
    @DmitriyMalayevProfile 4 місяці тому

    gist?

  • @HassanMalikTW
    @HassanMalikTW 3 роки тому

    The extremely gooey mouth of yours is in the microphone. keep that out please!!

  • @DmitriyMalayevProfile
    @DmitriyMalayevProfile 4 місяці тому

    // for of vs. for-in loops
    // Iterable vs. Enumerable
    //for in is looking for all of the enumerable properties (not the values) of the object and loops through them
    let names = ["Gandalf", "Bilbo", "Aragorn", "Gimli"]
    names.elf = "Legolas"
    Object.defineProperty(
    names, "ent", { value: "Treebeard", enumerable: true }
    )
    let middleEarth = {
    "towns": ["Hobbiton", "Rivendell"],
    "races": ["Elves", "Hobbits", "Men"]
    }
    middleEarth.creator = "JRR Tolkien"
    Object.defineProperty(middleEarth, "age", {value: "3rd", enumerable: false})
    for (let p in names){
    console.log("in names: ", p)
    }
    for (let p in middleEarth){
    console.log("in middleEarth", p)
    }
    //Iterators
    //The for of loop is designed to use the iterators. An iterator is another object attached to the array that tells some other function how to access all the different values inside it.
    //Arrays, Strings, Sets, Maps, and NodeLists have built-in Iterators. Object do not (must be custom made)
    for (let p of names){
    console.log("in names: ", p)
    }
    for (let p of middleEarth.towns){
    console.log("in middleEarth", p)
    }

  • @hamzaakbar5321
    @hamzaakbar5321 5 років тому

    best thx