Arrays:
- In JavaScript an arrays is an ordered collection data or values,also know as elements.
- Is assigned numeric position in the array called its index.The index start at 0. So the 1st element position is 0 and second element position is 1 and so on.
Why we use array?
- If you have list of items (list of cars names for example) storing the cars single variables could look like this
let car1 ="Saab"
let car2 ="Volvo"
let car3 = "BMW"
- However,what if you want to loop through the cars and find the specific one? And what if you had not 3 cars,but 300?
- So the solution is Array [] An array can hold many value under the single name variable,and you can access the value by referring to an** index number**
Example:
*`Creating the empty array *
- let a=[] console.log(a) //[]`
`Creating an Array and Initializing with Values
- let b=[10,20,30,40] console.log(b) //[10,20,30,40]`
3. let a = ["HTML", "CSS", "JS"];
console.log(a[0]); //HTML 0 index position
console.log(a[1]);//CSS 1index position
4. let a = ["HTML", "CSS", "JS"];
Accessing Last Array Elements
let list = a[a.length - 1];//JS
**5. Replace CSS instant of Bootstrap Array Elements**
let a = ["HTML", "CSS", "JS"];
a[1] ="Bootstrap"
console.log(a) // [ 'HTML', 'Bootstrap', 'JS' ]
**6. Add the element to the end of array**
let a = ["HTML", "CSS", "JS"];
a.push("Node.js")
console.log(a) // [ 'HTML', 'Bootstrap', 'JS',Node.js ]
**7.Add the element to the beginning**
let a = ["HTML", "CSS", "JS"];
a.unshift("Web development ")
console.log(a) // [ Web development ,'HTML', 'Bootstrap', 'JS',Node.js ]
**8. Check the Type of an Arrays**
let a = ["HTML", "CSS", "JS"];
console.log(typeof a)// Object
**9. Check the an Arrays length**
let a = ["HTML", "CSS", "JS"];
let length =a.length
consloe.log (length) // 3
10. Removing Elements from an Array
Remove the elements from an array we have different methods like pop(), shift(), or splice().
- The pop() method removes an element from the last index of the array.
- The shift() method removes the element from the first index of the array.
- The splice() method removes or replaces the element from the array.
Top comments (0)