Arrays Session 2 and 3

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

console.

log("Arrays Basics Session 2")

// 1. Array creation using, let array1 = ["A","B","C"];


// 2. Array creation using, let array2 = new Array("D","E","F");

let array1 = ["AB","B","C"];


console.log(array1);

let array2 = new Array('D', 'E', 100);


console.log(array2);

//3. Get any element from array using index, let item = array_name[index]
let item = array2[1];
console.log(item)

//4. Replace existing item in array using, array_name[index] = value


array2[0] = 'X';
console.log(array2)
//5. Add new element in array, array_name[index] = value
array2[3] = 1000;
console.log(array2)

//6. Add new element at random index


array2[10] = "This is weird";
console.log(array2)
let item1 = array2[9];
console.log(item1)

console.log("Arrays Basics Session 3")

//1. Hot to get length of the array. <name of the array>.length


let arr = [1, 2, 3, 4, 5, 6, 7, 8, "A", "B", 10.5];
let length_of_the_array = arr.length;
console.log(length_of_the_array);
console.log(arr)

//2. Append item at the end of array using length


arr[arr.length] = 11;
console.log(arr);
console.log(arr.length);

//3. Append items at the end of the array


let new_length = arr.push(12, 13, 14, 15, 16, 17, 18, "Abhijeet");
console.log(arr);
console.log(new_length);

//4. Iteration over the array


// 4.1 Classic for
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
console.log(item);
}
// 4.2 for..of loop
// for (let <variable name> of <array name>) {}
for (let item of arr) {
console.log(item);
}

You might also like