I am currently learning JavaScript from a book and codecadmey and have been having a confusing time with arrays. Both sources differ in how they set up arrays.
What are the differences between these:
var arrayname = new array ();
var arrayname = [];
Also in this code below, it seems to me that it is treating the variable text as an array while the for loop searches through it. That also confuses me. How is it that a normal variable can be treated as an array?
It would be great if you could help! Thanks.
var text = "Lorem Zachary ipsum dolor sit amet, consectetur adipiscing elit. Fusce ac dapibus felis, vel interdum ipsum. Sed non justo sed sapien faucibus interdum. Ut vehicula mauris hendrerit, dapibus diam eu, varius leo. Integer eu semper mi, eget feugiat ante. Donec pretium turpis dolor, eu imperdiet nunc vehicula at. Phasellus id mi sodales Zachary eros aliquam venenatis. Nam a eros orci. Sed commodo accumsan sapien, nec rhoncus elit venenatis in. Etiam vitae lorem libero. Quisque porta nibh id mauris auctor laoreet. Nunc porttitor metus et mi luctus hendrerit. Zachary Curabitur quis semper justo. Morbi sed augue commodo, blandit tortor eu, bibendum nibh. ";
var myName = "Zachary";
var hits = [];
for (var i = 0; i<text.length; i++){
if (text[i] === "Z"){
for(var j = i; j< (myName.length + i); j++)
hits.push(text[j]);
}
}
if (hits.length===0){
console.log("your name wasn't found");
}
else{
console.log(hits);
}