GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Docs
  • Types
  • Array

This view

  • Original page
  • New address

GeezerVison is showing maker.makecode.com — Array

Skip to main content
About FAQ Projects GitHub
Reference
music pins control serial
Blocks
On Start
Loops
repeat for while for of
Logic
if Boolean
Variables
assign change var var
Math JavaScript blocks Custom blocks
Python
Calling Sequencing Variables Operators Statements Functions Classes
JavaScript
Calling Sequencing Variables Operators Statements Functions Types Classes Interfaces Generics
Types
Number String Boolean Array Function
Miscellaneous
About Support Translate Sharing projects Offline support Save Reset Beta version Home page content
Developers
Command Line Interface Visual Studio Code support Blocks Embed

Array

An Array is a list of other items that have a basic (primitive) type.

An array is a list of items that are numbers, booleans, or strings. Arrays have a length which is the number of items they contain. You get and change the values of items at different places in an array. You find items in an array by knowing their positions.

Arrays are flexible, they can grow and shrink in size. You can add and remove items at any place in the array.

Create an array

An array is created by making a list of items.

let scores = [9, 8, 3, 5, 6, 8];

Here the array automatically becomes an array of numbers because it was created with items that are numbers.

You can use different types, like string.

let directions = ["East", "North", "South", "West"];

If you want to create an array with no items in it yet (empty array), you have to tell what type the array should be. This is because there are no items in it that automatically decide the type.

let scores: number[] = [];

Items in an array

When an item is added to an array it becomes an element of the array. Array elements are found using an index. This is the position in the array of the element, or the element's order in the array. The positions in an array begin with the index of 0. The first place in the array is 0, the second place is 1, and so on.

let scores = [9, 8, 3, 5, 6, 8];
let firstScore= scores[0];
let secondScore = scores[1];

Length of arrays

Arrays always have a length. The last element of an array has an index that is length of array - 1. Indexes begin with 0 so the last element's index is one less than the array length.

let directions = ["East", "North", "South", "West"];
let count = directions.length;

Advanced

Arrays are a very useful way to collect and organize information. There are more advanced operations and functions you can use on arrays.

Here's an example using insertAt to insert a number into the middle of an array.

let scores = [8, 5, 9, 3, 2, 4];
scores.insertAt(3, 1);

See also

Edit this page on GitHub
Edit template of this page on GitHub
Edit template of this page on GitHub

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.