Skip to content

Commit f0bbbe8

Browse files
authored
Revising JS Substring concept (#5418)
* Revising JS Substring concept * Update substring.md minor changes ---------
1 parent de8a3a7 commit f0bbbe8

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

content/javascript/concepts/substring/substring.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Title: 'Substring'
3-
Description: 'The .substring() method returns part of a string. If given two arguments, they are the start and end indexes of the characters returned. If given one argument, it returns characters from that point to the end of the string. javascript // Returns characters from startIndex to end of string string.substring(startIndex); // Returns characters from startIndex to endIndex string.substring(startIndex, endIndex); '
3+
Description: 'Extracts a portion of a string between two given indices and returns a new string. If only one index is given, it goes to the end.'
44
Subjects:
55
- 'Web Development'
66
- 'Computer Science'
@@ -13,39 +13,41 @@ CatalogContent:
1313
- 'paths/create-a-back-end-app-with-javascript'
1414
---
1515

16-
The `.substring()` method returns part of a string. If given two arguments, they are the start and end indexes of the characters returned. If given one argument, it returns characters from that point to the end of the string.
16+
The **`substring()`** method in JavaScript extracts a portion of a string from one position to another (exclusive) and returns a new string. If the second position is omitted, it returns characters from the first position to the end of the string.
1717

1818
## Syntax
1919

20-
```javascript
20+
```pseudo
2121
// Returns characters from startIndex to end of string
2222
string.substring(startIndex);
23+
```
24+
25+
Or alternatively:
2326

27+
```pseudo
2428
// Returns characters from startIndex to endIndex
2529
string.substring(startIndex, endIndex);
2630
```
2731

2832
## Details
2933

30-
- `.substring()` returns characters at the start index up to, but not including, the character at the end index.
31-
32-
- If the end index is omitted `.substring()` returns characters at the start index up through the end of the string.
34+
- `.substring()` returns characters from the start index up to, but not including, the character at the end index.
3335

34-
- If the start and end indexes are equal, `.substring()` returns an empty string.
36+
- If the end index is omitted, `.substring()` returns characters from the start index through the end of the string.
3537

36-
- Indexes less than zero are interpreted as zero.
38+
- If the start and end indices are equal, `.substring()` returns an empty string.
3739

38-
- Indexes that are `NaN` are treated as zero.
40+
- Indices that are less than zero or are `NaN` are interpreted as zero.
3941

40-
- Indexes that are greater than `string.length` are treated as `string.length`.
42+
- Indices that are greater than `string.length` are treated as `string.length`.
4143

4244
- If the first argument is greater than the second argument, the first argument is treated as the end index and the second argument is treated as the start index.
4345

4446
## Example 1
4547

4648
Using `.substring()` to display characters from a given string.
4749

48-
```javascript
50+
```js
4951
const str = 'Codecademy';
5052

5153
console.log(str.substring(0, 4));
@@ -62,7 +64,7 @@ console.log(str.substring(4, 99));
6264

6365
Using `.substring()` to display the last `6` characters from a given string.
6466

65-
```javascript
67+
```js
6668
const str = 'Codecademy';
6769

6870
console.log(str.substring(str.length - 6));

0 commit comments

Comments
 (0)