JavaScript Kurs

JavaScript Arrays

Python 3 Tutorial für Anfänger.

Creating Arrays

const fruits = ["apple", "banana", "cherry"];
const numbers = [1, 2, 3, 4, 5];
const mixed = [1, "hello", true, null];

Accessing Elements

console.log(fruits[0]);        // apple
console.log(fruits[fruits.length - 1]); // cherry
console.log(fruits.at(-1));    // cherry (ES2022)

Essential Array Methods

// Add / Remove
fruits.push("date");            // add to end
fruits.pop();                   // remove from end
fruits.unshift("avocado");      // add to beginning
fruits.shift();                 // remove from beginning

// Find
fruits.indexOf("banana");       // 1 (or -1 if not found)
fruits.includes("cherry");      // true

// Transform
const upper = fruits.map(f => f.toUpperCase());
const long  = fruits.filter(f => f.length > 5);
const total = numbers.reduce((sum, n) => sum + n, 0);

Spread Operator

const a = [1, 2, 3];
const b = [4, 5, 6];
const combined = [...a, ...b]; // [1,2,3,4,5,6]