DEV Community

Cover image for Day 97/100 Donuts to Code
Rio Cantre
Rio Cantre

Posted on • Originally published at dev.to

Day 97/100 Donuts to Code

banner

Intro to Arrays

An array is a data structure that you can use to store multiple values and arrays are also organized.

An array is useful because it stores multiple values into a single, organized data structure.

You can define a new array by listing values separated with commas between square brackets[].

var donuts = ["glazed", "jelly" , "powdered"];
Enter fullscreen mode Exit fullscreen mode

But strings aren't the only type of data you can store in an array. You can also store numbers, booleans... and really anything!

var mixedData = ["abcd", 1, true, undefined, null, "all the things"]; 
Enter fullscreen mode Exit fullscreen mode

You can even store an array in an array to create a nested array!

var arraysInArrays = [[1, 2, 3], ["Julia", "James"], [true, false, true, false]];
Enter fullscreen mode Exit fullscreen mode

Nested arrays can be particularly hard to read, so it's common to write them on one line, using a newline after each comma:

var arraysInArrays = [
    [1, 2, 3], 
    ["Julia", "James"], 
    [true, false, true, false]
]; 
Enter fullscreen mode Exit fullscreen mode

Indexing

Remember that elements in an array are indexed starting at the position 0. To access an element in an array, use the name of the array immediately followed by square brackets containing the index of the value you want to access.

var donuts = ["glazed", "powdered", "sprinkled"];

console.log(donuts[0]); // "glazed" is the first element in the `donuts` array
Enter fullscreen mode Exit fullscreen mode

Pop

Alternatively, you can use the pop() method to remove elements from the end of an array.

var donuts = ["glazed", "chocolate frosted", "Boston creme", "glazed cruller", "cinnamon sugar", "sprinkled", "powdered"];

donuts.pop(); // pops "powdered" off the end of the `donuts` array
donuts.pop(); // pops "sprinkled" off the end of the `donuts` array
donuts.pop(); // pops "cinnamon sugar" off the end of the `donuts` array
Enter fullscreen mode Exit fullscreen mode

With the pop() method you don’t need to pass a value; instead, pop() will always remove the last element from the end of the array.

Also, pop() returns the element that has been removed in case you need to use it.

var donuts = ["glazed", "chocolate frosted", "Boston creme", "glazed cruller", "cinnamon sugar", "sprinkled", "powdered"];

donuts.pop(); // the `pop()` method returns "powdered" because "powdered" was the last element on the end of `donuts` array
Enter fullscreen mode Exit fullscreen mode

Code Snippets

var donuts = ["jelly donut", "chocolate donut", "glazed donut"];

donuts.forEach(function(donut) {
     donut += " hole";
     donut = donut.toUpperCase();
     console.log(donut);
}); 


for (var i = 0; i < donuts.length; i++) {
     donuts[i] += " hole";
     donuts[i] = donuts[i].toUpperCase();
     console.log(donuts[i]);
}
Enter fullscreen mode Exit fullscreen mode

Summary

Received a meaningful comment and it is a great tool of motivation. I'm grateful for the people who are there supporting me.

resources

Discussion (0)