Reputation: 8413
Suppose I have a function in JavaScript:
function something (variable) {
console.log(variable);
}
How do I set that if there is no variable passed, then it is null by default?
Upvotes: 8
Views: 15644
Reputation: 173642
JavaScript isn't very picky when it comes to the number of required function arguments when you call a function; any arguments that are mentioned in the declaration but not passed will be set to the type undefined
.
For example:
function test(foo)
{
console.log(foo === undefined); // true
}
To set default values there are at least three options:
function test(foo)
{
console.log(foo || 'default value');
}
The above will output the value of foo
if it's truthy, or 'default value'
otherwise.
function test(foo)
{
console.log(foo === undefined ? foo : 'default value');
}
This will output the value of foo
if it's not undefined
, or 'default value'
otherwise.
Lastly, you can count the number of arguments that were passed:
function test(foo)
{
console.log(arguments.length > 0 ? foo : 'default value');
}
This will output the value of foo
(regardless of its type) if an argument was passed.
Although undefined
is not writeable since ES5, not all browsers will be so vigilant to enforce this. There are two alternatives you could use if you're worried about this:
foo === void 0;
typeof foo === 'undefined'; // also works for undeclared variables
Upvotes: 7
Reputation:
Or a more friendly (IMO) option would be:
function getProfile( singleVariable )
{
singleVariable = singleVariable || false;
if (singleVariable) {
alert('we have a var')
}
else {
alert('nothing opassed');
}
}
getProfile();
getProfile('tom');
Then when you start passing lots of parameters over, but want the function to be flexible you can do:
function getProfile(params)
{
params = params || {};
if (params.username) {
alert(params.username)
}
if (params.id) {
alert(params.id);
}
}
getProfile();
getProfile({username:'tom', id: 123654987});
Instead of
function getProfile(singleVariable, otherVar, othervar2)
{
singleVariable = singleVariable || false;
otherVar = otherVar|| false;
otherVar2 = singleVariable2 || false;
if( singleVariable ){
alert('we have a var')
}
else {
alert('nothing opassed');
}
}
getProfile('tom', false, 'smith');
That false is required and is annoying.. passing an abject is far more efficient
Upvotes: 1
Reputation: 122956
It should be as simple as:
function something (variable) {
console.log(variable || null);
}
In general you can assign default values to parameters like this:
function something (somevar1, somevar2 /* ... somevarn */) {
somevar1 = somevar1 || 'somevar1 not present';
somevar1 = somevar2 || 2;
somevar3 = somevar3 || {foo: 'bar', foobar: null}
/* etc. */
}
Or, if you need a defence against 0
, false
, etc. (and to serve @Ketola), you could cook up something like:
function something (somevar1) {
somevar1 = ['', 0, false, null, undefined].indexOf(somevar1) > -1
&& null || somevar1;
}
... this || that
is known as short circuit evaluation.
Upvotes: 2
Reputation: 12864
Test the value of your variable like this:
function something (variable) {
variable = (typeof variable !== 'undefined') ? variable : null;
}
Upvotes: 1
Reputation: 562
All the above will work for sure, but this is the simplest approach and I use this the most.
variable = variable ? variable : undefined; // you can use null as well
Upvotes: 2
Reputation: 11671
You can check the size of arguments
variable,
function something (variable) {
if(arguments.length===0){console.log("variable is null");}
else{
console.log(variable);
}
}
Also have a look here in order to check if the variable is null,
How to determine if variable is 'undefined' or 'null'?
Upvotes: 2