Discuss Scratch

Jonathan50
Scratcher
1000+ posts

Is there JavaScript lists?

Firedrake969 wrote:

Arrays are mutable.
Ok
Znapi
Scratcher
500+ posts

Is there JavaScript lists?

Smitop wrote:

Jonathan50 wrote:

Also unlike lists, arrays have a fixed amount of items.
But this code works…
var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
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.
Znapi
Scratcher
500+ posts

Is there JavaScript lists?

Smitop wrote:

var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
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?

Last edited by Znapi (Sept. 9, 2015 01:48:36)

Jonathan50
Scratcher
1000+ posts

Is there JavaScript lists?

Znapi wrote:

Smitop wrote:

var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
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?
Try running the code, 3-99 are all undefined
ChocolatePi
Scratcher
1000+ posts

Is there JavaScript lists?

Jonathan50 wrote:

Znapi wrote:

Smitop wrote:

var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
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?
Try running the code, 3-99 are all undefined
Uh, but so is 100

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
Scratcher
1000+ posts

Is there JavaScript lists?

ChocolatePi wrote:

Jonathan50 wrote:

Znapi wrote:

Smitop wrote:

var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
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?
Try running the code, 3-99 are all undefined
Uh, but so is 100

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
+1 to that!
scratchisthebest
Scratcher
1000+ posts

Is there JavaScript lists?

ChocolatePi wrote:

I know, sometimes Javascript seems like is a poorly designed language
Ftfy. ;D

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
Scratcher
500+ posts

Is there JavaScript lists?

scratchisthebest wrote:

ChocolatePi wrote:

I know, sometimes Javascript seems like is a poorly designed language
Ftfy. ;D

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…

ChocolatePi wrote:

Javascript seems like a poorly designed language
JS 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)

Powered by DjangoBB