
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to know if two arrays have the same values in JavaScript?
Let’s say the following are our arrays −
var firstArray=[100,200,400]; var secondArray=[400,100,200];
You can sort both the arrays using the sort() method and use for loop to compare each value as in the below code −
Example
var firstArray=[100,200,400]; var secondArray=[400,100,200]; function areBothArraysEqual(firstArray, secondArray) { if (!Array.isArray(firstArray) || ! Array.isArray(secondArray) || firstArray.length !== secondArray.length) return false; var tempFirstArray = firstArray.concat().sort(); var tempSecondArray = secondArray.concat().sort(); for (var i = 0; i < tempFirstArray.length; i++) { if (tempFirstArray[i] !== tempSecondArray[i]) return false; } return true; } if(areBothArraysEqual(firstArray,secondArray)) console.log("Both are equals"); else console.log("Both are not equals");
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo156.js.
Output
PS C:\Users\Amit\JavaScript-code> node demo156.js Both are equals
- Related Articles
- Check if values of two arrays are the same/equal in JavaScript
- How to compare two arrays to see how many same elements they have in JavaScript?
- How to Know If Two Convex Regular Polygons have Same Centre or Not in Java?
- Merge two arrays with alternating Values in JavaScript
- Comparing corresponding values of two arrays in JavaScript
- How to Know if You Have Bedbugs?
- How to merge two arrays in JavaScript?
- How to join two arrays in JavaScript?
- How to multiply two Arrays in JavaScript?
- How to quickly swap two arrays of the same size in C++?
- How to return rows that have the same column values in MySQL?
- Check if two String objects have the same value in C#
- How to know if your child have allergy symptoms?
- How to get the difference between two arrays in JavaScript?
- Fastest Method to Check If Two Files Have Same Contents

Advertisements