for in / for of - Beau teaches JavaScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 มี.ค. 2017
  • For... in and for... of loops allow you to loop through property names and values in JavaScript.
    🔗 Code: codepen.io/beaucarnes/pen/oBKW...
    🐦 Beau Carnes on Twitter: / carnesbeau
    ⭐JavaScript Playlists⭐
    ▶JavaScript Basics: • JavaScript Basics Course
    ▶Data Structures and Algorithms: • Data Structures and Al...
    ▶Design Patterns: • Design Patterns - Beau...
    ▶ES6: • ES6 - Beau teaches Jav...
    ▶Clean Code: • Clean Code - Beau teac...
    -
    We're busy people who learn to code, then practice by building projects for nonprofits. Learn Full-stack JavaScript, build a portfolio, and get great references with our open source community.
    Join our community at freecodecamp.com
    Read great tech articles at medium.freecodecamp.com

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

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

    MAAN! You helped me so much! in the first 10s you just explained a stuff I spent a few hours to know and couldn't. You're amazing!

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

    Just remember!
    1. for (every prop in objects) { // do this } **objects - enumerable**
    2. for (every item of array) { // do this } **arrays - iterable***

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

      bruuuh this makes it so much easier to understand tyty

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

      bruh i watch the video im getting more confused and after reading this comment i starting to understand :v

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

      another helpful way to think about for...in when dealing with arrays:
      for...in looping will return the INdex of an array
      for...of looping will return the value OF an array

  • @climbers1376
    @climbers1376 5 ปีที่แล้ว +5

    "for in" loops through innumerable properties, property "names" 0:05:
    --1:58-2:16, object defined properties
    --3:12, array numeric indexes & object defined properties
    "for of" loops through iterable objects (i.e. array, map, set), property "values" 0:08:
    --4:01-4:18, values in arrays

    • @5tivi
      @5tivi 2 หลายเดือนก่อน

      very well put summary, but is it "innumerable" or "enumerable"? Those two words are antonyms

  • @khaledkharaz8037
    @khaledkharaz8037 7 ปีที่แล้ว +39

    I was trying to understand this video i watched it many time but no point Then I decided to take break 30 min and come back to concentrate in last 10 video in this tuturiol and come back to this video just in one minute i find it very simple and clear new lesson today to take break after study couple of hour then come back will find everything easy, I love your video really very clear and important for everyone want to be perofessinal in JS Thank you bro

    • @BeauCarnes
      @BeauCarnes 7 ปีที่แล้ว +8

      Same thing happens to me sometimes. Breaks can be really helpful!

  • @studentcoder5840
    @studentcoder5840 5 ปีที่แล้ว +11

    Wow, this is quite good! Though I think that instead of "x" the first variable name should've been "propertyName" -- just to make it more explicit and thus clear what's happening. And then maybe go over the code with "x" afterwards just to show that it really doesn't matter what that first variable is called.
    Slightly more problematically, in the beginning, stating that "'for...in' will loop through property names and 'for...of' will loop through property values" can be confusing at first when compared to the later statement (beginning at around 2:15) that the former works on what is enumerable while the latter what is iterable. In that vein, finally, I also thought that the later contrast between the two was good but felt just a bit rushed somehow.
    Still, overall a nice video for us beginners; thanks!

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

    Clearly and beautifully explained.
    Thanks a lot!

  • @__jake.m
    @__jake.m 2 ปีที่แล้ว

    Thank you for this! You've clearly described the difference between the two! 💪🏽

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

    This helped clear the difference between the 2 loops, thanks a lot

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

    Thanks for clearing my confusion between them :)

  • @ousmand742
    @ousmand742 4 ปีที่แล้ว

    is it possible to iterate through the values of the object with the for...in -- not using object.values ()

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

    Thank you! had to watch it few times but you taught it VERY well. Kudos!

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

    your videos are helping me with the "WHY?" . thank you for these. :):)

  • @90AlmostFamous
    @90AlmostFamous 6 ปีที่แล้ว +4

    just remember by this:
    for (let index in Arr) {
    console.log(index + Arr[index] ); //gives fname beau
    }
    for (let item of Arr) {
    console.log(item); //gives 3,5,7
    }

  • @LeninNCM
    @LeninNCM 5 ปีที่แล้ว

    Thank you mate, best as always

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

    This makes a lot of sense. I see how I need to restructure the semantics of my knockoff JavaScript interpreter.

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

    thanks but i have a question, in the end when you console log everything, why the for (of) loop didn't print the foo value ??

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

      Because the foo property is created as a key because arrays are actuallyobjects in js,...
      But the keys for arrays are indices therefore the value of the key foo cannot be added to the array....
      Note sure if I'm making sense to you..

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

      @@Shad1991 thank u! it really helps

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

    ty for explanation clear and to the point.

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

    one way of remembering : 'i' n --> ob 'j' ect [look visually similar]
    so the other must be : off --> array
    OR "I want to get to know you _in person_ " if you know that person is normally best as an object ?

    • @Qornv
      @Qornv 4 ปีที่แล้ว

      yeah, very "intuitive" way

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

    Thank you for this

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

    I’m stuck on fitting a method inside a function. 😖 it sounds so simple!
    I can get it to run without being in a function (.split()) but the assignment is to create a function that splits a string of first + last name into an array of the split 2 strings.
    😫 this seems like it should be simple

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

    Very clear explanation

  • @ll-sz9fl
    @ll-sz9fl 5 ปีที่แล้ว +2

    Thank God you have two arms Beau!

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

    yeah i did not understand shoit. Mainly cuz i have more of a problem understanding syntax. It's so helpful when code is turned into english text, then I get it mad quickk

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

    is it your actual typing speed? or editing?

    • @Qornv
      @Qornv 4 ปีที่แล้ว

      editing

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

      No, its fast forward, haven't you noticed the cursor blink?

  • @lyloft
    @lyloft 7 ปีที่แล้ว

    why the `for of` can't output the `foo` value of the arr? It's confusing. Do you mind to explain that a little bit explicit? Many thanks.

    • @cheeseburger1884
      @cheeseburger1884 7 ปีที่แล้ว +5

      'for of' only sees the iterable ""parts"" of an object, in this case [3,5,7] (an array, which is iterable).
      'for of' will ignore key:value pairs because they are not iterable.

    • @lyloft
      @lyloft 7 ปีที่แล้ว

      Thanks, btw how can I know the pair is iterabal or not

    • @cheeseburger1884
      @cheeseburger1884 7 ปีที่แล้ว

      you should google "javascript iterables". It gets pretty complex, I don't know it so I can't explain.

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

    Need to watch another video on "for..in" loops on the objects..

  • @ApBananen
    @ApBananen 6 ปีที่แล้ว

    perfect video :)

  • @kausarpial8062
    @kausarpial8062 5 ปีที่แล้ว

    good explanation 👍

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

    Thank you

  • @hoanglam4964
    @hoanglam4964 5 ปีที่แล้ว

    Please give me the application you used to write this

  • @arvee-4699
    @arvee-4699 7 ปีที่แล้ว +3

    feels like javaScript god made these videos :D

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

    Your explanations are awesome! Just for the records...you are the usain bolt of typing

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

    tnx duuuude🙂

  • @jackm1115
    @jackm1115 4 หลายเดือนก่อน

    Why use For Of loop when you could just use a traditional loop to get the job done? I'm guessing it costs less memory, but with this explanation For Of and traditional loops seems identical. Cheers :)

  • @taihatranduc8613
    @taihatranduc8613 3 ปีที่แล้ว

    you are so good

  • @lycan2494
    @lycan2494 5 ปีที่แล้ว

    underrated

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

    Is he using code pen?

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

    Thanks Tom Green

  • @RAYDENfilipp
    @RAYDENfilipp 7 ปีที่แล้ว

    What is "let"??

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

      Check out this video for an explanation of let: th-cam.com/video/1mgLWu69ijU/w-d-xo.html

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

      Great explanation. If I got it correct, in this case we can access "person" and "arr" let-type variables anywhere in the code, because they were declared in general scope? Am I right?

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

      Yeah, that is right.

    • @lycan2494
      @lycan2494 5 ปีที่แล้ว

      new version of var

  • @rockNbrain
    @rockNbrain 3 ปีที่แล้ว

    tks

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

    Wonder if I can use this code for evil,,, hmmm

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

    To those who maybe are still confused I'm going to help you out and I'm going to be very specific with each individual word because they all mean something very important.
    ( let x in person ) - Also known as - ( Let X go into the variable person )
    1. (let) - tells x what to do
    2. (x)- is what will be going into something
    3. (In) - tells x to attach to the key properties inside the object
    4. (person) - is the destination for x

  • @user-um5px2li8g
    @user-um5px2li8g 6 ปีที่แล้ว +1

    Dude you're best, and bearded, HAHAHA

  • @mariachaudhry4608
    @mariachaudhry4608 4 ปีที่แล้ว

    strange i have two arms too :o

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

    kim bor

  • @sabertoothwallaby2937
    @sabertoothwallaby2937 3 ปีที่แล้ว

    Typical showing of your 2 arms