0

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);
}
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
Zachooz
  • 535
  • 1
  • 12
  • 25
  • 1
    possible duplicate of [What's wrong with var x = new Array();](http://stackoverflow.com/questions/885156/whats-wrong-with-var-x-new-array) – Blender Jul 23 '13 at 20:11
  • 1
    As for your second question: [string.charAt(x) or string\[x\]?](http://stackoverflow.com/questions/5943726/string-charatx-or-stringx) – Blender Jul 23 '13 at 20:12
  • 1
    Also `var arrayname = array[];` is not valid JavaScript syntax. – voithos Jul 23 '13 at 20:12
  • 1
    A string is nothing more than a char array. It just has a wrapper class around it called String to make it transparent to you. – ObieMD5 Jul 23 '13 at 20:15
  • oops I made a mistake I meant var arrayname = []; – Zachooz Jul 23 '13 at 20:53
  • yea blender it was similar to What's wrong with var x = new Array(); – Zachooz Jul 23 '13 at 20:55

1 Answers1

1

In javascript, and most languages String implements array like properties to allow you to work with the individual characters. However, in javascript it should be made clear that String is distinct from Array and in fact properties like length are read only on String, but are editable on Array.

The code:

var arrayname = new Array(); 

Dimensions the arrayname object as an instance of the Array class. Since the Array class provides the functionality of having a length and allows you to iterate through its members, you can now immediately start assigning values in javascript like:

arrayname[0] = "test";

But this is not the only way to dimension an array. There are also shorthand versions. Even the following will work:

var fieldCount = resultSet.fieldCount;
var fields = [];
for(var i = 0; i < fieldCount;i++) {
    fields.push(resultSet.fieldName(i));
};

So while there are many shorthand methods, these are basically just different ways of creating the same object, an instance of type Array.

But as pointed out by others I am not familiar with the syntax you used:

var arrayname = array[];

I don't think it is correct, but I haven't tested it for sure. It is possible this came from a source that was not javascript.

Ted
  • 3,212
  • 25
  • 20
  • Yea I made a mistake because I'm new to arrays I meant var arrayname = []; instead of var arrayname = array[]; – Zachooz Jul 23 '13 at 20:56