Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting a default value to an undefined variable in javascript

Ideally I want to be able to write something like:

function a( b ) {
    b.defaultVal( 1 );
    return b;
}

The intention of this is that if b is any defined value, b will remain as that value; but if b is undefined, then b will be set to the value specified in the parameter of defaultVal(), in this case 1.

Is this even possible?

Ive been toying about with something like this:

String.prototype.defaultVal=function(valOnUndefined){
    if(typeof this==='undefined'){
        return valOnUndefined;
    }else{
        return this;
    }
};

But I'm not having any success applying this kind of logic to any variable, particularly undefined variables.

Does anyone have any thoughts on this? Can it be done? Or am I barking up the wrong tree?

like image 584
Jimmery Avatar asked Sep 13 '12 15:09

Jimmery


People also ask

What is default value of undefined in JavaScript?

Default Parameters If a function in JavaScript is called with missing arguments(less than declared), the missing values are set to undefined. Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter: Example function myFunction(x, y) { if (y === undefined) { y = 2;

Did You Know you can set a default variable value in JavaScript?

Did you know that you can set a default variable value in JavaScript? Here is an example declaration statement, where foo is set to bar if bar is defined, but otherwise set to Default Value: Because bar is undefined in the above example, foo is set to Default Value.

How do I set a variable to undefined in Java?

Set a variable to undefined in the js javascript command line terminal that comes with Java on Ubuntu 12.10. WARNING! When setting your variable to undefined you are setting your variable to another variable. If some sneaky person runs undefined = 'rm -rf /'; then whenever you set your variable to undefined, you will receive that value.

What are default parameters in JavaScript?

In JavaScript, function parameters default to undefined. However, it's often useful to set a different default value. This is where default parameters can help. In the past, the general strategy for setting defaults was to test parameter values in the function body and assign a value if they are undefined .


1 Answers

Why not use the default operator:

function someF(b)
{
    b = b || 1;
    return b;
}

Job done! Here's more info about how it works.

Just as a side-note: your prototype won't work, because you're augmenting the String prototype, whereas if your variable is undefined, the String prototype doesn't exactly apply.

To be absolutely, super-duper-sure that you're only switching to the default value, when b really is undefined, you could do this:

var someF = (function(undefined)//get the real undefined value
{
    return function(b)
    {
        b = b === undefined ? 1 : b;
        return b;
    }
})(); // by not passing any arguments
like image 158
Elias Van Ootegem Avatar answered Sep 17 '22 18:09

Elias Van Ootegem