JS - Day 6
/// array
let names =['ram', 'sham', 'ganesh', 'mahesh'] // this is the array
console.log(names) // prints array as it is
console.log(names[0]) // prints element at 0 index i.e. ram
let a = names.length
console.log(a) // prints length of array
/// methods of array - push(), unshift(), pop(), shift(), includes(), indexOf()
let cities = ['pune', 'mumbai', 'nagpur']
let b = cities.push('solapur') // put new element at end in array and returns new length of array when printed
console.log(b) // prints new length of array
console.log(cities) // prints arraay with 'solapur' at end
let c = cities.unshift('dharashiv') // put new element at start in array and returns new length of array when printed
console.log(c) // prints new length of array
console.log(cities) // prints array with 'dharashiv' at start
let d = cities.pop() // removes last element from array and returns removed element when printed
console.log(d) // prints removed element
console.log(cities) // prints modified array without 'solapur'
let e = cities.shift() // removes first element from array and returns removed element when printed
console.log(e) // prints removed element
console.log(cities) // prints modified array without 'dharashiv'
let f = cities.includes('pune') // finds if there is 'pune' in array and returns boolean when printed
console.log(f) // prints boolean // in this case 'true'
let g = cities.indexOf('mumbai') // finds the index value of element 'mumbai' in array & prints index value when printed
console.log(g) // prints index value // in this case '1'
Comments
Post a Comment