JavaScript – Symbols


What is Symbol ?

A Symbol is something like a character, which is used as a conventional representation of someother object.

What is a symbol in programming language ?

A Symbol is a primitive data type which is in human-readable form.

Symbol can be used as identifiers.

What is Symbol in Javascript ?

Symbols in Javascript is a new primitive date type like Number,String and Boolean.

Symbol is a function that is used to identify the object properties.

What are the features of Symbol ?

Some of the features of Symbol is,

1. Symbol() method will always returns a unique value.

2. Symbol values may be used as an identifier for object properties.

3. Symbols are immutable, just like numbers and strings.

4. Symbols cannot be typecasted to primitive data types.

Best javascript tutorial !!!

Click to Learn More about – Javascript online learning

Syntax :


Symbol([description])  

Symbol – This defines the Symbol


description – Its a String value passed as param, this is an optional parameter,

1. Converting Symbol with variable

Symbol ‘id’ is combined with var ‘user’.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
		var user = { name: "Abdul Kalaam" };
		var id = Symbol("id");
		user[id] = "Some User ID Value";
		document.write("Printing the user name value = " +  user.name +"<br/>"); 
		document.write("Printing the combined user[id] value = " +  user[id] +"<br/>"); 
		</script>
	</head>
	<body></body>
</html>

Output :

Printing the user name value = Abdul Kalaam

Printing the combined user[id] value = Some User ID Value

There are few the Symbol properties in javascript listed below,

SnoPropertiesDescription
1Symbol.hasInstance()Symbol.hasInstance() property is used to determine if a constructor object recognizes an object as its instance.
2Symbol.isConcatSpreadable()Symbol.isConcatSpreadable() property is used to configure if an object should be flattened to its array elements.
3Symbol.match()Symbol.match() property is a method to identify the matching of a regular expression against a string.
4Symbol.prototype()Symbol.prototype() property is used for produce the prototype for the symbol constructor.
5Symbol.replace()Symbol.replace() property replaces matched substring of a string.
6Symbol.search()Symbol.search() property returns the index within a string that matches with the regular expression.
7Symbol.split()Symbol.split() property splits a string at the indices that match the regular expression.
8Symbol.toPrimitive()Symbol.toPrimitive() property is used to convert an object to its equivalent primitive value.
9Symbol.unscopables()Symbol.unscopables() property is a well-known object property whose property name are excluded from with environment.

Basics of javascript for beginners !!!

Click to Learn More about – Javascript online learning

There are few the Symbol methods in javascript listed below,

SnoMethods / FunctionDescription
1Symbol.for()Symbol.for() method is used to search for existing symbol in a runtime-wide symbol registry with the provided key and returns if it is found.
Otherwise new symbol gets created with this key.
2Symbol.keyFor()Symbol.keyFor() method uses the global symbol registry to look up the key for the symbol.
So it doesn’t work for non-global symbols.
If the symbol is not global, it won’t be able to find it and returns undefined.
3Symbol.toString()Symbol.toString() method is used to return a string representation of an object.