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: readme.md
+40-40Lines changed: 40 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
-
# Modern JavaScript cheatsheet
1
+
# Modern JavaScript Cheatsheet
2
2
3
3
## Introduction
4
4
5
5
### Motivation
6
6
7
-
This document is a cheatsheet for JavaScript you will frequently encounter in modern projects and in most contemporary sample code.
7
+
This document is a cheatsheet for JavaScript you will frequently encounter in modern projects and most contemporary sample code.
8
8
9
9
This guide is not intended to teach you JavaScript from the ground up, but to help developers with basic knowledge who may struggle to get familiar with modern codebases (or let's say to learn React for instance) because of the JavaScript concepts used.
10
10
11
-
Besides, I will sometimes provide personal tips that may be debatable, but will take care to mention that it's a personal recommendation when I do so.
11
+
Besides, I will sometimes provide personal tips that may be debatable but will take care to mention that it's a personal recommendation when I do so.
12
12
13
-
> **Note:** Most of the concepts introduced here are coming from a JavaScript language update (ES2015, often called ES6). You can find new features added by this update [here](http://es6-features.org); it's very well done.
13
+
> **Note:** Most of the concepts introduced here are coming from a JavaScript language update (ES2015, often called ES6). You can find new features added by this update [here](http://es6-features.org); it's very well done.
14
14
15
-
### Complementary resources
15
+
### Complementary Resourceqs
16
16
17
17
When you struggle to understand a notion, I suggest you look for answers on the following resources:
18
18
@@ -23,7 +23,7 @@ When you struggle to understand a notion, I suggest you look for answers on the
@@ -223,9 +223,9 @@ console.log(myVar) // raises a ReferenceError !
223
223
let myVar =2;
224
224
```
225
225
226
-
By contrast with *var* variables, if you try to read or write on a *let* or *const* variable before they are assigned an error will be raised. This phenomenom is often called [*Temporal dead zone*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_Dead_Zone_and_errors_with_let) or *TDZ*.
226
+
By contrast with *var* variables, if you try to read or write on a *let* or *const* variable before they are assigned an error will be raised. This phenomenon is often called [*Temporal dead zone*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_Dead_Zone_and_errors_with_let) or *TDZ*.
227
227
228
-
> **Note:** Technically, *let* and *const* variables declarations are being hoisted too, but not their assignation. Since they're made so that they can't be used before assignation it intuitively feels like there is no hoisting, but there is. Find out more on this [very detailed explanation here](http://jsrocks.org/2015/01/temporal-dead-zone-tdz-demystified) if you want to know more.
228
+
> **Note:** Technically, *let* and *const* variables declarations are being hoisted too, but not their assignation. Since they're made so that they can't be used before assignation, it intuitively feels like there is no hoisting, but there is. Find out more on this [very detailed explanation here](http://jsrocks.org/2015/01/temporal-dead-zone-tdz-demystified) if you want to know more.
229
229
230
230
In addition, you can't re-declare a *let* variable:
231
231
@@ -236,7 +236,7 @@ let myVar = 3; // Raises a SyntaxError
236
236
237
237
##### const
238
238
239
-
```const``` declared variables behave like *let* variables, but in addition they can't be reassigned.
239
+
```const``` declared variables behave like *let* variables, but also they can't be reassigned.
240
240
241
241
To sum it up, *const* variables:
242
242
@@ -282,7 +282,7 @@ person = ["Nick"] // raises an error, because reassignment is not allowed with c
282
282
283
283
### <aname="arrow_func_concept"></a> Arrow function
284
284
285
-
The ES6 JavaScript update has introduced *arrow functions*, which are another way to declare and use functions. Here are the benefits they bring:
285
+
The ES6 JavaScript update has introduced *arrow functions*, which is another way to declare and use functions. Here are the benefits they bring:
286
286
287
287
- More concise
288
288
-*this* is picked up from surroundings
@@ -304,7 +304,7 @@ console.log(double(2)) // 4
304
304
305
305
-*this* reference
306
306
307
-
In an arrow function, *this* is equal to the *this* value of the enclosing execution context. Basically, with arrow functions you don't have to do the "that = this" trick before calling a function inside a function anymore.
307
+
In an arrow function, *this* is equal to the *this* value of the enclosing execution context. Basically, with arrow functions, you don't have to do the "that = this" trick before calling a function inside a function anymore.
308
308
309
309
```js
310
310
functionmyFunc() {
@@ -348,9 +348,9 @@ Since there only is a return value here, we can do an implicit return.
348
348
constdouble= (x) => x *2;
349
349
```
350
350
351
-
To do so, we only need to **remove the brackets** and the **return** keyword. That's why it's called an *implicit* return, the *return* keyword is not there but this function will indeed return ```x * 2```.
351
+
To do so, we only need to **remove the brackets** and the **return** keyword. That's why it's called an *implicit* return, the *return* keyword is not there, but this function will indeed return ```x * 2```.
352
352
353
-
> **Note:** If your function does not return a value (with *side effects*), it doesn't do an explicit nor an implicit return.
353
+
> **Note:** If your function does not return a value (with *side effects*), it doesn't do an explicit nor an implicit return.
354
354
355
355
- Only one argument
356
356
@@ -368,7 +368,7 @@ Parenthesis around the parameter can be avoided:
368
368
369
369
- No arguments
370
370
371
-
When there is no argument provided to an arrow function, you need to provide parentheses or it won't be valid syntax.
371
+
When there is no argument provided to an arrow function, you need to provide parentheses, or it won't be valid syntax.
372
372
373
373
```js
374
374
() => { // parenthesis are provided, everything is fine
@@ -386,7 +386,7 @@ When there is no argument provided to an arrow function, you need to provide par
386
386
387
387
##### *this* reference
388
388
389
-
To understand this subtlety introduced with arrow functions, you must understand how [this](#this_def) behaves in JavaScript.
389
+
To understand this subtlety introduced with arrow functions, you must know how [this](#this_def) behaves in JavaScript.
390
390
391
391
In an arrow function, *this* is equal to the *this* value of the enclosing execution context. What it means is that an arrow function doesn't create a new *this*, it grabs it from its surrounding instead.
392
392
@@ -453,7 +453,7 @@ The default parameter is applied in two and only two situations:
453
453
454
454
In other words, if you pass in *null* the default parameter **won't be applied**.
455
455
456
-
> **Note:** Default value assignment can be used with destructured parameters as well (see next notion to see an example)
456
+
> **Note:** Default value assignment can be used with destructured parameters as well (see next notion to see an example)
457
457
458
458
#### External resource
459
459
@@ -464,13 +464,13 @@ In other words, if you pass in *null* the default parameter **won't be applied**
464
464
465
465
*Destructuring* is a convenient way of creating new variables by extracting some values from data stored in objects or arrays.
466
466
467
-
To name a few useful cases, *destructuring* can be used to destructure function parameters or *this.props* in React projects for instance.
467
+
To name a few use cases, *destructuring* can be used to destructure function parameters or *this.props* in React projects for instance.
468
468
469
469
#### Explanation with sample code
470
470
471
471
- Object
472
472
473
-
Lets consider the following object for all the samples:
473
+
Let's consider the following object for all the samples:
474
474
475
475
```js
476
476
constperson= {
@@ -576,9 +576,9 @@ To sum it up:
576
576
-**Array.prototype.filter()** takes an array, decides element by element if it should keep it or not and returns an array with the kept elements only
577
577
-**Array.prototype.reduce()** takes an array and aggregates the elements into a single value (which is returned)
578
578
579
-
I recommend to use them as much as possible in following the principles of functional programming, because they are composable, concise and elegant.
579
+
I recommend to use them as much as possible in following the principles of functional programming because they are composable, concise and elegant.
580
580
581
-
With those three methods you can avoid the use of *for* and *forEach* loops in must situations. When you are tempted to do a *for* loop, try to do it with *map*, *filter* and *reduce* composed. You might struggle to do it at first because it requires you to learn a new way of thinking, but once you've got it things gets easier.
581
+
With those three methods, you can avoid the use of *for* and *forEach* loops in most situations. When you are tempted to do a *for* loop, try to do it with *map*, *filter* and *reduce* composed. You might struggle to do it at first because it requires you to learn a new way of thinking, but once you've got it things gets easier.
What's happening here? We are using .map on the *numbers* array, map is iterating on each element of the array and passes it to our function. The goal of the function is to produce and return a new value from the one passed so that map can replace it.
627
+
What's happening here? We are using .map on the *numbers* array, the map is iterating on each element of the array and passes it to our function. The goal of the function is to produce and return a new value from the one passed so that map can replace it.
628
628
629
629
Lets extract this function to make it more clear, just for this once:
```numbers.map(doubleN)``` produces ```[doubleN(0), doubleN(1), doubleN(2), doubleN(3), doubleN(4), doubleN(5), doubleN(6)]``` which is equal to ```[0, 2, 4, 6, 8, 10, 12]```.
638
638
639
-
> **Note:** If you do not need to return a new array and just want to do a loop that have side effects, you might just want to use a for / forEach loop instead of a map.
639
+
> **Note:** If you do not need to return a new array and just want to do a loop that has side effects, you might just want to use a for / forEach loop instead of a map.
640
640
641
641
##### Array.prototype.filter()
642
642
@@ -664,7 +664,7 @@ const sum = numbers.reduce(
664
664
console.log(sum) //21
665
665
```
666
666
667
-
Just like for .map and .filter methods, .reduce is applied on an array and takes a function as first parameter.
667
+
Just like for .map and .filter methods, .reduce is applied on an array and takes a function as the first parameter.
668
668
669
669
This time though, there are changes:
670
670
@@ -682,7 +682,7 @@ The accumulator variable is equal to the return value of your function at the **
682
682
683
683
###### At first iteration step
684
684
685
-
```acc = 0``` because we passed in 0 as second parameter for reduce
685
+
```acc = 0``` because we passed in 0 as the second parameter for reduce
*arr2* first element is an array, because *arr1* is injected as is into *arr2*. But what we want is *arr2* to be an array of letters. To do so, we can *spread* the elements of *arr1* into *arr2*.
774
+
*arr2*the first element is an array because *arr1* is injected as is into *arr2*. But what we want is *arr2* to be an array of letters. To do so, we can *spread* the elements of *arr1* into *arr2*.
In function parameters, we can use the rest operator in order to inject parameters into an array we can loop in. There is already an **argument** object bound to every function that is equal to an array of all the parameters passed-in to the function.
785
+
In function parameters, we can use the rest operator to inject parameters into an array we can loop in. There is already an **argument** object bound to every function that is equal to an array of all the parameters passed into the function.
But lets say that we want this function to create a new student with its grades and with its average grade. Wouldn't it be more convenient to extract the first two parameters into two separated variables, and then have all the grades in an array we can iterate over?
802
+
But let's say that we want this function to create a new student with its grades and with its average grade. Wouldn't it be more convenient to extract the first two parameters into two separate variables, and then have all the grades in an array we can iterate over?
803
803
804
804
That's exactly what the rest operator allows us to do!
805
805
@@ -829,11 +829,11 @@ console.log(student);
829
829
// }
830
830
```
831
831
832
-
> **Note:** createStudent function is bad because we don't check if grades.length exists or is different from 0. But its easier to read this way so I didn't handled this case.
832
+
> **Note:** createStudent function is bad because we don't check if grades.length exists or is different from 0. But it's easier to read this way, so I didn't handle this case.
833
833
834
834
##### Object properties spreading
835
835
836
-
For this one I recommend you read previous explanations about the rest operator on iterables and function parameters.
836
+
For this one, I recommend you read previous explanations about the rest operator on iterables and function parameters.
837
837
838
838
```js
839
839
constmyObj= { x:1, y:2, a:3, b:4 };
@@ -842,7 +842,7 @@ console.log(x); // 1
842
842
console.log(y); // 2
843
843
console.log(z); // { a: 3, b: 4 }
844
844
845
-
// z is the rest of the object destructured: myObj object minus x and y properties destructured
845
+
// z is the rest of the object destructured: myObj object minus x and y properties destructured
846
846
847
847
constn= { x, y, ...z };
848
848
console.log(n); // { x: 1, y: 2, a: 3, b: 4 }
@@ -909,7 +909,7 @@ console.log(myObj.y) // 20
909
909
910
910
A promise is an object which can be returned synchronously from an asynchronous function ([ref](https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261#3cd0)).
911
911
912
-
Promises can be used to avoid [callback hell](http://callbackhell.com/) and they are more and more frequently encountered in modern JavaScript projects.
912
+
Promises can be used to avoid [callback hell](http://callbackhell.com/), and they are more and more frequently encountered in modern JavaScript projects.
913
913
914
914
#### Sample code
915
915
@@ -973,7 +973,7 @@ xFetcherPromise
973
973
})
974
974
```
975
975
976
-
```.then``` is a method that once called will put the xFetcherPromise in **pending** state. When called, the promise body runs and in this case an Ajax call is being done.
976
+
```.then``` is a method that once called will put the xFetcherPromise in **pending** state. When called, the promise body runs, and in this case, an Ajax call is being done.
977
977
978
978
If it succeeds, *resolve* is called and the function passed as ```.then``` parameter is executed.
979
979
@@ -1012,13 +1012,13 @@ const name = "Nick";
1012
1012
1013
1013
ES6 modules are used to access variables or functions in a module explicitly exported by the modules it imports.
1014
1014
1015
-
I highly recommend to take a look at MDN resources on import / export (see external resources below), it is both simple and complete.
1015
+
I highly recommend to take a look at MDN resources on import/export (see external resources below), it is both straightforward and complete.
1016
1016
1017
1017
#### Explanation with sample code
1018
1018
1019
1019
- Named exports
1020
1020
1021
-
Named exports are useful to export several values from a module. You can only name-export variables (not functions or class), so if you want to name-export a function, you have to store it in a variable before.
1021
+
Named exports are used to export several values from a module. You can only name-export variables (not functions or class), so if you want to name-export a function, you have to store it in a variable before.
1022
1022
1023
1023
```js
1024
1024
// mathConstants.js
@@ -1084,14 +1084,14 @@ console.log(result) // 3
1084
1084
1085
1085
*this* operator behaves differently than in other languages and is in most cases determined by how a function is called. ([Ref: MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)).
1086
1086
1087
-
This notion having many subtleties and being quite hard, I highly suggest you to deep dive in the external resources below. Thus, I will provide what I personally have in mind to determine what *this* is equal to. I have learned this tip from [this article written by Yehuda Katz](http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/).
1087
+
This notion is having many subtleties and being quite hard, I highly suggest you to deep dive in the external resources below. Thus, I will provide what I personally have in mind to determine what *this* is equal to. I have learned this tip from [this article written by Yehuda Katz](http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/).
1088
1088
1089
1089
```js
1090
1090
functionmyFunc() {
1091
1091
...
1092
1092
}
1093
1093
1094
-
// After each statement you find the value of *this* in myFunc
1094
+
// After each statement, you find the value of *this* in myFunc
1095
1095
1096
1096
myFunc.call("myString", "hello") // "myString" -- first .call parameter value is injected into *this*
1097
1097
@@ -1126,10 +1126,10 @@ JavaScript is a [prototype-based](https://en.wikipedia.org/wiki/Prototype-based_
1126
1126
1127
1127
The word *class* is indeed error prone if you are familiar with classes in other languages. If you do, avoid assuming how JavaScript classes work on this basis and consider it an entirely different notion.
1128
1128
1129
-
Since this document is not an attempt to teach you the language from the ground up, I will consider you know what prototypes are and how they behave. But here are some links I found great to understand this notion:
1129
+
Since this document is not an attempt to teach you the language from the ground up, I will believe you know what prototypes are and how they behave. But here are some links I found great to understand this notion:
1130
1130
1131
1131
-[Understanding Prototypes in JS - Yehuda Katz](http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/)
1132
-
-[A plain english guide to JS prototypes - Sebastian Porto](http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/)
1132
+
-[A plain English guide to JS prototypes - Sebastian Porto](http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/)
1133
1133
-[Inheritance and the prototype chain - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain)
1134
1134
1135
1135
#### Samples
@@ -1170,7 +1170,7 @@ console.log(myPerson.stringSentence()) // "Hello, my name is Manu and I'm 23
1170
1170
For prototype understanding:
1171
1171
1172
1172
-[Understanding Prototypes in JS - Yehuda Katz](http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/)
1173
-
-[A plain english guide to JS prototypes - Sebastian Porto](http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/)
1173
+
-[A plain English guide to JS prototypes - Sebastian Porto](http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/)
1174
1174
-[Inheritance and the prototype chain - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain)
0 commit comments