0

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.

rioV8
  • 24,506
  • 3
  • 32
  • 49
jeppoo1
  • 650
  • 1
  • 10
  • 23
  • 1
    Are you running your code in a browser? And how to you want to visualize the content? Do you want to show it on the webpage or do you want to debug it using something like `console.log` or `alert`? – Evert vd H. Apr 02 '20 at 10:02
  • @EvertvdH. I'm just at the beginning of a very beginner JS course. I actually don't know how I should visualize the content. Do you have any recommendations? A browser (Chrome) was at least used in some of the video lessons. – jeppoo1 Apr 02 '20 at 12:33
  • For now I would just follow whatever the course is using – Evert vd H. Apr 02 '20 at 12:41

1 Answers1

1

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.

Pushkin
  • 3,336
  • 1
  • 17
  • 30