
- 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
Difference between first and the second array in JavaScript
We are required to write a JavaScript function that takes in two arrays of literals. The arrays might contain some identical entries as well.
The purpose of our function is to simply find out and return an array of all such elements that exists in the first array but not in the second.
Example
The code for this will be −
const arr1 = ['1', '2', '3', '4/2', '5/4', '6−2']; const arr2 = ['1', '2', '3', '5/4', '4/2', '6−1', '7/2', '8−2']; const differenceBetween = (arr1 = [], arr2 = []) => { const res = []; for(let i = 0; i < arr1.length; i++){ const el = arr1[i]; if(arr2.includes(el)){ continue; }; res.push(el); }; return res; }; console.log(differenceBetween(arr1, arr2));
Output
And the output in the console will be −
['6−2']
- Related Articles
- Find difference between first and second largest array element in Java
- Maximize first array over second in JavaScript
- Difference Between First level cache and Second level cache in Hibernate
- Sort the second array according to the elements of the first array in JavaScript
- Calculate the difference between the first and second element of each subarray separately and return the sum of their differences in JavaScript
- Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array
- Find the first, second and third minimum elements in an array in C++
- Product of maximum in first array and minimum in second in C
- Find the first, second and third minimum elements in an array in C++ program
- Difference between sum and product of an array in JavaScript
- Elements present in first array and not in second using STL in C++
- Maximum difference between first and last indexes of an element in array in C
- Find elements which are present in first array and not in second in C++
- Find the second most frequent element in array JavaScript
- Difference between numbers and string numbers present in an array in JavaScript

Advertisements