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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:49:19+00:00 2026-06-05T20:49:19+00:00

I am doing a comparison on dates in javascript. In this case date1 is

  • 0

I am doing a comparison on dates in javascript. In this case date1 is empty "" and I can see the same in firebug. As per the code below, the first alert shouldn’t be called because date1 == "", but for some reason the alert alert(" This is called...."); is invoked. What is wrong here?

if(date1 != null || date1 != ""){

        if( (date1 != null || date2 != "") && (date1 < date2)){
                alert(" This is called....");
                break;
        }
        else{
            alert(" That is called....");
            break;
        }

    }

The above if condition is inside a for loop, hence the break.

  • 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-05T20:49:20+00:00Added an answer on June 5, 2026 at 8:49 pm

    I think you mean to be using && instead of || in your first comparision to make sure that both conditions are true and I think you have typo where you’re using date1 instead of date2 in the second test. Further, you can just use if (date1) to simultaenously rule out null and "" and undefined and 0 and NaN and any other falsey value.

    I think you want something like this:

    if (date1) {
        if(date2 && date1 < date2) {
            alert(" This is called....");
        } else {
            alert(" That is called....");
        }
        break;
    }
    

    If what you’re really trying to do is make sure that date1 and date2 are legal numbers, then I’d suggest you do this:

    if (typeof date1 == "number") {
        if(typeof date2 == "number" && date1 < date2) {
            alert(" This is called....");
        } else {
            alert(" That is called....");
        }
        break;
    }
    

    Or, if they’re supposed to be Date objects, then you can test for that:

    if (date1 instanceof Date) {
        if(date2 instanceof Date && date1 < date2) {
            alert(" This is called....");
        } else {
            alert(" That is called....");
        }
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you please suggest to me the syntax for doing floating point comparison in
The MDN JavaScript guide states the following when doing a comparison between Java and
Doing a mysqli query and COUNTing the results, is there a preferred comparison operator
We're doing some code cleanup, fixing signed/unsigned comparisons, running static analysis, etc, on the
Doing the getting started of Sinatra. I get this error: ./sinatra.rb:5: undefined method `get'
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
Per the Core Data Programming Guide: You can specify that an attribute is optional—that
I'm doing DateTime comparison but I don't want to do comparison at second, millisecond
I have a piece of code where I see a warning saying I am

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.