Sitemap

JavaScript-3

2 min readFeb 18, 2024

--

JavaScript Arrays

Get Janujah Sivarattinam’s stories in your inbox

Join Medium for free to get updates from this writer.

It is a method of categorizing variables and attributes. Arrays can be Defined as a collection of object of the same type.

var car = ["Mercedes","Tesla","Volvo"]

pop(): This method is used for removing the last element of an array.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.pop())

//Output
Volvo

push(): This method is used for adding a new element at the very end of an array.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.push())

//Output
3

concat(): This method is used for joining various arrays into a single array.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.concat())

//Output
[ 'Mercedes', 'Tesla', 'Volvo' ]

reverse(): This method is used for reversing the order of the elements in an array.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.reverse())

//Output
[ 'Volvo', 'Tesla', 'Mercedes' ]

shift(): This method is used for removing the first element of an array.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.shift())

//Output
Mercedes

slice(): This method is used for pulling a copy of a part of an array into a new array.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.slice(1)) // 1 is an index of the array

//Output
[ 'Tesla', 'Volvo' ]

splice(): This method is used for adding elements in a particular way and position.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.splice(2)) // 2is an index of the array

//Output
[ 'Volvo' ]

toString(): This method is used for converting the array elements into strings.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.tostring())

//Output
Mercedes,Tesla,Volvo

unshift(): This method is used for adding new elements at the beginning of the array.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.unshift())

//Output
3

valueOf(): This method is used for returning the primitive value of the given object.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.valueOf())

//Output
[ 'Mercedes', 'Tesla', 'Volvo' ]

indexOf(): This method is used for returning the first index at which a given element is found in an array.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.indexOf("Tesla"))

//Output
1

lastIndexOf(): This method is used for returning the final index at which a given element appears in an array.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.lastIndexOf("Volvo"))

//Output
2

join(): This method is used for combining elements of an array into one single string and then returning it.

var car = ["Mercedes","Tesla","Volvo"];

console.log(car.join("|"))

//Output
Mercedes|Tesla|Volvo

sort(): This method is used for sorting the array elements based on some condition.

var car = ["Susuki","Mercedes","Tesla","Volvo"];

console.log(car.sort())

//Output
[ 'Mercedes', 'Susuki', 'Tesla', 'Volvo' ]

--

--