You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/03-string/article.md
-11Lines changed: 0 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,6 @@ Looks natural, right? But single or double quotes do not work this way.
43
43
44
44
If we use them and try to use multiple lines, there'll be an error:
45
45
46
-
47
46
```js run
48
47
let guestList ="Guests: // Error: Unexpected token ILLEGAL
49
48
* John";
@@ -53,7 +52,6 @@ Single and double quotes come from ancient times of language creation when the n
53
52
54
53
Backticks also allow us to specify a "template function" before the first backtick. The syntax is: <code>func`string`</code>. The function `func` is called automatically, receives the string and embedded expressions and can process them. You can read more about it in the [docs](mdn:/JavaScript/Reference/Template_literals#Tagged_template_literals). This is called "tagged templates". This feature makes it easier to wrap strings into custom templating or other functionality, but it is rarely used.
55
54
56
-
57
55
## Special characters
58
56
59
57
It is still possible to create multiline strings with single quotes by using a so-called "newline character", written as `\n`, which denotes a line break:
If we're interested in all occurrences, we can run `indexOf` in a loop. Every new call is made with the position after the previous match:
257
253
258
-
259
254
```js run
260
255
let str ='As sly as a fox, as strong as an ox';
261
256
@@ -411,15 +406,13 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
411
406
alert( str.slice(-4, -1) ); // gif
412
407
```
413
408
414
-
415
409
`str.substring(start [, end])`
416
410
: Returns the part of the string *between*`start` and `end`.
417
411
418
412
This is almost the same as `slice`, but it allows `start` to be greater than `end`.
419
413
420
414
For instance:
421
415
422
-
423
416
```js run
424
417
let str = "st*!*ring*/!*ify";
425
418
@@ -435,7 +428,6 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
435
428
436
429
Negative arguments are (unlike slice) not supported, they are treated as `0`.
437
430
438
-
439
431
`str.substr(start [, length])`
440
432
: Returns the part of the string from `start`, with the given `length`.
441
433
@@ -461,7 +453,6 @@ Let's recap these methods to avoid any confusion:
461
453
|`substring(start, end)`| between `start` and `end`| negative values mean `0`|
462
454
|`substr(start, length)`| from `start` get `length` characters | allows negative `start`|
463
455
464
-
465
456
```smart header="Which one to choose?"
466
457
All of them can do the job. Formally, `substr` has a minor drawback: it is described not in the core JavaScript specification, but in Annex B, which covers browser-only features that exist mainly for historical reasons. So, non-browser environments may fail to support it. But in practice it works everywhere.
467
458
@@ -537,7 +528,6 @@ The characters are compared by their numeric code. The greater code means that t
537
528
- All lowercase letters go after uppercase letters because their codes are greater.
538
529
- Some letters like `Ö` stand apart from the main alphabet. Here, it's code is greater than anything from `a` to `z`.
539
530
540
-
541
531
### Correct comparisons
542
532
543
533
The "right" algorithm to do string comparisons is more complex than it may seem, because alphabets are different for different languages.
@@ -667,7 +657,6 @@ In reality, this is not always the case. The reason being that the symbol `Ṩ`
667
657
668
658
If you want to learn more about normalization rules and variants -- they are described in the appendix of the Unicode standard: [Unicode Normalization Forms](http://www.unicode.org/reports/tr15/), but for most practical purposes the information from this section is enough.
669
659
670
-
671
660
## Summary
672
661
673
662
- There are 3 types of quotes. Backticks allow a string to span multiple lines and embed expressions `${…}`.
0 commit comments