What is the Arrays In Javascript?

What is the Arrays In Javascript?

What is the Array

Array in javascript is the store. Array storing the multiple collection of multiple items under single variable name. arrya is the Non-primitive dataType. or Array is the variable that store the multiple data.

  • you can resize the array according to your need

  • array contains the multiple data types variable

  • the first index of array is always starting from 0.

Creating an array

const bikes = ["splender", "Pulsar", "Bullet"];

you can also create an empty array than after using the arrays mathod and manually you can fill the value in array.

ex. let arr=[]

arr[0]="hello",

arr[1]="world"

mathod is array

  1. push

  2. pop

  3. filter

  4. map

  5. reduce

  6. fill

  7. split

  8. slice

  9. splice

  10. concat

  11. reverse

  12. sort

  13. indexof

  14. forEach etc.....

map

map mathod is array mathod. map mathod creates as new array from calling a function for every array element.

  • map mathod call a function once for each element in an array

  • map does not execute the function for empty elements.

  • map does not change the original array.

Syntax:

array.map(function(index,arr),thisvalue)

let arr = [3, 4, 5, 6];

for (let i = 0; i < arr.length; i++){
  arr[i] = arr[i] * 3;
}

console.log(arr); // [9, 12, 15, 18]

Unshift

the unshift mathod adds one or more elements to the begining of an array and returns the new lenght of the array.

  • the unshift method overwrites the original array.

syntax:

array.unshift(item1,item2)

var languages = ["JavaScript", "Python", "Java", "Lua"];

var count = languages.unshift("C++");
console.log(languages); // [ 'C++', 'JavaScript', 'Python', 'Java', 'Lua' ]
console.log(count); // 5

var priceList = [12, 21, 35];

var count1 = priceList.unshift(44, 10, 1.6);
console.log(priceList); // [ 44, 10, 1.6, 12, 21, 35 ]
console.log(count1); // 6

push mathod

Javascript push mathod appends the given element in the last of the array and returns the length of the new array.

syntax:

array.push(element1,element2)

let arr=[1,2,4]
arr.push(24) //[1,2,4,24]

console.log(arr)   //arr=[1,2,4,24]

pop mathod

pop mathod is the array mathod in javascript. and it remove and the final element of array. and return the pop value in array. and array lenght will change

  • syntax : array.pop(ele1)
let arr=["peanut","chacha","business","sankhla","heavylegs"]

arr.pop()

console.log(arr)  //arr=["peanut","chacha","business","sankhla"]

filter mathod

filter mathod in javascript is the create the shallow copy of a portion of given array.

  • filter mathod creates a new array and returns all of the items which pass the condition specified in the callback.

  • filter mathod does not change the original array.

  • filter mathod does not execute the function for empty elements

  • syntax: array.filter(fun(currentvalue,index,arr),thisvalue)

let array=[1,2,3,4,5,6,8]

let array2=array.filter((num)=>num<4)

console.log(array2)   //[1,2,3]

slice mathod

the slice mathod return the swallow copy of a portion of an array into the new array and object selected from start to end. where start and end represent the index of items in that array.

  • the slice method return the selected element in the array, as a new array.

  • the slice method select from given to start where end is not encluded.

  • slice() method does not change the original array.

syntax : array.slice(start,end)

let num=[1,2,3,4,5]
let arr=num.slice(1,3)

console.log(arr) // [2,3]

splice method

the splice() method change the content the of an array by removing or replace existing elements of add new elements in the place.

  • splice() method add or remove the element of array.

  • splice() method overides the original array.

syntax: arr.splice(index,howmany,item1.....)

let arr=[1,3,4,5,6,7]
arr.splice(2,2,1)

console,log(arr) // [1,3,1,6,7]

fill method

fill mathod fill the value in the given array.

  • fill mathod overwrites the original array.

  • start and end position can be specified. if not all elements will be filled.

syntax: array.fill(value,start,end)

let array=[1,2,3,4,5,6]
array.find(10,3,4)
console.log(array)  //[1,2,3,10,5,6]

findIndex()

findindex() method returns the index of the first element in an array that satisfies the provided testing function. if no element is satisfy the index -1 return.

  • findindex() method executes a function for each array element

  • this mathod does not change original array

  • this mathod return -1 if no match is found

  • this mathod does not execute of empty array

let arr=['brock','lesnar','moongfali','chacha']
let name=(item)=>'mongfali';
console.log(arr.findIndex("index is",name))  // index is 2

Conclusion

in this we know some array mathod. many mathods have so you should read the officially docs of mdn.