JavaScript Tutorial For Beginners #20 - Functions

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024
  • Hey class, so in this JavaScript tutorial I want to introduce you to the world of functions. Yep, I think you're ready for them :). Functions allow you to chunk parts of code together into logical sections, and call that code at any time. For example, you may have a function to cycle through all of your images on a web page, and implement a zoom functionality on the. You'd probably call this function something like imageZoom.
    So, functions are a really cool and integral part of JavaScript, and something you should try and get into the habit of using early on in your JavaScript development, so that your code is always readable and tidy for yourself and other developers.
    At first, functions in JavaScript can be a tricky concept, so by all means fire away with any questions you have down 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

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

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

    Bro at 33 I start my career in web dev .with ur lessons it seems easy .thanks for these pricey tutorial s
    For free .love the way u explain.😍😍😍😍😍😍😍😍love from INDIA

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

    You're very good at explaining. No BS, no fluff.
    Thank you.

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

    I could say this on every video... I wish I'd found these sooner instead wasted about a month doing 10 different "beginners" course done by "techies" that don't understand the word "beginner" - those were so much gobblygook that I had to go to the hospital because my head exploded. Anyway, I was just thinking that 4 of those guys down rated the video because they were mad that you keep pulling aside the curtain aside to reveal the great and powerful Oz.

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

      So true. Ive watched so many tutorials by people that know everything about how to code, but apparently don't know Jack about how to teach.
      Not helpful!

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

      Same here I wasted 4months searching tutorial for beginner

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

      You are so right!!!

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

    You explain things at an awesome pace for me man thank you!!! Love your videos!!

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

    Best js tutorial out in the youtube..
    when do you use return value in function?..got a little confused on how to use "return value".

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

    Superb explanation. Much better than Udemy. Thanks shinobi

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

    This example will take any number of numbers:
    function getAvg (...args) {
    sum = 0;
    for (i=0; i < args.length; i++) {
    sum += args[i];
    }
    return sum/args.length;
    }

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

      Awesome! I was expecting the author to mention it since I felt it was begging for an explanation on this.

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

    I fcking love the intro :) ... and of course the way you explain stuff.

  • @css-chef5209
    @css-chef5209 8 ปีที่แล้ว +3

    Thanks for the tutorial, this is the best explanation that i found so far. I will definitely recommend you channel

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

    Hi there, thank you for the video upload. May I ask what does "return" do? When is it necessary to include "return"?
    Thank you!

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

      +daren tan Hey, a return statement returns a value to you. For example, if you say ' var result = addNums(a,b); ' and you return an integer in the function addNums, then the return value will be stored in the variable 'result' .

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

      +The Net Ninja hi there! Thanks so much. I understand much better now and thank you for all your videos too!

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

      @@NetNinja Hi, could we say that when you return the average variable you change it from a local variable to a global variable ?

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

    Bro, Please make the font size a bit bigger on Bracket!

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

    function getAverage () {
    var sum = 0;
    for (i=0; i

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

      it might be a litte bit out of the scope of the beginner audience, and the code isn't optimised, but with some help from google I made the function average any number of arguments.
      Basically, the for loop checks all arguments, adds them to the variable "sum", and then divides by the amount of variables.
      Thought maybe it would be of interest to some people here.

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

      exactly just what i was thinking, how to calculate an indefinite numbers or when the number should not be limited only by 2 or 3 or 4 numbers, but up to the users, thanks for the insight!

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

    i assume 'get average' here is an arbitrary name and not a pre-defined function/pre-programmed function. you could have named it anything? so because of this you created your own function which is (a+b)/2. am i correct in my thinking? thank you, p.s. keep up the good work

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

    Best of best in TH-cam channel ..I recommended your channel.so much. ...

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

      Thank you 😊😊😊

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

    var a = parseInt(prompt("1 Number: "));
    var b = parseInt(prompt("2 Number: "));
    document.write("The average is: " + (a+b)/2);
    This works also, but I was struggling to write it as a function. Do you have any advise?
    God bless you

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

    8:31 made me wonder - do functions have a ".length"-method? I would have tested it, but am not at my API atm and I am almost certain to forget the question until then. 😀Also - sorry for the necro

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

    // Enjoyed this one [Code] function getAverage(a,b,c){
    var average = (a + b + c) / 2;
    console.log(average);
    return average;
    }
    var finalResult = getAverage(10,10,10);
    document.write("your final result is .... " + finalResult);

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

    At around three minutes in, could you have the "average" variable as a local variable? Then in the code block, you can just say "average = (a + b) / 2"?

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

      Yes, that's exactly what was done in the video.

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

    loved ur videos thanks very helpful

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

    why does brackets keep giving me error in the words document and console? it says they are not defined

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

    Sounds basic but I can’t wrap me head around the return part of the function…

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

    🌺🌺

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

    this went from lv 19 to lv 30 real quick

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

    This is all simple to understand when you show it,but why do we need this and when to use it?

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

      he explains it in the first part of the video. Obviously getting an average of random numbers isn't very interesting, but a function can do anything you want. It's mostly a more convenient way to group parts of code that you need to use repeated and/or in several places.

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

    now you said additional numbers get ignored but what if you have (a + b + c) and you only put two numbers in the get average section

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

      good question past self the answer would be it would say c is not defined

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

      @@rarestaiden185 yes, it is undefined and every calculation should result in a “NaN”

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

    Hiii there,if we don't write return function in the main function's parenthesis then what happens??

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

      you will get error something like undefined.

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

      your variable, in this case, the myResult variable will get undefined notification on your console log, because the result of the function is not being stored in the variable

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

    why do you used the return thing to average? didn't understand that part, what woudl happen if you just put return?

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

      If you only put return, with my basic knowledge, it would return an undefined value

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

      @Fierce Deity, you are right. If we put only return then we are not returning any specific value therefore, it would return an undefined value since the return value is not defined.

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

    Okay I love the kungfu sound played after every outro LOL can someone link me to this sound just so i can stale it for myself

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

    why doesnt myresult hv hv dese () in console.log like myresult();

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

    is it possible to do?
    console.log(getAverage(7, 8););

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

    let total=0, count=1;
    while (count

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

      I'll try to explain your code in plain math so:
      total = 0 cont = 1
      while loop does this under the hood:
      1. loop: total = 1(count) and on the last line count gets incremented by 1 so now is 2.
      2. loop: total(1) += 2(count) and now total is 3. count again gets incremented by 1 so now is 3.
      3. loop: total(3) += 3(count) now total is 6. cont again gets incremented by 1 so now is 4.
      4. loop: total(6) += 4(count) now total is 10. count gets incremented by 1 so now is 5.
      .
      .
      .
      10. loop: total(45) += 10(count) now loop stops cuz 10

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

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

    nutshell mean in your video? Nice video and great explanation.........................

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

      'In a nutshell' is just an English expression for giving a short explanation, usually in as few amount of words as is possible.

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

    i dont get the return thing :-/

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

    could you say 'var myResult = average';

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

      Hi lardosian, As far as I understand we cannot do 'var myResult = average;' because the variable average is a local variable of the getAverage function. Therefore, we cannot access it outside the function, as outside getAverage function it will be undefined.

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

    whaa

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

    Please can you remake these videos in a blackboard and chalk style (a bit like Khan Academy) and in colour.......
    as it is hard to see on a mobile phone.
    Appreciate your videos.....but would be perfect if we could read what your inputting.
    Maybe use Camtasia

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

    i am so confused like for the next 100 yers

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

    Sounds basic but I can’t wrap me head around the return part of the function…