Javascript index,length,push,pop,shift,unshift 1

Javascript index,length,push,pop,shift,unshift

var friends = ["Abrar", "Adil", "Adnan", "Alvi", "Ashraf"];
var friendsIndex = friends.indexOf("Alvi");
//Output:3
var friendsLength = friends.length;
//Output: 5
var friendsPositonChange = friends[3] = "Arif";
//Output: [ 'Abrar', 'Adil', 'Adnan', 'Arif', 'Ashraf' ]

var friendsPush = friends.push("Tahmina")
//Output:[ 'Abrar', 'Adil', 'Adnan','Arif', 'Ashraf', 'Tahmina' ]
var friendsPop = friends.pop();
//Output: [ 'Abrar', 'Adil', 'Adnan', 'Arif', 'Ashraf' ]

var friendsShift = friends.shift();
//Output: [ 'Adil', 'Adnan', 'Arif', 'Ashraf' ]

var friendsUnshift = friends.unshift("Shafi")
//Output: [ 'Shafi', 'Adil', 'Adnan', 'Arif', 'Ashraf' ]
console.log(friends)

Here is what the above code is Doing:
1. We created an array called friends.
2. We used the indexOf() method to find the index of a particular element in the array.
3. We used the length property to find the length of the array.
4. We used the assignment operator to change the value of an element in the array.
5. We used the push() method to add an element to the end of the array.
6. We used the pop() method to remove the last element from the array.
7. We used the shift() method to remove the first element from the array.
8. We used the unshift() method to add an element to the beginning of the array.

Similar Posts