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

TypedArray subclass pages say contents are initialized to 0 #33743

Closed
rhashimoto opened this issue May 24, 2024 · 3 comments · Fixed by #34071
Closed

TypedArray subclass pages say contents are initialized to 0 #33743

rhashimoto opened this issue May 24, 2024 · 3 comments · Fixed by #34071
Labels
Content:JS JavaScript docs goal: accuracy (Experimental label) Issues about inaccurate/incorrect content.

Comments

@rhashimoto
Copy link
Contributor

MDN URL

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array

What specific section or headline is this issue about?

main heading

What information was incorrect, unhelpful, or incomplete?

For each of BigInt64Array, BigUint64Array, Float32Array, etc. (all the TypedArray subclasses), the initial description contains:

The contents are initialized to 0. [or 0n]

This initialization is only done if the TypedArray subclass is created by passing a length to the constructor. It is not done if the instance is created from an existing array or a buffer.

Try this in a console:

const b0 = new Uint8Array([42]);
const b1 = new Uint8Array(b0.buffer);
console.log(b1[0]);  // <= prints 42, *not* 0

What did you expect to see?

It should say something like:

The contents are initialized to 0 [or 0n for BigInt64Array or BigUint64Array] if constructed with only a length.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@rhashimoto rhashimoto added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label May 24, 2024
@github-actions github-actions bot added the Content:JS JavaScript docs label May 24, 2024
@Josh-Cena
Copy link
Member

It should say "initialized to 0 unless an existing buffer is provided".

@Josh-Cena Josh-Cena added goal: accuracy (Experimental label) Issues about inaccurate/incorrect content. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels May 31, 2024
@rhashimoto
Copy link
Contributor Author

rhashimoto commented May 31, 2024

It should say "initialized to 0 unless an existing buffer is provided".

That isn't quite correct, either. For example:

new Uint8Array([1, 2, 3]);

Here a buffer is not provided and the array is not initialized to 0.

Changing it to "existing buffer or array" is closer but still not exactly right, e.g.:

new Uint8Array(new Set([1, 2, 3]));
new Uint8Array((function*() { yield 1; yield 2; yield 3; })());

Maybe "initialized to 0 unless an existing buffer or initialization data is provided"?

@Josh-Cena
Copy link
Member

Yeah, you are right. "unless initialization data is explicitly provided" sounds better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs goal: accuracy (Experimental label) Issues about inaccurate/incorrect content.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants