# Why is splice() not working in the web editor?

**URL:** <https://discourse.processing.org/t/why-is-splice-not-working-in-the-web-editor/41870>\
**Category:** Beginners\
**Created:** [April 29, 2023, 3:01pm UTC](https://discourse.processing.org/t/why-is-splice-not-working-in-the-web-editor/41870 "2023-04-29T15:01:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![swenzel](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@swenzel](https://discourse.processing.org/u/swenzel)\
**Post date:** [April 29, 2023, 3:01pm UTC](https://discourse.processing.org/t/why-is-splice-not-working-in-the-web-editor/41870/1 "2023-04-29T15:01:27Z")

</div>

I may be missing something very simple here. I am trying to use splice to add 0 onto the front of this array. However, the result of both console logs are [0,1,2,3,4,5]. It’s like it is retroactively adding the zero to the first console log. I try this exact code on other javascript editors and it works as intended. Any help would be great.

```auto
function setup() {
  let A = [1,2,3,4,5]
  console.log(A)
  A.splice(0,0,0)
  console.log(A)
}

```

---

<div class="post-metadata">

**Author:** ![swenzel](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@swenzel](https://discourse.processing.org/u/swenzel)\
**Post date:** [April 29, 2023, 3:15pm UTC](https://discourse.processing.org/t/why-is-splice-not-working-in-the-web-editor/41870/2 "2023-04-29T15:15:00Z")

</div>

```auto
function setup() {
  createCanvas(400, 400);
  let A = [2,3,4,5,6]
  console.log(A.length)
  console.log(A)
  console.log(A.length)
  if(A.length == 5){
    A.push(1) 
    console.log(A)
  }
}

```

More confusion on my part. Screenshot of console:

 ![Screen Shot 2023-04-29 at 9.14.39 AM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/a/ba45abf8035aae49782108c766b250137635c76f.png)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 29, 2023, 4:14pm UTC](https://discourse.processing.org/t/why-is-splice-not-working-in-the-web-editor/41870/3 "2023-04-29T16:14:05Z")

</div>

> [@Strange access to data class](https://discourse.processing.org/t/strange-access-to-data-class/40063/6):
>
> Both outputs in my browser got the expected values: [1, 2, 3] & [3, 1, 2]. But b/c you’re printing an array, if a browser by chance delays the actual printing to the console, it’s possible that you’ll see the array’s most current values for all outputs. As a workaround you can extract the arrays values when printing it using the spread ... syntax: console.log(...myTest.groupe);

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 29, 2023, 6:06pm UTC](https://discourse.processing.org/t/why-is-splice-not-working-in-the-web-editor/41870/4 "2023-04-29T18:06:43Z")

</div>

> [@swenzel](#):
>
> `A.splice(0,0,0)`

BtW, we can use method **unshift()** in order to append at the front (head) of an array: `A.unshift(0)`

> **[Array.prototype.unshift() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift)**
>
> The unshift() method of Array instances adds the specified elements to the
> beginning of an array and returns the new length of the array.
