How to check if a Set contains a value in JavaScript

Checking if a Set contains a specific value is fundamental for membership testing, validation logic, and conditional operations in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve used Set membership testing extensively in permission systems, feature flags, and state validation logic. From my expertise, the most efficient approach is using the has() method which provides O(1) time complexity for value lookups. This method offers superior performance compared to array-based contains operations for large datasets.

Use the has() method to efficiently check if a Set contains a specific value.

const permissions = new Set(['read', 'write', 'admin'])
const hasWriteAccess = permissions.has('write')
console.log(hasWriteAccess) // true

Here permissions.has('write') checks if the Set contains the value ‘write’ and returns a boolean result. The has() method provides constant time O(1) lookup performance regardless of the Set size, making it significantly faster than array.includes() for large collections. The method performs strict equality comparison, so it works correctly with primitive values and object references.

Best Practice Note:

This is the same approach we use in CoreUI components for permission checking, feature flag validation, and active state management. Set.has() is ideal for membership testing in large collections where performance matters, offering much better performance than array-based contains operations.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to disable a button in JavaScript
How to disable a button in JavaScript

How to loop through a 2D array in JavaScript
How to loop through a 2D array in JavaScript

How to replace all occurrences of a string in JavaScript?
How to replace all occurrences of a string in JavaScript?

How to Use JavaScript setTimeout()
How to Use JavaScript setTimeout()

Answers by CoreUI Core Team