Results 1 to 3 of 3

Thread: Default values in functions?

  1. #1
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default Default values in functions?

    I've had the feeling that I'm becoming more used to programming in JS, though I still don't do it very much. But then today I ran into an odd translation issue from PHP that has left me confused and gave a 'parse error' in several browsers (Chrome, Safari, at least).

    In PHP I'd assign a default value to a variable in a function like this:
    PHP Code:
    function example($var=1) {
      return 
    $var;

    In Javascript, I tried to do the same, but apparently that's bad Javascript:
    Code:
    function example(var=1) {
      return var;
    }
    Is there a workaround for this? Since JS has annoying ways to check if a variable isn't set, it's hard to give a default value anyway. For the moment I worked around this in my current project, but I'd like to know what's going on here.

    Is there just a different perspective I should adopt when writing JS?

    In PHP, it's bad code (will cause a fatal error) to omit any argument, unless it has a default value like that. What about JS? It seems to not mind skipping one.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,954
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    Javascript is different, you don't need to define everything, but you can, and if you do, the syntax is also different. And the word var is reserved, so you cannot use it as a variable name. There are several ways to handle it. The simplest is:

    Code:
    function example(myvar) {
      myvar = myvar || 1;
      return myvar;
    }
    If myvar is undefined, you will get 1. But you will also get 1 if myvar is 0. So if it's a number you're after and 1 is what you want if it's not a number:

    Code:
    function example(myvar) {
      myvar = typeof myvar !== 'number'? 1 : myvar;
      return myvar;
    }
    It's really kind of messy though, to equal PHP, you would do:

    Code:
    function example(myvar) {
      myvar = typeof myvar === 'undefined'? 1 : myvar;
      return myvar;
    }
    That way you will get whatever myvar is as long as it's not undefined.

    But you almost never see that. Usually it's just assumed that myvar will be whatever's expected, and that if it isn't, the script will break and the author must trace back to where that happened and fix it. Alternatively you can test myvar in various ways, for what it's most likely to be, and as a result of that determine what to do next, that's also pretty common:

    Code:
    function example(myvar) {
      if (typeof myvar === 'number') {do something;}
      else if (typeof myvar === 'string') {do something else;}
      else {do yet another thing;}
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    djr33 (04-19-2013)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    And the word var is reserved, so you cannot use it as a variable name.
    Oops. That's unrelated to my problematic code. That was just a (bad) example for here

    Some of those ideas are interesting, especially the x||y example. I like that.
    It's really kind of messy though, to equal PHP, you would do:
    Hm, yes, but ok. I'll make a note of that code.

    ...That way you will get whatever myvar is as long as it's not undefined.

    But you almost never see that.
    What I'm trying to do is create exceptions. My function looks something like this:
    Code:
    function myfunction(myvar,ignoresomething) {
        if (ignoresomething!=1) {
            alert(myvar);
        }
    }
    In short, what I want to do is skip part of a function if there's some special circumstance, as determined by that second value. (I'll usually in PHP set that by default to 0, meaning proceed like normal, while I'll mark an exception with a 1, so that the function skips one part or another.) Basically I want to preserve most of the behavior of the function, but I want to skip one step.

    It seems like it will work most of the time to use != (although PHP would be unhappy about using an undefined variable), but for some reason I'm in the habit of using if (somevar==1) to note WHEN to skip it, rather than WHEN NOT to skip it. No big deal.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Similar Threads

  1. Validate optional form fields with default values
    By Rain Lover in forum JavaScript
    Replies: 0
    Last Post: 10-22-2012, 05:25 PM
  2. Default function values
    By JasonDFR in forum PHP
    Replies: 2
    Last Post: 02-07-2009, 03:28 PM
  3. Replies: 1
    Last Post: 07-14-2008, 11:57 PM
  4. Replies: 3
    Last Post: 05-27-2008, 05:36 PM
  5. Chained Selects- Default Values?
    By Colin in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 06-25-2007, 05:21 PM

Bookmarks

Posting Permissions

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