Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

doc: update question 467 substr vs substring #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
sudheerj merged 1 commit into sudheerj:master from mauroaccornero:update-question-467
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8845,8 +8845,19 @@ The execution context is created when a function is called. The function's code
**[⬆ Back to Top](#table-of-contents)**

467. ### What is the difference between substring and substr methods?
Both substring() and substr() are string methods, which are used to find substring of a given string. But there are some notable differences with their usage,

There are subtle differences between the substring() and substr() methods, so you should be careful not to get them confused.

- The two parameters of substr() are start and length, while for substring(), they are start and end.
- substr()'s start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0.
- Negative lengths in substr() are treated as zero, while substring() will swap the two indexes if end is less than start.

Furthermore, substr() is considered a legacy feature in ECMAScript, so it is best to avoid using it if possible.

```javascript
const text = "Mozilla";
console.log(text.substring(2, 5)); // "zil"
console.log(text.substr(2, 3)); // "zil"
```

**[⬆ Back to Top](#table-of-contents)**

Expand Down

AltStyle によって変換されたページ (->オリジナル) /