Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
JAVASCRIPT TEXT / CHAPTER 03 / Learn / LESSON 08
Eighth class. In Data you saw alias versus copy. Here we apply that to sorting and edits.
If you make copy = [...amounts] or amounts.slice(), sorting copy does not touch the original. If you sort the original array, it changes.
Mini example:
const amounts = [40, 12]; const copy = [...amounts]; copy.sort((a, b) => a - b); console.log(amounts); console.log(copy);
Common mistake: sorting the original list when you meant to keep order; or thinking alias = amounts is a copy.
A shallow copy keeps the original order.
DATA STANDBY
›