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 post
Share on other sites

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

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

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...