10

I am doing a lot of front-end development and I see myself doing this a lot:

function doSomething(arg){
    var int = arg ? arg : 400
    //some code after
}

So I was wondering if the was a way to do this, but shorter and cleaner (I don't like to see arg twice in the same line).

I've seen some people doing something like that :

var int = arg || 400;

And since I don't know in which order I needed to place the value, I tried arg || 400 and 400 || arg, but it will always set int to the value at the right, even if arg is undefined.

I know in PHP you can do something like function doSomething(arg = 400) to set a default value and in a jQuery plugin you can use .extend() to have default property, but is there a short way with a single variable? Or do i have to keep using my way?

Thank for any help and if you can give me resources, it would be appreciated.

2
  • 2
    Don't let falsely values bite you in the butt. If you are working with numbers, better off checking for undefined. Commented May 22, 2013 at 16:18
  • 1
    This might help you to understand the Coalesce Operator better. Commented May 22, 2013 at 16:20

6 Answers 6

15

There's really no shorter clean way than

var int = arg || 400;

In fact, the correct way would be longer, if you want to allow arg to be passed as 0, false or "":

var int = arg===undefined ? 400 : arg;

A slight and frequent improvement is to not declare a new variable but use the original one:

if (arg===undefined) arg=400;
Sign up to request clarification or add additional context in comments.

11 Comments

Note that you shouldn't check for undefined in that way, just to be safe. You should use (typeof arg === 'undefined') instead.
@ColinDeClue Just to be safe against what ? Yourself having redefined undefined ? It's not possible in ES5. Passing null?
Well, no, you can't redefine undefined. Try it. And even if you could, it makes no sense in trying to protect from stupid libraries, don't use those stupid libraries (supposing there is one).
That's why I precised ES5. But do you really saw a library so stupid ? A stupid library could break your code in so many ways, there're no sense in guarding against that.
@JustinJohn: Yes, you can, but why would you do it? It's more complicated and does the same thing.
|
1

In ES6, you can set default value to arguments the same way we do in PHP :

function foo( a = [], b = 'string', c = false ){}

Default value can also be set to a previous argument ou even a function's return value

function foo( a = true, b = a, c = bar() ){}

This will also work with ES6 compilers. The end result will look like this

function foo() {
  var a = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
  var b = arguments.length <= 1 || arguments[1] === undefined ? a : arguments[1];
  var c = arguments.length <= 2 || arguments[2] === undefined ? bar() : arguments[2];
}

Comments

0
function func(x,y){
   if(typeof(x)==='undefined') x = 10;
   if(typeof(y)==='undefined') y = 20;

   //code goes here
}

2 Comments

typeof is not a method call
It is not being used as such here, neither is it being used as a function. The parentheses around x and y are not necessary but create no problem.
0

The problem with your solutions is that a value that evaluates to false (for example "false" or "0") also trigger the default value. So for every parameter that could possibly ever have a value that evaluates to false you have to check for "undefined" explicitly.

var int = (typeof x === 'undefined') ? default : x

If this is not possible you can use

var int = x ? x : default
OR
var int = x || default

Another option would be to use the arguments array and check if the parameters were given. But this can only be used if your optional parameters are the last ones.

function test(x, y) {
    var int1 = (arguments.length > 0) ? x : default;
    var int2 = (arguments.length > 1) ? y : default;
}

6 Comments

or skip the typeof all together and just check if it equals undefined.
@epascarello: That's not safe. undefined is mutable in some browsers.
Modern browsers prevent it from being overwritten. Plus in the 12 years I been coding JavaScript I have never had that happen! Now people overriding submit is a different story. And if you are really scared, store a variable that is undefined and use it.
And chart of support for Immutable undefined: kangax.github.io/es5-compat-table/#Immutable%20undefined The fear can die down.
@epascarello: Lots of people are still supporting IE8 and IE7. I ran into a user the other day who was still using Windows XP...
|
0

Not directly related to the question, but why create a new variable to mirror an argument?

In this situation I would use :

!arg && (arg = 400);

However, this tests arg for falsity which means that the values false, 0, '', null and undefined would all cause arg to be set to 400. If this is not the desired result, perhaps a value of 0 is a valid arg value then I usually test argument.length :

function f (arg) {
  !arguments.length && (arg = 400);

This checks if any value was passed and sets arg only in the case that the call specified no arguments at all.

Only is specific instances where 0 is not a desired value would I use the construct

 arg || 400

which again suffers from the falsity test

If it is important that arg be numeric you could use :

 typeof arg !== 'number' && (arg = 400);

which would ensure that arg was a number and in the rest of the code.

In conclusion: it depends on exactly how you want to use the argument, what values are valid and how much you trust the callers of your code.

Comments

0

I would check arguments.length:

var f = function(arg) {
  var myArg = arguments.length > 0 ? arg : 400;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.