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 Overflow
People also ask

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 โ€บ 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.
๐ŸŒ
Sonar
sonarsource.com โ€บ blog โ€บ union-intersection-difference-javascript-sets
Union, intersection, difference, and more are coming to JavaScript Sets | Sonar
February 15, 2024 - JavaScript Set methods for union, intersection, and difference are now part of the language specification, providing native collection operations that previously required manual implementation or third-party libraries.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ get-the-difference-between-two-sets-using-javascript
Get the Difference between Two Sets using JavaScript - GeeksforGeeks
July 12, 2025 - To get the difference between two sets in JavaScript, you need to identify elements present in the first set but not in the second.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Itsourcecode
itsourcecode.com โ€บ home โ€บ mastering javascript set difference: a step-by-step guide
Mastering JavaScript Set Difference: A Step-by-Step Guide
July 9, 2026 - In this syntax, the calculateSetDiffrence function takes two sets as arguments and returns a new set containing the difference between them. Moreover, the filter method is used to retain only those elements from Set A that are not present in ...
๐ŸŒ
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.
๐ŸŒ
Medium
hasunnilupul.medium.com โ€บ whats-new-in-javascript-exploring-set-methods-for-comparing-set-like-objects-6c3052d275f9
Whatโ€™s New in JavaScript: Exploring Set Methods for Comparing Set-like Objects | by Nilupul Perera | Medium
January 23, 2025 - Set A Set B Union 1, 2, 3, 4 3, 4, 5, 6 1, 2, 3, 4, 5, 6 ยท The difference() method returns a new Set with elements in the first set but not in the second.
๐ŸŒ
Designcise
designcise.com โ€บ web โ€บ tutorial โ€บ how-to-get-the-difference-of-two-sets-in-javascript
How to Get the Difference of Two Sets in JavaScript? - Designcise
July 27, 2021 - function difference(setA, setB) { const diff = new Set(setA); for (const elem of setB) { diff.delete(elem); } return diff; } const setA = new Set([1, 2, 3, 4]); const setB = new Set([3, 4, 5, 6]); console.log(difference(setA, setB)); // Set(2) ...
๐ŸŒ
GitHub
gist.github.com โ€บ jabney โ€บ d9d5c13ad7f871ddf03f
Fast JavaScript set operations: union, intersection, difference, complement, and equals. Includes support for objects. ยท GitHub
A ฮ” B so.difference(a, b); // => [1, 2, 4, 5] // The relative complement, or a minus b. A\B so.complement(a, b); // => [1] // Set equality.
๐ŸŒ
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....
๐ŸŒ
DEV Community
dev.to โ€บ hasunnilupul โ€บ whats-new-in-javascript-exploring-set-methods-for-comparing-set-like-objects-1hp4
What's New in JavaScript: Exploring Set Methods for Comparing Set-like Objects - DEV Community
January 23, 2025 - To recap, here are the new Set ... Combines all unique elements from two sets. difference(): Returns elements in the first set but not in the second....
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ javascript โ€บ array โ€บ set operations
Set operations in JavaScript - 30 seconds of code
January 1, 2024 - The symmetric difference of two sets is a set containing all elements that exist in either of the sets but not in both.
๐ŸŒ
web.dev
web.dev โ€บ blog โ€บ the javascript set methods are now part of baseline
The JavaScript Set methods are now part of Baseline | Blog | web.dev
June 26, 2024 - const evens = new Set([2, 4, 6, 8]); const squares = new Set([1, 4, 9]); console.log(evens.union(squares)); // Set(6) { 2, 4, 6, 8, 1, 9 } difference() returns a new set containing elements in this set but not in the given set.