Immutable Updates with "with"

I have been a full-stack developer for twelve years and am currently part of the Cloud FinOps team at Atlassian. I previously ran an App development agency, managing various teams across design and development, leading web, backend and native mobile app developers in Australia and Europe. My strengths are system architecture design and communicating complicated tech to everyday people. Having helped scale tech at numerous startups, I am passionate about making a positive difference. When not working, you can find me writing tech articles and teaching Javascript and React at SheCodes, an initiative helping to get more women into tech. I regularly give talks on various topics, focusing on the impact the technology we build can have. I am devastated by the state of the world, love a good political rant and can often be swearing at no one in particular. I enjoy building things in JavaScript, Deno and Node.
Are you looking to update the value in an array but not change the original? Say hello to with(). The with function allows you to update a value in an array but return a copy with the updated value, not update the original. Pretty simple to use:
const consoles = ["PS4", "Switch"]
const updatedConsoles = consoles.with(0, "PS5")
console.log(updatedConsoles) // [ 'PS5', 'Switch' ]
The with function has two parameters: the index you want to update and the value you want to update it with. Trying to update an index that is not in the array will throw a range error. with is currently supported in all major browsers and is a handy function for immutable array transformations.



