Table of contents
- Array:
- LET CREATE ARRAY
- Let's Learn the method of Array
- 1. To find the length of an array we use the
- length ()method
- 2. Finding the index of items we use the index of() method
- 3. To Add items to the array, we use
- b. For the add items at the start of the array list, we have the
- 4. To Remove items from the array, we use
- 5.Accessing every item IN ARRAY WE HAVE
- methods for...of
Array:
we can say that array is a list -like-object. hey, wait what do you mean by a list-like -object? let's have patience. At the end of this article, you will understand. It's my promise.
As we know an array holds value or we can say stores Multiple values the under an umbrella or list better we can say it's a list. The array uses the ‘ [ ] symbol ’ to declare an array in JavaScript.
This is the core understanding of Array :
Array objects can be stored in variables and dealt with in much the same way as any other type of value, the difference being that we can access each value inside the list individually, and do super useful and efficient things with the list, like loop through it and do the same thing to every value.
LET CREATE ARRAY
let's create a variable name car and store value in it. The array uses the ‘ [ ] symbol ’ to declare an array in JavaScript.
In the array, we can store various data types — strings, numbers, objects, and even other arrays.
let cars = ['Tesla', 'Maruti', 'BMW', 'Honda', 'Ford', 'Hyundai', 'Nissan', 'Porsche' 'AXM'];
const random = ['cars', 795, [0, 1, 2]];
console.log(cars)
Let's Learn the method of Array
1. To find the length of an array we use the
length ()method
For finding the length of an array we use the length () method.
let's understand the example
const shopping = ['bread', 'milk', 'cheese', 'hummus', 'noodles'];
console.log(shopping.length);
we have declared a variable name shopping and stored the value in it.
For the print length of the index, we have used the length() method.
2. Finding the index of items we use the index of() method
let birds = ['Parrot', 'Falcon', 'Owl' 'bat'];
console.log(birds.indexOf('bat')); // 3
console.log(birds.indexOf('Rabbit')); // -1
you will think that in the bird variable no rabbit is stored then how is the value showing? And your question is valid
let's answer your question indexof() method takes array items as arguments if the item is found it returns with an index number and if not it will return -1.
3. To Add items to the array, we use
a. Push() Method.
to add one or multiple items we use the push() method to add list item to the array.
const cities = ['DHANBAD', 'Ranchi'];
cities.push('kolkata');
console.log(cities);
output:[ "DHANBAD", "Ranchi", "kolkata ]
and you are happy to know that you can control the item to add the start of array or end of the array let's learn it
b. For the add items at the start of the array list, we have the
Method unshift().
let cities = ['DHANBAD', 'Ranchi'];
cities..unshift('kolkata');
console.log(cities);
output:[ "kolkata","DHANBAD", "Ranchi", ]
4. To Remove items from the array, we use
a. Pop() Method.
To remove the last item from the array, use pop() .
let cities = ['DHANBAD', 'Ranchi'];
cities.pop();
console.log(cities);
output:[ "Ranchi" ]
to save the item into new variable we can do this
let cities = ['DHANBAD', 'Ranchi'];
let removecities =(cities.pop());
console.log(removecities);
b. To remove the first item from an array, use
method shift():
let cities = ['DHANBAD', 'Ranchi','kolkata'];
cities.shift();
let removecities =(cities.shift());
console.log (removecities);
console.log(cities);
output:
DHANBAD
[ 'Ranchi' ]
c.To Remove list items using the indexof items we use
Method splice().
there is two way first usinf item value
let cities = ['DHANBAD', 'Ranchi''kolkata','Mumbai' ];
let index = cities.indexOf(''Ranchi'');
if (index !== -1) {
cities.splice(index, 1);
}
console.log(cities);
b.remove more than one item:
let cities = ['DHANBAD', 'Ranchi''kolkata','Mumbai' ];
let index = cities.indexOf('Mumbai');
if (index !== -1) {
cities.splice(index, 2);
}
console.log(cities);
in this example, the variable index is acting AS a starting point to remove the list items. and the index number as a destination point.
5.Accessing every item IN ARRAY WE HAVE
a. To access every item in the array we use
methods for...of
const cities = ['DHANBAD', 'Ranchi''kolkata','Mumbai' ];
for (const birds of cities) {
console.log(cities);
}
for the beginner it is it but just practice it and ace it we will just to the other article on the array method and enjoy the journey.