Skip to content

Day 5 method string.splice()  #811

Open
@NaraRaa

Description

@NaraRaa
  const numbers = [1, 2, 3, 4, 5, 6]
	numbers.splice(3, 3, 7, 8, 9)
  console.log(numbers.splice(3, 3, 7, 8, 9))  // -> [1, 2, 3, 7, 8, 9] //it removes three item and replace three items

small mistake - in case anyone who copy-pasted the code, got [7, 8, 9] instead of [1, 2, 3, 7, 8, 9] and started questioning life,
this is because the author called the splice() method twice, and splice() would modify the original array.

Corrected version:

  const numbers = [1, 2, 3, 4, 5, 6]
  console.log(numbers.splice(3, 3, 7, 8, 9)) // [4, 5, 6] - returns the removed items
  console.log(numbers) // [1, 2, 3, 7, 8, 9] - returns current the array numbers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions