title: How can I detect when a variable changes value in JavaScript?
body: <p>Before I start, I'd just like to say that there are already answered questions on this topic, and I looked them up - but because I'm still somewhat of a beginner, I can't really figure out the answer as it's too complex for me. So I wanted to give my own example, a dead simple one, and try to understand based on that.</p>
  
  <p>Essentially what I want to do is run a function when an already existing variable, with a defined value, changes its value. Something like this:</p>
  
  <pre><code>var testVar = 5;
  function letMeKnow() {
  console.log("The variable has changed!);
  }
  </code></pre>
  
  <p>What I want is, when I go to the console and type, say <code>testVar = 7</code>, I want the <code>letMeKnow()</code> function to run, and print something out in the console. More specifically, I want JavaScript to detect that the variable has changed, and run the function automatically. I know that this used to be possible with <code>Object.observe()</code> or <code>Object.watch()</code>, but since those are deprecated I suppose that I have to use getters and setters to achieve this. My question is, how would I do that for this example, keeping it as simple as possible?</p>
  
  <p>Thanks!</p>