Skip to content

Commit d4c195d

Browse files
author
Ahmad Awais
authored
FIX: Grammar, TitleCase, Spellings, Sentence Structure
1 parent 4d1de4d commit d4c195d

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

readme.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
# Modern JavaScript cheatsheet
1+
# Modern JavaScript Cheatsheet
22

33
## Introduction
44

55
### Motivation
66

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

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

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

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.
1414
15-
### Complementary resources
15+
### Complementary Resourceqs
1616

1717
When you struggle to understand a notion, I suggest you look for answers on the following resources:
1818

@@ -23,7 +23,7 @@ When you struggle to understand a notion, I suggest you look for answers on the
2323
- [Reddit (JavaScript)](https://www.reddit.com/r/javascript/)
2424
- [Google](https://www.google.com/) to find specific blog and resources
2525

26-
## Table of contents
26+
## Table of Contents
2727

2828
- [Modern JavaScript cheatsheet](#modern-javascript-cheatsheet)
2929
* [Introduction](#introduction)
@@ -223,9 +223,9 @@ console.log(myVar) // raises a ReferenceError !
223223
let myVar = 2;
224224
```
225225

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

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.
229229
230230
In addition, you can't re-declare a *let* variable:
231231

@@ -236,7 +236,7 @@ let myVar = 3; // Raises a SyntaxError
236236

237237
##### const
238238

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

241241
To sum it up, *const* variables:
242242

@@ -282,7 +282,7 @@ person = ["Nick"] // raises an error, because reassignment is not allowed with c
282282

283283
### <a name="arrow_func_concept"></a> Arrow function
284284

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

287287
- More concise
288288
- *this* is picked up from surroundings
@@ -304,7 +304,7 @@ console.log(double(2)) // 4
304304

305305
- *this* reference
306306

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

309309
```js
310310
function myFunc() {
@@ -348,9 +348,9 @@ Since there only is a return value here, we can do an implicit return.
348348
const double = (x) => x * 2;
349349
```
350350

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

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.
354354
355355
- Only one argument
356356

@@ -368,7 +368,7 @@ Parenthesis around the parameter can be avoided:
368368

369369
- No arguments
370370

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

373373
```js
374374
() => { // 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
386386

387387
##### *this* reference
388388

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

391391
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.
392392

@@ -453,7 +453,7 @@ The default parameter is applied in two and only two situations:
453453

454454
In other words, if you pass in *null* the default parameter **won't be applied**.
455455

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)
457457
458458
#### External resource
459459

@@ -464,13 +464,13 @@ In other words, if you pass in *null* the default parameter **won't be applied**
464464

465465
*Destructuring* is a convenient way of creating new variables by extracting some values from data stored in objects or arrays.
466466

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

469469
#### Explanation with sample code
470470

471471
- Object
472472

473-
Lets consider the following object for all the samples:
473+
Let's consider the following object for all the samples:
474474

475475
```js
476476
const person = {
@@ -576,9 +576,9 @@ To sum it up:
576576
- **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
577577
- **Array.prototype.reduce()** takes an array and aggregates the elements into a single value (which is returned)
578578

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

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

583583
#### Sample code
584584

@@ -624,7 +624,7 @@ const doubledNumbers = numbers.map(function(n) {
624624
console.log(doubledNumbers); // [0, 2, 4, 6, 8, 10, 12]
625625
```
626626

627-
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.
628628

629629
Lets extract this function to make it more clear, just for this once:
630630

@@ -636,7 +636,7 @@ console.log(doubledNumbers); // [0, 2, 4, 6, 8, 10, 12]
636636

637637
```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]```.
638638

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.
640640
641641
##### Array.prototype.filter()
642642

@@ -664,7 +664,7 @@ const sum = numbers.reduce(
664664
console.log(sum) //21
665665
```
666666

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

669669
This time though, there are changes:
670670

@@ -682,7 +682,7 @@ The accumulator variable is equal to the return value of your function at the **
682682

683683
###### At first iteration step
684684

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
686686

687687
```n = 0``` first element of the *number* array
688688

@@ -771,7 +771,7 @@ const arr1 = ["a", "b", "c"];
771771
const arr2 = [arr1, "d", "e", "f"]; // [["a", "b", "c"], "d", "e", "f"]
772772
```
773773

774-
*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*.
775775

776776
With spread operator
777777

@@ -782,7 +782,7 @@ const arr2 = [...arr1, "d", "e", "f"]; // ["a", "b", "c", "d", "e", "f"]
782782

783783
##### Function rest parameter
784784

785-
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.
786786

787787
```js
788788
function myFunc() {
@@ -799,7 +799,7 @@ myFunc("Nick", "Anderson", 10, 12, 6);
799799
// 6
800800
```
801801

802-
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?
803803

804804
That's exactly what the rest operator allows us to do!
805805

@@ -829,11 +829,11 @@ console.log(student);
829829
// }
830830
```
831831

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.
833833
834834
##### Object properties spreading
835835

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

838838
```js
839839
const myObj = { x: 1, y: 2, a: 3, b: 4 };
@@ -842,7 +842,7 @@ console.log(x); // 1
842842
console.log(y); // 2
843843
console.log(z); // { a: 3, b: 4 }
844844

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
846846

847847
const n = { x, y, ...z };
848848
console.log(n); // { x: 1, y: 2, a: 3, b: 4 }
@@ -909,7 +909,7 @@ console.log(myObj.y) // 20
909909

910910
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)).
911911

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

914914
#### Sample code
915915

@@ -973,7 +973,7 @@ xFetcherPromise
973973
})
974974
```
975975

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

978978
If it succeeds, *resolve* is called and the function passed as ```.then``` parameter is executed.
979979

@@ -1012,13 +1012,13 @@ const name = "Nick";
10121012

10131013
ES6 modules are used to access variables or functions in a module explicitly exported by the modules it imports.
10141014

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

10171017
#### Explanation with sample code
10181018

10191019
- Named exports
10201020

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

10231023
```js
10241024
// mathConstants.js
@@ -1084,14 +1084,14 @@ console.log(result) // 3
10841084

10851085
*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)).
10861086

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/).
10881088

10891089
```js
10901090
function myFunc() {
10911091
...
10921092
}
10931093

1094-
// After each statement you find the value of *this* in myFunc
1094+
// After each statement, you find the value of *this* in myFunc
10951095

10961096
myFunc.call("myString", "hello") // "myString" -- first .call parameter value is injected into *this*
10971097

@@ -1126,10 +1126,10 @@ JavaScript is a [prototype-based](https://en.wikipedia.org/wiki/Prototype-based_
11261126

11271127
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.
11281128

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

11311131
- [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/)
11331133
- [Inheritance and the prototype chain - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain)
11341134

11351135
#### Samples
@@ -1170,7 +1170,7 @@ console.log(myPerson.stringSentence()) // "Hello, my name is Manu and I'm 23
11701170
For prototype understanding:
11711171

11721172
- [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/)
11741174
- [Inheritance and the prototype chain - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain)
11751175

11761176
For classes understanding:
@@ -1189,7 +1189,7 @@ Source: [MDN](https://developer.mozilla.org/en-US/docs/Glossary/Scope)
11891189

11901190
### <a name="mutation_def"></a> Variable mutation
11911191

1192-
A variable is said to have been mutated when its initial value has changed afterwards.
1192+
A variable is said to have been mutated when its initial value has changed afterward.
11931193

11941194
```js
11951195
var myArray = [];

0 commit comments

Comments
 (0)