What does if (hits1.length)
? That is, what is this length?
var text = "Walla walla Wee Gillis salt and pepper Wee Gillis peas and carrots Wee Gillis walla walla Wee Gillis salt and pepper Wee Gillis peas and carrots"; var myName = "Wee Gillis"; var hits1 = []; for (var i = 0; i < text.length; i++) { if (text[i] === myName[0]) { if (text.substring(i, i + myName.length) === myName) { hits1.push(i); } console.log("\nsubstring\n"); if (hits1.length) { for (i = 0; i < hits1.length; i++) { console.log(hits1[i], text.substring(hits1[i], hits1[i] + myName.length)); } } } }