Back to JavaScript Cheat Sheet

JavaScript Array Cheat Sheet

Master JavaScript arrays with this comprehensive cheat sheet. Learn array methods, syntax, and best practices for working with arrays in JavaScript.
6 min readLogan FordLogan Ford

Learn JavaScript: Free JavaScript Course & Array Cheat Sheet

If you're looking for the best free JavaScript course or a JavaScript online course to improve your skills, you're in the right place. This JavaScript training guide covers essential JavaScript array methods, performance optimization, and common interview questions. Whether you're a beginner looking for a JavaScript beginner course or an advanced developer, this tutorial will help you learn JavaScript online for free!

JavaScript Arrays: The Basics

An array is a fundamental data structure in JavaScript used to store multiple values in an ordered list. Learning about arrays is crucial for web development, as they help in managing and manipulating data efficiently.

Declaring Arrays in JavaScript

1const numbers = [1, 2, 3, 4, 5];
2
3const fruits = ['apple', 'banana', 'cherry'];

Common JavaScript Array Methods

JavaScript provides various built-in methods for manipulating arrays. Mastering these will help you in JavaScript programming classes and real-world projects.

Adding & Removing Elements

  • push() – Adds elements to the end.

  • pop() – Removes the last element.

  • unshift() – Adds elements to the beginning.

  • shift() – Removes the first element.

1const arr = [1, 2, 3];
2
3arr.push(4); // [1, 2, 3, 4]
4
5arr.pop(); // [1, 2, 3]
6
7arr.unshift(0); // [0, 1, 2, 3]
8
9arr.shift(); // [1, 2, 3]

Iterating Over Arrays

  • forEach() – Loops through each item.

  • map() – Creates a new array by applying a function to each element. Read more about map.

  • filter() – Returns a new array with elements that meet a condition. Read more about filter.

  • reduce() – Reduces an array to a single value. Read more about reduce.

1const numbers = [1, 2, 3, 4, 5];
2
3numbers.forEach(num => console.log(num));
4
5const doubled = numbers.map(num => num * 2);
6
7const evens = numbers.filter(num => num % 2 === 0);
8
9const sum = numbers.reduce((acc, num) => acc + num, 0);

Searching & Transforming

  • find() – Finds the first matching element.
  • some() – Checks if at least one element meets a condition.
  • every() – Checks if all elements meet a condition.
  • indexOf() – Finds the index of an element.
  • includes() – Checks if an element exists.
1const nums = [10, 20, 30, 40];
2
3console.log(nums.find(n => n > 25)); // 30
4
5console.log(nums.includes(20)); // true
6
7console.log(nums.indexOf(30)); // 2

Sorting & Reversing

  • sort() – Sorts an array (modifies the original array).
  • reverse() – Reverses the array order.
  • toSorted() (ES2023) – Returns a sorted copy without modifying the original.
1const scores = [40, 10, 100, 50];
2
3scores.sort((a, b) => a - b); // [10, 40, 50, 100]

Removing Duplicates

1const uniqueNumbers = [...new Set([1, 2, 2, 3, 4, 4, 5])]; // [1, 2, 3, 4, 5]

JavaScript Performance Tips

Optimizing your JavaScript code can improve efficiency, especially in web applications and server-side development.

  1. Use push() instead of unshift()push() is faster since it avoids shifting indexes.

  2. Use Set for unique values – More efficient than filtering duplicates manually.

  3. Use map() and reduce() wiselymap() creates a new array, while forEach() modifies in place.

  4. Sort with compare functions – Default sort() converts values to strings, causing incorrect ordering.

JavaScript Array Interview Questions

If you're taking a JavaScript programming course or preparing for an interview, practice these questions:

JavaScript Array Interview Questions

If you're taking a JavaScript programming course or preparing for an interview, practice these questions:

  1. Reverse an array in place – without using .reverse().

  2. Find the missing number in a sequence.

  3. Implement a custom map function that mimics Array.prototype.map.

  4. Rotate an array by k positions.

  5. Find the most frequent element in an array.

Additional JavaScript Resources

Looking for the sites to learn JavaScript or JavaScript resources? Check these out:

Where to Learn JavaScript for Free

If you're asking "where can I learn JavaScript?", try our free JavaScript tutorial. This JavaScript online class will help you master JavaScript programming step by step.


TechBlitz is the best site to learn javascript, our free course teaches you HTML, CSS and JavaScript. Ensuring you learn the basics and become a better JavaScript developer in no time!

Learn to code, faster

Join 650+ developers who are accelerating their coding skills with TechBlitz.

Share this article