www.webdeveloper.com
Results 1 to 9 of 9

Thread: JavaScript Functions - Optional Parameters

  1. #1
    Join Date
    Mar 2006
    Posts
    115

    JavaScript Functions - Optional Parameters

    I know in PHP you can have optional parameters by doing something like:
    PHP Code:
    function myFunc$a$b=0)
    {
      
    // $b is my optional parameter

    I was wondering how I would create a JavaScript function that could utilize optional parameters as that PHP function might.

    Thanks in advance!

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Code:
    function myFunc ( $a, $b)
    {
      // $b is my optional parameter
    }
    In JavaScript all parameters are optional.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  3. #3
    Join Date
    Mar 2006
    Posts
    115
    ok, well how would I do this then:
    Code:
    // javascript
    function myFunc(a, b)
    {
      // How do I give a default value for 'b' if they did not specify one
    }
    
    //html
    <... onclick="myFunc();" />
    <... onclick="myFunc(4);" />
    <... onclick="myFunc(4, 'Hello');" />
    So in the html section, all these functions will run without throwing an error? If so, how do I know to give the parameter that the function didn't use a default value?

  4. #4
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Set the value inside

    {
    if(b==null)b = 0;


    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  5. #5
    Join Date
    Mar 2006
    Posts
    115
    That seems so rigged.

    Is that the only way to do it?

    If it is, guess I will have to.

  6. #6
    Join Date
    Feb 2009
    Posts
    1
    If you have an optional parameter you have to put blank like this

    // javascript
    function myFunc(a, b)
    {
    // How do I give a default value for 'b' if they did not specify one
    }

    //html
    <... onclick="myFunc('','');" />
    <... onclick="myFunc(4,'');" />
    <... onclick="myFunc(4, 'Hello');" />

  7. #7
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773

  8. #8
    Join Date
    Oct 2008
    Posts
    349
    Quote Originally Posted by intrivious View Post
    That seems so rigged.

    Is that the only way to do it?

    If it is, guess I will have to.
    It only appears rigged if you view it from a non-JavaScript perspective. It is the way of life in JavaScript.

  9. #9
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    It's also worth mentioning that when you do not pass a parameter to a function, for which the function defines an argument for, that argument has an undefined value.
    PHP Code:
    function myFunc(ab) {
      
    // All arguments are required

    PHP Code:
    myFunc(2); // b is an undefined value inside myFunc 
    You can also detect the length of the arguments object inside a function:
    PHP Code:
    function myFunc(ab) {
      if (
    arguments.length 2) {
        
    alert("You need to pass a second argument!");
        return;
      }
      
    // Continue processing


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