Skip to content

Commit 17e26e8

Browse files
author
GrosSacASac
committed
js tricks
1 parent 46a246e commit 17e26e8

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

js/tricks.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# JS tricks
2+
3+
`Object.assign` is useful on arrays.
4+
5+
```
6+
const initialScores = [0, 0, 0];
7+
// player a, player b, total games
8+
const scores = [5, 2, 7];
9+
10+
// reset scores
11+
Object.assign(scores, initialScores);
12+
```
13+
14+
`{}` can be used to create local scopes anywhere
15+
16+
```
17+
const minMax = function (numbers) {
18+
const results = [];
19+
// variables localResult does not clash,
20+
// independent blocks are used
21+
{
22+
const localResult = Math.min(...numbers);
23+
results.push(localResult);
24+
}
25+
26+
{
27+
const localResult = Math.max(...numbers);
28+
results.push(localResult);
29+
}
30+
return results
31+
};
32+
33+
```
34+
35+

0 commit comments

Comments
 (0)