முக்கிய உள்ளடக்கத்திற்குச் செல்லவும்
eLearner.app
தொகுதி 3 · பாடம் 1 இன் 4பாடத்திட்டத்தில் 9/32~10 min
தொகுதி பாடங்கள் (1/4)

வரிசைகள்: வரிசைப்படுத்தப்பட்ட பட்டியல்

An array is an ordered list of values. In JavaScript the elements can be of different types from each other (although in "good" code they tend to be homogeneous). It's written with square brackets:

JS
const colori = ['rosso', 'verde', 'blu'];
const misti = [1, 'due', true, null];
const vuoto = [];

Reading and writing by index

Indices start at 0. The last valid index is array.length - 1.

JS
const colori = ['rosso', 'verde', 'blu'];
colori[0]; // 'rosso'
colori[2]; // 'blu'
colori[99]; // undefined  ← no error, just undefined
colori.length; // 3

colori[1] = 'giallo'; // now colori is ['rosso', 'giallo', 'blu']
colori[colori.length] = 'x'; // manual push, not recommended

Adding and removing elements

The four classic methods (push / pop / unshift / shift) modify the array in place:

JS
const a = [1, 2, 3];
a.push(4); // a = [1, 2, 3, 4],  returns the new length
a.pop(); // a = [1, 2, 3],     returns the removed element (4)
a.unshift(0); // a = [0, 1, 2, 3],  adds at the head
a.shift(); // a = [1, 2, 3],     removes from the head

Including, searching

JS
['rosso', 'verde', 'blu'].includes('verde'); // true
['rosso', 'verde', 'blu'].indexOf('blu'); // 2
['rosso', 'verde', 'blu'].indexOf('giallo'); // -1

Try it

உடற்பயிற்சி#js.m3.l1.e1
முயற்சிகள்: 0ஏற்றுகிறது…

Given the array `numbers = [10, 20, 30, 40]`, return as the last expression the last element using length.

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

The last valid index is length - 1.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்

Review exercise

உடற்பயிற்சி#js.m3.l1.e2
முயற்சிகள்: 0ஏற்றுகிறது…

Start from `fruits = ['mela', 'pera']`. Add 'banana' at the end with push, then return the full array as the last expression: ['mela','pera','banana'].

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

push mutates the array; you don't need to reassign.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்