Is JavaScript Symbol Primitive Data Type?
In JavaScript, there are six primitive data types: number, string, boolean, null, undefined, and symbol. While the first five primitive types are commonly known, the symbol data type might be less familiar to some developers.
The Symbol Data Type
A symbol is a new addition to JavaScript as of ECMAScript 2015 (ES6). It represents a unique identifier that can be used as a property key for objects. Unlike other data types, symbols are guaranteed to be unique.
To create a symbol in JavaScript, you can use the Symbol()
function without the new
keyword:
const mySymbol = Symbol();
console.log(typeof mySymbol); // output: symbol
Note: The use of the typeof
operator confirms that mySymbol
is indeed of type “symbol”. This helps differentiate it from other data types.
Symbols as Object Property Keys
Symbols are typically used as property keys in objects. They ensure that properties with symbol keys are not accidentally accessed or modified by other code. This helps prevent naming collisions when working with objects.
const obj = {};
const mySymbol = Symbol();
obj[mySymbol] = 'Hello!';
console.log(obj[mySymbol]); // output: Hello!
console.log(obj); // output: {}
const anotherSymbol = Symbol();
obj[anotherSymbol] = 'World!';
console.log(obj[anotherSymbol]); // output: World!
console.log(obj); // output: {}
In the example above, we create an empty obj
object and assign values to it using symbols as property keys. The console.log(obj)
statement shows that the object remains empty when logged because symbols are not enumerable in for..in
loops or with the Object.keys()
method.
Symbols and Global Symbols
In addition to creating unique symbols, JavaScript provides a set of predefined symbols known as global symbols. These symbols are accessible through the Symbol function, which has properties for common use cases.
- Symbol.iterator: Used to define an iterator method for an object.
- Symbol.match: Used to specify a regular expression method for string matching.replace: Used to specify a regular expression method for string replacement.search: Used to specify a regular expression method for searching within a string.species: Used to define the constructor function that is used to create derived objects.
- (and many more.)
The global symbol properties provide standardized ways of extending built-in JavaScript objects or defining custom behavior using symbols as keys.
The Symbol.for() Method
The Symbol.for()
method is used to create and retrieve shared symbols. Shared symbols are accessible across different realms, such as multiple frames or iframes in a web browser.
const globalSymbol = Symbol.for('myGlobalSymbol');
const localSymbol = Symbol('myLocalSymbol');
console.log(Symbol.keyFor(globalSymbol)); // output: myGlobalSymbol
console.keyFor(localSymbol)); // output: undefined
console.for('myGlobalSymbol') === globalSymbol); // output: true
console.log(Symbol('myLocalSymbol') === localSymbol); // output: false
In the example above, globalSymbol
is a shared symbol created using Symbol.for()
. We can retrieve the key associated with this symbol using the Symbol.keyFor()
method. On the other hand, localSymbol
is a symbol created without using Symbol.for()
, so no key is associated with it.
In Conclusion
Symbols are a unique addition to JavaScript that provide a way to create and use unique identifiers. They are primarily used as property keys in objects to prevent naming collisions. Symbols have their own data type and are not considered objects like arrays or functions.
By understanding symbols and their uses, you can take advantage of their benefits in your JavaScript code.