0

I was reading about .splice() method in MDN. When I got to the first parameter(starting index) I understood all cases but the last one.
What does "and will be set to 0 if absolute value is greater than the length of the array" exactly means?

Parameters

start
Index at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end of the array (with origin -1) and will be set to 0 if absolute value is greater than the length of the array.

Link to the documentation

2
  • 3
    What part don't you understand? en.wikipedia.org/wiki/Absolute_value Commented Dec 4, 2017 at 22:36
  • 3
    If you have a 5-entry array and you pass -15 as the first argument, it treats it as if you passed 0. Commented Dec 4, 2017 at 22:37

1 Answer 1

1

If the absolute value is greater than the array length it will do the same thing as if you passed 0.

var arr = [0, 1, 2, 3, 4, 5, 6];
arr.splice(-8, 1); 
// Absolute value is 8, array length is 7
// Because 8 > 7, the first parameter is set to 0
// So this is the same as arr.splice(0, 1);

console.log(arr);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.