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

  • Home
  • SEARCH
  • 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 9190339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:26:20+00:00 2026-06-17T20:26:20+00:00

In JavaScript, is it possible to test whether a condition remains true throughout a

  • 0

In JavaScript, is it possible to test whether a condition remains true throughout a program’s entire execution? Here, I want to ensure that the variable a is always divisible by 3, from the start of the program to the end.

//Assert that the following is always true, and print an error message if not true
ensureAlwaysTrue(a % 3 == 0); //print an error message if a is not divisible by 0
                              //from this point onward

a = 6;

a = 10 //print error message, since a % 3 != 0

function ensureAlwaysTrue(){
    //this is the function that I'm trying to implement.
}

One solution would be add statements to check the assertions after every single variable assignment, but that would be redundant and cumbersome. Is there a more concise way to check whether a condition is true throughout a program’s execution?

  • 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-17T20:26:22+00:00Added an answer on June 17, 2026 at 8:26 pm

    Based on the idea from @Chris (http://stackoverflow.com/a/14534019/1394841), you could enclose your whole code in a string, and then do something like:

    function ensureAlwaysTrue(condition, code)
    {
        var statements = code.split(";");
        for (var i = 0; i < statements.length-1; i++) { //last statement is empty
            eval(statements[i] + ";");
            if (!condition()) error();
            //break; //if desired
        }
    }
    
    var code =
    "a = 6;\
    a = 10; //print error message, since a % 3 != 0\
    ";
    
    ensureAlwaysTrue(function(){return (a % 3 == 0);}, code);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Interesting test of Javascript RegExp Regular expression test can't decide between true
Possible Duplicate: jQuery test for whether an object has a method? I want to
Possible Duplicate: How do I test for an empty Javascript object from JSON? var
Possible Duplicate: Calling Python from JavaScript I have a test.py and test.js . I
Possible Duplicate: Javascript regex returning true.. then false.. then true.. etc var r =
Possible Duplicate: How do I test for an empty Javascript object from JSON? Is
Is it possible to send a javascript function to test if a form field
Possible Duplicate: Javascript array sort and unique I was surprised to see that there
Possible Duplicates: Test for value in Javascript Array Best way to find an item
Possible Duplicate: Javascript infamous Loop problem? I have the following code: function test() {

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.