Mastering Array Functions in JavaScript: Tips, Tricks, and Best Practices


An array is an object that can store multiple values at once.

The Array in JavaScript is a global object which contains a list of items. It is similar to any variable, in that you can use it to hold any type of data.

This is zero-based, which means that the index of the first element is 0. An element inside an array can be of any type, and different elements of the same array can be of different types : string, boolean, even objects or other arrays.

JavaScript provides a number of built-in functions, or methods, that can be used to manipulate arrays. These functions are part of the Array.prototype object and can be used on any array object in JavaScript. Some of the most commonly used array functions in JavaScript include:

length() concat() join()
constructor() every() forEach()
map() includes() pop()
push() reduce() shift()
slice() splice() sort()
indexOf() fill() delete
  1. .push() - adds one or more elements to the end of an array
  2. .pop() - removes the last element from an array
  3. .shift() - removes the first element from an array
  4. .unshift() - adds one or more elements to the beginning of an array
  5. .slice() - returns a shallow copy of a portion of an array
  6. .splice() - adds and/or removes elements from an array
  7. .sort() - sorts the elements of an array in place
  8. .reverse() - reverses the order of the elements in an array
  9. .concat() - returns a new array that is a concatenation of the original array and one or more additional arrays or values
  10. .join() - creates and returns a new string by concatenating all of the elements in an array with a specified separator.

These are just a few examples of the many array functions available in JavaScript, and there are many more to explore.

Example

let a=[10,20,30,40];
console.log(a);
console.table(a);
console.log(a[1]);

let b=new Array(10,20,30,40);
console.table(b);

let c=new Array("Joes",30,true,{m1:100,m2:75,m3:65});
console.table(c);
To download raw file Click Here

Given an array containing functions and the task is to access its element in different ways using JavaScript.

forEach

  • This method calls a function for each element in an array.
  • This method is not executed for empty elements.
Source Code
const number=[1,2,30,4,5,6,7,8,9,10];
//value,index,array
number.forEach((value)=>{
  console.log(value);
});

number.forEach((value,index)=>{
  console.log("Index : "+index+" Value: "+value);
});

const users =[
  {full_name:"Ram",age:12,city:"Salem",salary:10000},
  {full_name:"Sam",age:15,city:"Chennai",salary:10500},
  {full_name:"Ravi",age:22,city:"Namakkal",salary:12000},
  {full_name:"Joes",age:18,city:"Hosur",salary:6000},
  {full_name:"Aureen",age:47,city:"Dharmapuri",salary:10000},
  {full_name:"Stanley",age:10,city:"Salem",salary:8000},
];

console.table(users);

users.forEach((value)=>{
  console.log(value.full_name);
});
To download raw file Click Here

map

  • Creates a new array from calling a function for every array element.
  • Calls a function once for each element in an array.
  • Does not execute the function for empty elements.
  • Does not change the original array.
Source Code
const numbers=[1,2,3,4,5,6,7,8,9,10];

//map(value,index,array)
let sqrt=numbers.map((value)=>{
    return Math.sqrt(value).toFixed(2);
});

console.table(sqrt);

const users =[
  {name:"Sam",age:15,city:"Chennai",salary:10500},
  {name:"Ravi",age:22,city:"Namakkal",salary:12000},
  {name:"Joes",age:18,city:"Hosur",salary:6000},
  {name:"Aureen",age:47,city:"Dharmapuri",salary:10000},
  {name:"Stanley",age:10,city:"Salem",salary:8000},
  {name:"Ram",age:12,city:"Salem",salary:10000},
];

console.table(users);

let eligible_status=users.map((user)=>({
  /*name:user.name,
  age:user.age,
  city:user.city,
  salary:user.salary,*/
  ...user,
  status:user.age>=18?"Eligible":"Not Eligible"
}));

console.table(eligible_status);
To download raw file Click Here

slice

  • returns selected elements in an array, as a new array.
  • selects from a given start, up to a (not inclusive) given end.
  • does not change the original array.
Source Code
const numbers=[1,2,3,4,5,6,7,8,9,10];

//slice(start,end)
console.log(numbers);
console.log("Slice :"+numbers.slice());
console.log("Slice(2) :"+numbers.slice(2));
console.log("Slice(2,5) :"+numbers.slice(2,5));
To download raw file Click Here

Splice

method adds and/or removes array elements.This method also overwrites the original array.

Source Code
/*
  Splice is to Remove Elements in array
  It will change original array

  removed_element=Splice(start,length,new elements)
*/

const n1=[1,2,3,4,5,6,7,8,9,10];

console.log("Before Splice : "+n1);
let removed_elements=n1.splice(2);
console.log("Removed Items : "+removed_elements);
console.log("After Splice :"+n1);


const n2=[1,2,3,4,5,6,7,8,9,10];
console.log("Before Splice : "+n2);
removed_elements=n2.splice(2,2);
console.log("Removed Items : "+removed_elements);
console.log("After Splice :"+n2);


const n3=[1,2,3,4,5,6,7,8,9,10];
console.log("Before Splice : "+n3);
removed_elements=n3.splice(2,2,[25,36,45]);
console.log("Removed Items : "+removed_elements);
console.log(n3);

const n4=[1,2,3,4,5,6,7,8,9,10];
console.log("Before Splice : "+n4);

n4.splice(2,0,100,300);
console.log("After Splice :"+n4);
To download raw file Click Here

concat

Concatenates (joins) two or more arrays.returns a new array, containing the joined arrays, It does not change the existing arrays.

Source Code
//concat
const a=[10,20,30];
const b=[40,50,60];
const c=[70,80,90];

let d=a.concat(b);
console.log(d);

d=a.concat(b,c);
console.log(d);

d=a.concat(b,c,25,35,45,55);
console.log(d);

d=a.concat(b,c,25,35,45,55,['a','b','c']);
console.log(d);

console.table(d);
To download raw file Click Here

sort

sorts the elements of an array.Mainly this method sorts the elements as strings in alphabetical and ascending order. And this function overwrites the original array.

Source Code
const names=["Kumar","Aureen","Joes","Zara","Stanley"];
console.log("Before Sort : "+names);
names.sort();
console.log("After Sort : "+names);

const num=[10,100,25,150,45,80,9];
console.log("Before Sort : "+num);
num.sort();
console.log("After Sort : "+num);

num.sort((a,b)=>{
  return a-b;
});
console.log("Asc Compare Sort : "+num);
num.sort((a,b)=>{
  return b-a;
});
console.log("Desc Compare Sort : "+num);

const users =[
  {name:"Ram",age:12,city:"Salem",salary:10000},
  {name:"Sam",age:15,city:"Chennai",salary:10500},
  {name:"Ravi",age:22,city:"Namakkal",salary:12000},
  {name:"Joes",age:18,city:"Hosur",salary:6000},
  {name:"Aureen",age:47,city:"Dharmapuri",salary:10000},
  {name:"Stanley",age:10,city:"Salem",salary:8000},
];
console.table(users);

users.sort((a,b)=>{
  return a.age-b.age;
});
console.table(users);

users.sort((a,b)=>{
  if(a.name>b.name) return 1;
  if(a.name<b.name) return -1;
  return 0;
});

console.table(users);
To download raw file Click Here

fill

The fill() method fills specified elements in an array with a value.Start and end position can be specified. If not, all elements will be filled.

Source Code
//Fill(value,start,end)

let n=[1,2,3,4,5,6]

console.log("Before Fill : "+n);
//n.fill(20);
//n.fill(20,3)
n.fill(20,3,5);
console.log("After Fill  : "+n);
To download raw file Click Here

includes

  • This method returns true if an array contains a specified value.
  • This method returns false if the value is not found.
  • This is also case sensitive.
Source Code
//Includes(value,start_index)
const products = ["Pen", "Pencil", "Eraser", "Box", "Pen"];
let result = products.includes("Pen");
console.log(result);
result = products.includes("Scale");
console.log(result);
result = products.includes("Pencil", 2);
console.log(result);
To download raw file Click Here

join()

The join() function in JavaScript is a method of the Array object, it is used to join all elements of an array into a single string. The elements of the array are separated by a specified delimiter or separator, which can be a string or a character.

The syntax for the join() function is as follows:

array.join(separator)

The join() function can also be used to join arrays of numbers, booleans, and other data types. The join() method does not modify the original array, it only returns a new string.

It's important to note that this method will not work if the array contains undefined or null elements. If it's possible that some elements in the array are undefined or null, it's a good practice to filter them before using the join function.

Source Code
//array.join(separator)
const products = ["Pen", "Pencil", "Eraser", "Box"];
console.log(products);

console.log(products.join()); //Deafult , as Separator
console.log(products.join('|')); // Pipe | as Separator
To download raw file Click Here

The above code to creates an array of strings called products which contains four elements: "Pen", "Pencil", "Eraser", and "Box".

The first console.log() statement simply outputs the array itself to the console. The second console.log() statement calls the join() method on the products array with no argument, which means it uses the default separator, which is a comma (,). This will output the elements of the array as a single string with commas separating each element: "Pen,Pencil,Eraser,Box"

The third console.log() statement calls the join() method on the products array and passes the separator | as an argument. This will output the elements of the array as a single string with pipe | characters separating each element: "Pen|Pencil|Eraser|Box"

It's important to note that the join() method does not modify the original array, it only returns a new string.

In this example, the join function is used to concatenate all the elements of the array into a single string, the first call to join will use the default separator (,) and the second call will use the separator |. The output of both calls will be different and it will be useful in different scenarios.

reverse()

The reverse() function in JavaScript is a method of the Array object, it is used to reverse the order of the elements in an array. It modifies the original array in place, meaning that it changes the order of the elements in the original array, and it doesn't return a new array.

The syntax for the reverse() function is as follows:

array.reverse()

You can also use the reverse function on an array of string, booleans, or any other data type. The reverse() method works by swapping the first element with the last one, the second element with the second to last one, and so on, until it reaches the middle of the array.

You can also use the reverse() method with an array of objects, but the order of the elements will be based on their memory addresses rather than their properties. If you need to sort an array of objects based on a specific property, you can use the sort() function with a comparator function, which compares the values of the specified property.

It's also important to note that, this method doesn't work on objects, it only works on Arrays.

For example, the below code demonstrates the use of the reverse() method in JavaScript.

The first part of the code creates an array called n which contains six elements: 1, 2, 3, 4, 5, and 6. The first console.log() statement outputs the original array to the console, labeled as "Before Reverse".

The second line of the code calls the reverse() method on the n array, which changes the order of the elements in the array. The elements are now in reverse order: 6, 5, 4, 3, 2, 1 The second console.log() statement outputs the reversed array to the console, labeled as "After Reverse".

The second part of the code creates an object called x which has properties 0, 1, 2, 3 and a length property 4. The first console.log() statement outputs the object to the console.

The next line of the code calls the reverse() method on the x object, but this will throw an error because the reverse method is only available for Arrays. To overcome this issue, the code uses the Array.prototype.reverse.call(x) method. This allows you to call the reverse() method on the x object, and it changes the order of the properties of the object. The second console.log() statement outputs the reversed object to the console.

It's important to note that, this way of reversing the properties of an object is not a standard way and it's not a recommended one because it's not a good practice to modify the Array's prototype, and it may cause unexpected behavior.

Source Code
const n = [1, 2, 3, 4, 5, 6];
console.log("Before Reverse : ", n);
n.reverse();
console.log("After Reverse : ", n);

//Array Element With Length Property
const x = { 0: 10, 1: 20, 2: 30, 3: 40, length: 4 };
console.log(x);

Array.prototype.reverse.call(x);
console.log(x);
To download raw file Click Here

In summary, the reverse() method is useful to change the order of the elements in an array, reversing the order of the elements in the original array and it doesn't return a new array.

push()

The push() function in JavaScript is a method of the Array object, it is used to add one or more elements to the end of an array. It modifies the original array in place, meaning that it adds new elements to the end of the original array, and it doesn't return a new array.

The syntax for the push() function is as follows:

array.push(element1, element2, ..., elementX)

The push() method can also be used to add elements of any data type, including strings, booleans, and objects. It also increases the length of the array by the number of elements added.

It's important to note that, the push() method modifies the original array, it doesn't return a new array and you can use it to add elements to the end of an array regardless of its size.

The code demonstrates the use of the push() method in JavaScript.

The first part of the code creates an array called n which contains 5 elements: 1, 2, 3, 4, and 5. The first console.log() statement outputs the original array to the console.

The next line calls the push() method on the n array, passing the value 60 as an argument. The element 60 is added to the end of the array, making the new array: [1, 2, 3, 4, 5, 60] The second console.log() statement outputs the value returned by the push method, which is the new length of the array.

The next line calls the push() method on the n array again, passing multiple values 70, 85, 90, and 100 as arguments. These elements are added to the end of the array, making the new array [1, 2, 3, 4, 5, 60, 70, 85, 90, 100] The third console.log() statement outputs the array to the console.

The second part of the code creates an array called fruits which contains one element "Apple" The first console.log() statement outputs the original array to the console.

The next line calls the push() method on the fruits array, passing the value "Orange" as an argument. The element "Orange" is added to the end of the array, making the new array: ["Apple", "Orange"] The second console.log() statement outputs the array to the console.

The next line calls the push() method on the fruits array again, passing multiple values "Banana" and "Pineapple" as arguments. These elements are added to the end of the array, making the new array ["Apple", "Orange", "Banana", "Pineapple"] The third console.log() statement outputs the array to the console.

The third part of the code creates two arrays called users1 and users2 which contains ["Ram", "Sam", "Ravi"] and ["Rajesh", "Kumar"] respectively. The next line uses the spread operator (...) and calls the push() method on the users1 array, passing the users2 array as an argument. This will merge the two arrays and add the elements

Source Code
let n = [1, 2, 3, 4, 5]
console.log(n);
console.log(n.push(60));
console.log(n);
console.log(n.push(70, 85, 90, 100));
console.log(n);

let fruits = ["Apple"]
console.log(fruits);
fruits.push("Orange");
console.log(fruits);
fruits.push("Banana", "Pineapple");
console.log(fruits);

//Merging Two Arrays
let users1 = ["Ram", "Sam", "Ravi"];
let users2 = ["Rajesh", "Kumar"];

users1.push(...users2);
console.log(users1);
To download raw file Click Here

In summary, the push() method is useful to add one or more elements to the end of an array, it increases the length of the array by the number of elements added, it modifies the original array and it doesn't return a new array.

pop()

The pop() function in JavaScript is a method of the Array object, it is used to remove the last element from an array and returns the removed element. It modifies the original array in place, meaning that it removes the last element of the original array, and it doesn't return a new array.

The syntax for the pop() function is as follows:

array.pop()

It's important to note that, the pop() method modifies the original array and it also decreases the length of the array by 1. If you call the pop method on an empty array, it returns undefined.

This Example demonstrates the use of the pop() method in JavaScript.

The code creates an array called users which contains four elements: 'Ram', 'Sam', 'Ravi' and 'Kumar'. The first console.log() statement outputs the original array to the console.

The next line calls the pop() method on the users array, which removes the last element from the array (in this case 'Kumar') and returns the removed element. The returned element is logged to the console. The second console.log() statement outputs the modified array to the console, which no longer contains the last element 'Kumar'.

The next line calls the pop() method on the users array again, which removes the last element from the array (in this case 'Ravi') and returns the removed element. The returned element is logged to the console. The third console.log() statement outputs the modified array to the console, which no longer contains the last element 'Ravi'.

It's important to note that, the pop() method modifies the original array and it also decreases the length of the array by 1. Each time the pop method is called, it removes the last element of the array and returns it. If the array is empty and the pop method is called, it returns undefined.

Source Code
//POP in JavaScript.
const users = ['Ram', 'Sam', 'Ravi', 'Kumar'];
console.log(users);
console.log(users.pop());
console.log(users);
console.log(users.pop());
console.log(users);
To download raw file Click Here

In summary, the code demonstrates the use of the pop() method to remove the last element of an array and return it. The method modifies the original array, decreases the length of the array by 1 and it doesn't return a new array.

shift()

The shift() function in JavaScript is a method of the Array object, is used to remove and return the first element of an array. It modifies the original array and changes its length. If the array is empty, undefined is returned.

The syntax for the shift() function is as follows:

array.shift()
Source Code
//Shift()
let students = ["Kumar", "Aureen", "Joes", "Zara", "Stanley", "Rajesh"];

console.log("Before shift : " + students);
let element = students.shift();
console.log("After shift : " + students);
console.log("Removed Element : " + element);


console.log("Before shift : " + students);
element = students.shift();
console.log("After shift : " + students);
console.log("Removed Element : " + element);
To download raw file Click Here

This code demonstrates the usage of the shift() function in JavaScript.

The code defines an array named students which contains 6 elements: "Kumar", "Aureen", "Joes", "Zara", "Stanley", and "Rajesh".

The first call to console.log() outputs the original array, "Before shift : Kumar,Aureen,Joes,Zara,Stanley,Rajesh".

Then, the shift() function is called on the students array, which removes the first element ("Kumar") from the array and assigns it to the variable element.

The second call to console.log() outputs the modified array, "After shift : Aureen,Joes,Zara,Stanley,Rajesh".

The third call to console.log() outputs the removed element, "Removed Element : Kumar"

Then, again the shift() function is called on the modified array, which removes the first element ("Aureen") from the array and assigns it to the variable element.

The fourth call to console.log() outputs the modified array after the second shift, "After shift : Joes,Zara,Stanley,Rajesh".

The fifth call to console.log() outputs the removed element, "Removed Element : Aureen"

The shift() function is used to remove the first element of an array and to return the removed element. Each time the shift() function is called on the array, it will remove the first element of the array and return that element, until all elements of the array are removed.

unshift()

In JavaScript, the unshift() function is used to add one or more elements to the beginning of an array and returns the new length of the array. It modifies the original array by adding new elements to the beginning of the array.

The syntax for the unshift() function is as follows:

array.unshift(element1,element2.....elementX)

Here's an example of how it can be used:

Source Code
//Unshift()
// Add First element at start
students = ["Kumar", "Aureen", "Joes", "Zara", "Stanley", "Rajesh"];
console.log("Before unshift : " + students)

let len = students.unshift("Tiya");
console.log("Length : " + len)
console.log("After unshift : " + students)

//Mulitiple Values
len = students.unshift("Riya", "Diya");
console.log("Length : " + len);
console.log("After unshift : " + students);
To download raw file Click Here

This code demonstrates the usage of the unshift() function in JavaScript.

The code defines an array named students which contains 6 elements: "Kumar", "Aureen", "Joes", "Zara", "Stanley", and "Rajesh".

The first call to console.log() outputs the original array, "Before unshift : Kumar,Aureen,Joes,Zara,Stanley,Rajesh".

Then, the unshift() function is called on the students array with the argument "Tiya" , which adds the "Tiya" element to the beginning of the array and returns the new length of the array which is assigned to variable len and then printed with the help of the second call to console.log().

The third call to console.log() outputs the modified array, "After unshift : Tiya,Kumar,Aureen,Joes,Zara,Stanley,Rajesh".

Then, again the unshift() function is called on the modified array with the arguments "Riya" and "Diya", which add the "Riya" and "Diya" elements to the beginning of the array and returns the new length of the array which is assigned to variable len and then printed with the help of the fourth call to console.log().

The fifth call to console.log() outputs the modified array, "After unshift : Riya,Diya,Tiya,Kumar,Aureen,Joes,Zara,Stanley,Rajesh".

The unshift() function is used to add one or more elements to the beginning of an array and returns the new length of the array. Each time the unshift() function is called on the array, it will add the new element(s) to the beginning of the array and return the new length of the array, until all desired elements are added.

indexOf()

In JavaScript, the indexOf() function JavaScript is used to search an array for a specific element and return the first index at which the element can be found. If the element is not present in the array, it will return -1.

The syntax for the indexOf() function is as follows:

array.indexOf(element)

Here's an example of how it can be used:

Source Code
students = ["Tiya", "Aureen", "Joes", "Zara", "Stanley", "Rajesh"];

let i=students.indexOf("Tiya");
console.log("Index : "+i);

let user="Tutor Joes";
let index=user.indexOf("o");
console.log("Index : "+index);

index=user.indexOf("o",5);
console.log("Index : "+index);

To download raw file Click Here

This code demonstrates the usage of the indexOf() function in JavaScript.

The code defines an array named students which contains 6 elements: "Tiya", "Aureen", "Joes", "Zara", "Stanley", and "Rajesh".

The first line of code calls the indexOf() function on the students array with the argument "Tiya", which searches the array for the first occurrence of "Tiya" and returns its index, which is then assigned to the variable i and printed with the help of the first call to console.log().

The second line of code defines a variable named user which is assigned the string value "Tutor Joes".

The third line of code calls the indexOf() function on the user string with the argument "o", which searches the string for the first occurrence of "o" and returns its index, which is then assigned to the variable index and printed with the help of the second call to console.log().

The fourth line of code calls the indexOf() function on the user string with the arguments "o" and 5 which specifies that it should start searching for the "o" from index 5, and returns its index, which is then assigned to the variable index and printed with the help of the third call to console.log().

In the last console.log() statement, the third call to console.log() outputs the index of the second o in the string which is 6.

It is important to note that the indexOf() method returns the first index at which the element can be found. If the element is not present in the array or string, it will return -1.

lastIndexOf()

In JavaScript, the lastIndexOf() function JavaScript is used to search an array or a string for a specific element and return the last index at which the element can be found. If the element is not present in the array or string, it will return -1. The lastIndexOf() function is similar to the indexOf() function, but instead of searching from the beginning of the array or string, it starts searching from the end.

It is important to note that the lastIndexOf() method checks for strict equality (===) between the elements

The syntax for the lastIndexOf() function is as follows:

array.lastIndexOf(element)

Here's an example of how it can be used:

Source Code
students = ["Tiya", "Aureen", "Joes", "Zara", "Stanley","Tiya", "Rajesh"];

let i=students.indexOf("Tiya");
console.log(i);
i=students.lastIndexOf("Tiya");
console.log(i);

let address="Tutor Joes Cherry Road Salem Joes";
i=address.indexOf("Joes");
console.log(i);
i=address.lastIndexOf("Joes");
console.log(i);

To download raw file Click Here

This code demonstrates the usage of the indexOf() and lastIndexOf() functions in JavaScript.

The code defines an array named students which contains 7 elements: "Tiya", "Aureen", "Joes", "Zara", "Stanley", "Tiya", and "Rajesh".

The first line of code calls the indexOf() function on the students array with the argument "Tiya", which searches the array for the first occurrence of "Tiya" and returns its index, which is then assigned to the variable i and printed with the help of the first call to console.log().

The second line of code calls the lastIndexOf() function on the students array with the argument "Tiya", which searches the array for the last occurrence of "Tiya" and returns its index, which is then assigned to the variable i and printed with the help of the second call to console.log().

The third line of code defines a variable named address which is assigned the string value "Tutor Joes Cherry Road Salem Joes".

The fourth line of code calls the indexOf() function on the address string with the argument "Joes", which searches the string for the first occurrence

every() & some()

In JavaScript, the every() and some() functions are used to perform a test on all elements of an array and return a Boolean value indicating whether all or some of the elements pass the test, respectively.

The every() function takes a callback function as an argument, which is called for each element in the array. The callback function is passed three arguments: the current element, the index of the current element, and the array itself. If the callback function returns true for every element in the array, the every() function returns true. If the callback function returns false for any element in the array, the every() function returns false.

The some() function also takes a callback function as an argument, and it also calls it for each element in the array. If the callback function returns true for any element in the array, the some() function returns true. If the callback function returns false

Here's an example of how it can be used:

Source Code
n = [12, 18, 10, 8];

let result=n.every((value)=>{
  return value%2==0;
});

console.log("All Elements are Even :" ,result);

result=n.some((value)=>{
  return value%2==0;
});

console.log("All Elements are Even :" ,result);


function checkEven(value) {
  return value % 2 == 0;
}

result=n.every(checkEven);

console.log("checkEven All Elements are Even :" ,result);


const users = [
  { name: "Ram", age: 25},
  { name: "Tiya", age: 45},
  { name: "Raja", age: 18},
  { name: "Sara", age: 12}
];

function isEligible(element) {
  return element.age>=18;
}

result = users.every(isEligible);
console.log("Every Eligible :" , result);

result = users.some(isEligible);
console.log("Some Eligible :" , result);
To download raw file Click Here

This code demonstrates the usage of the every() and some() functions in JavaScript.

The code defines an array named n which contains 4 elements: 12, 18, 10, 8.

The first line of code calls the every() function on the n array and passed a callback function that checks if each element is even by using the modulus operator. Since all elements of the array are even, the every() function returns true.

The second line of code calls the some() function on the n array with the same callback function, which checks if any element of the array is even. Since all elements of the array are even, the some() function also returns true.

The third line of code defines a function named checkEven which takes a value and checks if it is even. The every() function is called on the n array with checkEven function passed as an argument. As the result it returns true.

The code then defines an array of objects named users which contains 4 objects, each representing a user with properties name and age.

The isEligible function is defined which takes an element and checks if its age is greater than or equal to 18.

The every() function is called on the users array with the isEligible function passed as an argument. As it only Raja object is not eligible, it returns false

The some() function is called on the users array with the isEligible function passed as an argument. As Ram and Tiya objects are eligible, it returns true

List of Programs


JS Practical Questions & Answers


JS Practical Project