6. Confusing Arrays with Slices
Ricardo Maia
Senior Fullstack Software Engineer | Senior Front-End Engineer | Senior Back-End Engineer | React | NextJs | Typescript | Angular | Go | AWS | DevOps
Arrays and slices behave differently, and confusing the two can cause problems.
Example:
Modifying the slice also modifies the underlying array, which may not be expected.
Differences between Arrays and Slices
Modifying Slices Affects the Underlying Array
When modifying a slice, the underlying array is also affected because both share the same data storage. This can lead to unexpected behaviors if misunderstood.
Example:
Output:
CopyEdit
Array before: [1 2 3] Array after: [10 2 3]
Explanation
How to Avoid Issues?
Now, modifying newSlice won't affect the original array.
2. Always review the scope of changes made to a slice to understand where they will have an impact.
This distinction between arrays and slices is powerful but requires caution to avoid surprises!
Fullstack Engineer | Software Developer | React | Next.js | TypeScript | Node.js | JavaScript | AWS
1 个月Insightful
.NET Software Engineer | Full Stack Developer | C# | Angular & Blazor | Azure & AWS | Microservices Expert
1 个月Nice content! Thanks for sharing
Senior Software Engineer | Ruby On Rails | Backend Developer | AWS | Heroku
1 个月Great content!
AI Solutions Architecture | LLM ML Engineer | Golang | Kotlin | Flutter | React Native | Angular | Figma | Java | .Net | Nodejs | DevOps | Maven | JUnit | CI/CD | GitHub | Design Patterns | Multicloud
1 个月I agree
AI Solutions Architecture | LLM ML Engineer | Golang | Kotlin | Flutter | React Native | Angular | Figma | Java | .Net | Nodejs | DevOps | Maven | JUnit | CI/CD | GitHub | Design Patterns | Multicloud
1 个月Very helpful