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

Should conceptually ordered enums still be strings? #443

Open
LeaVerou opened this issue May 16, 2023 · 1 comment
Open

Should conceptually ordered enums still be strings? #443

LeaVerou opened this issue May 16, 2023 · 1 comment
Labels
tc39-tracker Issues where TC39 input would be useful Topic: JS

Comments

@LeaVerou
Copy link
Member

We currently have the blanket guidance that all enums should be strings, not numbers.

Indeed, JS engines have no performance benefit from using numbers instead of strings. However, when the enum is ordered, numbers offer an additional benefit: comparison.
Case in point: HTMLMediaElement.readyState. A developer can do:

if (myAudio.readyState < HTMLMediaElement.HAVE_FUTURE_DATA) {
	// ...
}

With strings, this would look like:

if (myAudio.readyState === "nothing" || myAudio.readyState === "metadata" || myAudio.readyState === "current") {
 // ...
}

which is far more awkward.

Therefore, I'm not sure it's the latter we should be recommending. Using ints here seems like a reasonable choice by the API designers.

However, if we change our guidance to be "you can use numbers when the enum is conceptually ordered", doesn't that make the Web Platform inconsistent, when some enums are strings and others are numbers (of course it already is inconsistent anyway, but the point of the design principles is to reduce inconsistency over time, not encourage it).

@LeaVerou LeaVerou added Topic: JS tc39-tracker Issues where TC39 input would be useful labels May 16, 2023
@annevk
Copy link
Member

annevk commented May 16, 2023

I don't think this example is compelling enough to change the advice. Also, while it might take a little longer to write, it's quite a bit clearer.

And if enough people end up writing something like that, that might perhaps be reason to add an instance method for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tc39-tracker Issues where TC39 input would be useful Topic: JS
Projects
None yet
Development

No branches or pull requests

2 participants