Thursday, September 18, 2025
HomeLanguagesJavascriptJavaScript Symbol() Method

JavaScript Symbol() Method

Symbols are new primitive built-in object types introduced as part of ES6. Symbols return unique identifiers that can be used to add unique property keys to an object that won’t collide with keys of any other code that might add to the object. They are used as object properties that cannot be recreated. It basically helps us to enable encapsulation or information hiding.

Syntax:

Symbol( optional_string )

Parameters: The optional_string is an optional parameter that acts as a description of Symbol().

Return Value: This method returns a new Symbol object.

Below are the examples of the Symbol() method.

Example 1:

Javascript




<script>
    let symbol1 = Symbol("Geeks")
    let symbol2 = Symbol("Geeks")
  
    // Each time Symbol() method 
    // is used to create new global
    // Symbol
    console.log(symbol1 == symbol2);
</script>


Output:

false

Example 2:

Javascript




<script>
  
    // Symbol returns a symbol primitive 
    console.log(typeof Symbol("GFG"));
    console.log(typeof Object("GFG"));
</script>


Output:

symbol
object
RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6660 POSTS0 COMMENTS
Nicole Veronica
11835 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7053 POSTS0 COMMENTS
Thapelo Manthata
6735 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS