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 5936657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:25:47+00:00 2026-05-22T15:25:47+00:00

How do I tell Javascript to use zero as a variable if the passed

  • 0

How do I tell Javascript to use zero as a variable if the passed variable if empty? In PHP I can use the following.

<?php
   function test($var=0){
      echo $var;
   }
?>

So it would set $var to 0 if the passed variable was empty. I can’t seem to figure this out in Javascript.

Thanks

  • 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-05-22T15:25:48+00:00Added an answer on May 22, 2026 at 3:25 pm

    If you call a method without providing a value for one (or more) of the arguments, those arguments will be undefined.

    Thus within the method you can test whether the variable is defined, and if not assign it a default value:

    function test(arg) {
        if (typeof(arg) === 'undefined') {
            arg = "default"; // or whatever default value you want
        };
    
        // Rest of method
    }
    

    It’s not very elegant, but Javascript doesn’t have a nice native default-arguments syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can anybody tell me what's the point if any for a javascript function like
How can I tell if a given javascript function will work across browsers? For
How can I tell in JavaScript what path separator is used in the OS
How do you tell if a function in JavaScript is defined? I want to
Can anyone tell me how to write javascript code that removes everything after the
Can someone tell me what exactly the two above lines of javascript do? And
Can I tell, using javascript, whether a user has clicked on the X icon
I can use javascript to get html tags and divs to appear but I
Could you please tell me, what is the use of JSON (in Javascript and
Below I use the id of a clicked item to tell my function which

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.