Skip to content

Commit 5883dcd

Browse files
Orkhan HuseynliOrkhan Huseynli
authored andcommitted
2 parents 2f477ab + 2af7402 commit 5883dcd

File tree

73 files changed

+438
-352
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+438
-352
lines changed

1-js/01-getting-started/1-intro/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Engines are complicated. But the basics are easy.
3838
2. Then it converts ("compiles") the script to the machine language.
3939
3. And then the machine code runs, pretty fast.
4040
41-
The engine applies optimizations at each step of the process. It even watches the compiled script as it runs, analyzes the data that flows through it, and applies optimizations to the machine code based on that knowledge. When it's done, scripts run quite fast.
41+
The engine applies optimizations at each step of the process. It even watches the compiled script as it runs, analyzes the data that flows through it, and further optimizes the machine code based on that knowledge.
4242
```
4343

4444
## What can in-browser JavaScript do?

1-js/02-first-steps/12-while-for/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ But we can force the exit at any time using the special `break` directive.
212212

213213
For example, the loop below asks the user for a series of numbers, "breaking" when no number is entered:
214214

215-
```js
215+
```js run
216216
let sum = 0;
217217

218218
while (true) {

1-js/02-first-steps/15-function-expressions-arrows/article.md renamed to 1-js/02-first-steps/15-function-expressions/article.md

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Function expressions and arrows
1+
# Function expressions
22

33
In JavaScript, a function is not a "magical language structure", but a special kind of value.
44

@@ -350,112 +350,11 @@ welcome(); // ok now
350350
```smart header="When to choose Function Declaration versus Function Expression?"
351351
As a rule of thumb, when we need to declare a function, the first to consider is Function Declaration syntax. It gives more freedom in how to organize our code, because we can call such functions before they are declared.
352352
353-
That's also better for readability, as it's easier to look up `function f(…) {…}` in the code than `let f = function(…) {…}`. Function Declarations are more "eye-catching".
353+
That's also better for readability, as it's easier to look up `function f(…) {…}` in the code than `let f = function(…) {…};`. Function Declarations are more "eye-catching".
354354
355355
...But if a Function Declaration does not suit us for some reason, or we need a conditional declaration (we've just seen an example), then Function Expression should be used.
356356
```
357357

358-
359-
## Arrow functions [#arrow-functions]
360-
361-
There's one more very simple and concise syntax for creating functions, that's often better than Function Expressions. It's called "arrow functions", because it looks like this:
362-
363-
364-
```js
365-
let func = (arg1, arg2, ...argN) => expression
366-
```
367-
368-
...This creates a function `func` that has arguments `arg1..argN`, evaluates the `expression` on the right side with their use and returns its result.
369-
370-
In other words, it's roughly the same as:
371-
372-
```js
373-
let func = function(arg1, arg2, ...argN) {
374-
return expression;
375-
};
376-
```
377-
378-
...But much more concise.
379-
380-
Let's see an example:
381-
382-
```js run
383-
let sum = (a, b) => a + b;
384-
385-
/* The arrow function is a shorter form of:
386-
387-
let sum = function(a, b) {
388-
return a + b;
389-
};
390-
*/
391-
392-
alert( sum(1, 2) ); // 3
393-
394-
```
395-
396-
If we have only one argument, then parentheses around parameters can be omitted, making that even shorter:
397-
398-
```js run
399-
// same as
400-
// let double = function(n) { return n * 2 }
401-
*!*
402-
let double = n => n * 2;
403-
*/!*
404-
405-
alert( double(3) ); // 6
406-
```
407-
408-
If there are no arguments, parentheses should be empty (but they should be present):
409-
410-
```js run
411-
let sayHi = () => alert("Hello!");
412-
413-
sayHi();
414-
```
415-
416-
Arrow functions can be used in the same way as Function Expressions.
417-
418-
For instance, here's the rewritten example with `welcome()`:
419-
420-
```js run
421-
let age = prompt("What is your age?", 18);
422-
423-
let welcome = (age < 18) ?
424-
() => alert('Hello') :
425-
() => alert("Greetings!");
426-
427-
welcome(); // ok now
428-
```
429-
430-
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
431-
432-
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
433-
434-
```smart header="Multiline arrow functions"
435-
436-
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
437-
438-
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
439-
440-
Like this:
441-
442-
```js run
443-
let sum = (a, b) => { // the curly brace opens a multiline function
444-
let result = a + b;
445-
*!*
446-
return result; // if we use curly braces, use return to get results
447-
*/!*
448-
};
449-
450-
alert( sum(1, 2) ); // 3
451-
```
452-
453-
```smart header="More to come"
454-
Here we praised arrow functions for brevity. But that's not all! Arrow functions have other interesting features. We'll return to them later in the chapter <info:arrow-functions>.
455-
456-
For now, we can already use arrow functions for one-line actions and callbacks.
457-
```
458-
459358
## Summary
460359

461360
- Functions are values. They can be assigned, copied or declared in any place of the code.
@@ -467,8 +366,3 @@ For now, we can already use arrow functions for one-line actions and callbacks.
467366
In most cases when we need to declare a function, a Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization, and is usually more readable.
468367

469368
So we should use a Function Expression only when a Function Declaration is not fit for the task. We've seen a couple of examples of that in this chapter, and will see more in the future.
470-
471-
Arrow functions are handy for one-liners. They come in two flavors:
472-
473-
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
474-
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.

1-js/02-first-steps/15-function-expressions-arrows/1-rewrite-arrow/solution.md renamed to 1-js/02-first-steps/16-arrow-functions-basics/1-rewrite-arrow/solution.md

File renamed without changes.

1-js/02-first-steps/15-function-expressions-arrows/1-rewrite-arrow/task.md renamed to 1-js/02-first-steps/16-arrow-functions-basics/1-rewrite-arrow/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Rewrite with arrow functions
33

4-
Replace Function Expressions with arrow functions in the code:
4+
Replace Function Expressions with arrow functions in the code below:
55

66
```js run
77
function ask(question, yes, no) {
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Arrow functions, the basics
2+
3+
There's another very simple and concise syntax for creating functions, that's often better than Function Expressions.
4+
5+
It's called "arrow functions", because it looks like this:
6+
7+
```js
8+
let func = (arg1, arg2, ...argN) => expression
9+
```
10+
11+
...This creates a function `func` that accepts arguments `arg1..argN`, then evaluates the `expression` on the right side with their use and returns its result.
12+
13+
In other words, it's the shorter version of:
14+
15+
```js
16+
let func = function(arg1, arg2, ...argN) {
17+
return expression;
18+
};
19+
```
20+
21+
Let's see a concrete example:
22+
23+
```js run
24+
let sum = (a, b) => a + b;
25+
26+
/* This arrow function is a shorter form of:
27+
28+
let sum = function(a, b) {
29+
return a + b;
30+
};
31+
*/
32+
33+
alert( sum(1, 2) ); // 3
34+
```
35+
36+
As you can, see `(a, b) => a + b` means a function that accepts two arguments named `a` and `b`. Upon the execution, it evaluates the expression `a + b` and returns the result.
37+
38+
- If we have only one argument, then parentheses around parameters can be omitted, making that even shorter.
39+
40+
For example:
41+
42+
```js run
43+
*!*
44+
let double = n => n * 2;
45+
// roughly the same as: let double = function(n) { return n * 2 }
46+
*/!*
47+
48+
alert( double(3) ); // 6
49+
```
50+
51+
- If there are no arguments, parentheses will be empty (but they should be present):
52+
53+
```js run
54+
let sayHi = () => alert("Hello!");
55+
56+
sayHi();
57+
```
58+
59+
Arrow functions can be used in the same way as Function Expressions.
60+
61+
For instance, to dynamically create a function:
62+
63+
```js run
64+
let age = prompt("What is your age?", 18);
65+
66+
let welcome = (age < 18) ?
67+
() => alert('Hello') :
68+
() => alert("Greetings!");
69+
70+
welcome(); // ok now
71+
```
72+
73+
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
74+
75+
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
76+
77+
## Multiline arrow functions
78+
79+
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
80+
81+
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
82+
83+
Like this:
84+
85+
```js run
86+
let sum = (a, b) => { // the curly brace opens a multiline function
87+
let result = a + b;
88+
*!*
89+
return result; // if we use curly braces, then we need an explicit "return"
90+
*/!*
91+
};
92+
93+
alert( sum(1, 2) ); // 3
94+
```
95+
96+
```smart header="More to come"
97+
Here we praised arrow functions for brevity. But that's not all!
98+
99+
Arrow functions have other interesting features.
100+
101+
To study them in-depth, we first need to get to know some other aspects of JavaScript, so we'll return to arrow functions later in the chapter <info:arrow-functions>.
102+
103+
For now, we can already use arrow functions for one-line actions and callbacks.
104+
```
105+
106+
## Summary
107+
108+
Arrow functions are handy for one-liners. They come in two flavors:
109+
110+
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
111+
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.

1-js/02-first-steps/16-javascript-specials/article.md renamed to 1-js/02-first-steps/17-javascript-specials/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ We covered three ways to create a function in JavaScript:
272272
- Parameters can have default values: `function sum(a = 1, b = 2) {...}`.
273273
- Functions always return something. If there's no `return` statement, then the result is `undefined`.
274274
275-
Details: see <info:function-basics>, <info:function-expressions-arrows>.
275+
Details: see <info:function-basics>, <info:arrow-functions-basics>.
276276
277277
## More to come
278278

1-js/03-code-quality/01-debugging-chrome/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ There are buttons for it at the top of the right panel. Let's engage them.
137137
<span class="devtools" style="background-position:-62px -192px"></span> -- "Step over": run the next command, but *don't go into a function*, hotkey `key:F10`.
138138
: Similar to the previous the "Step" command, but behaves differently if the next statement is a function call. That is: not a built-in, like `alert`, but a function of our own.
139139

140-
The "Step" command goes into it and and pauses the execution at its first line, while "Step over" executes the nested function call invisibly, skipping the function internals.
140+
The "Step" command goes into it and pauses the execution at its first line, while "Step over" executes the nested function call invisibly, skipping the function internals.
141141

142142
The execution is then paused immediately after that function.
143143

1-js/03-code-quality/06-polyfills/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Here Babel comes to the rescue.
1919

2020
Actually, there are two parts in Babel:
2121

22-
1. First, the transpiler program, which rewrites the code. The developer runs it on their own computer. It rewrites the code into the older standard. And then the code is delivered to the website for users. Modern project build systems like [webpack](http://webpack.github.io/) provide means to run transpiler automatically on every code change, so that very easy to integrate into development process.
22+
1. First, the transpiler program, which rewrites the code. The developer runs it on their own computer. It rewrites the code into the older standard. And then the code is delivered to the website for users. Modern project build systems like [webpack](http://webpack.github.io/) provide means to run transpiler automatically on every code change, so that it's very easy to integrate into development process.
2323

2424
2. Second, the polyfill.
2525

0 commit comments

Comments
 (0)