Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:36:08+00:00 2026-06-13T04:36:08+00:00

I saw something like this in Javascript : function name (secret) { secret =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:36:10+00:00Added an answer on June 13, 2026 at 4:36 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw a java function that looked something like this- public static<T> foo() {...}
I saw that javascript has something similar. Is it possible to do something like
I recently saw a C# constructor that look something like this... public Class foo
I hope this is something straightforward that I'm doing wrong. I saw something online
I've been examining HTTP 1.1 specifications and well I saw something like this: Request-Line
I saw something like this in some graphics libs today, and it seems like
I was looking at some code and saw something like this: int d =
In javascript it's possible to do things like this: SomeObject.somefunction(function(someparameters){ //do some things. });
I have a Javascript object like this: var jsonDataActual = { source: [{ name:
I came across a piece of code which looks like this: jQuery(function($) { $('#saySomething').click(function()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.