Javascript - List (array) length and getting an item from the list not working

456 Views Asked by At

I'm practicing JavaScript (just started this week) on vscode with the Quokka.js extension. Right now I'm initializing a list called "things" and trying to get the length of the list and getting some items out of it. This is my code:

var things = ['bananas', 'apples', 7];
things.length;
things[0];

The last two rows return nothing to me, not even "undefined". How do I get vscode to return the length and the first object from the list using [0]? If it is not possible in vscode, what program should I use for learning JavaScript?

I also tried initializing the list as an array with

Array things = ['bananas', 'apples', 7];

but this does not seem to be allowed. Moreover, for example the command

things.splice

seems to work in vscode.

1

There are 1 best solutions below

1
On BEST ANSWER

Even if you're using Quokka, it's better to output using console.log. Quokka works very well with console.log.

Also try not to use var or declare array using Array. This is JavaScript, not Java.

// Do not use var
let things = ['bananas', 'apples', 7];
console.log(things.length);
console.log(things[0]);
// This will not work
// This does not make any sense either
Array things = ['bananas', 'apples', 7];

JavaScript Array is not Class or an interface by using which you can declare it's instances. JavaScript Array is a global object. There are no classes in JavaScript.