diff --git a/1-js/01-getting-started/1-intro/article.md b/1-js/01-getting-started/1-intro/article.md index 69bb189a..e9fc6ad1 100644 --- a/1-js/01-getting-started/1-intro/article.md +++ b/1-js/01-getting-started/1-intro/article.md @@ -26,7 +26,7 @@ Different engines have different "codenames", for example: - [V8](https://en.wikipedia.org/wiki/V8_(JavaScript_engine)) -- in Chrome and Opera. - [SpiderMonkey](https://en.wikipedia.org/wiki/SpiderMonkey) -- in Firefox. -- ...There are other codenames like "Trident", "Chakra" for different versions of IE, "ChakraCore" for Microsoft Edge, "Nitro" and "SquirrelFish" for Safari etc. +- ...There are other codenames like "Trident", "Chakra" for different versions of IE, "ChakraCore" for Microsoft Edge, "Nitro" and "SquirrelFish" for Safari, etc. The terms above are good to remember, because they are used in developer articles on the internet. We'll use them too. For instance, if "a feature X is supported by V8", then it probably works in Chrome and Opera. @@ -45,7 +45,7 @@ The engine applies optimizations on every stage of the process. It even watches The modern JavaScript is a "safe" programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it. -The capabilities greatly depend on the environment that runs JavaScript. For instance, [Node.JS](https://wikipedia.org/wiki/Node.js) supports functions that allow JavaScript to read/write arbitrary files, perform network requests etc. +The capabilities greatly depend on the environment that runs JavaScript. For instance, [Node.JS](https://wikipedia.org/wiki/Node.js) supports functions that allow JavaScript to read/write arbitrary files, perform network requests, etc. In-browser JavaScript can do everything related to webpage manipulation, interaction with the user and the webserver. diff --git a/1-js/01-getting-started/2-code-editors/article.md b/1-js/01-getting-started/2-code-editors/article.md index 34b7ab7a..b3b4326a 100644 --- a/1-js/01-getting-started/2-code-editors/article.md +++ b/1-js/01-getting-started/2-code-editors/article.md @@ -16,14 +16,12 @@ If you haven't considered selecting an IDE yet, look at the following variants: - [Visual Studio Code](https://code.visualstudio.com/) (free). - [Netbeans](http://netbeans.org/) (paid). -All of the IDEs except cross-platform. +All of the IDEs are cross-platform. -For Windows, there's also a "Visual Studio" editor, don't mess it with "Visual Studio Code". "Visual Studio" is a paid and actually very powerful Windows-only editor, well-suited for .NET platform. A free version of it is called [Visual Studio Community](https://www.visualstudio.com/vs/community/). +For Windows, there's also a "Visual Studio" editor, don't confuse it with "Visual Studio Code". "Visual Studio" is a paid and actually very powerful Windows-only editor, well-suited for .NET platform. A free version of it is called ([Visual Studio Community](https://www.visualstudio.com/vs/community/). Many IDEs are paid, but have a trial period. Their cost is usually negligible compared to a qualified developer's salary, so just choose the best one for you. - - ## Lightweight editors "Lightweight editors" are not as powerful as IDEs, but they're fast, elegant and simple. @@ -53,7 +51,7 @@ I'm using: ## Let's not argue -The editors in the lists above are those that either I or my friends who I consider good developers have been using for a long time and are happy with. +The editors in the lists above are those that either I or my friends whom I consider good developers have been using for a long time and are happy with. There are other great editors in our big world. Please choose the one you like the most. diff --git a/1-js/02-first-steps/02-structure/article.md b/1-js/02-first-steps/02-structure/article.md index ebaa3752..66bf227f 100644 --- a/1-js/02-first-steps/02-structure/article.md +++ b/1-js/02-first-steps/02-structure/article.md @@ -6,7 +6,7 @@ The first thing to study is the building blocks of the code. Statements are syntax constructs and commands that perform actions. -We've already seen a statement `alert('Hello, world!')`, which shows the message. +We've already seen a statement `alert('Hello, world!')`, which shows the message "Hello world!". We can have as many statements in the code as we want. Another statement can be separated with a semicolon. @@ -46,7 +46,7 @@ alert(3 + + 2); ``` -The code outputs `6`, because JavaScript does not insert semicolons here. It is intuitively obvious that if the line ends with a plus `"+"`, then it is an "incomplete expression", no semicolon required. And in this case that works as intended. +The code outputs `6` because JavaScript does not insert semicolons here. It is intuitively obvious that if the line ends with a plus `"+"`, then it is an "incomplete expression", so the semicolon is not required. And in this case that works as intended. **But there are situations where JavaScript "fails" to assume a semicolon where it is really needed.** @@ -98,7 +98,7 @@ It's recommended to put semicolons between statements even if they are separated As time goes on, the program becomes more and more complex. It becomes necessary to add *comments* which describe what happens and why. -Comments can be put into any place of the script. They don't affect the execution, because the engine simply ignores them. +Comments can be put into any place of the script. They don't affect the execution because the engine simply ignores them. **One-line comments start with two forward slash characters `//`.** @@ -156,4 +156,4 @@ Please, don't hesitate to comment your code. Comments increase the overall code footprint, but that's not a problem at all. There are many tools which minify the code before publishing to the production server. They remove comments, so they don't appear in the working scripts. Therefore comments do not have any negative effects on production at all. -Further in the tutorial, there will be a chapter that also explains how to write better comments. +Further in the tutorial there will be a chapter that also explains how to write better comments. diff --git a/1-js/03-code-quality/02-coding-style/article.md b/1-js/03-code-quality/02-coding-style/article.md index 3ab32554..accc407c 100644 --- a/1-js/03-code-quality/02-coding-style/article.md +++ b/1-js/03-code-quality/02-coding-style/article.md @@ -1,14 +1,12 @@ -# Coding style +# Coding Style Our code must be as clean and easy to read as possible. -That is actually an art of programming -- to take a complex task and code it in a way that is both correct and human-readable. - -One thing to help is the good code style. +That is actually the art of programming -- to take a complex task and code it in a way that is both correct and human-readable. ## Syntax -A cheatsheet with the rules (more details below): +Here is a cheatsheet with some suggested rules (see below for more details): ![](code-style.png) ![](figure-bracket-style.png) -As a summary: -- For a really short code, one line is acceptable: like `if (cond) return null`. -- But a separate line for each statement in brackets is usually better. +In summary: +- For very short code, one line is acceptable. For example: `if (cond) return null`. +- But a separate line for each statement in brackets is usually easier to read. -### Line length +### Line Length -The maximal line length should be limited. No one likes to eye-follow a long horizontal line. It's better to split it. +No one likes to read a long horizontal line of code. It's best practice to split them up and limit the length of your lines. -The maximal line length is agreed on the team-level. It's usually 80 or 120 characters. +The maximum line length should be agreed upon at the team-level. It's usually 80 or 120 characters. ### Indents There are two types of indents: -- **A horizontal indent: 2(4) spaces.** +- **Horizontal indents: 2 or 4 spaces.** A horizontal indentation is made using either 2 or 4 spaces or the "Tab" symbol. Which one to choose is an old holy war. Spaces are more common nowadays. @@ -107,9 +105,9 @@ There are two types of indents: } ``` -- **A vertical indent: empty lines for splitting code into logical blocks.** +- **Vertical indents: empty lines for splitting code into logical blocks.** - Even a single function can often be divided in logical blocks. In the example below, the initialization of variables, the main loop and returning the result are split vertically: + Even a single function can often be divided into logical blocks. In the example below, the initialization of variables, the main loop and returning the result are split vertically: ```js function pow(x, n) { @@ -125,21 +123,21 @@ There are two types of indents: Insert an extra newline where it helps to make the code more readable. There should not be more than nine lines of code without a vertical indentation. -### A semicolon +### Semicolons -A semicolon should be present after each statement. Even if it could possibly be skipped. +A semicolon should be present after each statement, even if it could possibly be skipped. -There are languages where a semicolon is truly optional. It's rarely used there. But in JavaScript there are few cases when a line break is sometimes not interpreted as a semicolon. That leaves a place for programming errors. +There are languages where a semicolon is truly optional and it is rarely used. In JavaScript, though, there are cases where a line break is not interpreted as a semicolon, leaving the code vulnerable to errors. -As you become more mature as a programmer, you may choose a no-semicolon style, like [StandardJS](https://standardjs.com/), but that's only when you know JavaScript well and understand possible pitfalls. +As you become more mature as a programmer, you may choose a no-semicolon style like [StandardJS](https://standardjs.com/). Until then, it's best to use semicolons to avoid possible pitfalls. -### Nesting levels +### Nesting Levels -There should not be too many nesting levels. +Try to avoid nesting code too many levels deep. -Sometimes it's a good idea to use the ["continue"](info:while-for#continue) directive in the loop to evade extra nesting in `if(..) { ... }`: +Sometimes it's a good idea to use the ["continue"](info:while-for#continue) directive in a loop to avoid extra nesting. -Instead of: +For example, instead of adding a nested `if` conditional like this: ```js for (let i = 0; i < 10; i++) { @@ -162,7 +160,7 @@ A similar thing can be done with `if/else` and `return`. For example, two constructs below are identical. -The first one: +Option 1: ```js function pow(x, n) { @@ -180,7 +178,7 @@ function pow(x, n) { } ``` -And this: +Option 2: ```js function pow(x, n) { @@ -199,13 +197,13 @@ function pow(x, n) { } ``` -...But the second one is more readable, because the "edge case" of `n < 0` is handled early on, and then we have the "main" code flow, without an additional nesting. +The second one is more readable because the "edge case" of `n < 0` is handled early on. Once the check is done we can move on to the "main" code flow without the need for additional nesting. -## Functions below the code +## Function Placement -If you are writing several "helper" functions and the code to use them, then there are three ways to place them. +If you are writing several "helper" functions and the code that uses them, there are three ways to organize the functions. -1. Functions above the code that uses them: +1. Functions declared above the code that uses them: ```js // *!*function declarations*/!* @@ -235,7 +233,6 @@ If you are writing several "helper" functions and the code to use them, then the walkAround(); // --- *!*helper functions*/!* --- - function createElement() { ... } @@ -248,39 +245,37 @@ If you are writing several "helper" functions and the code to use them, then the ... } ``` -3. Mixed: a function is described where it's first used. +3. Mixed: a function is declared where it's first used. Most of time, the second variant is preferred. -That's because when reading a code, we first want to know "what it does". If the code goes first, then it provides that information. And then maybe we won't need to read functions at all, especially if their names are adequate to what they're doing. +That's because when reading code, we first want to know *what it does*. If the code goes first, then it provides that information. Then, maybe we won't need to read the functions at all, especially if their names are descriptive of what they actually do. -## Style guides +## Style Guides -A style guide contains general rules about "how to write": which quotes to use, how many spaces to indent, where to put line breaks, etc. A lot of minor things. +A style guide contains general rules about "how to write" code, e.g. which quotes to use, how many spaces to indent, where to put line breaks, etc. A lot of minor things. -In total, when all members of a team use the same style guide, the code looks uniform. No matter who of the team wrote it, it's still the same style. +When all members of a team use the same style guide the code tends looks uniform, regardless of which team member wrote it. -Surely, a team may think out a style guide themselves. But as of now, there's no need to. There are many tried, worked-out style guides, which are easy to adopt. +Of course, a team can always write their own style guide. Most of the time though, there's no need to. There are many existing tried and true options to choose from, so adopting one of these is usually your best bet. -For instance: +Some popular choices: - [Google JavaScript Style Guide](https://google.github.io/styleguide/javascriptguide.xml) - [Airbnb JavaScript Style Guide](https://github.com/airbnb/javascript) - [Idiomatic.JS](https://github.com/rwaldron/idiomatic.js) - [StandardJS](https://standardjs.com/) -- (there are more) +- (plus many more) -If you're a novice developer, then you could start with the cheatsheet above in the chapter, and later browse the style guides to pick up the common principles and maybe choose one. +If you're a novice developer, start with the cheatsheet at the beginning of this chapter. Once you've mastered that you can browse other style guides to pick up common principles and decide which one you like best. -## Automated linters +## Automated Linters -There are tools that can check the code style automatically. They are called "linters". +Linters are tools that can automatically check the style of your code and make suggestions for refactoring. -The great thing about them is that style-checking also finds some bugs, like a typo in a variable or function name. +The great thing about them is that style-checking can also find some bugs, like typos in variable or function names. Because of this feature, installing a linter is recommended even if you don't want to stick to one particular "code style". -So it's recommended to install one, even if you don't want to stick to a "code style". They help to find typos -- and that's already good enough. - -Most well-known tools are: +Here are the most well-known linting tools: - [JSLint](http://www.jslint.com/) -- one of the first linters. - [JSHint](http://www.jshint.com/) -- more settings than JSLint. @@ -288,15 +283,16 @@ Most well-known tools are: All of them can do the job. The author uses [ESLint](http://eslint.org/). -Most linters are integrated with editors: just enable the plugin in the editor and configure the style. +Most linters are integrated with many popular editors: just enable the plugin in the editor and configure the style. For instance, for ESLint you should do the following: 1. Install [Node.JS](https://nodejs.org/). 2. Install ESLint with the command `npm install -g eslint` (npm is a JavaScript package installer). 3. Create a config file named `.eslintrc` in the root of your JavaScript project (in the folder that contains all your files). +4. Install/enable the plugin for your editor that integrates with ESLint. The majority of editors have one. -Here's an example of `.eslintrc`: +Here's an example of an `.eslintrc` file: ```js { @@ -313,22 +309,16 @@ Here's an example of `.eslintrc`: } ``` -Here the directive `"extends"` denotes that we base on the "eslint:recommended" set of settings, and then we specify our own. +Here the directive `"extends"` denotes that the configuration is based on the "eslint:recommended" set of settings. After that, we specify our own. -Then install/enable the plugin for your editor that integrates with ESLint. The majority of editors have it. +It is also possible to download style rule sets from the web and extend them instead. See for more details about installation. -It is possible to download style rule sets from the web and extend them instead. See for more details about installation. - -Using a linter has a great side-effect: linters catch typos. For instance, when an undefined variable is accessed, a linter detects it and (if integrated with an editor) highlights it. In most cases that's a mistype. So we can fix it right ahead. - -For that reason even if you're not concerned about styles, using a linter is highly recommended. - -Also certain IDEs support built-in linting, that also may be good, but not so tunable as ESLint. +Also certain IDEs have built-in linting, which is convenient but not as customizable as ESLint. ## Summary -All syntax rules from this chapter and the style guides aim to increase readability, so all of them are debatable. +All syntax rules described in this chapter (and in the style guides referenced) aim to increase the readability of your code, but all of them are debatable. -When we think about "how to write better?", the sole criterion is "what makes the code more readable and easier to understand? what helps to avoid errors?" That's the main thing to keep in mind when choosing the style or discussing which one is better. +When we think about writing "better" code, the questions we should ask are, "What makes the code more readable and easier to understand?" and "What can help us avoid errors?" These are the main things to keep in mind when choosing and debating code styles. -Read style guides to see the latest ideas about that and follow those that you find the best. +Reading popular style guides will allow you to keep up to date with the latest ideas about code style trends and best practices. diff --git a/1-js/04-object-basics/01-object/4-const-object/task.md b/1-js/04-object-basics/01-object/4-const-object/task.md index 95f7a64d..a9aada63 100644 --- a/1-js/04-object-basics/01-object/4-const-object/task.md +++ b/1-js/04-object-basics/01-object/4-const-object/task.md @@ -4,7 +4,7 @@ importance: 5 # Constant objects? -Is it possible to change an object declared with `const`, how do you think? +Is it possible to change an object declared with `const`? What do you think? ```js const user = { diff --git a/1-js/04-object-basics/01-object/article.md b/1-js/04-object-basics/01-object/article.md index cf161926..806eac44 100644 --- a/1-js/04-object-basics/01-object/article.md +++ b/1-js/04-object-basics/01-object/article.md @@ -205,7 +205,7 @@ let obj = { for: 1, let: 2, return: 3 -} +}; alert( obj.for + obj.let + obj.return ); // 6 ``` @@ -605,7 +605,7 @@ for (let key in user) { } */!* -// now clone is a fully independant clone +// now clone is a fully independent clone clone.name = "Pete"; // changed the data in it alert( user.name ); // still John in the original object diff --git a/1-js/04-object-basics/03-symbol/article.md b/1-js/04-object-basics/03-symbol/article.md index ba398941..b4336762 100644 --- a/1-js/04-object-basics/03-symbol/article.md +++ b/1-js/04-object-basics/03-symbol/article.md @@ -75,7 +75,7 @@ user[id] = "ID Value"; alert( user[id] ); // we can access the data using the symbol as the key ``` -What's the benefit over using `Symbol("id")` over a string `"id"`? +What's the benefit of using `Symbol("id")` over a string `"id"`? Let's make the example a bit deeper to see that. diff --git a/1-js/05-data-types/01-primitives-methods/article.md b/1-js/05-data-types/01-primitives-methods/article.md index 4d40cd5d..a40fdf55 100644 --- a/1-js/05-data-types/01-primitives-methods/article.md +++ b/1-js/05-data-types/01-primitives-methods/article.md @@ -1,19 +1,22 @@ # Methods of primitives -JavaScript allows us to work with primitives (strings, numbers etc) as if they were objects. +JavaScript allows us to work with primitives (strings, numbers, etc.) as if they were objects. -They also provide methods to call as such. We will study those soon, but first we'll see how it works, because, of course, primitives are not objects (and here we will make it even more clear). +They also provide methods to call as such. We will study those soon, but first we'll see how it works because, of course, primitives are not objects (and here we will make it even clearer). -Let's look at the key distinction between primitives and objects. +Let's look at the key distinctions between primitives and objects. A primitive -: Is a value of a primitive type. There are 6 primitive types: `string`, `number`, `boolean`, `symbol`, `null` and `undefined`. + +- Is a value of a primitive type. +- There are 6 primitive types: `string`, `number`, `boolean`, `symbol`, `null` and `undefined`. An object -: Is capable of storing multiple values as properties. -Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript, e.g. functions are objects. -One of the best things about objects is that we can store a function as one of its properties: +- Is capable of storing multiple values as properties. +- Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript; functions, for example, are objects. + +One of the best things about objects is that we can store a function as one of its properties. ```js run let john = { @@ -28,7 +31,7 @@ john.sayHi(); // Hi buddy! So here we've made an object `john` with the method `sayHi`. -Many built-in objects already exist, such as those that work with dates, errors, HTML elements etc. They have different properties and methods. +Many built-in objects already exist, such as those that work with dates, errors, HTML elements, etc. They have different properties and methods. But, these features come with a cost! diff --git a/1-js/05-data-types/02-number/article.md b/1-js/05-data-types/02-number/article.md index b42e9f1e..2a5a371e 100644 --- a/1-js/05-data-types/02-number/article.md +++ b/1-js/05-data-types/02-number/article.md @@ -349,7 +349,7 @@ But in real life we often have values in units, like `"100px"` or `"12pt"` in CS That's what `parseInt` and `parseFloat` are for. -They "read" a number from a string until they can. In case of an error, the gathered number is returned. The function `parseInt` returns an integer, whilst `parseFloat` will return a floating-point number: +They "read" a number from a string until they can't. In case of an error, the gathered number is returned. The function `parseInt` returns an integer, whilst `parseFloat` will return a floating-point number: ```js run alert( parseInt('100px') ); // 100 diff --git a/1-js/05-data-types/07-map-set-weakmap-weakset/article.md b/1-js/05-data-types/07-map-set-weakmap-weakset/article.md index 8af8b6cd..a7838efd 100644 --- a/1-js/05-data-types/07-map-set-weakmap-weakset/article.md +++ b/1-js/05-data-types/07-map-set-weakmap-weakset/article.md @@ -324,7 +324,7 @@ Now where do we need such thing? The idea of `WeakMap` is that we can store something for an object that exists only while the object exists. But we do not force the object to live by the mere fact that we store something for it. ```js -weakMap.put(john, "secret documents"); +weakMap.set(john, "secret documents"); // if john dies, secret documents will be destroyed ``` @@ -378,7 +378,7 @@ With a regular `Map`, cleaning up after a user has left becomes a tedious task: `WeakSet` behaves similarly: - It is analogous to `Set`, but we may only add objects to `WeakSet` (not primitives). -- An object exists in the set while it has reachable from somewhere else. +- An object exists in the set while it is reachable from somewhere else. - Like `Set`, it supports `add`, `has` and `delete`, but not `size`, `keys()` and no iterations. For instance, we can use it to keep track of whether an item is checked: @@ -429,4 +429,4 @@ The most notable limitation of `WeakMap` and `WeakSet` is the absence of iterati - Also does not support `size/clear()` and iterations. -`WeakMap` and `WeakSet` are used as "secondary" data structures in addition to the "main" object storage. Once the object is removed from the main storage, so it only stays in `WeakMap/WeakSet`, they clean up automatically. +`WeakMap` and `WeakSet` are used as "secondary" data structures in addition to the "main" object storage. Once the object is removed from the main storage, if it is only found in the `WeakMap/WeakSet`, it will be cleaned up automatically. diff --git a/2-ui/1-document/07-modifying-document/article.md b/2-ui/1-document/07-modifying-document/article.md index 90e3f2bb..9c38b334 100644 --- a/2-ui/1-document/07-modifying-document/article.md +++ b/2-ui/1-document/07-modifying-document/article.md @@ -409,7 +409,7 @@ The syntax: The call to `document.write(html)` writes the `html` into page "right here and now". The `html` string can be dynamically generated, so it's kind of flexible. We can use JavaScript to create a full-fledged webpage and write it. -The method comes from times when there were no DOM, no standards... Really old times. It still lives, because there are scripts using it. +The method comes from times when there was no DOM, no standards... Really old times. It still lives, because there are scripts using it. In modern scripts we can rarely see it, because of the following important limitation: diff --git a/2-ui/2-events/04-default-browser-action/article.md b/2-ui/2-events/04-default-browser-action/article.md index 2e338dc5..b2889132 100644 --- a/2-ui/2-events/04-default-browser-action/article.md +++ b/2-ui/2-events/04-default-browser-action/article.md @@ -17,7 +17,7 @@ There are two ways to tell the browser we don't want it to act: - The main way is to use the `event` object. There's a method `event.preventDefault()`. - If the handler is assigned using `on` (not by `addEventListener`), then we can just return `false` from it. -In the example below there a click to links don't lead to URL change: +In the example below a click to links doesn't lead to URL change: ```html autorun height=60 no-beautify Click here diff --git a/4-frames-and-windows/03-cross-window-communication/article.md b/4-frames-and-windows/03-cross-window-communication/article.md index ffcc1257..a4193a62 100644 --- a/4-frames-and-windows/03-cross-window-communication/article.md +++ b/4-frames-and-windows/03-cross-window-communication/article.md @@ -32,7 +32,7 @@ Now let's see how some examples. First, about pages that come from the same orig ````warn header="Subdomains may be same-origin" There's a small exclusion in the "Same Origin" policy. -If windows share the same second-level domain, for instance `john.site.com`, `peter.site.com` and `site.com` (so that theyr common second-level domain is `site.com`), they can be treated as coming from the "same origin". +If windows share the same second-level domain, for instance `john.site.com`, `peter.site.com` and `site.com` (so that their common second-level domain is `site.com`), they can be treated as coming from the "same origin". To make it work, all such pages (including the one from `site.com`) should run the code: diff --git a/6-async/05-async-await/article.md b/6-async/05-async-await/article.md index 23b58240..0a76f1a0 100644 --- a/6-async/05-async-await/article.md +++ b/6-async/05-async-await/article.md @@ -183,7 +183,7 @@ The meaning is the same: it ensures that the returned value is a promise and ena ```` ## Error handling -If a promise resolves normally, then `await promise` returns the result. But in case of a rejection, it throws the error, just if there were a `throw` statement at that line. +If a promise resolves normally, then `await promise` returns the result. But in case of a rejection, it throws the error, just as if there were a `throw` statement at that line. This code: