Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error in the code snippet for Symbol #33765

Merged
merged 1 commit into from
May 27, 2024
Merged

Conversation

chohanbin
Copy link
Contributor

@chohanbin chohanbin commented May 27, 2024

Motivation

Correct the code snippet for demonstrating how a string won't get coerced into a symbol.

Additional details

In the reference doc for Symbol, under description section, the explanation above one of the snippet says:

Note that Symbol("foo") does not coerce the string "foo" into a Symbol.

However, the code snippet says:

Symbol("foo") === Symbol("foo"); // false
MDN JavaScript Symbol doc error

Executing this statement in the Firefox dev console yields true not false as the code snippet states:

>> Symbol.for("foo") === Symbol.for("foo")
true 

I'm guessing that the code snippet was intended to be:

Symbol("foo") === "foo"; // false

Testing this new statement in the dev console:

>> Symbol.for("foo") === "foo"
false 
Testing on Dev console

Related issues and pull requests

The written instruction above the snippet says "Note that Symbol("foo") does not coerce the string "foo" into a Symbol." but the code snippet illustrates "Symbol("foo") === Symbol("foo"); // false" The intention seemed to have been "Symbol("foo") === "foo"; // false"
@chohanbin chohanbin requested a review from a team as a code owner May 27, 2024 00:44
@chohanbin chohanbin requested review from Josh-Cena and removed request for a team May 27, 2024 00:44
@github-actions github-actions bot added Content:JS JavaScript docs size/xs 0-5 LoC changed labels May 27, 2024
Copy link
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Also, congratulations on your first merged MDN content PR. Welcome to the team!

Copy link
Contributor

@estelle estelle merged commit de65135 into mdn:main May 27, 2024
7 checks passed
@Josh-Cena
Copy link
Member

Josh-Cena commented May 27, 2024

@estelle the previous content is correct. It is a demonstration for the following sentence: It creates a new symbol each time, because the result of the comparison is false. If a string has a unique coercion to a symbol then the result should be true (and indeed that's what Symbol.for does: a one-to-one coercion from string to symbol).

I'm traveling this week, but I'll be happy if either of y'all could have a reverting PR ASAP; I will review it. Just so I don't forget to do it when I come back.

@estelle
Copy link
Member

estelle commented May 27, 2024

reverted in #33782

wbamberg added a commit to wbamberg/content that referenced this pull request May 28, 2024
* upstream/main: (55 commits)
  Replace `.` with `#` in example given selectors are `#ids` (mdn#33791)
  update info in cross browser testing strategies (mdn#33730)
  Clarify that `navigator.storage.persist()` depends on heuristics (mdn#33780)
  fix typo (mdn#33785)
  feat: improvements on Glossary/Hoisting (mdn#33787)
  CSS update: overview of shapes guide (mdn#33771)
  CSS update: Shapes from box values (mdn#33770)
  Fix issue 033506: correct droppedEntriesCount (mdn#33538)
  Revert "=== Symbol("foo")" (mdn#33782)
  docs(css): FF126 - Support for `shape()` function (mdn#33446)
  Bump lint-staged from 15.2.4 to 15.2.5 (mdn#33777)
  Bump ajv from 8.13.0 to 8.14.0 (mdn#33776)
  Add missing spaces for `subtlecrypto` (mdn#33774)
  fix: typo in `color_and_luminca` (mdn#33775)
  feat: improvments on gutters (mdn#33751)
  FF127Relnote- data: and javascript: URLS forbidden in base HREF (mdn#33738)
  update the content of SVG `<view>` element (mdn#33710)
  Clipboard.write() - log and fixes (mdn#33769)
  updated ClipboardItem and Clipboard documentation and examples using … (mdn#33262)
  Fix error in the code snippet for Symbol (mdn#33765)
  ...
@chohanbin chohanbin deleted the patch-2 branch June 14, 2024 20:08
@chohanbin
Copy link
Contributor Author

chohanbin commented Jun 14, 2024

My apologies, @estelle and @Josh-Cena . I realize that I mistakenly treated Symbol() and Symbol.for the same (keeping the former in the code change, while testing with the latter in my PR description).

I now see the distinction between the two more clearly, based on what @Josh-Cena had commented, and based on the later section in the same doc:

The above syntax using the Symbol() function will create a Symbol whose value remains unique throughout the lifetime of the program. To create Symbols available across files and even across realms (each of which has its own global scope), use the methods Symbol.for() and Symbol.keyFor() to set and retrieve Symbols from the global Symbol registry.

I'm glad the error I introduced was reverted right away. Thank you and apologies again.

One suggestion I have though, is somehow clarifying this statement a bit further:

Note that Symbol("foo") does not coerce the string "foo" into a Symbol. It creates a new Symbol each time

Perhaps I'm a bit slow-witted than most other readers, but I think appending a clause like this might've clarified things for me a bit sooner:

Note that Symbol("foo") does not coerce the string "foo" into a Symbol. It creates a new Symbol each time, regardless of what gets passed into Symbol()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs size/xs 0-5 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants