1

How do I set default value of timing param?

function active_timer(timing){
    interval = setInterval(function(){
        console.log('interval');
    }, timing);
}

I can do like this

function active_timer(timing){
    var time = 1000;
    if(timing){
        time = timing
    }
    interval = setInterval(function(){
        console.log('interval');
    }, time);
}

My problem is with the naming, timing and time actually are the same thing, how can I write less redundant code in javascript?

1
  • 2
    interval = setInterval(function() { ... }, timing || 1000); Commented Aug 20, 2016 at 16:03

4 Answers 4

1

An argument is a (local) variable just like any other.

timing = timing || 1000;

You can overwrite it without a care in the world.

Sign up to request clarification or add additional context in comments.

1 Comment

why not a var in front?
1

You could add a check in your function,

if (typeof timing == 'undefined')
    timing = defaultValue;

or (ternary operator)

timing = (typeof timing == 'undefined') ? defaultValue : timing;

or

timing = timing || defaultValue 

Example typeof

function x(y) 
{
  if (typeof y == 'undefined')
    y = 5;

  return y;
}

alert(x()); // 5

alert(x(25)); // 25

JSFiddle

Reading Material

typeof

MDN - Default Parameters

Comments

1

In ES2015 (aka "ES6") and later, you can specify a default value for the argument:

// ES2015+
function active_timer(timing = 1000) {
// -------------------------^^^^^^^
    interval = setInterval(function(){
        console.log('interval');
    }, timing);
}

In ES5 and earlier (well, technically all of this applies to ES2015+ as well): You can assign values to function arguments, so you don't need a separate variable unless you want one. If active_timer is called without any arguments, timing will have the value undefined.

You have a few options for how to determine you need to apply the default:

if (timing === undefined) {
    timing = 1000;
}

or

if (typeof timing === "undefined") {
    timing = 1000;
}

or

if (arguments.length === 0) {   // NOT RECOMMENDED
    timing = 1000;
}

(Not recommended because using arguments within a function can impact its performance.)

Or if you know active_timer will never be called with 0, you can use the curiously-powerful || operator:

function active_timer(timing){
    interval = setInterval(function(){
        console.log('interval');
    }, timing || 1000); // Only if you know timing won't be 0
}

The result of a || b is the value of a if it' truthy, or the value of b if a is falsy. The falsy values are 0, "", undefined, null, NaN, and of course, false.

6 Comments

Ah, that last one is much better, didn't know it was possible (+1).
@Script47: Apologies, I just moved it so it's not the last one anymore. :-) Yup, great new feature as of ES2015.
How supported is ES6?
@Script47: In modern browsers, support is quite good these days (kangax.github.io/compat-table/es6). But because there are still people using obsolete browsers, most of us transpile ES2015 to ES5 using tools like Babel.
I found the ES6 string.includes function yesterday (slightly confused, thought it would be called contains...) and found it is supported in Chrome though not recognized in Webstorm(?), I may need to update it.
|
0

You could use the default parameter and arrow function of ES2015:

function activeTimer(timing = 1000) {
    setInterval( () => {
        console.log('interval:' + timing);
    }, timing);
}

activeTimer();

See also the fiddle.

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.