30

I have this code:

var phrase = function (variable, defaultPhrase) {
    if (typeof variable === "undefined") {
        return defaultPhrase;
    }
    else {
        return variable;
    }
}

It's called like this:

Ext.Msg.show({title: phrase(js_shutdown,'Shutdown'), //...

What I want to do is to use a default phrase when the variable is not defined, but when I pass an undefined variable to phrase(), JS throws an undefined variable error. How do I fix this? Any other ideas to do this?

2

7 Answers 7

64

You don't need a function. The || operator is usually used:

Ext.Msg.show({ title: js_shutdown || 'Shutdown', //...

You can see || as:

someValue || defaultValue

For strings, defaultValue is used if someValue === "".

If the variable is not defined at all, you'll need to inline the typeof x === "undefined" check, because you cannot pass the variable to a function (that's a ReferenceError).

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

1 Comment

Wrong. if someValue = 0 instead of being undefined, defaultValue will be used.
15

Usually using || is enough, like others have suggested. However, if you want to have 0, false and null as acceptable values, then you indeed need to check if the type of the variable is undefined. You can use the ternary operator to make it a one-liner:

var variable;
var defaultPhrase = "Default";
var phrase = (typeof variable === "undefined" ? defaultPhrase : variable);
console.log(phrase);
// => "Default"

Comments

4

In javascript, you typically use the OR operator || to provide an alternative value when a variable is undefined:

return variable || defaultPhrase || ''

In case variable is undefined, it will evaluate to false then the second part of the test will be evaluated, if it is also undefined, you can still return an empty string.

1 Comment

No, null and 0 also fall through with this operator.
4

It's a javascript error to reference an undefined variable with no scope in your function call. So, if the variable js_shutdown doesn't exist in scope, then this:

Ext.Msg.show({title: phrase(js_shutdown,'Shutdown'), //...

is an error.

For example, this code causes an error on the line that calls the phrase() function:

var Ext = {};
Ext.Msg = {};
Ext.Msg.show = function() {console.log("success")};

function phrase(variable, defaultPhrase) {
    return(variable || defaultPhrase);
}

Ext.Msg.show({title: phrase(js_shutdown,'Shutdown')});​

because the javascript engine isn't able to find js_shutdown in any scope.

But, this is OK:

var Ext = {};
Ext.Msg = {};
Ext.Msg.show = function() {console.log("success")};

function phrase(variable, defaultPhrase) {
    return(variable || defaultPhrase);
}

Ext.Msg.show({title: phrase(window.js_shutdown,'Shutdown')});​

You can see that this works here: http://jsfiddle.net/jfriend00/JFz6R/

Because you've told the JS engine exactly where to look for js_shutdown and when it isn't there, it just passes undefined to the phrase function (as you want).

Comments

1

Use the logical OR operator:

 var phrase = variable || defaultPhrase;

Or inline:

Ext.Msg.show({title: (js_shutdown || 'Shutdown')), //...

Comments

0

I would usually code this like title: js_shutdown || 'Shutdown' in the absence of possible security issues.

Comments

-1

Shouldn't it be:

var phrase = function (variable, defaultPhrase){
    if(variable == undefined){
        return defaultPhrase;
    }else{
        return variable;
    }
}

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.