JavaScript Array Reference #2 – Quick Learn

Bayram EKER
4 min readNov 15, 2022

--

Array reference JavaScript quick learning series part 2

1-) keys()

The keys() method returns an Array Iterator object with the keys of an array.

const fruits = ["Muz", "Portakal", "Elma", "kiwi"];
const keys = fruits.keys();

for (let x of keys) {
console.log(x);
}

OUTPUT:

0

1

2

3

2-) lastIndexOf()

The lastIndexOf() method returns the last index (position) of a specified value.

const fruits = ["Armut", "Muz", "Kiwi", "Çilek"];
let index = fruits.lastIndexOf("Kiwi");

console.log(index);

OUTPUT : 2

3-) length

The length property sets or returns the number of elements in an array.

const fruits = ["Muz", "Kiwi", "Elma", "Çilek"];
let length = fruits.length;

console.log(length);

OUTPUT : 4

4-) map()

map() creates a new array from calling a function for every array element.

const numbers = [4, 9, 16, 25];
const newArr = numbers.map(Math.sqrt)

console.log(newArr);

OUTPUT : 2,3,4,5

5-) pop()

The pop() method removes (pops) the last element of an array.

const fruits = ["Elma", "Armut", "Portakal", "Muz"];


console.log(fruits.pop());

OUTPUT : Elma,Armut,Portakal

6-) prototype

prototype allows you to add new properties and methods to arrays.

prototype is a property available with all JavaScript objects.

// Add a new method
Array.prototype.myUcase = function() {
for (let i = 0; i < this.length; i++) {
this[i] = this[i].toUpperCase();
}
};

// Use the method on any array
const fruits = ["Elma", "Armut", "Portakal", "Kiwi"];


console.log(fruits.myUcase());

OUTPUT : ELMA,ARMUT,PORTAKAL,KIWI

7-) push()

The push() method adds new items to the end of an array.

The push() method changes the length of the array.

const fruits = ["Elma", "Armut", "Portakal", "Muz"];


console.log(fruits.push("Kiwi"));

OUTPUT : Elma,Armut,Portakal,Muz,Kiwi

8-) reduce()

The reduce() method executes a reducer function for array element.

The reduce() method returns a single value: the function's accumulated result.

const numbers = [175, 50, 25];

console.log(numbers.reduce(myFunc));

function myFunc(total, num) {
return total - num;
}

OUTPUT: 100

9-) reduceRight()

The reduceRight() method executes a reducer function for each array element.

The reduceRight() method works from right to left.

const numbers = [175, 50, 25];

console.log(numbers.reduceRight(myFunc));

function myFunc(total, num) {
return total - num;
}

OUTPUT: -200

10-) reverse()

The reverse() method reverses the order of the elements in an array.

The reverse() method overwrites the original array.

const fruits = ["Elma", "Armut", "Muz", "Kiwi"];
fruits.reverse();

console.log(fruits.reverse());

OUTPUT: Kiwi,Armut,Muz,Kiwi

11-) shift()

The shift() method removes the first item of an array.

The shift() method changes the original array.

const fruits = ["Elma", "Armut", "Muz", "Kiwi"];
fruits.shift();

console.log(fruits);

OUTPUT: Armut,Muz,Kiwi

12-) slice()

The slice() method returns selected elements in an array, as a new array.

The slice() method selects from a given start, up to a (not inclusive) given end.

const fruits = ["Elma", "Armut", "Portakal", "Limon", "Muz"];
const citrus = fruits.slice(1, 3);

console.log(citrus);

OUTPUT: Armut,Portakal

13-) some()

The some() method checks if any array elements pass a test (provided as a callback function).

The some() method executes the callback function once for each array element.

const ages = [3, 10, 18, 20];

console.log(ages.some(checkAdult));

ages.some(checkAdult);
function checkAdult(age) {
return age > 18;
}

OUTPUT: true

14-) sort()

The sort() sorts the elements of an array.

The sort() overwrites the original array.

The sort() sorts the elements as strings in alphabetical and ascending order.

const fruits = ["Elma", "Armut", "Muz", "Kiwi"];
console.log(fruits.sort());

OUTPUT: Armut,Elma,Kiwi,Muz

15-) splice()

The splice() method adds and/or removes array elements.

The splice() method overwrites the original array.

const fruits = ["Elma", "Armut", "Muz", "Portakal"];
fruits.splice(2, 0, "Limon", "Kiwi");

console.log(fruits);

OUTPUT: Elma,Armut,Limon,Kiwi,Muz,Portakal

const fruits = ["Elma", "Armut", "Muz", "Portakal", "Kiwi"]];
fruits.splice(2, 2);

console.log(fruits);

OUTPUT: Elma,Armut,Kiwi

16-) toString()

The toString() method returns a string with array values separated by commas.

The toString() method does not change the original array.

const fruits = ["Elma", "Armut", "Muz", "Portakal"];
let text = fruits.toString();

console.log(text);

OUTPUT: “Elma,Armut,Muz,Portakal” — (String)

17-) unshift()

The unshift() method adds new elements to the beginning of an array.

The unshift() method overwrites the original array.

const fruits = ["Elma", "Armut", "Muz", "Portakal"];
fruits.unshift("Limon","Ananas");

console.log(fruits);

OUTPUT: Limon,Ananas,Elma,Armut,Muz,Portakal

18-) valueOf()

The valueOf() method returns the array itself.

The valueOf() method does not change the original array.

const fruits = ["Elma", "Armut", "Muz", "Portakal"];
const myArray = fruits.valueOf();

console.log(myArray);

OUTPUT : Elma,Armut,Muz,Portakal

Thank you for reading this article.

If you find this article useful please kindly share it with your network and feel free to use the comment section for questions, answers, and contributions.

You might also like :

--

--

No responses yet