hits1 is an array, its length is equal to the number of elements put into it.
hits1.push(i)puts a number i in it if a match is found in text with the string myName. Theif (hits1.length)decrypted asif ((bool)hits1.length), while (bool) returns false if it has been given a void or 0, and true in other cases. Accordingly, if this length turns out to be zero (i.e. no match is found), the list of matches does not attempt to be output; if not zero, the array is looped.
And why it does not work when you write if(hits.length === 0)