Let\'s say I do this:
re = /cat/;
re = /cat/;
From reading Zakas\' book about Javascript, it seems that when executing the second line, no
Im not familiar with the book but this is how it works so far as I understand it.
The var statement creates new variables which have no type and attachs them to the local scope.
var re;
var i;
or
var re,i
The null statement produces a null type object that exists apart.
null
Assigning variables in a var statement just points it to that object but it does not become that object; they are separate things that share a relationship.
var re=null,i;
Using a regex statement creates a new regex object which we may or may not assign to a variable.
/cat/g
or
re=/cat/g
When i reproduce your example it only returns true once in firefox52, it never returns false, but if i assign the return value of the test to another variable, and log it, I get true ten times.
var re=null,i;
for (i=0;i<10;i++){
re=/cat/g;
var x=re.test('catastrophe');
console.log(x)}
//returns true ten times
I think that Zacas is explaining an eccentricity found in some browsers due to their implementation of javascript. Using a regex or any statement should create a new object every time but there are many things called javascript, and a lot of them will reuse objects as often as possible and occasionally lead to strange behaviour that is eventually fixed.
I hope that helps