JavaScript forEach() method in 8 minutes! ➿

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

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

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

    // forEach() = method used to iterate over the elements
    // of an array and apply a specified function (callback)
    // to each element
    // array.forEach(callback)
    // element, index, array are provided
    // -------------- EXAMPLE 1 --------------
    const numbers = [1, 2, 3, 4, 5];
    numbers.forEach(cube);
    numbers.forEach(display);
    function double(element, index, array){
    array[index] = element * 2;
    }
    function triple(element, index, array){
    array[index] = element * 3;
    }
    function square(element, index, array){
    array[index] = Math.pow(element, 2);
    }
    function cube(element, index, array){
    array[index] = Math.pow(element, 3);
    }
    function display(element){
    console.log(element);
    }
    // -------------- EXAMPLE 2 --------------
    let fruits = ["apple", "orange", "banana", "coconut"];
    fruits.forEach(capitalize);
    fruits.forEach(display);
    function upperCase(element, index, array){
    array[index] = element.toUpperCase();
    }
    function lowercase(element, index, array){
    array[index] = element.toLowerCase();
    }
    function capitalize(element, index, array){
    array[index] = element.charAt(0).toUpperCase() + element.slice(1);
    }
    /*
    function display(element){
    console.log(element);
    }
    */

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

      so these videos are made 1 month back and set on auto upload at specific time . nice of 🫵

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

      index and array are built in functions to provide the index values and array collection of fruits and numbers..?

    • @Nagira-j2f
      @Nagira-j2f 8 หลายเดือนก่อน

      array[index] can also write “return” ??

  • @piotrmazgaj
    @piotrmazgaj 4 หลายเดือนก่อน +9

    This is my seal. I have watched the entire video, understood it, and I can explain it in my own words, thus I have gained knowledge. This is my seal.

    • @something7419
      @something7419 2 วันที่ผ่านมา

      your seal is cute

  • @vctorroferz
    @vctorroferz 10 หลายเดือนก่อน +5

    Hey it is just incredible how you can just explain these concepts in just 8 minutes! thanks so much for makeing these videos ! :)

  • @mjylove2
    @mjylove2 9 หลายเดือนก่อน +2

    amazing explanation ! understood!

  • @TheProgrammingSensei
    @TheProgrammingSensei 9 หลายเดือนก่อน +3

    forEach also has thisArg option.

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

    very concise, thanks bro

  • @عبدالرحمنالشناوي-ص7ط
    @عبدالرحمنالشناوي-ص7ط 2 หลายเดือนก่อน +2

    ok i got confused for about an hour here about why we use index array so i will leave this comment here if anyone have the same problem.
    foa we can write the above code and get the same result as following
    >>>
    function double (element) {
    element = element*2;
    console.log(element)
    }
    >>>
    the main difference here is this method will not change the content of the original array so i will use it if i want to keep the original array untouched.
    second
    small note that caused me so much headache... the order of the arguments matter.
    >>>
    //the right way to write it
    function whatever (element , index , array);
    //wrong way to write it
    function whatever (index , array , element)
    >>>
    hope this helps anyone or i am just super dumb idk xd.

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

    you can actually just make function inside if you're not aware of that, but this helps to use what you learned on callback ✈✈✈✈

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

    Great video 👍 thank you 😊

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

    bro, i don't get what you mean when you say the element, index and array are already provided for? please help me understand what you mean by it. and thanks for making such cool tutorials

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

      i suggest u do it on paper.

    • @wintalyrics1226
      @wintalyrics1226 10 หลายเดือนก่อน +5

      Array is an object, So it has properties and methods. So when we create an array variable it will have the properties and methods.
      Like it automatically counts the number of elements and assign it to the element property.

    • @natnaeltaye
      @natnaeltaye 9 หลายเดือนก่อน

      @@wintalyrics1226 hey yoo i like your explanation .
      i think your from ethiopia, i'm either so we can help each other out in this coding journey so if you down leave me your email or phone number.

    • @natnaeltaye
      @natnaeltaye 9 หลายเดือนก่อน

      @@wintalyrics1226 hey yoo i like your explanation.
      i think your from ethiopia, i'm either so we can help each other out in this coding journey if you down leave me your email or your phone number.

  • @MotoPortalTR
    @MotoPortalTR 19 วันที่ผ่านมา

    Tenku tenku ❤❤turkey izmir

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

    thank!! love that!

  • @Suraj-zg5qc
    @Suraj-zg5qc 3 หลายเดือนก่อน +1

    When I write .forEach(double) after the calling function ". forEach(display)" it shows origanl array of 1,2,3,4. Can you explain why sequence matters here. Thanks.

    • @saiprasanth614
      @saiprasanth614 3 หลายเดือนก่อน

      make sure that you declared the function separately before implementing it in the function call

    • @Priyansh-n9z
      @Priyansh-n9z หลายเดือนก่อน

      Define double first

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

    Love you bro ❤❤

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

    thanks bro :)

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

    ❤❤❤

  • @Stock632
    @Stock632 8 หลายเดือนก่อน +1

    Supereb