This commit is contained in:
Ilya Kantor 2017-10-17 10:08:15 +03:00
commit ccc0e9327f
3 changed files with 15 additions and 15 deletions

View file

@ -19,7 +19,7 @@ let double = "double-quoted";
let backticks = `backticks`; let backticks = `backticks`;
``` ```
Single and double quotes are essentially the same. Backticks however allow us to embed any expression into the string, including function calls: Single and double quotes are essentially the same. Backticks, however, allow us to embed any expression into the string, including function calls:
```js run ```js run
function sum(a, b) { function sum(a, b) {
@ -89,7 +89,7 @@ Examples with unicode:
```js run ```js run
alert( "\u00A9" ); // © alert( "\u00A9" ); // ©
alert( "\u{20331}" ); // 佫, a rare chinese hieroglyph (long unicode) alert( "\u{20331}" ); // 佫, a rare chinese hieroglyph (long unicode)
alert( "\u{1F60D}"); // 😍, a smiling face symbol (another long unicode) alert( "\u{1F60D}" ); // 😍, a smiling face symbol (another long unicode)
``` ```
All special characters start with a backslash character `\`. It is also called an "escape character". All special characters start with a backslash character `\`. It is also called an "escape character".
@ -166,7 +166,7 @@ alert( str.charAt(1000) ); // '' (an empty string)
We can also iterate over characters using `for..of`: We can also iterate over characters using `for..of`:
```js run ```js run
for(let char of "Hello") { for (let char of "Hello") {
alert(char); // H,e,l,l,o (char becomes "H", then "e", then "l" etc) alert(char); // H,e,l,l,o (char becomes "H", then "e", then "l" etc)
} }
``` ```
@ -379,8 +379,8 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
```js run ```js run
let str = "stringify"; let str = "stringify";
alert( str.slice(0,5) ); // 'strin', the substring from 0 to 5 (not including 5) alert( str.slice(0, 5) ); // 'strin', the substring from 0 to 5 (not including 5)
alert( str.slice(0,1) ); // 's', from 0 to 1, but not including 1, so only character at 0 alert( str.slice(0, 1) ); // 's', from 0 to 1, but not including 1, so only character at 0
``` ```
If there is no second argument, then `slice` goes till the end of the string: If there is no second argument, then `slice` goes till the end of the string:
@ -548,7 +548,7 @@ For instance:
alert( 'Österreich'.localeCompare('Zealand') ); // -1 alert( 'Österreich'.localeCompare('Zealand') ); // -1
``` ```
This method actually has two additional arguments specified in [the documentation](mdn:js/String/localeCompare), which allow it to specify the language (by default taken from the environment) and setup additional rules like case sensivity or should `"a"` and `"á"` be treated as the same etc. This method actually has two additional arguments specified in [the documentation](mdn:js/String/localeCompare), which allows it to specify the language (by default taken from the environment) and setup additional rules like case sensitivity or should `"a"` and `"á"` be treated as the same etc.
## Internals, Unicode ## Internals, Unicode

View file

@ -297,7 +297,7 @@ But for arrays there is another form of loop, `for..of`:
let fruits = ["Apple", "Orange", "Plum"]; let fruits = ["Apple", "Orange", "Plum"];
// iterates over array elements // iterates over array elements
for(let fruit of fruits) { for (let fruit of fruits) {
alert( fruit ); alert( fruit );
} }
``` ```
@ -356,7 +356,7 @@ arr.length = 5; // return length back
alert( arr[3] ); // undefined: the values do not return alert( arr[3] ); // undefined: the values do not return
``` ```
So, the simplest way to clear the array is: `arr.length=0`. So, the simplest way to clear the array is: `arr.length = 0;`.
## new Array() [#new-array] ## new Array() [#new-array]
@ -385,9 +385,9 @@ In the code above, `new Array(number)` has all elements `undefined`.
To evade such surprises, we usually use square brackets, unless we really know what we're doing. To evade such surprises, we usually use square brackets, unless we really know what we're doing.
## Multidimentional arrays ## Multidimensional arrays
Arrays can have items that are also arrays. We can use it for multidimentional arrays, to store matrices: Arrays can have items that are also arrays. We can use it for multidimensional arrays, to store matrices:
```js run ```js run
let matrix = [ let matrix = [
@ -458,9 +458,9 @@ We can use an array as a deque with the following operations:
- `unshift(...items)` adds items to the beginning. - `unshift(...items)` adds items to the beginning.
To loop over the elements of the array: To loop over the elements of the array:
- `for(let i=0; i<arr.length; i++)` -- works fastest, old-browser-compatible. - `for (let i=0; i<arr.length; i++)` -- works fastest, old-browser-compatible.
- `for(let item of arr)` -- the modern syntax for items only, - `for (let item of arr)` -- the modern syntax for items only,
- `for(let i in arr)` -- never use. - `for (let i in arr)` -- never use.
We will return to arrays and study more methods to add, remove, extract elements and sort arrays in the chapter <info:array-methods>. We will return to arrays and study more methods to add, remove, extract elements and sort arrays in the chapter <info:array-methods>.

View file

@ -325,7 +325,7 @@ Then the next call is scheduled in `(*)` if we're not done yet.
Pauses between `count` executions provide just enough "breath" for the JavaScript engine to do something else, to react on other user actions. Pauses between `count` executions provide just enough "breath" for the JavaScript engine to do something else, to react on other user actions.
The notable thing is that both variants: with and without splitting the job by `setInterval` -- are comparable in speed. There's no much difference in the overall counting time. The notable thing is that both variants: with and without splitting the job by `setTimeout` -- are comparable in speed. There's no much difference in the overall counting time.
To make them closer let's make an improvement. To make them closer let's make an improvement.
@ -461,4 +461,4 @@ For example, the in-browser timer may slow down for a lot of reasons:
- The browser tab is in the background mode. - The browser tab is in the background mode.
- The laptop is on battery. - The laptop is on battery.
All that may decrease the minimal timer resolution (the minimal delay) to 300ms or even 1000ms depending on the browser and settings. All that may increase the minimal timer resolution (the minimal delay) to 300ms or even 1000ms depending on the browser and settings.