18

I saw something like this in Javascript :

   function name (secret) {
          secret = secret || {};

i can't find anywhere what exactly means secret = secret || {};

Is that mean create global variable with value of passed argument or an object?

When do you use it? When there is no argument passed?

nico
  • 50,859
  • 17
  • 87
  • 112
Teq1
  • 631
  • 1
  • 6
  • 20

1 Answers1

26

What it means

If the variable secret is falsy (one of the following):

  • false
  • 0
  • '' (empty string)
  • null
  • undefined
  • NaN

..then set it to {} (an empty object - it's the same as new Object()).


Alternative code

It could also be written as this:

if (!secret) secret = {};

But as it's longer most people prefer the above.


Why?

This solution is useful as javascript has no default function parameters.

For example an example in PHP could look like this:

<?php
    function foo($bar = 'default') {
        echo $bar;
    }
?>

and in JS that could be

function foo(bar) {
    bar = bar || 'default';
    console.log(bar);
}
foo(); //default
foo(NaN); //default
foo(undefined); //default
foo(null); //default
foo(false); //default
foo(0); //default
foo(''); //default
foo('something'); //something
foo(12); //12
foo(1.2); //1.2

What if I only want to set a default if nothing else has been set?

If you only want to check for no value (no falsy values), then you can use the typeof function in JS:

function foo(bar) {
    if (typeof bar == 'undefined') bar = 'default';
    console.log(bar);
}
foo(); //default
foo(undefined); //default
foo(NaN); //NaN
foo(null); //null
foo(false); //false
foo(0); //0
foo(''); //(empty string)
foo('something'); //something
foo(12); //12
foo(1.2); //1.2
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • 2
    Actually, it will also set `secret` to `{}` if `secret` is any "falsy" value (0, empty string, `false`, etc.) – Ted Hopp Oct 22 '12 at 17:08
  • 3
    Don't you mean _"if `secret` **is** empty ..."_? Also, the `typeof` test is _not_ equivalent, since it doesn't handle anything except `undefined`. (The irony, of course, is that `{}` is also a falsy value, so it will reassign `secret` to a new empty object even when it is an empty object.) – Ted Hopp Oct 22 '12 at 17:12
  • @TedHopp Yes - you're of course completely correct - I've revised my answer. – h2ooooooo Oct 22 '12 at 17:18
  • Better, but please also fix the "isn't" (at the start of the answer). – Ted Hopp Oct 22 '12 at 17:20
  • @TedHopp Oh yes, I must be tired as I didn't see your previous answer. I've tested with `{}` and `[]` and both return their respectable values in the original example (in my Chrome 22 at least), so are you sure that they're falsy? (or is it only falsy according to the standard, and isn't implemented in Chrome?) – h2ooooooo Oct 22 '12 at 17:22
  • 1
    Good. I also must have been too tired when I wrote that. Of course, empty lists and arrays are truthy values, not falsy. For the record, the falsy values are: `null`, `undefined`, 0, `""`, and `NaN`. – Ted Hopp Oct 22 '12 at 17:27