Declaring an Array
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 57 of 77.
An array is a collection of items, and it can contain values of different types, such as numbers, strings, or even other arrays. Arrays are created using square brackets [], and the items inside the array are separated with commas.
Here is an example of how to create an array:
let myArray = [1, 2, "three", true];To check the length of the array, we can use the length property:
let length = myArray.length;The variable length will hold 4 because there are 4 elements in the array.
Challenge
EasyCreate an array called shoppingList that contains the following items: bread, eggs, milk, and butter.
Output the array in the end.
Cheat sheet
Arrays are collections of items created using square brackets [] with items separated by commas:
let myArray = [1, 2, "three", true];To get the length of an array, use the length property:
let length = myArray.length;Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants11Arrays Part 1
Declaring an ArrayAccessing Array ElementsModifying ArraysArray Methods Part 1Array Methods Part 2Recap - Array Processor3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False