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/02-first-steps/09-comparison/article.md
+8-7Lines changed: 8 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,9 @@ In JavaScript they are written like this:
9
9
- Equals: `a == b`, please note the double equality sign `=` means the equality test, while a single one `a = b` means an assignment.
10
10
- Not equals. In maths the notation is <code>≠</code>, but in JavaScript it's written as <code>a != b</code>.
11
11
12
-
In this article we'll learn more about different types of comparisons, how JavaScript makes them, including important peculiarities.
12
+
In this article we'll learn more about different types of comparisons, how JavaScript makes them, including important peculiarities.
13
+
14
+
At the end you'll find a good recipe to avoid "javascript quirks"-related issues.
13
15
14
16
## Boolean is the result
15
17
@@ -24,7 +26,7 @@ For example:
24
26
alert( 2>1 ); // true (correct)
25
27
alert( 2==1 ); // false (wrong)
26
28
alert( 2!=1 ); // true (correct)
27
-
```
29
+
```
28
30
29
31
A comparison result can be assigned to a variable, just like any value:
30
32
@@ -196,13 +198,12 @@ We get these results because:
196
198
- Comparisons `(1)` and `(2)` return `false` because `undefined` gets converted to `NaN` and `NaN` is a special numeric value which returns `false` for all comparisons.
197
199
- The equality check `(3)` returns `false` because `undefined` only equals `null`, `undefined`, and no other value.
198
200
199
-
### Evade problems
200
-
201
-
Why did we go over these examples? Should we remember these peculiarities all the time? Well, not really. Actually, these tricky things will gradually become familiar over time, but there's a solid way to evade problems with them:
201
+
### Avoid problems
202
202
203
-
Just treat any comparison with `undefined/null` except the strict equality `===` with exceptional care.
203
+
Why did we go over these examples? Should we remember these peculiarities all the time? Well, not really. Actually, these tricky things will gradually become familiar over time, but there's a solid way to avoid problems with them:
204
204
205
-
Don't use comparisons `>= > < <=` with a variable which may be `null/undefined`, unless you're really sure of what you're doing. If a variable can have these values, check for them separately.
205
+
- Treat any comparison with `undefined/null` except the strict equality `===` with exceptional care.
206
+
- Don't use comparisons `>= > < <=` with a variable which may be `null/undefined`, unless you're really sure of what you're doing. If a variable can have these values, check for them separately.
Copy file name to clipboardExpand all lines: 1-js/13-modules/01-modules-intro/article.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,25 @@
1
1
2
2
# Modules, introduction
3
3
4
-
As our application grows bigger, we want to split it into multiple files, so called "modules". A module usually contains a class or a library of functions.
4
+
As our application grows bigger, we want to split it into multiple files, so called "modules". A module may contain a class or a library of functions for a specific purpose.
5
5
6
6
For a long time, JavaScript existed without a language-level module syntax. That wasn't a problem, because initially scripts were small and simple, so there was no need.
7
7
8
8
But eventually scripts became more and more complex, so the community invented a variety of ways to organize code into modules, special libraries to load modules on demand.
9
9
10
-
For instance:
10
+
To name some (for historical reasons):
11
11
12
12
-[AMD](https://en.wikipedia.org/wiki/Asynchronous_module_definition) -- one of the most ancient module systems, initially implemented by the library [require.js](http://requirejs.org/).
13
13
-[CommonJS](http://wiki.commonjs.org/wiki/Modules/1.1) -- the module system created for Node.js server.
14
14
-[UMD](https://github.com/umdjs/umd) -- one more module system, suggested as a universal one, compatible with AMD and CommonJS.
15
15
16
16
Now all these slowly become a part of history, but we still can find them in old scripts.
17
17
18
-
The language-level module system appeared in the standard in 2015, gradually evolved since then, and is now supported by all major browsers and in Node.js. So we'll study it from now on.
18
+
The language-level module system appeared in the standard in 2015, gradually evolved since then, and is now supported by all major browsers and in Node.js. So we'll study the modern JavaScript modules from now on.
19
19
20
20
## What is a module?
21
21
22
-
A module is just a file. One script is one module.
22
+
A module is just a file. One script is one module. As simple as that.
23
23
24
24
Modules can load each other and use special directives `export` and `import` to interchange functionality, call functions of one module from another one:
25
25
@@ -57,8 +57,8 @@ Like this:
57
57
58
58
The browser automatically fetches and evaluates the imported module (and its imports if needed), and then runs the script.
59
59
60
-
```warn header="Modules work only via HTTP, not in local files"
61
-
If you try to open a web-page locally, via `file://` protocol, you'll find that `import/export` directives don't work. Use a local web-server, such as [static-server](https://www.npmjs.com/package/static-server#getting-started) or use the "live server" capability of your editor, such as VS Code [Live Server Extension](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer) to test them.
60
+
```warn header="Modules work only via HTTP(s), not in local files"
61
+
If you try to open a web-page locally, via `file://` protocol, you'll find that `import/export` directives don't work. Use a local web-server, such as [static-server](https://www.npmjs.com/package/static-server#getting-started) or use the "live server" capability of your editor, such as VS Code [Live Server Extension](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer) to test modules.
Copy file name to clipboardExpand all lines: 2-ui/1-document/01-browser-environment/article.md
+5-7Lines changed: 5 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
2
3
3
The JavaScript language was initially created for web browsers. Since then it has evolved and become a language with many uses and platforms.
4
4
5
-
A platform may be a browser, or a web-server or another *host*, even a coffee machine. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*.
5
+
A platform may be a browser, or a web-server or another *host*, even a "smart" coffee machine, if it can run JavaScript. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*.
6
6
7
7
A host environment provides own objects and functions additional to the language core. Web browsers give a means to control web pages. Node.js provides server-side features, and so on.
8
8
9
-
Here's a bird's-eye view of what we have when JavaScript runs in a web-browser:
9
+
Here's a bird's-eye view of what we have when JavaScript runs in a webbrowser:
Here we used `document.body.style`, but there's much, much more. Properties and methods are described in the specification:
53
-
54
-
-**DOM Living Standard** at <https://dom.spec.whatwg.org>
52
+
Here we used `document.body.style`, but there's much, much more. Properties and methods are described in the specification: [DOM Living Standard](https://dom.spec.whatwg.org).
55
53
56
54
```smart header="DOM is not only for browsers"
57
55
The DOM specification explains the structure of a document and provides objects to manipulate it. There are non-browser instruments that use DOM too.
@@ -60,9 +58,9 @@ For instance, server-side scripts that download HTML pages and process them can
60
58
```
61
59
62
60
```smart header="CSSOM for styling"
63
-
CSS rules and stylesheets are structured in a different way than HTML. There's a separate specification, [CSS Object Model (CSSOM)](https://www.w3.org/TR/cssom-1/), that explains how they are represented as objects, and how to read and write them.
61
+
There's also a separate specification, [CSS Object Model (CSSOM)](https://www.w3.org/TR/cssom-1/) for CSS rules and stylesheets, that explains how they are represented as objects, and how to read and write them.
64
62
65
-
CSSOM is used together with DOM when we modify style rules for the document. In practice though, CSSOM is rarely required, because usually CSS rules are static. We rarely need to add/remove CSS rules from JavaScript, but that's also possible.
63
+
CSSOM is used together with DOM when we modify style rules for the document. In practice though, CSSOM is rarely required, because we rarely need to modify CSS rules from JavaScript (usually we just add/remove CSS classes, not modify their CSS rules), but that's also possible.
Copy file name to clipboardExpand all lines: 2-ui/1-document/07-modifying-document/article.md
+26-13Lines changed: 26 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ Here's how it will look:
28
28
*/!*
29
29
```
30
30
31
-
That was an HTML example. Now let's create the same `div` with JavaScript (assuming that the styles are in the HTML or an external CSS file).
31
+
That was the HTML example. Now let's create the same `div` with JavaScript (assuming that the styles are in the HTML/CSS already).
32
32
33
33
## Creating an element
34
34
@@ -48,21 +48,28 @@ To create DOM nodes, there are two methods:
48
48
let textNode = document.createTextNode('Here I am');
49
49
```
50
50
51
+
Most of the time we need to create element nodes, such as the `div` for the message.
52
+
51
53
### Creating the message
52
54
53
-
In our case the message is a `div` with `alert` class and the HTML in it:
55
+
Creating the message div takes 3 steps:
54
56
55
57
```js
58
+
// 1. Create <div> element
56
59
let div =document.createElement('div');
60
+
61
+
// 2. Set its class to "alert"
57
62
div.className="alert";
63
+
64
+
// Fill it with the content
58
65
div.innerHTML="<strong>Hi there!</strong> You've read an important message.";
59
66
```
60
67
61
-
We created the element, but as of now it's only in a variable. We can't see the element on the page, as it's not yet a part of the document.
68
+
We've created the element. But as of now it's only in a variable named `div`, not in the pageyet. So we can't see it.
62
69
63
70
## Insertion methods
64
71
65
-
To make the `div` show up, we need to insert it somewhere into `document`. For instance, in`document.body`.
72
+
To make the `div` show up, we need to insert it somewhere into `document`. For instance, into `<body>` element, referenced by`document.body`.
66
73
67
74
There's a special method `append` for that: `document.body.append(div)`.
68
75
@@ -90,14 +97,20 @@ Here's the full code:
90
97
</script>
91
98
```
92
99
93
-
This set of methods provides more ways to insert:
100
+
Here we called `append` on `document.body`, but we can call `append` method on any other element, to put another element into it. For instance, we can append something to `<div>` by calling `div.append(anotherElement)`.
94
101
95
-
-`node.append(...nodes or strings)` -- append nodes or strings at the end of `node`,
96
-
-`node.prepend(...nodes or strings)` -- insert nodes or strings at the beginning of `node`,
97
-
-`node.before(...nodes or strings)` –- insert nodes or strings before `node`,
98
-
-`node.after(...nodes or strings)` –- insert nodes or strings after `node`,
102
+
Here are more insertion methods, they specify different places where to insert:
103
+
104
+
-`node.append(...nodes or strings)` -- append nodes or strings *at the end* of `node`,
105
+
-`node.prepend(...nodes or strings)` -- insert nodes or strings *at the beginning* of `node`,
106
+
-`node.before(...nodes or strings)` –- insert nodes or strings *before*`node`,
107
+
-`node.after(...nodes or strings)` –- insert nodes or strings *after*`node`,
99
108
-`node.replaceWith(...nodes or strings)` –- replaces `node` with the given nodes or strings.
100
109
110
+
Arguments of these methods are an arbitrary list of DOM nodes to insert, or text strings (that become text nodes automatically).
111
+
112
+
Let's see them in action.
113
+
101
114
Here's an example of using these methods to add items to a list and the text before/after it:
102
115
103
116
```html autorun
@@ -121,7 +134,7 @@ Here's an example of using these methods to add items to a list and the text bef
121
134
</script>
122
135
```
123
136
124
-
Here's a visual picture what methods do:
137
+
Here's a visual picture of what the methods do:
125
138
126
139

127
140
@@ -139,7 +152,7 @@ before
139
152
after
140
153
```
141
154
142
-
These methods can insert multiple lists of nodes and text pieces in a single call.
155
+
As said, these methods can insert multiple nodes and text pieces in a single call.
143
156
144
157
For instance, here a string and an element are inserted:
145
158
@@ -150,7 +163,7 @@ For instance, here a string and an element are inserted:
150
163
</script>
151
164
```
152
165
153
-
All text is inserted *as text*.
166
+
Please note: the text is inserted "as text", not "as HTML", with proper escaping of characters such as `<`, `>`.
154
167
155
168
So the final HTML is:
156
169
@@ -166,7 +179,7 @@ In other words, strings are inserted in a safe way, like `elem.textContent` does
166
179
167
180
So, these methods can only be used to insert DOM nodes or text pieces.
168
181
169
-
But what if we want to insert HTML "as html", with all tags and stuff working, like `elem.innerHTML`?
182
+
But what if we'd like to insert an HTML string "as html", with all tags and stuff working, in the same manner as `elem.innerHTML` does it?
0 commit comments