Jump to content

Error in Javascript substr() method documentation


Recommended Posts

The web page, "JavaScript String substr() Method" (https://www.w3schools.com/jsref/jsref_substr.asp), contains the following text:

If start is negative, substr() uses it as a character index from the end of the string.
If start is negative or larger than the length of the string, start is set to 0
Clearly, when start is negative, both of these statements cannot be correct.  A quick test on my part determined that the first statement is wrong.  Please confirm this for yourself, and update the document accordingly.
Link to comment
Share on other sites

The first statement is correct. The following code outputs "ld!"

  var str = "Hello world!";
  var res = str.substr(-3);

The other statement should be revised to "If start is negative and larger than the length of the string, start is set to 0"

The fastest way to report errors to the W3Schools staff is to click the "REPORT ERROR" button at the bottom of every page, since they don't actively participate in the forums.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...