By: Stella in Javascript Tutorials on 2023年04月25日 [フレーム]
Symbol() is a built-in function in JavaScript that returns a unique symbol value. Symbols are a new primitive data type introduced in ECMAScript 6, which can be used as keys in objects, allowing for more efficient property lookup and avoiding naming collisions.
Here's an example of how to create and use a symbol:
const mySymbol = Symbol('mySymbol');
const obj = {};
obj[mySymbol] = 'Hello, world!';
console.log(obj[mySymbol]); // Output: 'Hello, world!'
In this example, mySymbol is a new symbol value with a description of 'mySymbol'. This description is useful for debugging purposes, but doesn't affect the value of the symbol itself. The obj object is then created, and the mySymbol symbol is used as a key to store the string 'Hello, world!'. This string can then be accessed using the mySymbol key.
One important thing to note is that symbols are always unique, even if they have the same description. For example:
const mySymbol1 = Symbol('mySymbol');
const mySymbol2 = Symbol('mySymbol');
console.log(mySymbol1 === mySymbol2); // Output: false
ven though mySymbol1 and mySymbol2 have the same description, they are two separate symbol values and are not equal to each other.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Javascript )
Dynamically modify the option set in Dynamics 365 forms
promise and .then() in JavaScript
reduce() and filter() in JavaScript
History and evolution of Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
Latest Articles (in Javascript)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate