The .length property tells you how many elements are in a JavaScript array.
const numbers = [1, 2, 3, 4]; console.log(numbers.length); // Output: 4
Array elements are accessed by their index, which starts at 0. Use the array name followed by square brackets with the index inside to get an element.
const myArray = [100, 200, 300]; console.log(myArray[0]); // Output: 100 console.log(myArray[1]); // Output: 200 console.log(myArray[2]); // Output: 300
The .push() method adds one or more elements to the end of an array and returns the new length of the array.
const cart = ['apple', 'orange']; cart.push('pear'); console.log(cart); // Output: ['apple', 'orange', 'pear'] const numbers = [1, 2]; numbers.push(3, 4, 5); console.log(numbers); // Output: [1, 2, 3, 4, 5]
The .pop() method removes the last element from an array and returns it.
const ingredients = ['eggs', 'flour', 'chocolate']; const poppedIngredient = ingredients.pop(); console.log(poppedIngredient); // Output: 'chocolate' console.log(ingredients); // Output: ['eggs', 'flour']
JavaScript arrays are mutable, meaning you can change the contents even if the array is declared with const.
const names = ['Alice', 'Bob']; names.push('Carl'); console.log(names); // Output: ['Alice', 'Bob', 'Carl']
Arrays can hold different types of data and their contents can be changed using methods like .push() and .pop().
const numberArray = [0, 1, 2, 3]; const mixedArray = [1, 'chicken', false]; console.log(numberArray); // Output: [0, 1, 2, 3] console.log(mixedArray); // Output: [1, 'chicken', false]
A for loop can go through an array in reverse by starting with the last index and decreasing the index with each iteration.
const items = ['apricot', 'banana', 'cherry']; for (let i = items.length - 1; i >= 0; i--) { console.log(`${i}. ${items[i]}`); } // Output: // 2. cherry // 1. banana // 0. apricot
A do...while loop runs a block of code once before checking if a condition is true. If the condition is true, it repeats the loop. This loop always runs at least once.
let x = 0; let i = 0; do { x += i; console.log(x); i++; } while (i < 5); // Output: // 0 // 1 // 3 // 6 // 10
A for loop allows you to repeat a block of code a specific number of times. It uses three parts: the initialization, the condition, and the increment.
for (let i = 0; i < 4; i++) { console.log(i); } // Output: 0 // Output: 1 // Output: 2 // Output: 3
You can loop through each item in an array using its .length property to determine when to stop.
const array = ['a', 'b', 'c']; for (let i = 0; i < array.length; i++) { console.log(array[i]); } // Output: // 'a' // 'b' // 'c'
The break keyword lets you exit a loop before it finishes all its iterations, continuing the code after the loop.
for (let i = 0; i < 99; i++) { if (i > 5) { break; } console.log(i); } // Output: // 0 // 1 // 2 // 3 // 4 // 5
A nested loop is when one loop runs inside another loop. The inner loop will complete all its iterations for each iteration of the outer loop.
for (let outer = 0; outer < 2; outer++) { for (let inner = 0; inner < 3; inner++) { console.log(`${outer}-${inner}`); } } // Output: // '0-0' // '0-1' // '0-2' // '1-0' // '1-1' // '1-2'
Loops allow you to repeat a block of code until a certain condition is met. They are essential for automating repetitive tasks.
let i = 0; while (i < 5) { console.log(i); i++; } // Output: // 0 // 1 // 2 // 3 // 4
A while loop keeps running as long as its condition is true. It's useful when you don't know how many times you need to repeat the code in advance.
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!