Skip to content

Commit 2d37bb8

Browse files
authored
when to use maps and sets, reorder
1 parent f2934eb commit 2d37bb8

1 file changed

Lines changed: 42 additions & 12 deletions

File tree

js/red-javascript-style-guide/readme.md

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,48 @@ d.toLocaleDateString();
202202
// and other toString variants
203203
```
204204

205+
### sets
206+
207+
Yes
208+
209+
```js
210+
new Set();
211+
```
212+
213+
#### When to use sets over arrays
214+
215+
* No duplicates are wanted
216+
* The order is irrelevant
217+
218+
219+
### maps
220+
221+
Yes
222+
223+
```js
224+
new Map();
225+
```
226+
227+
#### When to use maps over objects
228+
229+
In the following situations:
230+
231+
* Keys are something else than string or symbol
232+
* Continuous adding and removal of key-value pairs
233+
* Keys are unknown or can be anything
234+
235+
### absence of value
236+
237+
Use `undefined`, avoid `null`. `undefined` is the default that is already used by the language, for example: destructuring when missing, default return value, unassigned variable etc.
238+
239+
### booleans
240+
241+
`true` or `false`, use `Boolean` function to force cast to a boolean. Do not use `!!` to cast to a boolean. Leverage truthy values in `if` and `while`.
242+
243+
### promises
244+
245+
Prefer promises over callbacks for one-time futures. Use `async`, `await` , `Promise.all`.
246+
205247
### if else
206248

207249
Always use multi-line brackets.
@@ -243,10 +285,6 @@ import * as z from "./z.js";
243285
const y = 5;
244286
```
245287

246-
### promises
247-
248-
Prefer promises over callbacks for one-time futures. Use `async`, `await` , `Promise.all`.
249-
250288
### Limit variable reach
251289

252290
Historically it has been done with an immediately invoked function expression, now that let and const are available and block scoped use a simple block. With an iife:
@@ -276,14 +314,6 @@ let nextSquare;
276314
}
277315
```
278316

279-
### absence of value
280-
281-
Use `undefined`, avoid `null`. `undefined` is the default that is already used by the language, for example: destructuring when missing, default return value, unassigned variable etc.
282-
283-
### booleans
284-
285-
`true` or `false`, use `Boolean` function to force cast to a boolean. Do not use `!!` to cast to a boolean. Leverage truthy values in `if` and `while`.
286-
287317
### global built-ins
288318

289319
Use direcly.

0 commit comments

Comments
 (0)