• Skip to main content
  • Skip to search
  • Skip to select language
    • Technologies Overview
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs
    • Browser Extensions
    • MathML
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
    • Send Feedback
    • Contribute to MDN
    • Report a content issue 🌐
    • Report a platform issue 🌐
  1. Web technology for developers
  2. JavaScript
  3. JavaScript reference
  4. Standard built-in objects
  5. Set
  6. Set.prototype.clear()
  • Change language

Table of contents

  • Syntax
  • Examples
  • Specifications
  • Browser compatibility
  • See also

Set.prototype.clear()

The clear() method removes all elements from a Set object.

Syntax

clear()

Return value

undefined.

Examples

Using the clear() method

var mySet = new Set();
mySet.add(1);
mySet.add('foo');

mySet.size;       // 2
mySet.has('foo'); // true

mySet.clear();

mySet.size;       // 0
mySet.has('bar')  // false

Specifications

Specification
ECMAScript Language Specification
# sec-set.prototype.clear

Browser compatibility

BCD tables only load in the browser

See also

  • Set
  • Set.prototype.delete()

Found a problem with this page?

  • Edit on GitHub
  • Source on GitHub
  • Report a problem with this content on GitHub
  • Want to fix the problem yourself? See our Contribution guide.

Last modified: Jul 20, 2021, by MDN contributors

Change your language

Related Topics

  1. Standard built-in objects
  2. Set
  3. Properties
    1. get Set[@@species]
    2. Set.prototype.size
  4. Methods
    1. Set.prototype[@@iterator]()
    2. Set.prototype.add()
    3. Set.prototype.clear()
    4. Set.prototype.delete()
    5. Set.prototype.entries()
    6. Set.prototype.forEach()
    7. Set.prototype.has()
    8. Set.prototype.values()
  5. Inheritance:
  6. Function
  7. Properties
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  8. Methods
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  9. Object
  10. Properties
    1. Object.prototype.constructor
    2. Object.prototype.__proto__
  11. Methods
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.setPrototypeOf()
    9. Object.prototype.toLocaleString()
    10. Object.prototype.toSource()
    11. Object.prototype.toString()
    12. Object.prototype.valueOf()
  • Web Technologies
  • Learn Web Development
  • About MDN
  • Feedback
  • About
  • MDN Web Docs Store
  • Contact Us
  • Firefox

MDN

  • MDN on Twitter
  • MDN on Github

Mozilla

  • Mozilla on Twitter
  • Mozilla on Instagram

© 2005-2022 Mozilla and individual contributors. Content is available under these licenses.

  • Terms
  • Privacy
  • Cookies