dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: JavaScript - functions

  1. #1
    Join Date
    Dec 2013
    Posts
    7

    JavaScript - functions

    Just started a javascript course. What is the benefit of using a function for the following script. Verses living the function off. It appears to do the same thing.

    <html>
    <head>
    <title> Greeting </title>
    <script>
    function greet(who){
    alert ("Hello, "+ who + '' + who2);
    }
    var who = prompt ("what is your first name");
    var who2 = prompt ("what is your last name");
    alert("Hello " + who + ' ' + who2);
    </script>
    </head>
    <body>
    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,496
    It does not do the same thing.

    You NEVER call the function, so it does nothing!

  3. #3
    Join Date
    Oct 2013
    Posts
    5
    This fill you in : http://jsfiddle.net/wTBqW/

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,802
    Functions are good for helping you reuse code blocks that do specific tasks without having to write the code again and again, you just call a function and the interpreter then fetches that code block, runs it and the output is then generated if any output is needed, etc.

    Functions are your friend
    STOP using $ prefix on JavaScript variable names...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  5. #5
    Join Date
    Oct 2013
    Posts
    1
    From the Kindle edition of my book, "A Smarter Way to Learn JavaScript":

    A function is a block of JavaScript that robotically does the same thing again and again, whenever you invoke its name. It saves you repetitive coding and makes your code easier to understand.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles