
- 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
Deviations in two JavaScript arrays in JavaScript
We have two arrays of numbers like these −
const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];
We are required to write a JavaScript function that takes in two such arrays and returns the element from arrays that are not common to both.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; const difference = (first, second) => { const res = []; for(let i = 0; i < first.length; i++){ if(second.indexOf(first[i]) === -1){ res.push(first[i]); } }; for(let j = 0; j < second.length; j++){ if(first.indexOf(second[j]) === -1){ res.push(second[j]); }; }; return res; }; console.log(difference(arr1, arr2));
Output
The output in the console will be −
[ 6, 5, 1 ]
- Related Articles
- Finding deviations in two Number arrays in JavaScript
- Joining two Arrays in Javascript
- Combining two arrays in JavaScript
- Balancing two arrays in JavaScript
- Combine two different arrays in JavaScript
- isSubset of two arrays in JavaScript
- Reverse sum of two arrays in JavaScript
- How to merge two arrays in JavaScript?
- Sorting arrays by two criteria in JavaScript
- How to join two arrays in JavaScript?
- How to multiply two Arrays in JavaScript?
- Multiply and Sum Two Arrays in JavaScript
- Equality of two arrays JavaScript
- Intersection of two arrays JavaScript
- Alternatively merging two arrays - JavaScript

Advertisements