Arrays

Before we go over objects, which are the most common datatype when sending and receiving information between applications (back-end to front-end, front-end to back-end, or back-end to websites, etc.), and this is especially true when interacting with the Algorand blockchain, I think it's important to have an understanding of how arrays work.

Below is an example of an array:

let myArray = [7, "Hello", false, 63.5];

An array can hold all types of data, and you can have all kinds of data in one array. You use arrays when you need exactly that, a list. Arrays are also mutable, as opposed to tuples—which means we can rearrange, extend, and replace items in an array, meaning they're super flexible!

An array is a collection of data enclosed between square brackets [ ], and separated by commas. An example of common kinds of data you would have in an array on Algorand could be one that has asset IDs.

let asset_ids = [1265975021, 1138500612, 400593267];

Or perhaps an array of addresses:

let addresses = [
'WWYUMYPM2Y5NIIZTF4O5N73A4ZTZQWXS6TNP23U37LQ6WWF543SRTGKWUU',
'7IWZ342UGNQ2JVS2E6EGFD4MPUNL4ZIWDYNFZIANR6U7WZXORCRQCCN3YY',
'HZ57J3K46JIJXILONBBZOHX6BKPXEM2VVXNRFSUED6DKFD5ZD24PMJ3MVA'];

Although arrays don't have to be organized in any way and are not descriptive, they can be manipulated. For example, if you wanted to remove duplicate entries in an array, you could use the Set object.

let myArray = [1, 1, 2, 3, 4, 4];
let myArrayWithoutDuplicates = [...new Set(myArray)];
console.log(myArrayWithoutDuplicates);

# Output:# [1, 2, 3, 4]

Let's look back at the first example of an array:

let myArray = [7, "Hello", false, 63.5];

In the array above we have an integer at the first spot, 7; a string in the second spot, "Hello"; a boolean (true or false value) in the third spot; a float (decimal value), in the fourth spot. I refer to the places these items are in the array as "spots", but the correct term is actually "indexes". We referenced them as the first, second, third, and fourth spot— however, in programming arrays are zero-indexed. This means that we always start from zero, and use an integer to refer to their position in the array. This feels strange, but it is something you should have ingrained into your mind, as this is universal across all programming when indexing for positions in an array.

The correct reference to the positions would be Index 0 for 7, Index 1 for "Hello", Index 2 for false, and Index 3 for 63.5. But, how would we see this utilized in a programming scenario?

To interact with the array, we must first assign it to a variable:

let myArray = [7, "Hello", false, 63.5];

Now, we can use index notation to pick out items of our choice by using their position, let's start with just logging the array to the terminal:

console.log(myArray);

# Output: [7, "Hello", false, 63.5]

... and now let's log the item at index 1 (the second item since the first item is always 0) using index notation:

let firstIndexMyArray = myArray[1];
console.log(firstIndexMyArray);

# Output: "Hello"

Try logging the third index into the console using index notation. I've already defined the array for you below. Click run when you're ready to run the code! The output should be 63.5.

IDE WINDOW: let myArray = [7, "Hello", false, 63.5]; . . . CHECK OUTPUT IS 63.5 AND SHOW "SUCCESS"

Next chapter: Objects


Quiz

Question 1

What data structure is used to store a collection of values in JavaScript?





Question 2

Which of the following correctly initializes an array in JavaScript?





Question 3

What will be the output of the following code?

let myArray = [7, "Hello", false, 63.5];
console.log(myArray[2]);




Question 4

How do you remove duplicate entries from an array in JavaScript?

let myArray = [1, 1, 2, 3, 4, 4];
let myArrayWithoutDuplicates = [...new Set(myArray)];
console.log(myArrayWithoutDuplicates);




Code Editor