2

I am looking at this in a codebase:

  let lines = data.split('\n');
  this.lastLineData = lines.splice(lines.length - 1, 1)[0];

I am not seeing any difference in the above and this:

  let lines = data.split('\n');
  this.lastLineData = lines.pop();

is there any difference?

  • 2
    pop() will always remove and return the last element of the array; splice is more flexible, you can remove and return many items, from different positions... but in your specific case, they are similar – João Otero Mar 11 '19 at 07:57

1 Answers1

2

You can use splice to the same effect as pop. But you can also do a lot more with splice, whereas pop will always only remove and return the last item in an array.

There are performance implications, so make sure to choose the appropriate method for your use case.

djfdev
  • 5,747
  • 3
  • 19
  • 38