자바스크립트 clear - jabaseukeulibteu clear

  • Overview / Web Technology

    Web technology reference for developers

  • HTML

    Structure of content on the web

  • CSS

    Code used to describe document style

  • JavaScript

    General-purpose scripting language

  • HTTP

    Protocol for transmitting web resources

  • Web APIs

    Interfaces for building web applications

  • Web Extensions

    Developing extensions for web browsers

  • Web Technology

    Web technology reference for developers

  • View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The Set.clear() method in JavaScript is used for the removal of all the elements from a set and make it empty. No arguments are required to be sent as parameters to the Set.clear() method and it returns an undefined return value.

    Syntax:

    mySet.clear()

    Parameters: This method does not any accept any parameters.

    Return Value: This method returns an undefined value.

    Example 1:

    Javascript

    <script>

        let myset = new Set();

        myset.add(23);

        console.log(myset);

        console.log(myset.size);

        myset.clear();

        console.log(myset.size);

    </script>

    Output:

    Set(1) { 23 } 1 0

    Example 2:

    Javascript

    <script>

        let myset = new Set();

        myset.add("Manchester");

        myset.add("London");

        myset.add("Leeds");

        console.log(myset);

        console.log(myset.size);

        myset.clear();

        console.log(myset.size);

    </script>

    Output:

    Set(3) { 'Manchester', 'London', 'Leeds' } 3 0

    Supported Browsers:

    • Google Chrome:  38+
    • Firefox: 19+
    • Internet Explorer: 11+
    • Opera: 25+
    • Edge: 12+
    • Safari: 08+

    The JavaScript Set clear() method is used to remove all the elements from Set object.

    Syntax

    The clear() method is represented by the following syntax:

    JavaScript Set clear() method example

    Here, we will understand clear() method through various examples.

    Example 1

    Let's see a simple example of clear() method.

    Test it Now

    Output:

    Size before invoking clear() method: 3 Size after invoking clear() method: 0

    Example 2

    Let's see an example to determine whether the set object contains the specified element.

    Test it Now

    Output:

    Element present before invoking clear() method: true Element present after invoking clear() method: false


    W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

    Copyright 1999-2022 by Refsnes Data. All Rights Reserved.
    W3Schools is Powered by W3.CSS.

    Definition and Usage

    The clear() method removes all the Storage Object item for this domain.

    The clear() method belongs to the Storage Object, which can be either a localStorage object or a sessionStorrage object.

    Browser Support

    Method
    clear() 4 8 3.5 4 10.5

    Syntax

    Or:

    Parameter Values

    No parameters.

    Technical Details

    DOM Version:Return Value:
    Web Storage API
    No return value

    More Examples

    Example

    The same example, but using session storage instead of local storage.

    Remove all session items:

    sessionStorage.clear();

    Try it Yourself »

    Related Pages

    Web Storage Reference: removeItem() Method



    Toplist

    최신 우편물

    태그