Tip of the day - JavaScript

Un trucchetto al giorno per mantenerti allenato o per conoscere qualcosa di nuovo sul magico mondo di JavaScript.

8

Rounding Numbers

2021-02-03 - JavaScript

1// JS Tip of the Day
2// Rounding Numbers
3
4const myNumber = 3.14159;
5
6// Rounding Nearest
7console.log('Math.round', Math.round(myNumber)); // 3
8
9// Rounding Up
10console.log('Math.ceil', Math.ceil(myNumber)); // 4
11
12// Rounding Down
13console.log('Math.floor', +Math.floor(myNumber)); // 3
14
15// Rounding to a specified number of decimals - 2nd digit example
16// multiply and divide by 100 or a bigger power of 10 for more digits
17console.log('Math.ceil', Math.ceil(myNumber)); // 3.14
18
19// Rounding to a specified number of decimals - 2nd digit example
20// toFixed return a string and we have to convert it to a number
21console.log('Number.toFixed', +myNumber.toFixed(2)); // 3.14
22
23// Removing anything after the decimal point - ES6/ES2015
24console.log('Math.trunc', Math.trunc(myNumber)); // 3
#JStipoftheday
7

Object to Array and vice versa

2021-02-02 - JavaScript

1// JS Tip of the Day
2// Object to Array and vice versa
3
4const hero = {
5  name: 'Batman',
6  weapon: 'Batarang',
7  vehicle: 'Batmobile'
8};
9
10// From Object to Array
11const heroEntries = Object.entries(hero); // ES2017/ES8
12console.log(heroEntries);
13// [["name", "Batman"], ["weapon", "Batarang"], ["vehicle", "Batmobile"]]
14
15// From Array to Object
16const anotherHero = Object.fromEntries(heroEntries); // ES2019/ES10
17console.log(anotherHero);
18// { name: 'Batman',  weapon: 'Batarang',  vehicle: 'Batmobile' }
19
20
21
#JStipoftheday
6

Simple If with multiple conditions.

2021-02-01 - JavaScript

1// JS Tip of the Day
2// Simple If with multiple conditions
3
4// Long version
5if (hero === 'Batman' || hero === 'Superman' || hero === 'GreenArrow') {
6  // ...
7}
8
9// Short version
10if (['Batman', 'Superman', 'GreenArrow'].includes(hero)) {
11  // ...
12}
13
#JStipoftheday
5

How to get a (real) string length

2021-01-29 - JavaScript

1// JS Tip of the Day
2// How to get a (real) string length
3
4const bestHero =  "Batman";
5console.log(bestHero.length); // 6
6
7const word = "𥑮"; // Surrogate pairs
8console.log(word.length); // 2
9console.log([...word].length); // 1
10
11const rocket = "🚀"; // Surrogate pairs
12console.log(rocket.length); // 2
13console.log([...rocket].length); // 1
14
15const bestDirector = "Rene\u0301"; // Combining marks
16console.log(bestDirector); // René
17console.log(bestDirector.length); // 5
18console.log(bestDirector.normalize()); // René
19console.log(bestDirector.normalize().length); // 4
#JStipoftheday
4

Flat an array with the nested arrays

2021-01-28 - JavaScript

1// JS Tip of the Day
2// Flat an array with the nested arrays - ES10/ES2019
3
4const arr = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
5
6// Depth Level is 1
7console.log(arr.flat()); // [1, 2, 3, 4, [5, 6, [7, 8, [9, 10]]]];
8// or
9console.log(arr.flat(1)); // [1, 2, 3, 4, [5, 6, [7, 8, [9, 10]]]];
10
11// Depth Level is 2
12console.log(arr.flat(2)); // [1, 2, 3, 4, 5, 6, [7, 8, [9, 10]]];
13
14// Infinitely Deep
15console.log(arr.flat(Infinity)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
#JStipoftheday

Preferisci ricevere i tips via mail? Iscriviti alla newsletter.

Iscriviti alla newsletter

Tip of the day - JavaScript

Hai in mente un progetto e vorresti realizzarlo?
Sei interessato a migliorare le tue competenze o quelle del tuo team in ambito di programmazione e sviluppo?

Anche se - semplicemente - vuoi prendere un caffè con noi o vedere la nostra collezione di Action Figures scrivici tramite questo form.

Questo sito è protetto da reCAPTCHA e si applicano le Norme sulla privacy e i Termini di servizio di Google.