Second for loop -> hits.push(text[j]); statement


The code works but I want to understand it better.


Can someone help me break it down piece by piece? Please correct me if I am wrong.
i - we are looping through the whole text string until we find the value "T'
hits - refers to the array that we created earlier. This is where we will be printing our text
.push - the function to add the data to the array
text - the variable text
[j] - ?

/*jshint multistr:true */
var text = " Hi there. Hi there. Hi there. Hi there. Tyler.\ Hi there. Hi there. Tyler Tyler Tfja Tylaf Tylefffds"

var myName = "Tyler"

var hits = []; 

for (var i = 0; i <= text.length; i++){
    if (text[i] === "T"){
        for (var j = i; j <i + myName.length ; j++) {


If you are not familiar with array mutation, then array.push() will come as a surprise but don't let it throw you. It's simply a built-in method for extending (appending) an array.

var hits = [];     // an empty array

hits.push("H");    // adds a new element to the end of the array

hits.push{"e");    // adds another new element after the first

console.log(hits); // [ 'H', 'e' ]


Maybe if you read