See, This Is Why I Hate Using the Browser Console ("Memory")


#1

So it appears that despite clearing the console with CTRL+L, the console remembers stuff:

var l = 0;
var m = 0;

function nRecursive(n) {
	if (n === 0) {
		console.log(l + m);
	} else {
		l += m++;
		nRecursive(n - 1);
	}
}

nRecursive(5);

Simple code – I should know; I made it myself so by definition it’s “simple,” even if it is slightly recursive! – but then try running nRecursive(3) right after – even if you “clear” the console (or is that “clear screen” only??) with CTRL+L – and you’ll wind up getting 36 instead of 6!

But if you close the tab and open a new one and plug nRecursive(3) back into the console, the proper answer is returned.

So is there any way to “turn off” the console’s “memory”??

Using something like JS.do doesn’t present such problems (which is why there’s usually [until lately] been so much document.write() in my code!!)…

Thank you all again…!


#2

Using global variables can have consequences. Initialize l and m inside the function (not really possible here) or initialize them before each call to the function. Or, one final way is to include them in the argument list…

function nRecursive(l, m, n)
nRecursive(5, 0, 0)

Short of that, refresh the browser before each run.


#3

I don’t think it’s a matter global scope per se as it is simply the browser console being what it is – seems like the console isn’t “sectioned off” from the browser’s “JavaScript engine” which is why there is “memory”…

So now that I think about the matter some more, yes the console “remembers” because it is simply using the browser’s “JS engine” and there’s no reason to “wall off” one from the other because the console was never meant as a teaching tool…

Anyway, thanks for helping me think through this latest puzzle! And thanks for the idea of “argument list inclusion!” Looks niffty; I’ll try it now…


#4

BTW, just for the record: it is “a matter of global scope”…I wasn’t trying to disagree since you are right, strictly speaking; I was just thinking aloud about what more precisely was going on – so yes, it’s about global scopes but more specifically it’s about global scope because of the whole console running off the regular JS engine of the browser (and of course, why wouldn’t it run off the engine and why wouldn’t the engine operate as it normally does)…

See, this is the under-the-hood stuff that one doesn’t learn as a beginner but which could have great impact on a beginner’s mental sanity – so many times I was getting weird results and pulling out my hair over code logic when it’s simply, I now see, this matter of what’s happening “under the hood” or “behind the scenes!” I wish all the damned tutorials using the damned browser console – which is pretty much the whole damned lot of 'em – would put forth this disclaimer and an explanation upfront…


#5

There is only one namespace and it stays alive as long as the session does.

It is a debugging tool, and should be learned right up front.

It’s a question of cart before the horse. The console command line is disabled for a reason, and the explanation would go right over the early learner’s head. Chrome has all the documentation for how to get the most use of the inspection tools, as does Firefox and IE/Edge.

There is only so much information you give a learner before they get overwhelmed and imho the language fundamentals are a greater priority. Personally I try to get users familiar with their home environment as soon as there is evidence of enough wherewithal to stand on their own, but not before.


#6

Well, depends on how it’s explained, I believe; since the console is, in effect, used as a teaching tool – and I totally understand that – it makes more sense to give out a “gentle” (instead of overwhelming) explanation of a disclaimer, such as making sure to “refresh the browser,” as you’d put it, when running code, lest results from previous runs carry over into the current run, thereby really confusing a beginner!

It’s like, I don’t need to know everything about nuclear physics before being assigned to a nuclear attack sub – but by golly, I oughta be told that that innocuous little switch by the exit is the self-destruct mechanism!! :slight_smile:

BTW, just how do you “refresh the browser,” anyway?? You don’t mean shut it down and restart the browser, do you? I mean, CTRL+L doesn’t work, and I know closing and opening a new tab doesn’t work, either…just how’s the console’s memory cleared – if ever???


#7

Click the refresh button in the location bar or press F5. To really be sure you are starting with a fresh namespace, press Ctrl+F5.