Hits and push work for?


#1

can any one explain me in an easy way what "hits and push" work for please?


#2

@mark_dibe,
If you define a variable
you will assign a Value to this variable

In Javascript they have following Data-types

The VALUE can be anything from:

•Number
•String
•Boolean
•Symbol
•Object  ◦Function
         ◦Array
         ◦Date
         ◦RegExp
         ◦Math
•Null
•Undefined

For each of those Data-types,
Javascript presents you a set of 'predefined functions and they call them Methods.

With

var hits = [];

your created a variable hits with a Data-type-Array being an empty Array.

If you now do a google search using
javascript Array site:developer.mozilla.org
you will reach the =link=
= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
If you open the link,
you will get the official defintion of an Array
but if you look in the left-hand-column under the header Methods
you will find a whole bunch of those Methods,
and if you want to use them, you have to follow a specific-syntax

One of those Methods, is the push()-Method
with which you are able to append new Array-Elements to an existing Array.

Thus if you would use

hits.push("A");
console.log( hits );  //=>output==> ["A"]
hits.push("B");
console.log( hits );  //=>output==> ["A","B"]

In our "Search Text for Your Name"
we are going to use

hits.push( text[j] );