Javascript Fundamentals: Development for Absolute Beginners

JavaScript Functions - 07

Download this episode

Download Video

Description

Functions are central to JavaScript, much in the same way classes are central to Object Oriented Programming languages like Visual Basic or C#. In this lesson, Bob demonstrates the basics of creating and calling functions, passing arguments, using the arguments object, and creating function literals as well as anonymous functions.

Tag:

JavaScript

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • Rafael

      great work bob

      you are a very good professor

      tks for your series

    • MohamedBakr

      Thanks for this great series Smiley

    • shahana

      Hi bob,
      your demonstration is really admirable,thanx alot for a really good series.

    • Pavlo

      Thanks Bob! You and your team did great job! :)

    • Hi  Bob

      Thanks Bob! your videos are really good.very nice videos and easily understanding....
      Thanks alot..sharing videos..........

    • TGWC13

      function add() {   

      var tempValue = 0;   

      for (1 = 0; 1 < arguements.length; 1++)   

      {

       tempValue += arguements [1];      

      }  

       return tempValue;   

      }

        var myValue = add(1,2,3,4,5);  

       alert (myValue);

      I can not get this piece of code to show up when i save it this is the first exercise that i have done and not been able to work out where I am going wrong. Help!

    • TGWC13

      Try spelling argument correctly and you may get it to work...really sorry to have troubled ANYONE with my inability to spell correctly... Very Embarrassed!! I am really enjoying these exercises they're great!

    • BobTabor

      @TGWC13: We've all been there.  Smiley  Don't worry ... this is not simple stuff.  It's easy to overlook little things.

      @Rafael:  @MohamedBakr:  @shahana:  @Pavlo:  @Hi Bob:  Thanks for the nice words!  Smiley

    • Nico

      @TGWC13 your for loop is wrong. It's i not the number 1


      for (i = 0; i < arguements.length; i++)

      {

      tempValue += arguements [i];

      }

    • Nico

      <-- lol Apple logo on MS page


      I forgot to write some nice words ^^

      @Bob thank you very much for your great tutorials. I'm enjoying them very much. I started with the HTML5 & CSS3 Fundamentals and I really enjoyed all the hours "we" spent together :D

      Have you ever thought about going to Hollywood becoming a movie star? You do very well in front of the camera!

      Thanks a lot!

    • BobTabor

      @Nico: You're funny.  re: Mac ... I don't think Microsoft hates Apple per se ... Office for Mac is a pretty huge seller.  Smiley  They compete in some ways and partner in others.  It's a complicated relationship ... but I'm not a Microsoft employee and you should ignore whatever I say on the matter.

      And no, probably not going to pursue Hollywood anytime soon.  Not a lot of need for middle-aged gray-hair, overweight dudes on TV.  Smiley  Thanks though .. made me laugh.

    • Sal

      Nice words!
      Are you saying that an anonymous function is one created and run all at once?
      And, like the function literal, because I haven't given it a name, I can't run it (call it) again?

    • BobTabor

      @Sal: Yes, that is a characteristic of an anonymous function, but it's not its only characteristic or benefit.  I was going to try and enumerate them, but once again, Wikipedia does a nicer job than I could do on my own:

      http://en.wikipedia.org/wiki/Anonymous_function

    • shondralynn

      Hi Bob,

      I would like to know if it is possible to use Javascript for a Real Estate Website?

    • BobTabor

      @shondralynn: Absolutely, I'm betting most real estate sites use it in some capacity ... the question is, what functionality do you need and can JavaScript address it?  That's something only you can answer.

    • dsilvadarius

      I think the developers of this website need to check its spam detection algorithm.

      Here is a link to the screen cast.

      please answer the question in the video.

    • BobTabor

      @dsilvadarius: That video is marked private.  Sad  Couldn't see it.

      Also, which posts are you referencing?  I monitor each post and remove ones that are spammy.  Of course I leave the ones that make me feel good about myself.  Wink

    • dsilvadarius

      On a serious note. I wanted to learn JavaScript for a long time even prayed about it and your the answer. Your more than anyone can bargain for. You impart your knowledge very nicely. I discovered you also uploaded a series on vb and c#. Which language you recommend i take up having JavaScript under my belt.

    • dsilvadarius

      Which of the links below refer to your use of argument at 4:56 in this video ? (I shortened the urls to see if i can fool the spam detection algorithm)

      tinyurl.com/kevpmr5

      tinyurl.com/mh6xvu6

       

    • BobTabor

      @dsilvadarius: Hi, trying to catch up ... a bit confused ... are you good?  Still having problems?  Anything I can help with?

    • dsilvadarius

      Question 1) Have any idea why google chrome and opera output undefined in the console window even when every this is defined in the code. why do the f12 developer tools (internet explorer) and firefox (firebug) not show it?

      Question 2) How can a value be defined and have the value undefined at the same time?

      watch this[ www.youtube.com/watch?feature=player_detailpage&v=v2ifWcnQs6M#t=1184s ]watch 30 seconds to get an idea of what i'm talking about.

      about the question in the previous post i want to choose between c# and vb which would be better suited for me as a web developer having javascript under my belt?

    • dsilvadarius

      Did i offend you or something?  You don't normally take so long! I apologize if i did.

    • BobTabor

      @dsilvadarius: No, you certainly did not offend me.  I drove 14 hours from Dallas to Chicago this past week (and back yesterday) to be with my dad who is 81 and in ICU in the hospital.  Heart, lungs, kidneys ... you name it.  I was trying to help my mom with end of life type issues.  I"m back in the office today and digging out.

      Your question required I listen to Crockford which requires my full mental capacities, most of which I was unable to devote to anything last week.  I'll have an answer for you later today ... even if it is "I have no idea."  Thanks!

    • dsilvadarius

      oh! you reminded me of when my dad was in the ICU for heart attack. My mum was so devastated  had almost given up hope, a miracle was required and we got it. He's on life support (a stent) but he's in good shape. he goes for check up every now and then but he's good. So take your time i'm in no hurry. I'll be keeping your family in my prayers.

    • baerspective

      why is there no semi-colon after "for (i = 0; i < arguments.length; i++)"?

      btw: thank you so much for offering these videos. great lessons. not a natural coder but am following along as best i can. hope it will start to differ more from PHP. i did very poorly.....don't like PHP

    • Mike

      Possibly someone can help here. I am new to all of this and I am trying to get a handle on this. In lesson 7 we created an input button with the onclick firing a JS function. It works with the first example. I am playing around with this and am trying to get the onclick event to display Howdy, pardner (like the second example). The onclick event will display Howdy,undefined it will not display the pardner. I will post the code possibly someone can enlighten me.
      <!DOCTYPE html>
      <html>
      <head>

      <title>JavaScript Example</title>
      <meta charset="UTF-8">

      <script type="text/javascript">
      function myNewFunction(myValue) {
      alert("Howdy, " + myValue);


      }
      myNewFunction("pardner");






      </script>

      </head>

      <body>

      <h1>JavaScript Example</h1>
      <input type="submit" value="push this" onclick="myNewFunction();" />



      </body>


      </html>

    • Clint

      @Mike: you aren't passing in an argument which when it gets converted into a string is "undefined" as it can't figure out what it actually is.  If you did

      onclick="myNewFunction('foo');"

      it would work

       

    • Clint

      @baerspective: if I'm understanding your question correctly, if you do for(var x = 0; x < 10; x++); you're not really doing any work.  You use the { } to indicate where you are doing work for that loop OR it is the first executed line after the for loop

      If it is  for(var x = 0; x < 10; x++;), you don't really need to the semi-colon for the last statement.  It is just how the language works.  Think it is like English where we have short hands for certain things.

    • Clint

      @dsilvadarius:

      q1:  I need to know what code you're talking about.  Could be Trident vs WebKit vs Gecko's javascript engine / debugging.

      q2:  believe the right answer is you can define a variable like var foo; which then makes is there yet it doesn't have any defined value so it is undefined.

    • BriDominguez

      So there are different ways to create functions in JS. For example the way you are showing here is simpler than in other places which teach it like:

      var functionName = function(parameter,...)

      {

      code...code...code...

      };

       

      So are both versions identical in the way they function?

    • Clint
    • BobTabor

      @BriDominguez: ... and to add on what Clint said, we talk about those sorts of things later in this series.  I didn't want to discuss *all* the ways to create a function too early ... that would be overwhelming.  Smiley

    • Tyler

      Bob, you made a crucial mistake in this video. That shirt you're wearing does not translate well to the camera (Moire effect).

      I'm not sure if I should program a function to reduce the loud stripes on the shirt or quit programming altogether due to this mishap and inform my boss that Bob's stripes have forced my resignation.

    • BobTabor

      @Tyler: Actually, we have fixed this bug.  Download this and apply this patch to the existing video:

      http://bit.ly/17DQjhX

       

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.