Listing Things

At times it’s quite handy to be able to describe a list of things. In JavaScript, lists are made with square brackets. A list of amazing movies might look something like this:

 
var​ amazing_movies = [
 
'Star Wars'​,
 
'The Empire Strikes Back'​,
 
'Indiana Jones and the Raiders of the Lost Ark'
 
];

The things in the list can be any of the kinds of things that we have talked about so far: strings, numbers, Booleans. It is even possible to make a list with various types of things:

 
// Don't do this:
 
var​ useless_list = [
 
true,
 
3.14,
 
'OK'
 
];

But don’t do that. It’s silly. Just like in real life, computer lists should contain the same kinds of things. It wouldn’t make sense to include your favorite color, the time ...

Get 3D Game Programming for Kids now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.