- Getting Started
- Variables and Types
- Arrays
- Manipulating Arrays
- Operators
- Conditions
- Loops
- Objects
- Functions
- Callbacks
-
Object-Oriented JavaScript - Function Context
- Inheritance
Based on Javascript Tutorial
Arrays
Collapse Content
Show Content
JavaScript can hold an array of variables in an Array object. In JavaScript, an array also functions as a list, a stack or a queue.
To define an array, either use the brackets notation or the Array object notation:
var myArray = [1, 2, 3];
var theSameArray = new Array(1, 2, 3);
Addressing
We can use the brackets []
operator to address a specific cell in our array. Addressing uses zero-based indices, so for example, in myArray
the 2nd member can be addressed with index 1.
print(myArray[1]); // prints out 2
Arrays in JavaScript are sparse, meaning that we can also assign variables to random locations even though previous cells were undefined. For example:
var myArray = []
myArray[3] = "hello"
print(myArray);
Will print out:
[undefined × 3, "hello"]
Challenge
Define an array with the following three variables:
- A string set to "What is the meaning of life?"
- A number set to
42
- A boolean set to
true
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.