Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

The array on which we are going to perform splice methods.

const weekDays = ["Sunday", "Tuesday", "Monday", "Wednesday"];

Objective is to remove Tuesday from the current position and then add it back in the right position. Go to this link https://www.w3schools.com/jsref/jsref_splice.asp for more details

Step 1: Assign Tuesday to a variable

var day = "Tuesday"

Step 2: this code means, at position 1, remove 1 item and store the result.Tuesday happens to be in that position.

weekDays.splice(1, 1)

Step 3: now time to add it back but in a different position using the day variable. This code means, at position 2, remove 0 items and add 1 item called day

weekDays.splice(2, 0, day)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment