1

var alpha = "ABCDEFG";
var result = alpha.substring(0, 5);
document.write(result);

The question is, why the answer is ABCDE(0,4) instead of ABCDEF (0,5). All numbers in programming start with zero, right?

2

1 Answer 1

2

String.prototype.substring takes up to two arguments. One for the start index one for the end index (optional). While the starting index is included in the new string, the end index is excluded. When 4 is the end index that character will be excluded and you will see characters 0-3

Please read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

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.