العودة إلى الدورة

Accessing and Modifying Array Elements

JavaScript: الدورة الكاملة للمبتدئين من 'الصفر إلى الاحتراف'

53. Accessing and Modifying Array Elements

Accessing Elements

Use the bracket notation [] with the index number.

javascript const colors = ['Red', 'Green', 'Blue']; let firstColor = colors[0]; // Red

Modifying Elements

Because arrays are mutable (even if declared with const), we can change the value at any index using assignment.

javascript const scores = [85, 92, 78, 65];

scores[2] = 88; // Change the third score (index 2)

console.log(scores); // [85, 92, 88, 65]

Array Length

The .length property returns the number of elements in the array.

javascript const items = ['pen', 'book', 'paper']; console.log(items.length); // Output: 3

// The index of the last element is always length - 1 let lastItem = items[items.length - 1]; // paper