{"id":6691,"date":"2020-04-14T14:59:36","date_gmt":"2020-04-14T14:59:36","guid":{"rendered":"https:\/\/cheesecakelabs.com\/blog\/?p=6691\/"},"modified":"2026-08-15T08:09:06","modified_gmt":"2026-08-15T08:09:06","slug":"avoid-array-mutation","status":"publish","type":"post","link":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/","title":{"rendered":"How to Avoid Array Mutation"},"content":{"rendered":"<p style=\"text-align: center;\"><em>Originally posted on <a href=\"https:\/\/dev.to\/helderburato\/how-to-avoid-array-mutation-4bpa\">dev.to<\/a><\/em><\/p>\n<p><span style=\"font-weight: 400;\">In this article, I&#8217;ll focus on showing how to add, edit and remove items in an array causing mutation and non-mutation ways.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">One thing we need to keep in mind when writing code avoiding mutation is to return a new reference to the data after the update.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It&#8217;s a common approach when working with functional programming, and if you want to understand some concepts of functional programming, I recommend you read this <\/span><a href=\"https:\/\/dev.to\/helderburato\/understanding-concepts-of-functional-programming-with-javascript-2g1d\"><span style=\"font-weight: 400;\">article<\/span><\/a><span style=\"font-weight: 400;\"> I wrote some time ago.<\/span><\/p>\n<p><!--more--><\/p>\n<h2><span style=\"font-weight: 400;\">Why should you avoid array mutation?<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">You should avoid array mutation so that every update returns a new reference to the data instead of changing the array in place. That makes it safe to add, edit, and remove items without breaking code that still holds the original array. Working with immutable data has these positive impacts:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8211; Tracking data without mutation is much better;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8211; Immutable states help you implement unidirectional data flow that helps you handle data;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">I really recommend you read this <\/span><a href=\"https:\/\/alistapart.com\/article\/why-mutation-can-be-scary\/\"><span style=\"font-weight: 400;\">article<\/span><\/a><span style=\"font-weight: 400;\"> to go deeper into why you should avoid mutation.<\/span><\/p>\n<h2>Which array methods cause mutation?<\/h2>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-6695\" src=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/mutation.jpg\" alt=\"\" width=\"2108\" height=\"962\" srcset=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/mutation.jpg 2108w, https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/mutation-768x350.jpg 768w\" sizes=\"(max-width: 2108px) 100vw, 2108px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Array methods like push(), unshift(), splice(), pop(), and shift() cause mutation because they change the original array in place instead of returning a new one. The steps below cause mutation in the array when adding, removing, and editing elements from `family`.<\/span><\/p>\n<p>To show an example of mutating, we&#8217;ll use the following array:<\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">const heroesMutate = ['Spider-man', 'Thor', 'Hulk', 'Iron Man'];\nconsole.log(heroesMutate); \/\/ =&gt; [\"Spider-man\", \"Thor\", \"Hulk\", \"Iron Man\"]\n<\/code><\/pre>\n<h3>Including in the Array<\/h3>\n<p>Methods that will be used:<\/p>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-PT\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/push\"><span style=\"font-weight: 400;\">Array.prototype.push()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-PT\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/unshift\"><span style=\"font-weight: 400;\">Array.prototype.unshift()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-PT\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/splice\"><span style=\"font-weight: 400;\">Array.prototype.splice()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">See the following use-case examples for these methods:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nheroesMutate.push('Captain Marvel');\nconsole.log(heroesMutate); \/\/ =&gt; [\"Spider-man\", \"Thor\", \"Hulk\", \"Iron Man\", \"Captain Marvel\"]\n\nheroesMutate.unshift('Deadpool');\nconsole.log(heroesMutate); \/\/ =&gt; [\"Deadpool\", \"Spider-man\", \"Thor\", \"Hulk\", \"Iron Man\", \"Captain Marvel\"]\n\nheroesMutate.splice(2, 0, 'Black Panther');\nconsole.log(heroesMutate); \/\/ =&gt; [\"Deadpool\", \"Spider-man\", \"Black Panther\", \"Thor\", \"Hulk\", \"Iron Man\", \"Captain Marvel\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Editing the Array<\/h3>\n<p><span style=\"font-weight: 400;\">The following case will find index for the element we want to edit and set value to the index found:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst indexDeadpool = heroesMutate.indexOf('Deadpool');\nheroesMutate[indexDeadpool] = 'Wolverine';\n\nconsole.log(heroesMutate); \/\/ =&gt; [\"Wolverine\", \"Spider-man\", \"Black Panther\", \"Thor\", \"Hulk\", \"Iron Man\", \"Captain Marvel\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Removing in the Array<\/h3>\n<p><span style=\"font-weight: 400;\">Methods that will be used:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/pop\"><span style=\"font-weight: 400;\">Array.prototype.pop()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/shift\"><span style=\"font-weight: 400;\">Array.prototype.shift()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-PT\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/splice\"><span style=\"font-weight: 400;\">Array.prototype.splice()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">See the following use-case examples for these methods:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nheroesMutate.pop();\nconsole.log(heroesMutate); \/\/ =&gt; [\"Wolverine\", \"Spider-man\", \"Black Panther\", \"Thor\", \"Hulk\", \"Iron Man\"]\n\nheroesMutate.shift();\nconsole.log(heroesMutate); \/\/ =&gt; [\"Spider-man\", \"Black Panther\", \"Thor\", \"Hulk\", \"Iron Man\"]\n\nheroesMutate.splice(1, 1);\nconsole.log(heroesMutate); \/\/ =&gt; [\"Spider-man\", \"Thor\", \"Hulk\", \"Iron Man\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>How do you update an array without mutating it?<\/h2>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-6694\" src=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/without-mutation.jpg\" alt=\"\" width=\"2108\" height=\"962\" srcset=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/without-mutation.jpg 2108w, https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/without-mutation-768x350.jpg 768w\" sizes=\"(max-width: 2108px) 100vw, 2108px\" \/><\/p>\n<p>To update an array without mutating it, use methods that return a new array instead of changing the original: slice(), concat(), map(), filter(), and spread syntax. In this topic, we&#8217;ll add, remove, and edit items this way, so a new instance of the array is created every time.<\/p>\n<p><span style=\"font-weight: 400;\">Methods that will be used:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/slice\"><span style=\"font-weight: 400;\">Array.prototype.slice()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/concat\"><span style=\"font-weight: 400;\">Array.prototype.concat()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-BR\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map\"><span style=\"font-weight: 400;\">Array.prototype.map()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-BR\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/filtro\"><span style=\"font-weight: 400;\">Array.prototype.filter()<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Spread_syntax\"><span style=\"font-weight: 400;\">Spread syntax<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">See the following use-cases:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst villains = ['Loki', 'Thanos', 'Venom', 'Abomination'];\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Including in the Array<\/h3>\n<p><span style=\"font-weight: 400;\">Add to the end of the array:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst newVillains = villains.concat('Juggernaut');\nconst newVillains2 = [...newVillains, 'Magneto'];\nconst newVillains3 = ['Red Skull', ...newVillains2];\n\nconsole.log(villains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\"]\nconsole.log(newVillains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\", \"Juggernaut\"]\nconsole.log(newVillains2); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\", \"Juggernaut\", \"Magneto\"]\nconsole.log(newVillains3); \/\/ =&gt; [\"Red Skull\", \"Loki\", \"Thanos\", \"Venom\", \"Abomination\", \"Juggernaut\", \"Magneto\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">In the following example we&#8217;ll add <\/span><b>Ultron<\/b><span style=\"font-weight: 400;\">&nbsp; after <\/span><b>Thanos<\/b><span style=\"font-weight: 400;\"> in the array:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst newVillains = [...villains.slice(0, 2), 'Ultron', ...villains.slice(2, villains.length)];\n\nconsole.log(villains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\"]\nconsole.log(newVillains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Ultron\", \"Venom\", \"Abomination\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Editing the Array<\/h3>\n<p><span style=\"font-weight: 400;\">In the following example we&#8217;ll edit `Venom` to `Galactus`:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst indexVenom = villains.indexOf('Venom');\nconst newVillains = [...villains.slice(0, indexVenom), 'Galactus', ...villains.slice(indexVenom+1)];\nconst newVillains2 = newVillains.map(v =&gt; v === 'Abomination' ? 'Ultron' : v);\n\nconsole.log(villains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\"]\nconsole.log(newVillains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Galactus\", \"Abomination\"]\nconsole.log(newVillains2); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Galactus\", \"Ultron\"]\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Removing in the Array<\/h3>\n<p><span style=\"font-weight: 400;\">In the following example we&#8217;ll remove <\/span><b>Thanos<\/b><span style=\"font-weight: 400;\"> from the array:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">\nconst indexThanos = villains.indexOf('Thanos');\nconst newVillains = [...villains.slice(0, indexHelder), ...villains.slice(indexHelder+1)];\nconst newVillains2 = newVillains.filter(v =&gt; v !== 'Thanos');\n\nconsole.log(villains); \/\/ =&gt; [\"Loki\", \"Thanos\", \"Venom\", \"Abomination\"]\nconsole.log(newVillains); \/\/ =&gt; [\"Loki\", \"Venom\", \"Abomination\"]\nconsole.log(newVillains2); \/\/ =&gt; [\"Loki\", \"Abomination\"]\n<\/code><\/pre>\n<p>See that in all the examples that we developed above, a new instance of the array was created, thus avoiding the mutation of the initially defined arrays.<\/p>\n<p>&nbsp;<\/p>\n<h2>Wrapping Up<\/h2>\n<p>Avoiding mutations is a safe and one-way path.<\/p>\n<p><span style=\"font-weight: 400;\">When you realize that you&#8217;re writing code observing this type of detail, believe me, you will be writing better, secure code and avoiding possible bugs due to mutation.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Feel free to share your feedback and experience in the comments.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Enjoy programming! \u2728<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">References<\/span><\/h2>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.mozilla.org\/pt-PT\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\"><span style=\"font-weight: 400;\">Array &#8211; JavaScript | MDN<\/span><\/a><span style=\"font-weight: 400;\">;<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/www.marvel.com\/teams-and-groups\">Marvel Teams, Groups, Squads, &amp; Alliances<\/a>;<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Originally posted on dev.to In this article, I&#8217;ll focus on showing how to add, edit and remove items in an array causing mutation and non-mutation ways. One thing we need to keep in mind when writing code avoiding mutation is to return a new reference to the data after the update. It&#8217;s a common approach [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":6693,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_yoast_wpseo_title":"Avoid Array Mutation in JavaScript: Methods and Examples","_yoast_wpseo_metadesc":"Learn how to avoid array mutation when adding, editing, and removing array items while maintaining immutability and returning new data references.","_yoast_wpseo_meta-robots-noindex":"","_yoast_wpseo_canonical":"","footnotes":"","ckl_wpml_lang":"","ckl_wpml_source_id":0,"ckl_wpml_status":""},"categories":[1425,432],"tags":[303,400],"class_list":["post-6691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-product-engineering","category-engineering","tag-tag-frontend","tag-tag-javascript"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Avoid Array Mutation in JavaScript: Methods and Examples<\/title>\n<meta name=\"description\" content=\"Learn how to avoid array mutation when adding, editing, and removing array items while maintaining immutability and returning new data references.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Avoid Array Mutation in JavaScript: Methods and Examples\" \/>\n<meta property=\"og:description\" content=\"Learn how to avoid array mutation when adding, editing, and removing array items while maintaining immutability and returning new data references.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\" \/>\n<meta property=\"og:site_name\" content=\"Cheesecake Labs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cheesecakelabs\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-14T14:59:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-08-15T08:09:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2000\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Cheesecake Labs\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cheesecakelabs\" \/>\n<meta name=\"twitter:site\" content=\"@cheesecakelabs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\"},\"author\":{\"name\":\"Helder Burato Berto\"},\"headline\":\"How to Avoid Array Mutation\",\"datePublished\":\"2020-04-14T14:59:36+00:00\",\"dateModified\":\"2026-08-15T08:09:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\"},\"wordCount\":541,\"publisher\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png\",\"keywords\":[\"Front-end\",\"JavaScript\"],\"articleSection\":[\"Product Engineering\",\"Engineering\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\",\"url\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\",\"name\":\"Avoid Array Mutation in JavaScript: Methods and Examples\",\"isPartOf\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png\",\"datePublished\":\"2020-04-14T14:59:36+00:00\",\"dateModified\":\"2026-08-15T08:09:06+00:00\",\"description\":\"Learn how to avoid array mutation when adding, editing, and removing array items while maintaining immutability and returning new data references.\",\"breadcrumb\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage\",\"url\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png\",\"contentUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png\",\"width\":2000,\"height\":720,\"caption\":\"Round purple puzzle piece becoming a square purple puzzle piece\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cheesecakelabs.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Avoid Array Mutation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#website\",\"url\":\"https:\/\/cheesecakelabs.com\/blog\/\",\"name\":\"Cheesecake Labs\",\"description\":\"AI Implementation, Data and Product Engineering\",\"publisher\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cheesecakelabs.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#organization\",\"name\":\"Cheesecake Labs\",\"alternateName\":\"Cheesecake Labs Inc\",\"url\":\"https:\/\/cheesecakelabs.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cheesecakelabs.com\/#logo\",\"url\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2022\/06\/cheesecake-labs-1.png\",\"contentUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2022\/06\/cheesecake-labs-1.png\",\"caption\":\"Cheesecake Labs\",\"inLanguage\":\"en\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cheesecakelabs.com\/#primary-image\",\"url\":\"https:\/\/ckl-website-v4-strapi-prod.s3.us-east-2.amazonaws.com\/ai_software_development_company_83fb512983.webp\",\"contentUrl\":\"https:\/\/ckl-website-v4-strapi-prod.s3.us-east-2.amazonaws.com\/ai_software_development_company_83fb512983.webp\",\"width\":1920,\"height\":1080,\"caption\":\"Cheesecake Labs \u2014 AI, Data & Blockchain software development services\",\"inLanguage\":\"en\"},\"sameAs\":[\"https:\/\/www.facebook.com\/cheesecakelabs\",\"https:\/\/x.com\/cheesecakelabs\",\"https:\/\/www.instagram.com\/cheesecakelabs\/\",\"https:\/\/www.linkedin.com\/company\/cheesecake-labs\/\",\"https:\/\/www.youtube.com\/channel\/UCdGEQ5AHJcmIlaOaI5fGGVA\",\"https:\/\/clutch.co\/profile\/cheesecake-labs\",\"https:\/\/www.behance.net\/cheesecakelabs\",\"https:\/\/dribbble.com\/cheesecakelabs\",\"https:\/\/www.designrush.com\/agency\/profile\/cheesecake-labs\",\"https:\/\/www.g2.com\/products\/cheesecake-labs\/reviews\"],\"description\":\"Cheesecake Labs is a software development studio that designs and builds custom digital products \u2014 web, mobile, and platforms \u2014 combining product design and high-performance engineering.\",\"foundingDate\":\"2013\"},{\"@type\":\"Person\",\"name\":\"Helder Burato Berto\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#\/schema\/person\/image\/\",\"url\":false,\"contentUrl\":false,\"caption\":\"Helder Burato Berto\"},\"description\":\"10 years of experience in Marketing and Sales in the Technology sector. My main purpose is help, support and structure efficient operations and also develop independent and multidisciplinary teams.\",\"url\":\"https:\/\/cheesecakelabs.com\/blog\/autor\/helder\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Avoid Array Mutation in JavaScript: Methods and Examples","description":"Learn how to avoid array mutation when adding, editing, and removing array items while maintaining immutability and returning new data references.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/","og_locale":"en_US","og_type":"article","og_title":"Avoid Array Mutation in JavaScript: Methods and Examples","og_description":"Learn how to avoid array mutation when adding, editing, and removing array items while maintaining immutability and returning new data references.","og_url":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/","og_site_name":"Cheesecake Labs","article_publisher":"https:\/\/www.facebook.com\/cheesecakelabs","article_published_time":"2020-04-14T14:59:36+00:00","article_modified_time":"2026-08-15T08:09:06+00:00","og_image":[{"width":2000,"height":720,"url":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png","type":"image\/png"}],"author":"Cheesecake Labs","twitter_card":"summary_large_image","twitter_creator":"@cheesecakelabs","twitter_site":"@cheesecakelabs","twitter_misc":{"Written by":null,"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#article","isPartOf":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/"},"author":{"name":"Helder Burato Berto"},"headline":"How to Avoid Array Mutation","datePublished":"2020-04-14T14:59:36+00:00","dateModified":"2026-08-15T08:09:06+00:00","mainEntityOfPage":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/"},"wordCount":541,"publisher":{"@id":"https:\/\/cheesecakelabs.com\/blog\/#organization"},"image":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage"},"thumbnailUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png","keywords":["Front-end","JavaScript"],"articleSection":["Product Engineering","Engineering"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/","url":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/","name":"Avoid Array Mutation in JavaScript: Methods and Examples","isPartOf":{"@id":"https:\/\/cheesecakelabs.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage"},"image":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage"},"thumbnailUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png","datePublished":"2020-04-14T14:59:36+00:00","dateModified":"2026-08-15T08:09:06+00:00","description":"Learn how to avoid array mutation when adding, editing, and removing array items while maintaining immutability and returning new data references.","breadcrumb":{"@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#primaryimage","url":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png","contentUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2020\/04\/avoid_mutation.png","width":2000,"height":720,"caption":"Round purple puzzle piece becoming a square purple puzzle piece"},{"@type":"BreadcrumbList","@id":"https:\/\/cheesecakelabs.com\/blog\/avoid-array-mutation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cheesecakelabs.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Avoid Array Mutation"}]},{"@type":"WebSite","@id":"https:\/\/cheesecakelabs.com\/blog\/#website","url":"https:\/\/cheesecakelabs.com\/blog\/","name":"Cheesecake Labs","description":"AI Implementation, Data and Product Engineering","publisher":{"@id":"https:\/\/cheesecakelabs.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cheesecakelabs.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cheesecakelabs.com\/blog\/#organization","name":"Cheesecake Labs","alternateName":"Cheesecake Labs Inc","url":"https:\/\/cheesecakelabs.com\/","logo":{"@type":"ImageObject","@id":"https:\/\/cheesecakelabs.com\/#logo","url":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2022\/06\/cheesecake-labs-1.png","contentUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2022\/06\/cheesecake-labs-1.png","caption":"Cheesecake Labs","inLanguage":"en"},"image":{"@type":"ImageObject","@id":"https:\/\/cheesecakelabs.com\/#primary-image","url":"https:\/\/ckl-website-v4-strapi-prod.s3.us-east-2.amazonaws.com\/ai_software_development_company_83fb512983.webp","contentUrl":"https:\/\/ckl-website-v4-strapi-prod.s3.us-east-2.amazonaws.com\/ai_software_development_company_83fb512983.webp","width":1920,"height":1080,"caption":"Cheesecake Labs \u2014 AI, Data & Blockchain software development services","inLanguage":"en"},"sameAs":["https:\/\/www.facebook.com\/cheesecakelabs","https:\/\/x.com\/cheesecakelabs","https:\/\/www.instagram.com\/cheesecakelabs\/","https:\/\/www.linkedin.com\/company\/cheesecake-labs\/","https:\/\/www.youtube.com\/channel\/UCdGEQ5AHJcmIlaOaI5fGGVA","https:\/\/clutch.co\/profile\/cheesecake-labs","https:\/\/www.behance.net\/cheesecakelabs","https:\/\/dribbble.com\/cheesecakelabs","https:\/\/www.designrush.com\/agency\/profile\/cheesecake-labs","https:\/\/www.g2.com\/products\/cheesecake-labs\/reviews"],"description":"Cheesecake Labs is a software development studio that designs and builds custom digital products \u2014 web, mobile, and platforms \u2014 combining product design and high-performance engineering.","foundingDate":"2013"},{"@type":"Person","name":"Helder Burato Berto","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cheesecakelabs.com\/blog\/#\/schema\/person\/image\/","url":false,"contentUrl":false,"caption":"Helder Burato Berto"},"description":"10 years of experience in Marketing and Sales in the Technology sector. My main purpose is help, support and structure efficient operations and also develop independent and multidisciplinary teams.","url":"https:\/\/cheesecakelabs.com\/blog\/autor\/helder\/"}]}},"_links":{"self":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts\/6691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/comments?post=6691"}],"version-history":[{"count":2,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts\/6691\/revisions"}],"predecessor-version":[{"id":14492,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts\/6691\/revisions\/14492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/media\/6693"}],"wp:attachment":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/media?parent=6691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/categories?post=6691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/tags?post=6691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}