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/04-object-basics/07-optional-chaining/article.md
+45-15Lines changed: 45 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,51 +9,81 @@ The optional chaining `?.` is a safe way to access nested object properties, eve
9
9
10
10
If you've just started to read the tutorial and learn JavaScript, maybe the problem hasn't touched you yet, but it's quite common.
11
11
12
-
As an example, let's consider objects for user data. Most of our users have addresses in `user.address` property, with the street `user.address.street`, but some did not provide them.
12
+
As an example, consider objects for user data. Most of our users have addresses in `user.address` property, with the street `user.address.street`, but some did not provide them.
13
13
14
-
In such case, when we attempt to get `user.address.street`, we'll get an error:
14
+
In such case, when we attempt to get `user.address.street`, we may get an error:
15
15
16
16
```js run
17
-
let user = {}; //the user without "address" property
17
+
let user = {}; //a user without "address" property
18
18
19
19
alert(user.address.street); // Error!
20
20
```
21
21
22
-
That's the expected result, JavaScript works like this, but many practical cases we'd prefer to get `undefined` instead of an error (meaning "no street").
22
+
That's the expected result, JavaScript works like this. As `user.address` is `undefined`, the attempt to get `user.address.street` fails with an error. Although, in many practical cases we'd prefer to get `undefined` instead of an error here (meaning "no street").
23
23
24
-
...And another example. In the web development, we may need to get an information about an element on the page, that sometimes doesn't exist:
24
+
...And another example. In the web development, we may need the information about an element on the page. The element is returned by `document.querySelector('.elem')`, and the catch is again - that it sometimes doesn't exist:
25
25
26
26
```js run
27
-
//Error if the result of querySelector(...) is null
28
-
let html =document.querySelector('.my-element').innerHTML;
27
+
// the result of the call document.querySelector('.elem') may be an object or null
28
+
let html =document.querySelector('.elem').innerHTML;// error if it's null
29
29
```
30
30
31
-
Before `?.` appeared in the language, the `&&` operator was used to work around that.
31
+
Once again, we may want to avoid the error in such case.
32
32
33
-
For example:
33
+
How can we do this?
34
+
35
+
The obvious solution would be to check the value using `if` or the conditional operator `?`, before accessing it, like this:
...But that's quite inelegant. As you can see, the `user.address` is duplicated in the code. For more deeply nested properties, that becomes a problem.
44
+
45
+
E.g. let's try getting `user.address.street.name`.
46
+
47
+
We need to check both `user.address` and `user.address.street`:
Please note: the `?.` syntax makes optional the value before it, but not any further.
67
97
68
-
In the example above, `user?.` allows only `user` to be `null/undefined`.
98
+
In the example above, `user?.address.street` allows only `user` to be `null/undefined`.
69
99
70
100
On the other hand, if `user` does exist, then it must have `user.address` property, otherwise `user?.address.street` gives an error at the second dot.
71
101
72
102
```warn header="Don't overuse the optional chaining"
73
103
We should use `?.` only where it's ok that something doesn't exist.
74
104
75
-
For example, if according to our coding logic `user` object must be there, but `address` is optional, then `user.address?.street` would be better.
105
+
For example, if according to our coding logic `user` object must exist, but `address` is optional, then we should write `user.address?.street`, but not `user?.address?.street`.
76
106
77
107
So, if`user` happens to be undefined due to a mistake, we'll see a programming error about it and fix it. Otherwise, coding errors can be silenced where not appropriate, and become more difficult to debug.
78
108
```
@@ -84,7 +114,7 @@ If there's no variable `user` at all, then `user?.anything` triggers an error:
84
114
// ReferenceError: user is not defined
85
115
user?.address;
86
116
```
87
-
There must be a declaration(e.g. `let/const/var user`). The optional chaining works only for declared variables.
117
+
The variable must be declared(e.g. `let/const/var user` or as a function parameter). The optional chaining works only for declared variables.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/05-array-methods/2-filter-range/task.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ importance: 4
4
4
5
5
# Filter range
6
6
7
-
Write a function `filterRange(arr, a, b)` that gets an array `arr`, looks for elements between `a` and `b`in it and returns an array of them.
7
+
Write a function `filterRange(arr, a, b)` that gets an array `arr`, looks for elements with values higher or equal to `a` and lower or equal to `b`and return a result as an array.
8
8
9
9
The function should not modify the array. It should return the new array.
Copy file name to clipboardExpand all lines: 2-ui/1-document/05-basic-dom-node-properties/article.md
+3-35Lines changed: 3 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,13 +20,8 @@ The classes are:
20
20
21
21
-[EventTarget](https://dom.spec.whatwg.org/#eventtarget) -- is the root "abstract" class. Objects of that class are never created. It serves as a base, so that all DOM nodes support so-called "events", we'll study them later.
22
22
-[Node](http://dom.spec.whatwg.org/#interface-node) -- is also an "abstract" class, serving as a base for DOM nodes. It provides the core tree functionality: `parentNode`, `nextSibling`, `childNodes` and so on (they are getters). Objects of `Node` class are never created. But there are concrete node classes that inherit from it, namely: `Text` for text nodes, `Element` for element nodes and more exotic ones like `Comment` for comment nodes.
23
-
<<<<<<< HEAD
24
-
-[Element](http://dom.spec.whatwg.org/#interface-element) -- is a base class for DOM elements. It provides element-level navigation like `nextElementSibling`, `children` and searching methods like `getElementsByTagName`, `querySelector`. In the browser there may be not only HTML, but also XML and SVG documents. The `Element` class serves as a base for more specific classes: `SVGElement`, `XMLElement` and `HTMLElement`.
25
-
-[HTMLElement](https://html.spec.whatwg.org/multipage/dom.html#htmlelement) -- is finally the basic class for all HTML elements. It is inherited by various HTML elements:
26
-
=======
27
23
-[Element](http://dom.spec.whatwg.org/#interface-element) -- is a base class for DOM elements. It provides element-level navigation like `nextElementSibling`, `children` and searching methods like `getElementsByTagName`, `querySelector`. A browser supports not only HTML, but also XML and SVG. The `Element` class serves as a base for more specific classes: `SVGElement`, `XMLElement` and `HTMLElement`.
28
24
-[HTMLElement](https://html.spec.whatwg.org/multipage/dom.html#htmlelement) -- is finally the basic class for all HTML elements. It is inherited by concrete HTML elements:
29
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
30
25
-[HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) -- the class for `<input>` elements,
31
26
-[HTMLBodyElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlbodyelement) -- the class for `<body>` elements,
32
27
-[HTMLAnchorElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlanchorelement) -- the class for `<a>` elements
@@ -36,19 +31,12 @@ So, the full set of properties and methods of a given node comes as the result o
36
31
37
32
For example, let's consider the DOM object for an `<input>` element. It belongs to [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) class. It gets properties and methods as a superposition of:
38
33
39
-
<<<<<<< HEAD
40
-
-`HTMLInputElement` -- this class provides input-specific properties, and inherits from...
41
-
-`HTMLElement` -- it provides common HTML element methods (and getters/setters) and inherits from...
42
-
-`Element` -- provides generic element methods and inherits from...
43
-
-`Node` -- provides common DOM node properties and inherits from...
44
-
=======
45
34
It gets properties and methods as a superposition of (listed in inheritance order):
46
35
47
36
-`HTMLInputElement` -- this class provides input-specific properties,
48
37
-`HTMLElement` -- it provides common HTML element methods (and getters/setters),
49
38
-`Element` -- provides generic element methods,
50
39
-`Node` -- provides common DOM node properties,
51
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
52
40
-`EventTarget` -- gives the support for events (to be covered),
53
41
- ...and finally it inherits from `Object`, so "pure object" methods like `hasOwnProperty` are also available.
54
42
@@ -325,13 +313,6 @@ Consider the example:
325
313
</script>
326
314
```
327
315
328
-
<<<<<<< HEAD
329
-
In the line `(*)` we take the full HTML of `<div>...</div>` and replace it by `<p>...</p>`. In the outer document we can see the new content instead of the `<div>`. But the old `div` variable is still the same.
330
-
331
-
The `outerHTML` assignment does not modify the DOM element, but extracts it from the outer context and inserts a new piece of HTML instead of it.
332
-
333
-
Novice developers sometimes make an error here: they modify `div.outerHTML` and then continue to work with `div` as if it had the new content in it.
334
-
=======
335
316
Looks really odd, right?
336
317
337
318
In the line `(*)` we replaced `div` with `<p>A new element</p>`. In the outer document (the DOM) we can see the new content instead of the `<div>`. But, as we can see in line `(**)`, the value of the old `div` variable hasn't changed!
@@ -342,15 +323,10 @@ So what happened in `div.outerHTML=...` is:
342
323
- `div` was removed from the document.
343
324
- Another piece of HTML `<p>A new element</p>` was inserted in its place.
344
325
- `div` still has its old value. The new HTML wasn't saved to any variable.
345
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
346
326
347
327
That's possible with `innerHTML`, but not with `outerHTML`.
348
328
349
-
<<<<<<< HEAD
350
-
We can write to `outerHTML`, but should keep in mind that it doesn't change the element we're writing to. It creates the new content on its place instead. We can get a reference to new elements by querying DOM.
351
-
=======
352
329
We can write to `elem.outerHTML`, but should keep in mind that it doesn't change the element we're writing to ('elem'). It puts the new HTML in its place instead. We can get references to the new elements by querying the DOM.
353
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
354
330
355
331
## nodeValue/data: text node content
356
332
@@ -424,23 +400,23 @@ Compare the two:
424
400
<div id="elem2"></div>
425
401
426
402
<script>
427
-
let name = prompt("What's your name?", "<b>Winnie-the-pooh!</b>");
403
+
let name = prompt("What's your name?", "<b>Winnie-the-Pooh!</b>");
428
404
429
405
elem1.innerHTML = name;
430
406
elem2.textContent = name;
431
407
</script>
432
408
```
433
409
434
410
1. The first `<div>` gets the name "as HTML": all tags become tags, so we see the bold name.
435
-
2. The second `<div>` gets the name "as text", so we literally see `<b>Winnie-the-pooh!</b>`.
411
+
2. The second `<div>` gets the name "as text", so we literally see `<b>Winnie-the-Pooh!</b>`.
436
412
437
413
In most cases, we expect the text from a user, and want to treat it as text. We don't want unexpected HTML in our site. An assignment to `textContent` does exactly that.
438
414
439
415
## The "hidden" property
440
416
441
417
The "hidden" attribute and the DOM property specifies whether the element is visible or not.
442
418
443
-
We can use it in HTML or assign using JavaScript, like this:
419
+
We can use it in HTML or assign it using JavaScript, like this:
444
420
445
421
```html run height="80"
446
422
<div>Both divs below are hidden</div>
@@ -501,11 +477,7 @@ Each DOM node belongs to a certain class. The classes form a hierarchy. The full
501
477
Main DOM node properties are:
502
478
503
479
`nodeType`
504
-
<<<<<<< HEAD
505
-
: We can get `nodeType` from the DOM object class, but often we need just to see if it is a text or element node. The `nodeType` property is good for that. It has numeric values, most important are: `1` -- for elements,`3` -- for text nodes. Read-only.
506
-
=======
507
480
: We can use it to see if a node is a text or an element node. It has a numeric value: `1` for elements,`3` for text nodes, and a few others for other node types. Read-only.
508
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
509
481
510
482
`nodeName/tagName`
511
483
: For elements, tag name (uppercased unless XML-mode). For non-element nodes `nodeName` describes what it is. Read-only.
@@ -527,8 +499,4 @@ Main DOM node properties are:
527
499
528
500
DOM nodes also have other properties depending on their class. For instance, `<input>` elements (`HTMLInputElement`) support `value`, `type`, while `<a>` elements (`HTMLAnchorElement`) support `href` etc. Most standard HTML attributes have a corresponding DOM property.
529
501
530
-
<<<<<<< HEAD
531
-
But HTML attributes and DOM properties are not always the same, as we'll see in the next chapter.
532
-
=======
533
502
However, HTML attributes and DOM properties are not always the same, as we'll see in the next chapter.
Copy file name to clipboardExpand all lines: 2-ui/4-forms-controls/1-form-elements/article.md
+3-21Lines changed: 3 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -167,28 +167,18 @@ input.checked = true; // for a checkbox or radio button
167
167
```
168
168
169
169
```warn header="Use `textarea.value`, not `textarea.innerHTML`"
170
-
<<<<<<< HEAD
171
-
Please note that we should never use `textarea.innerHTML`: it stores only the HTML that was initially on the page, not the current value.
172
-
=======
173
170
Please note that even though `<textarea>...</textarea>` holds its value as nested HTML, we should never use `textarea.innerHTML` to access it.
174
171
175
172
It stores only the HTML that was initially on the page, not the current value.
176
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
177
173
```
178
174
179
175
### select and option
180
176
181
177
A `<select>` element has 3 important properties:
182
178
183
-
<<<<<<< HEAD
184
-
1. `select.options` -- the collection of `<option>` elements,
185
-
2. `select.value` -- the value of the chosen option,
186
-
3. `select.selectedIndex` -- the number of the selected option.
187
-
=======
188
179
1. `select.options` -- the collection of `<option>` subelements,
189
180
2. `select.value` -- the value of the currently selected `<option>`,
190
181
3. `select.selectedIndex` -- the number of the currently selected `<option>`.
191
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
192
182
193
183
They provide three different ways of setting a value for a `<select>`:
194
184
@@ -215,7 +205,9 @@ Here is an example:
215
205
</script>
216
206
```
217
207
218
-
Unlike most other controls, `<select>` allows to select multiple options at once if it has `multiple` attribute. That's feature is rarely used. In that case we need to use the first way: add/remove the `selected` property from `<option>` subelements.
208
+
Unlike most other controls, `<select>` allows to select multiple options at once if it has `multiple` attribute. Although such functionality is available, it is rarely used.
209
+
210
+
In cases that you have to, then use the first way: add/remove the `selected` property from `<option>` subelements.
219
211
220
212
We can get their collection as `select.options`, for instance:
221
213
@@ -240,13 +232,9 @@ The full specification of the `<select>` element is available at <https://html.s
240
232
241
233
### new Option
242
234
243
-
<<<<<<< HEAD
244
-
In the specification of [the option element](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) there's a nice short syntax to create `<option>` elements:
245
-
=======
246
235
This is rarely used on its own. But there's still an interesting thing.
247
236
248
237
In the [specification](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) there's a nice short syntax to create `<option>` elements:
249
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
250
238
251
239
```js
252
240
option = new Option(text, value, defaultSelected, selected);
@@ -304,14 +292,8 @@ Form navigation:
304
292
305
293
Value is available as `input.value`, `textarea.value`, `select.value` etc, or `input.checked` for checkboxes and radio buttons.
306
294
307
-
<<<<<<< HEAD
308
-
For `<select>` we can also get the value by the index `select.selectedIndex` or through the options collection `select.options`. The full specification of this and other elements is at <https://html.spec.whatwg.org/multipage/forms.html>.
309
-
310
-
These are the basics to start working with forms. In the next chapter we'll cover `focus` and `blur` events that may occur on any element, but are mostly handled on forms.
311
-
=======
312
295
For `<select>` we can also get the value by the index `select.selectedIndex` or through the options collection `select.options`.
313
296
314
297
These are the basics to start working with forms. We'll meet many examples further in the tutorial.
315
298
316
299
In the next chapter we'll cover `focus` and `blur` events that may occur on any element, but are mostly handled on forms.
0 commit comments