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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:54:35+00:00 2026-05-30T19:54:35+00:00

I use the following code currently if (oldValue) … It works well in case

  • 0

I use the following code currently

if (oldValue) ...

It works well in case oldValue is null, but in case it is 0, it also returns false, when I expect true. So, how should I check for null value? I was thinking about

if (oldValue!=null) ...

but it doesn’t work as I’ve expected.

  • 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-30T19:54:37+00:00Added an answer on May 30, 2026 at 7:54 pm

    To answer your question directly, if your allowed values are 0 and 1 the if statement should look like:

    if (0 === oldValue || 1 === oldValue) {
         ...
    }
    

    This is (in my opinion) the clearest way to state which values are allowed.

    For a more details explanation see below:


    This has to do with truthy and falsy values.

    null evaluates to false, as do 0, "", undefined and NaN, these are called falsy values.

    Likewise, some values evaluate to true. Such as: "a string", "0" (non empty string “0”), any number other than 0 (including negative numbers), Arrays and Objects (even empty ones). These are truthy values.

    There are some unexpected results:

    "" == false     // true
    0 == false      // true
    

    but:

    NaN == false    // false
    null == false   // false
    

    In practice you should always use the identity operator === instead of equality (==). This ensures that you know what type your variable is expected to be (String, Number, Object) and what the exceptional states are.

    Some examples:

    • If you’re getting a value from an input field it will always be a string – the special case is the empty string. Coincidentally this is a falsy value.
    • If you’re counting elements and you need to do something special in case there are no elements – the special case is 0. Coincidentally this is a falsy value.
    • If you’re trying to parse a number from a string using parseInt or parseFloat – the special case NaN (check with isNaN()). Coincidentally this is a falsy value.
    • If you’re checking if a substring occurs within a string using indexOf – the special case is -1 (because 0 is a valid index). This is not a falsy value, but if(str.indexOf(substr)) is most certainly wrong because it is unclear the author knows about the possibly allowed value 0 (which is falsy)

    The point here is: the special cases usually are well defined. Harnessing that allows you to always use the identity operator ===. The equality operator == and falsiness is a common source of bugs.

    For reference:

    "" === false    // false
    0 === false     // false
    NaN === false   // false
    null === false  // false
    false === false // true (of course)
    NaN === NaN     // strange, but makes sense
    "a" === "a"     // yay!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently use the following code to position a div directly below an input
Currently I use the following code to retrieve the IP address of the local
I currently use the following code to trigger an event from my Firefox Add-On.
I currently use the following code to retrieve and decompress string data from Amazon
We currently use the following code to create an email in Outlook so that
I currently use the following code to sanitize a string before storing them: ERB::Util::h(string)
I currently use the following code to print a double: return String.format(%.2f, someDouble); This
I currently use the following code to upload one file to a remote server:
I use the following code to create countdowns in Javascript. n is the number
I use the following code to compile a cpp file to object file. g++

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.