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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:07:39+00:00 2026-05-13T16:07:39+00:00

I’m a bit confused about JavaScript’s undefined and null values. What does if (!testvar)

  • 0

I’m a bit confused about JavaScript’s undefined and null values.

What does if (!testvar) actually do? Does it test for undefined and null or just undefined?

Once a variable is defined can I clear it back to undefined (therefore deleting the variable)?

Can I pass undefined as a parameter? E.g.:

function test(var1, var2, var3) {

}

test("value1", undefined, "value2");
  • 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-13T16:07:39+00:00Added an answer on May 13, 2026 at 4:07 pm

    I’m a bit confused about Javascript undefined & null.

    null generally behaves similarly to other scripting languages’ concepts of the out-of-band ‘null’, ‘nil’ or ‘None’ objects.

    undefined, on the other hand, is a weird JavaScript quirk. It’s a singleton object that represents out-of-band values, essentially a second similar-but-different null. It comes up:

    1. When you call a function with fewer arguments than the arguments list in the function statement lists, the unpassed arguments are set to undefined. You can test for that with eg.:

      function dosomething(arg1, arg2) {
          if (arg2===undefined)
          arg2= DEFAULT_VALUE_FOR_ARG2;
          ...
      }
      

      With this method you can’t tell the difference between dosomething(1) and dosomething(1, undefined); arg2 will be the same value in both. If you need to tell the difference you can look at arguments.length, but doing optional arguments like that isn’t generally very readable.

    2. When a function has no return value;, it returns undefined. There’s generally no need to use such a return result.

    3. When you declare a variable by having a var a statement in a block, but haven’t yet assigned a value to it, it is undefined. Again, you shouldn’t really ever need to rely on that.

    4. The spooky typeof operator returns 'undefined' when its operand is a simple variable that does not exist, instead of throwing an error as would normally happen if you tried to refer to it. (You can also give it a simple variable wrapped in parentheses, but not a full expression involving a non-existant variable.) Not much use for that, either.

    5. This is the controversial one. When you access a property of an object which doesn’t exist, you don’t immediately get an error like in every other language. Instead you get an undefined object. (And then when you try to use that undefined object later on in the script it’ll go wrong in a weird way that’s much more difficult to track down than if JavaScript had just thrown an error straight away.)

      This is often used to check for the existence of properties:

      if (o.prop!==undefined) // or often as truthiness test, if (o.prop)
         ...do something...
      

      However, because you can assign undefined like any other value:

      o.prop= undefined;
      

      that doesn’t actually detect whether the property is there reliably. Better to use the in operator, which wasn’t in the original Netscape version of JavaScript, but is available everywhere now:

      if ('prop' in o)
          ...
      

    In summary, undefined is a JavaScript-specific mess, which confuses everyone. Apart from optional function arguments, where JS has no other more elegant mechanism, undefined should be avoided. It should never have been part of the language; null would have worked just fine for (2) and (3), and (4) is a misfeature that only exists because in the beginning JavaScript had no exceptions.

    what does if (!testvar) actually do? Does it test for undefined and null or just undefined?

    Such a ‘truthiness’ test checks against false, undefined, null, 0, NaN and empty strings. But in this case, yes, it is really undefined it is concerned with. IMO, it should be more explicit about that and say if (testvar!==undefined).

    once a variable is defined can I clear it back to undefined (therefore deleting the variable).

    You can certainly assign undefined to it, but that won’t delete the variable. Only the delete object.property operator really removes things.

    delete is really meant for properties rather than variables as such. Browsers will let you get away with straight delete variable, but it’s not a good idea and won’t work in ECMAScript Fifth Edition’s strict mode. If you want to free up a reference to something so it can be garbage-collected, it would be more usual to say variable= null.

    can I pass undefined as a parameter?

    Yes.

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

Sidebar

Ask A Question

Stats

  • Questions 425k
  • Answers 425k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I guess the first part of this question is, does… May 15, 2026 at 12:21 pm
  • Editorial Team
    Editorial Team added an answer If you set your database engine to sqlite3 when you… May 15, 2026 at 12:21 pm
  • Editorial Team
    Editorial Team added an answer We tried adding Dfile.encoding="UTF-8" to Websphere's jvm and we added… May 15, 2026 at 12:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.