Skip to content

Commit d9caafe

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-340ce434
2 parents ad218c3 + 340ce43 commit d9caafe

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

1-js/02-first-steps/09-comparison/article.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ In JavaScript they are written like this:
99
- Equals: `a == b`, please note the double equality sign `=` means the equality test, while a single one `a = b` means an assignment.
1010
- Not equals. In maths the notation is <code>&ne;</code>, but in JavaScript it's written as <code>a != b</code>.
1111

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.
1315

1416
## Boolean is the result
1517

@@ -24,7 +26,7 @@ For example:
2426
alert( 2 > 1 ); // true (correct)
2527
alert( 2 == 1 ); // false (wrong)
2628
alert( 2 != 1 ); // true (correct)
27-
```
29+
```
2830

2931
A comparison result can be assigned to a variable, just like any value:
3032

@@ -196,13 +198,12 @@ We get these results because:
196198
- 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.
197199
- The equality check `(3)` returns `false` because `undefined` only equals `null`, `undefined`, and no other value.
198200

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
202202

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:
204204

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.
206207

207208
## Summary
208209

1-js/13-modules/01-modules-intro/article.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11

22
# Modules, introduction
33

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.
55

66
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.
77

88
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.
99

10-
For instance:
10+
To name some (for historical reasons):
1111

1212
- [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/).
1313
- [CommonJS](http://wiki.commonjs.org/wiki/Modules/1.1) -- the module system created for Node.js server.
1414
- [UMD](https://github.com/umdjs/umd) -- one more module system, suggested as a universal one, compatible with AMD and CommonJS.
1515

1616
Now all these slowly become a part of history, but we still can find them in old scripts.
1717

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.
1919

2020
## What is a module?
2121

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.
2323

2424
Modules can load each other and use special directives `export` and `import` to interchange functionality, call functions of one module from another one:
2525

@@ -57,8 +57,8 @@ Like this:
5757

5858
The browser automatically fetches and evaluates the imported module (and its imports if needed), and then runs the script.
5959

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.
6262
```
6363

6464
## Core module features

2-ui/1-document/01-browser-environment/article.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
The JavaScript language was initially created for web browsers. Since then it has evolved and become a language with many uses and platforms.
44

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*.
66

77
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.
88

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 web browser:
1010

1111
![](windowObjects.svg)
1212

@@ -49,9 +49,7 @@ document.body.style.background = "red";
4949
setTimeout(() => document.body.style.background = "", 1000);
5050
```
5151

52-
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).
5553

5654
```smart header="DOM is not only for browsers"
5755
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
6058
```
6159

6260
```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.
6462
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.
6664
```
6765

6866
## BOM (Browser Object Model)

2-ui/1-document/07-modifying-document/article.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Here's how it will look:
2828
*/!*
2929
```
3030

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).
3232

3333
## Creating an element
3434

@@ -48,21 +48,28 @@ To create DOM nodes, there are two methods:
4848
let textNode = document.createTextNode('Here I am');
4949
```
5050

51+
Most of the time we need to create element nodes, such as the `div` for the message.
52+
5153
### Creating the message
5254

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:
5456

5557
```js
58+
// 1. Create <div> element
5659
let div = document.createElement('div');
60+
61+
// 2. Set its class to "alert"
5762
div.className = "alert";
63+
64+
// Fill it with the content
5865
div.innerHTML = "<strong>Hi there!</strong> You've read an important message.";
5966
```
6067

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 page yet. So we can't see it.
6269

6370
## Insertion methods
6471

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`.
6673

6774
There's a special method `append` for that: `document.body.append(div)`.
6875

@@ -90,14 +97,20 @@ Here's the full code:
9097
</script>
9198
```
9299

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)`.
94101

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`,
99108
- `node.replaceWith(...nodes or strings)` –- replaces `node` with the given nodes or strings.
100109

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+
101114
Here's an example of using these methods to add items to a list and the text before/after it:
102115

103116
```html autorun
@@ -121,7 +134,7 @@ Here's an example of using these methods to add items to a list and the text bef
121134
</script>
122135
```
123136

124-
Here's a visual picture what methods do:
137+
Here's a visual picture of what the methods do:
125138

126139
![](before-prepend-append-after.svg)
127140

@@ -139,7 +152,7 @@ before
139152
after
140153
```
141154

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.
143156

144157
For instance, here a string and an element are inserted:
145158

@@ -150,7 +163,7 @@ For instance, here a string and an element are inserted:
150163
</script>
151164
```
152165

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 `<`, `>`.
154167

155168
So the final HTML is:
156169

@@ -166,7 +179,7 @@ In other words, strings are inserted in a safe way, like `elem.textContent` does
166179

167180
So, these methods can only be used to insert DOM nodes or text pieces.
168181

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?
170183

171184
## insertAdjacentHTML/Text/Element
172185

0 commit comments

Comments
 (0)