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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:23:22+00:00 2026-05-24T11:23:22+00:00

In Javascript, is there a good way to check if a variable was ever

  • 0

In Javascript, is there a good way to check if a variable was ever a true, (or any value), in the entire session? The best way I can think of right now is to perform a regular check like this, recording the truthiness in another variable:

if (variable){
  variablewasevertrue = true;
}

Then when I want to know if the original variable was ever true, I check if the new variablewasevertrue is true or undefined. There’s nothing more graceful like if (variable was ever true){ is there? That doesn’t seem very Javascript-y.

  • 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-24T11:23:22+00:00Added an answer on May 24, 2026 at 11:23 am

    No there is no if (variable was ever true) facility in the language. Variables store values, not history.

    Intercepting values as they’re assigned and checking is the only way to do it. If the variable is really a property (e.g. a global variable is a property of the global object) you can intercept changes easily using setters.

    So to have a history keeping global variable you could do

    var hasEverBeenTruthy = false;
    (function () {
      var value;
      Object.defineProperty(window, "myGlobal", {
        "get": function () { return value; },
        "set": function (newval) {
          if (newval) { hasEverBeenTruthy = true; }
          value = newval;
        }
      });
    })();
    

    This will work on modern browsers, and there are __defineSetter__ variants on many older browsers.

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

Sidebar

Related Questions

Are there any good recommendations anyone can provide for a good Javascript editor on
If you are using Java or JavaScript, is there a good way to do
Are there any good JavaScript frameworks out there which primary audience is not web
Are there any good reasons for why you would include JavaScript like this: <script
Is there a good way of performing a long-running operation in javascript? For example,
Is there a good profiler for javascript? I know that firebug has some support
There are several good Javascript editors for Markdown / Textile (e.g.: http://attacklab.net/showdown/ , the
What would be a good way to report errors in JavaScript instead of relying
Is there a good way to do this? I'm writing an extension that interacts
Is there a good way to determine if a person has a popup blocker

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.