-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Docs: Explain shallow copy behavior for slice method #320
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
Open
kartikeymishra2202
wants to merge
1
commit into
sudheerj:master
from
kartikeymishra2202:docs/clarify-slice-copy
Open
Docs: Explain shallow copy behavior for slice method #320
kartikeymishra2202
wants to merge
1
commit into
sudheerj:master
from
kartikeymishra2202:docs/clarify-slice-copy
+4,303
−4,240
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ramkrishna-KGL732
Ramkrishna-KGL732
approved these changes
Oct 12, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
##The Problem:Question-5.
The current documentation for Array.prototype.slice() is correct but incomplete. It omits the crucial detail that it performs a shallow copy. This can be misleading and lead to hard-to-debug issues when developers work with arrays of objects, as the original array's nested objects can be unintentionally mutated.
The Solution
This PR addresses this gap by making the behavior explicit and providing a clear example. The changes include:
Adding a new subsection, "A Note on Shallow Copying", to the slice() documentation.
Clearly explaining that slice() copies references to nested objects, not the objects themselves.
Providing a concise code example that demonstrates this effect.
Updating the slice vs. splice comparison table to reflect this important detail for quick reference.
The Benefit
This clarification will help developers avoid common pitfalls and prevent unintentional mutations. It makes the documentation more robust and helpful, especially for those who are new to this concept.