Discuss Scratch
- Znapi
-
500+ posts
Is there JavaScript lists?
Arrays in JS are more like lists. You can push, pop, iterate, etc. In other languages, arrays and lists are different, where an array does have a fixed number of elements and a list does not.But this code works… Also unlike lists, arrays have a fixed amount of items.var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
- Znapi
-
500+ posts
Is there JavaScript lists?
Sorry for the double post, but I have a question. In the above code, are the elements 3-99 also created when `array[100]` is created? Or is `array[100]` just creating a single property with the key 100, and the array has a huge gap?var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
Last edited by Znapi (Sept. 9, 2015 01:48:36)
- Jonathan50
-
1000+ posts
Is there JavaScript lists?
Try running the code, 3-99 are all undefinedSorry for the double post, but I have a question. In the above code, are the elements 3-99 also created when `array[100]` is created? Or is `array[100]` just creating a single property with the key 100, and the array has a huge gap?var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
- ChocolatePi
-
1000+ posts
Is there JavaScript lists?
Uh, but so is 100Try running the code, 3-99 are all undefinedSorry for the double post, but I have a question. In the above code, are the elements 3-99 also created when `array[100]` is created? Or is `array[100]` just creating a single property with the key 100, and the array has a huge gap?var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);

When you initialize an array using the constructor (new Array(n)), it just creates a bunch of empty slots with nothing in them (undefined). But, an array index out of range will also return undefined. Just remember that it has 100 empty elements and all others (whether they return undefined or not) do not exist.
I know, sometimes Javascript seems like a poorly designed language

- BookOwl
-
1000+ posts
Is there JavaScript lists?
+1 to that!Uh, but so is 100Try running the code, 3-99 are all undefinedSorry for the double post, but I have a question. In the above code, are the elements 3-99 also created when `array[100]` is created? Or is `array[100]` just creating a single property with the key 100, and the array has a huge gap?var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
When you initialize an array using the constructor (new Array(n)), it just creates a bunch of empty slots with nothing in them (undefined). But, an array index out of range will also return undefined. Just remember that it has 100 empty elements and all others (whether they return undefined or not) do not exist.
I know, sometimes Javascript seems like a poorly designed language
- scratchisthebest
-
1000+ posts
Is there JavaScript lists?
sometimes Javascript seems like is a poorly designed languageFtfy. ;D I know,
Note - Javascript is (pretty much) the only language where you can access elements past the end of the list. In Java, you'll get an ArrayIndexOutOfBoundsException, and (iirc) in C you get random garbage data and segmentation faults (but I'm not a C coder so take that with a grain of salt)
What I'm saying is, it's a really, really bad idea to access things past the end of an array, so you really really shouldn't if you don't have to, and you for sure shouldn't rely on it…
- Znapi
-
500+ posts
Is there JavaScript lists?
sometimes Javascript seems like is a poorly designed languageFtfy. ;D I know,
Note - Javascript is (pretty much) the only language where you can access elements past the end of the list. In Java, you'll get an ArrayIndexOutOfBoundsException, and (iirc) in C you get random garbage data and segmentation faults (but I'm not a C coder so take that with a grain of salt)
What I'm saying is, it's a really, really bad idea to access things past the end of an array, so you really really shouldn't if you don't have to, and you for sure shouldn't rely on it…
Javascript seems like a poorly designed languageJS is not like other languages. Every object in JS is keys and values, even arrays. Using `[]` syntax(bracket notation) to reference properties allows you to use objects that can be casted to strings as keys. As I understand it, an array is just a bunch of integer keys with assigned values. You can use bracket notation in your own objects too.
Personally I think JS is a really cool, useful, and unique language because of its object system.
Edits because I was checking my understanding of bracket notation.
Last edited by Znapi (Sept. 9, 2015 23:44:59)