
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare Arrays Using Array Prototype Every in JavaScript
We are required to write a JavaScript function that takes in two arrays of literals. Then our function should return true if all the elements of first array are included in the second array, irrespective of their count, false otherwise.
We have to use Array.prototype.every() method to make these comparisons.
Example
The code for this will be −
const arr1 = [0, 2, 2, 2, 1]; const arr2 = [0, 2, 2, 2, 3]; const compareArrays = (arr1, arr2) => { const areEqual = arr1.every(el => { return arr2.includes(el); }); return areEqual; }; console.log(compareArrays(arr1, arr2));
Output
And the output in the console will be −
false
Advertisements