Topic 4: JS array & methods of array - 1 to 7

//Revision Lecture 4: 28 Sep 2022

//Topic 4: //*JS array & methods of array *//

//*A) array:

//array: an array is a special variable, which can hold more than one value
//array has index numbers to its values/elements, starting from 0
//and you can access the values/elements by referring to an index number.
//array can be created in two ways: 1) using square brackets 2) using 'Array' keyword


//1) array creation using square brackets:

//             0       1        2         3
let names = ['raj', 'samir', 'ganesh', 'jayesh']
console.log(names)

//2) array creation using 'Array' keyword:

//                      0          1          2
let days = new Array('Monday', 'Tuesday', 'Wednesday')
console.log(days)


//*B) methods of array:

//               0         1        2         3
let fruits = ['apple', 'chikoo', 'mango', 'banana']


//methods of array 1 to 7:


// 1) at(): returns the element by its index

let a1 = fruits.at(0)
console.log(a1) 
// output--> apple

let a2 = fruits.at(2)
console.log(a2) 
// output--> mango

let a22 = fruits.at(3)
console.log(a22) 
// output--> banana

// 2) includes(): checks if an element is present in array and return boolean value

let a3 = fruits.includes('mango')
console.log(a3) 
// output--> true

let a4 = fruits.includes('chikoo')
console.log(a4) 
// output--> true

let a5 = fruits.includes('pineapple')
console.log(a5) 
// output--> false

// 3) reverse(): reverse the elements 

let numbers = [1,2,3,4,5]

let a6 = numbers.reverse()
console.log(a6) 
// output--> [5,4,3,2,1]

// 4) push(): adds element at end of array

let a7 = numbers.push('kedar')
console.log(a7) 
// output--> new length of array
console.log(numbers)
// output--> updated array--> [5,4,3,2,1, 'kedar']

numbers.push('ashish')
console.log(numbers)
// output--> updated array--> [5,4,3,2,1, 'kedar', 'ashish']

// 5) unshift(): adds element at start of array

numbers.unshift('tuljapur')
console.log(numbers)
// output--> updated array--> ['tuljapur', 5,4,3,2,1, 'kedar', 'ashish']

// 6) pop(): removes last element of array

numbers.pop()
console.log(numbers)
// output--> updated array--> ['tuljapur', 5,4,3,2,1, 'kedar']

// 7) shift(): removes first element of array

numbers.shift()
console.log(numbers)
// output--> updated array--> [5,4,3,2,1, 'kedar']

numbers.shift()
console.log(numbers)
// output--> updated array--> [4,3,2,1, 'kedar']

//-----------------------------

टिप्पण्या