I don't know if this is most effective, but perhaps the shortest:
var A = [1, 2, 3, 4];
var B = [1, 3, 4, 7];
var diff = A.filter(function(x) {
return B.indexOf(x) < 0;
});
console.log(diff); // [2]
Updated to ES6:
const A = [1, 2, 3, 4];
const B = [1, 3, 4, 7];
const diff = A.filter(x => !B.includes(x));
console.log(diff); // [2]
Answer from user187291 on Stack OverflowW3Schools
w3schools.com โบ jsref โบ jsref_set_difference.asp
JavaScript Set difference() Method
The difference() method returns the difference between two sets.
Top answer 1 of 16
232
I don't know if this is most effective, but perhaps the shortest:
var A = [1, 2, 3, 4];
var B = [1, 3, 4, 7];
var diff = A.filter(function(x) {
return B.indexOf(x) < 0;
});
console.log(diff); // [2]
Updated to ES6:
const A = [1, 2, 3, 4];
const B = [1, 3, 4, 7];
const diff = A.filter(x => !B.includes(x));
console.log(diff); // [2]
2 of 16
189
Well, 7 years later, with ES6's Set object it's quite easy (but still not as compact as python's A - B), and reportedly faster than indexOf for large arrays:
console.clear();
let a = new Set([1, 2, 3, 4]);
let b = new Set([5, 4, 3, 2]);
let a_minus_b = new Set([...a].filter(x => !b.has(x)));
let b_minus_a = new Set([...b].filter(x => !a.has(x)));
let a_intersect_b = new Set([...a].filter(x => b.has(x)));
let a_union_b = new Set([...a, ...b]);
console.log(...a_minus_b); // {1}
console.log(...b_minus_a); // {5}
console.log(...a_intersect_b); // {2,3,4}
console.log(...a_union_b); // {1,2,3,4,5}
13:13
FASTER JavaScript In 2025 With Sets - YouTube
01:00
Set methods in JavaScript - YouTube
04:41
New JavaScript "Set Methods" are here! - YouTube
JavaScript Set Difference() Method
JavaScript Set symmetricDifference
JavaScript Set Difference - YouTube
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard with IntelliSense, debugger, terminal, Git, and a huge extension marketplace. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
itsourcecode.com
itsourcecode.com โบ home โบ mastering javascript set difference: a step-by-step guide
Mastering JavaScript Set Difference: A Step-by-Step Guide
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools (F12), Console tab, paste code, Enter. For HTML pages, add a script tag pointing to your .js file. Locally: install Node.js (LTS), then node script.js in your terminal from the file folder.
itsourcecode.com
itsourcecode.com โบ home โบ mastering javascript set difference: a step-by-step guide
Mastering JavaScript Set Difference: A Step-by-Step Guide
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds top-level await, private class fields with #, and the .at() array method. For older browsers, transpile with Babel or use Vite, esbuild, or webpack.
itsourcecode.com
itsourcecode.com โบ home โบ mastering javascript set difference: a step-by-step guide
Mastering JavaScript Set Difference: A Step-by-Step Guide
MDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ Set โบ difference
Set.prototype.difference() - JavaScript - MDN Web Docs - Mozilla
The difference() method of Set instances takes a set and returns a new set containing elements in this set but not in the given set.
MDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ Set โบ symmetricDifference
Set.prototype.symmetricDifference() - JavaScript | MDN
May 22, 2026 - The symmetricDifference() method of Set instances takes a set and returns a new set containing elements which are in either this set or the given set, but not in both.
TutorialsPoint
tutorialspoint.com โบ javascript โบ javascript_set_difference_method.htm
JavaScript - Set.difference() Method
The JavaScript Set.difference() method takes a set as a parameter and returns a new set containing elements in this set but not in the given set. For instance, we have sets i.e.
MDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ Set
Set - JavaScript - MDN Web Docs
August 13, 2026 - Set.prototype.difference() Takes a set and returns a new set containing elements in this set but not in the given set. Set.prototype.entries() Returns a new iterator object that contains an array of [value, value] for each element in the Set object, in insertion order.
Can I Use
caniuse.com โบ mdn-javascript_builtins_set_difference
JavaScript built-in: Set: difference | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Kodezi
blog.kodezi.com โบ master-javascript-set-difference-concepts-code-and-challenges
Master javascript set difference: Concepts, Code, and Challenges
August 13, 2025 - In the world of coding, developers often encounter challenges when managing collections of data, particularly when applying JavaScript set difference. JavaScript set difference offers a robust solution to these issues by allowing you to store unique elements of any kind, whether primitive types or object references.
Programiz
programiz.com โบ javascript โบ examples โบ set-operations
JavaScript Program to Illustrate Different Set Operations
The set difference operation represents elements that are present in one set and not in another set.
RealCoding Tech Blog
realcoding.blog โบ 2026 โบ 03 โบ 22 โบ javascript-set-methods-intersection-union-en
JavaScript Set New Methods - intersection, union, difference Practical Guide | RealCoding Tech Blog
March 22, 2026 - const frontend = new Set(['React', 'Vue', 'Svelte', 'Angular']); const liked = new Set(['React', 'Svelte', 'Rust', 'Go']); // Intersection - elements in both frontend.intersection(liked); // Set {'React', 'Svelte'} // Union - all elements combined frontend.union(liked); // Set {'React', 'Vue', 'Svelte', 'Angular', 'Rust', 'Go'} // Difference - only in frontend frontend.difference(liked); // Set {'Vue', 'Angular'} // Symmetric difference - in one but not both frontend.symmetricDifference(liked); // Set {'Vue', 'Angular', 'Rust', 'Go'}
Mozilla
developer.mozilla.org โบ en-US โบ blog โบ javascript-set-methods
New JavaScript Set methods - MDN Web Docs - Mozilla
June 24, 2024 - For those of you looking for the ... returns a new set with all elements in this set and the given set. difference() returns a new set with elements in this set but not in the given set....