Lädt...


🔧 JavaScript: Arrays, Array Properties, Array Methods: push, pop, shift, unshift, Stacks, and Queues!


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Arrays

Arrays are ordered collections of values/variables that are stored sequentially. They are also considered to be objects.

I welcome you to play around with arrays in the console as you read this article. On a mac, you can press command + option + j to open console in Chrome. 🎉

// To make an empty array
let arr = [];

// To make an array of strings
let friends = ['Jim', 'Linda', 'Jerry'];

// To make an array of numbers
let ranNums = [35, 93, 2, 53];

// Arrays can also contain a mix of types
let whoKnows = ['String', 3, NaN, null];

Accessing Elements in an array

Let's say we have

let arr = [0, 1, 2, 3, 4];

as well as

let arr2 = [39, 84, 24, 94, 35];

Arrays have indices that start at 0. To access the first element in an array we would call arr[0] and to access the second we would call arr[1] and so forth. If we try to access an index that does not exist in an array, say arr[839], we will get undefined.

For example:

console.log(arr2[2]); // 24
console.log(arr2[934]); // undefined

Updating an array

If we want to update an item at a specific index, we can do something like:

arr2[0] = 99;
console.log(arr2); // [99, 84, 24, 94, 35]

Array Properties

We can check the length of an array as follows

let cats = ['Toby', 'Abigale', 'Bob'];
console.log(cats.length); // 3

Array Methods

Before we jump into array methods, a great way to think of an array is like a line of people waiting to get into a theme park. Let's say we have:

let line = ['Abby', 'Jim', 'Timmy'];

We can think of this such that Abby is first in line, then Jim, then Timmy.

If we want to add someone to the end of the line (array) we can call the .push() method:

line.push('Ethan');
console.log(line); // ['Abby', 'Jim', 'Timmy', 'Ethan']

Now, if Ethan got tied and wanted to leave the line we could remove him from the end of the line (array) by calling .pop():

line.pop();
console.log(line); // ['Abby', 'Jim', 'Timmy']

As Abby enters the theme park, we may want to remove her from the front of the line (array) by calling .shift()

line.shift();
console.log(line); // ['Jim', 'Timmy']

Jim and Timmy are waiting patiently in line, but Samuel is a total butthead and decides to cut the line (array) by calling .unshift()

line.unshift('Samuel');
console.log(line); // ['Samuel', 'Jim', 'Timmy']

Now, Ethan gets back in line with his friend Caitlyn. They can be appended to the line (array) together by calling .push()

line.push('Ethan', 'Caitlyn');
console.log(line); // ['Samuel', 'Jim', 'Timmy', 'Ethan', 'Caitlyn']

Stacks and Queues

Congratulations! Without realizing it, you learned how stacks and queues work.

A stack can be thought of like a stack of plates where you .push() and .pop() from the top (end of the array).

A queue can be thought of like a line where you .push() people onto the end of the line (array) and shift() them out of the front of the line (array).

...

🔧 JavaScript: Arrays, Array Properties, Array Methods: push, pop, shift, unshift, Stacks, and Queues!


📈 174.89 Punkte
🔧 Programmierung

🔧 The difference between Push, Pop, Shift and Unshift in Javascript Array


📈 85.34 Punkte
🔧 Programmierung

🔧 Mastering TypeScript: Implementing Push, Pop, Shift, and Unshift in Tuples


📈 69.34 Punkte
🔧 Programmierung

🔧 Lists, Stacks, Queues, and Priority Queues


📈 59.8 Punkte
🔧 Programmierung

🔧 Implementing Lists, Stacks, Queues, and Priority Queues - Common Features for Lists


📈 59.8 Punkte
🔧 Programmierung

🔧 Discovering JavaScript's Hidden Secrets: Understanding Stacks and Queues as Linear Data Structures.


📈 46.16 Punkte
🔧 Programmierung

🔧 Queues and Priority Queues


📈 41.98 Punkte
🔧 Programmierung

🪟 Xbox Cloud Gaming queues blow up under the strain of Palworld, with 40+ minute access queues reported


📈 40.32 Punkte
🪟 Windows Tipps

🔧 Stacks and Queues


📈 39.64 Punkte
🔧 Programmierung

🔧 Building a Monty Bytecode Interpreter: A Hands-On Approach to Learning Stacks and Queues


📈 39.64 Punkte
🔧 Programmierung

🕵️ I made a puzzle in my game to teach Stacks/Push/Pop


📈 39.52 Punkte
🕵️ Reverse Engineering

🔧 Learn the JavaScript Array.every() and Array.some() methods


📈 38.13 Punkte
🔧 Programmierung

🔧 JavaScript Array Methods, String Methods, & Math.random()


📈 38.02 Punkte
🔧 Programmierung

🔧 Introduction to Stacks & Queues in PHP


📈 37.99 Punkte
🔧 Programmierung

🔧 LIFO or FIFO? Guide to Stacks/Queues


📈 37.99 Punkte
🔧 Programmierung

🔧 JavaScript Array Tutorial – Array Methods in JS


📈 36.48 Punkte
🔧 Programmierung

🔧 JavaScript: Strings, String Methods, and String Properties


📈 34.92 Punkte
🔧 Programmierung

🔧 How to Filter an Array in JavaScript – JS Filtering for Arrays and Objects


📈 33 Punkte
🔧 Programmierung

🔧 Navigating Arrays in JavaScript with is.array and is.not_array: Your Companion in List Adventures


📈 33 Punkte
🔧 Programmierung

📰 ECMAScript 2023 Spec for JavaScript Includes New Methods for Arrays


📈 32.88 Punkte
📰 IT Security Nachrichten

🔧 How to Access Properties from an Array of Objects in JavaScript


📈 31.73 Punkte
🔧 Programmierung

🔧 Removing object properties from nested array of objects in JavaScript


📈 31.73 Punkte
🔧 Programmierung

🔧 Get specific properties from an array of Objects in JavaScript


📈 31.73 Punkte
🔧 Programmierung

🔧 Python list.pop() – How to Pop an Element from a Array


📈 31.59 Punkte
🔧 Programmierung

🔧 Mastering CSS Custom Properties: The Senior Developer's Approach to CSS Custom Properties.


📈 31.47 Punkte
🔧 Programmierung

🔧 How to Split an Array Into a Group of Arrays in JavaScript


📈 31.34 Punkte
🔧 Programmierung

📰 JavaScript Arrays – JS Array Funktionen erklärt (Cheatsheet)


📈 31.34 Punkte
Web Tipps

🔧 How to Merge Arrays in JavaScript – Array Concatenation in JS


📈 31.34 Punkte
🔧 Programmierung

🐧 Map an Array of Arrays – JavaScript


📈 31.34 Punkte
🐧 Linux Tipps

🔧 Adventures with JavaScript Arrays: Random Numbers Array


📈 31.34 Punkte
🔧 Programmierung

🔧 Adventures with JavaScript Arrays: Sorting an Array of Numbers


📈 31.34 Punkte
🔧 Programmierung

🔧 JavaScript 2D Array – Two Dimensional Arrays in JS


📈 31.34 Punkte
🔧 Programmierung

🔧 JavaScript Arrays 🔥 Array Awesomeness


📈 31.34 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Array Methods: forEach, map, filter, reduce, and find


📈 28.66 Punkte
🔧 Programmierung

matomo