JS: what is the purpose of utilizing Symbols when they cannot be accessed?

I'm a bit confused about the role of Symbols if they can't be easily accessed. I understand that you can create one using a key with for and then retrieve it with keyFor.

let keySymbol = Symbol.for("XXX");

console.log(Symbol.keyFor(keySymbol) // "XXX"; 

But when looking at the example below, why would someone do this:

let obj = {
    name: "Mike",
    age: 44,
}
obj[Symbol()] = false;

The symbol field doesn't show up when calling console.log(obj), nor when iterating through the object. So, what is the purpose of adding a symbol if it cannot be accessed or utilized in any way?

Answer №1

To gain a new perspective, consider tweaking your example slightly:

const uniqueKey = Symbol();
obj[uniqueKey] = false;

By retaining the reference to uniqueKey, you retain control over this property. Its value cannot be tampered with or accessed without knowledge of uniqueKey.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Challenges with JavaScript and JQuery - Extracting and Displaying YouTube Information on Website

While I have some experience with Javascript and JQuery, my latest project on FirstPersonTheater.net is not going as planned. I am using PHP to generate a video ID in javascript code to retrieve information about YouTube videos such as title, uploader, vie ...

Having trouble signing out in Nextjs?

As a newcomer to Reactjs and Nextjs, I am currently working on developing an admin panel. To handle the login functionality, I have implemented the following code in my index.js/login page using session storage: const data = { name: email, password: pa ...

The jQuery window.on event is not functioning when trying to handle the same hash change

How to fix jQuery window.on hashchange issue when clicking on the same hash? See the code snippet below for a potential solution: function addMargin() { let header = $('.header__wrapper').outerHeight(); let headerHeight = $('body& ...

Using a combination of Vue.js, Flask, and AWS Cognito for seamless authentication

Currently delving into the realm of Vue JS, I have grasped the concept of Vue JS <-> AWS Cognito authentication process and the integration of Vue JS <-> Flask. I am now curious to explore how to authenticate a user using AWS Cognito with Vue J ...

Dynamically populate 7 select boxes with options using JQuery

I have a webpage that includes 14 different dropdown menus, one for each day of the week (Monday to Sunday). Each day has two dropdowns: one for opening time and one for closing time. I used jQuery to populate all 14 dropdowns with a pre-defined list of ho ...

Exploring ways to connect my React application with a node backend on the local network?

On my MacBook, I developed a react app that I access at http://localhost:3000. In addition, I have a nodejs express mysql server running on http://localhost:5000. The issue arises when I try to open the IP address with port 3000 in the browser of my Window ...

What is the best way to retrieve properties from a different module in JavaScript?

I have a module named ProgressIndicator: ... export default class ProgressIndicator { constructor() { ... const indicatorGeometry = new THREE.PlaneGeometry(2, 2, 1, 1) const indicatorMaterial = new THREE.ShaderMate ...

Arranging elements based on specific coordinates

const renderTimeSlots = () => { const timeSlots = []; for (let i = parseInt(workStartsAt); i <= parseInt(workEndsAt); i++) { if (i !== 0) { timeSlots.push( <div className="flex flex-row cursor-pointer"> ...

Tips for showcasing the stored value from the database in a table

This problematic table was created: http://jsfiddle.net/ofd3nox3/ I am currently facing an issue with displaying the values stored in the database for this table per user. For example, a user has specified availability on Thursdays in the morning, Friday ...

Hear and register keypress in Angular service

I offer a dialog service Check it out below @Injectable() export class HomeDialogService { public constructor(private readonly dialogService: DialogService, private readonly userAgent: UserAgentService) {} @HostListener('document:keydown.escape ...

Capturing numerous data points with JavaScript

<span> <label class="label">Color</label> <span class="foo"><input name="Color" value="Blue" class="customs" maxlength="100" type="text"/></span> </span> </span> <span> <label cla ...

Instructions on how to modify a document's content by its unique identifier using Firebase Modular SDK (V9)

I am trying to figure out how to update an existing document for the same user ID in V9 Firebase whenever they log in, rather than creating a new one each time. Any suggestions on how to achieve this? Current Code setDoc( query(collectionRef), // ...

What is the best way to access event.target as an object in Angular 2?

Apologies for my limited English proficiency. . I am trying to write code that will call myFunction() when the user clicks anywhere except on an element with the class .do-not-click-here. Here is the code I have written: document.addEventListener(' ...

What is the best way to fill a dropdown menu with names and corresponding numeric IDs?

How can I effectively link my dropdown to a variable in the controller, using the id property of the dropdown array instead of the value for assignment? Meanwhile, still displaying the content of the drop down using the name property of the array for user ...

Steps to cancel an AJAX call in C# .NET:

In the ajax.cs class, I am calling methods from the javascript side. To register this on the default.aspx page load, I did the following: Ajax.Utility.RegisterTypeForAjax(typeof(ajax)); Occasionally, some methods take a long time to execute. In such case ...

What is the correct way to dynamically switch between RTL and LTR in React with Material UI?

I recently learned that in order to support right-to-left (RTL) languages with Material UI, you need to follow these steps. I have a select input that allows users to switch between languages, changing the overall direction of the app. The core of my appl ...

Refresh the image source using the AJAX success callback

Previously, I was updating Label values within the AJAX success function as shown below. Now, I am looking for a way to use the same method to change or update the "src" attribute of an <img id="myimage" src=""/> $.ajax({ url: 'clmcontrol_l ...

Obtaining every PDF file linked within a designated div/table section of a website

Is there a way to download all PDFs linked in a table on a webpage using javascript? Edit: To clarify, I am trying to find a solution where Javascript can automatically download all the PDFs contained within a specific table on a webpage viewed in a brow ...

Clear a variable that was sent through the post method in an ajax call

Within my JavaScript file, I have the following Ajax code: // Default settings for Ajax requests $.ajaxSetup({ type: 'POST', url: path + '/relay.php' + '?curr=' + currency + "&ver=" + Ma ...

The affluent text editor's input field fails to retain information

Currently, I am facing an issue with my HTML page. When using a plain HTML textarea, the pre-set options work fine. However, when switching to a rich editor, the textarea stops holding data as expected. Please refer to the demo pages for clarification. Pa ...