JavaScript Tutorial For Beginners #17 - For Loops

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ส.ค. 2015
  • An easier way to iterate through a loop is via the for loop, which we'll talk about in this JavaScript tutorial. A for loop does exactly the same thing as a while loop, but it has the advantage of organizing it's components more locally and neater.
    As always, if you have any questions, fire ahead with them below :).
    SUBSCRIBE TO CHANNEL - / @netninja
    ========== JavaScript for Beginners Playlist ==========
    • JavaScript Tutorials f...
    ========== CSS for Beginners Playlist ==========
    • CSS Tutorials For Begi...
    ========== HTML for Beginners Playlist ==========
    • HTML Tutorials For Beg...
    ========== The Net Ninja ============
    For more front-end development tutorials & to black-belt your coding skills, head over to - / @netninja or thenetninja.co.uk
    ========== Social Links ==========
    Twitter - @TheNetNinja - / thenetninjauk

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

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

    I found that thinking of them like this helps: A 'for loop' states a specific amount... for this many times; a 'while loop... while this is happening... not a specific amount'

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

    These vids really suit my ( early stages ) learning style. No queries yet but so good to be able to go over these points without taking up too much time.

  • @Kevin-te7rr
    @Kevin-te7rr 4 ปีที่แล้ว +9

    You explain so well

  • @raphaelboateng-amponsah1363
    @raphaelboateng-amponsah1363 7 ปีที่แล้ว +8

    Thank you so much for this I really appreciate the time and effort you put into these vids.

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

    your videos are so easy to understand. Can you please do an example of how to create the game hangman using JS? Or other games?

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

    hi there, great videos, really learning alot from your playlist of this series. Just a quick question, you dont seem to be using var to declare the new variables within the for loops, the code still seems to work, so just wanted to confirm, is var not required within the for loop or is it good practice to use it. e.g. you did:
    - for (age = 5; age < 10; age++) however should i be doing:
    - for (var age = 5; age < 10; age++) instead.
    Thanks again.

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

    Can you add another video showing different problems utilizing the for loop?

  • @mikhailbisserov8017
    @mikhailbisserov8017 7 ปีที่แล้ว +26

    You do need 'var' keyword for 'for' loop variable, otherwise you create a global variable which remains while page lives. Let alone cases when you recursively call a function containing 'for' loop. And if you use ECMA6 then 'let' variable is even better for 'for' loop.

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

      what is ECMA6 ?

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

      @@AniSepherd972 Some kinda coding secret organization

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

      @@AniSepherd972 what is Google?

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

      very good point!

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

    The string "this is link number" is misleading isn't it? The code doesn't really display the link. The for loop only use the length of the links variable to display it 6 times. It has nothing to do with the content inside the tags. You writing the code that way, as a beginner I was thinking the script get those elements and stores in a variable, then display them in console.

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

    Very helpful - thanks!!

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

    Great tutorials lads. Nothing better than showing 'actual examples' like looping through links, then just going over the fundamentals.

    • @DanielGutierrez-pd3py
      @DanielGutierrez-pd3py 6 ปีที่แล้ว

      Yes! I can understand these concepts so much better if I'm shown a practical use of this stuff.

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

    dude good work! for your links example there is a possibility that the JS executes before the HTML completes loading so you might want to handle that

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

    I have a question that has been bugging me for some time now. If I wanted to use the variable i inside the loop, to let's say push an 'input to the alert' into an array of objects, how would I go about that?

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

    You speak a bit too fast but at the same time it is the best explanation ever
    Thank you!

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

    What would be a typical usage case where a "for" loop would be preferable to using a "while" loop, other than to reduce clutter? Thanks again for the great content!

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

      You can loop over an array for example. It's very useful when interacting with the DOM too.

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

    everything's better explained in british accent

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

    love the thumbnail!!! XD very clever

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

      Haha thanks 😀

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

    Thanks sirrr...amazing explanation.. 👏

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

      Most welcome 😊

  • @lonw.7016
    @lonw.7016 6 ปีที่แล้ว

    I gave each element an id assignment because the console read an error for getElementByTagname The console logged 5 links and printed out.

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

    When I put '"links.length" nothing shows up on my screen, but the moment I change it into a number; the loops show up. lol?

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

    Thanks!

  • @GameNon-Quitters
    @GameNon-Quitters 6 ปีที่แล้ว

    I need help. Do you know how to turn off errors that are showing in brackets (probably new version) in js files on some codes which are absolutely correct. I just done the same code that is in the videos but those red circle errors keep showing on some codes.

    • @GameNon-Quitters
      @GameNon-Quitters 6 ปีที่แล้ว +1

      I did it xD needed to disable Lint Files on Save.

  • @ammarobeid4963
    @ammarobeid4963 10 หลายเดือนก่อน

    I started the while loop without age++, my PC is working well but the debugging has been crashed. when I started the debugging I get at the deprecated debugger for chrome, and i am no longer can start debugging in VS. Is there any solution for my problem?

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

    yeah

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

    thank you for all the info man... btw what type of text editor is that? look amazing!

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

      He's using Brackets

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

    I am using brackets and typed in the code, verbatim, with the following errors JSLint (6)3'i' was used before it was defined.for (i = 1; i < links.length; i++) {3Unexpected '++'.for (i = 1; i < links.length; i++) {3Unexpected '(space)'.for (i = 1; i < links.length; i++) {5'console' was used before it was defined.console.log("this is link number" + i);9Expected 'document' at column 1, not column 3.document.write("all liks are looped");Help?

  • @ALI-tg5pb
    @ALI-tg5pb 2 ปีที่แล้ว

    🌺🌺

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

    Is it not true to say .length is a property and not a method?

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

    error are appearing please help!

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

    for looks soo much cleaner but while seems easier to edit.

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

    Had me slightly confused as I thought the array would be zero based, which it is, but you don't use the array elements and I guess the zero-based array topic is yet to come up...

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

    Why we right '+ i' in consol.log?
    Couldn't we just say 'i'?

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

      Prashant Panwar You're merely trying to concatenate the text (string) with the link number, the way to do that is using '+'

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

      @@futurez12 Thanks for answering !

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

    I just wish I could figure out WHEN/HOW OFTEN these loops take place..

  • @harrywotnot3377
    @harrywotnot3377 8 ปีที่แล้ว

    When i attempted this tutorial "Your age is less than 10 " was printed out to the screen 5 times and then "You are now over 10"

    • @willb.755
      @willb.755 8 ปีที่แล้ว

      +harry whatnot You might have made the "console.log" "document.write"

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

    i still cant understand how .length pulled the amout of tags and not the number of characters

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

      it's an object, not a string.
      The short answer is: that's just how it works. But I feel the same way as you do, so you might think of it as a list of items. length in this case returns the length of the LIST.
      If you have a list of people, you'd want to know how many people are on the list, not what the length of their names are.

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

    My point is, I think it confuses beginners that you put "this is link number" while in reality it is not. It just loop no1 , 2 ,3 and so on.

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

    when a C programmer see this XD::
    for(;;){console.log("I hate web programming !");}

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

    It didn't work for me, And I don't know why.

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

      Nevermind, I was running the .js before the links

  • @AleksaCar-bk1rx
    @AleksaCar-bk1rx 3 ปีที่แล้ว

    2nd part of the video... can understand :-/

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

    Ivan brought me here.

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

      Ivan Brought his mum to the xmas party too last year, Nice guy

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

    Ivan brought me here

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

    it's very short explain it's not good for professional

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

      guess you missed the "for beginners" bit? I mean, I can see how you might. It's hidden way back in the title...

  • @user-fn1pn4nk4c
    @user-fn1pn4nk4c 8 หลายเดือนก่อน

    I didn't want to involve myself but it's been good when I came closer

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

    Ivan brought me here.