โ†ย Torna al blog

Dev Tips

Un trucchetto al giorno leva le complicazioni di torno! Piccole "scorciatoie" che possono facilitare la vita degli sviluppatori e far risparmiare tempo...e righe di codice.

13

Value to Boolean Conversion

2021-02-10

1// JS Tip of the Day
2// Value to Boolean Conversion
3
4// with Not Not Operator
5console.log(!!false); // false
6console.log(!!true); // true
7console.log(!!0); // false
8console.log(!!parseInt("Batman")); // false because NaN is falsy
9console.log(!!1); // true
10console.log(!!-1); // true -1 is truthy
11console.log(!!(1/0)); // true because Infinity is truthy
12console.log(!!(Infinity)); // true because Infinity is truthy
13console.log(!!"");  // false because empty string is falsy
14console.log(!!"Batman"); // true because non-empty string is truthy
15console.log(!!undefined); // false because undefined is falsy
16console.log(!!null); // false because null is falsy
17console.log(!!{}); // true because an (empty) object is truthy
18console.log(!![]); // true because an (empty) array is truthy;
19
20// or use a built-in Boolean function
21console.log(Boolean("Batman")); // true because non-empty string is truthy
22// ...
23
24
25
26
27
12

Exponential Operator

2021-02-09

1// JS Tip of the Day
2// Exponential Operator
3
4// Old School
5Math.pow(x, y);
6
7// ...or use ES2016/ES7 exponential operator
8x ** y
11

String occurrences quick replacement

2021-02-08

1// JS Tip of the Day
2// String occurrences quick replacement
3
4const heroes = '๐Ÿฆ‡ ๐Ÿคœ ๐Ÿ•ท๏ธ ๐Ÿคœ ๐Ÿœ';
5
6// with RegEx
7const heroesHandshake = heroes.replace(/\๐Ÿคœ/g, '๐Ÿค');
8console.log(heroesHandshake); // ๐Ÿฆ‡ ๐Ÿค ๐Ÿ•ท๏ธ ๐Ÿค  ๐Ÿœ
9
10// without RegEx - ES2021/ES12
11const heroesHandshakeReplaceAll = heroes.replaceAll('๐Ÿคœ', '๐Ÿค');
12console.log(heroesHandshakeReplaceAll); // ๐Ÿฆ‡ ๐Ÿค ๐Ÿ•ท๏ธ ๐Ÿค  ๐Ÿœ
10

Prettify JSON Stringify

2021-02-05

1// JS Tip of the Day
2// Prettify JSON Stringify
3
4const hero = { name: "Batman", weapon: "Batarang", vehicle: "Batmobile" };
5
6// Prettify with a String 
7console.log(JSON.stringify(hero, null, '--> '));
8/*
9{
10--> "name": "Batman",
11--> "weapon": "Batarang",
12--> "vehicle": "Batmobile"
13}
14*/
15
16// Prettify with a Tabulation Char 
17console.log(JSON.stringify(hero, null, '\t'));
18/*
19{
20        "name": "Batman",
21        "weapon": "Batarang",
22        "vehicle": "Batmobile"
23}
24*/
25
26// Prettify with a Number of Spaces
27console.log(JSON.stringify(hero, null, 2));
28/*
29{
30  "name": "Batman",
31  "weapon": "Batarang",
32  "vehicle": "Batmobile"
33}
34*/
9

Create pure Object

2021-02-04

1// JS Tip of the Day
2// Create pure Object
3
4// Object with constructor, prototype, ...
5const hero = { name: "Batman", weapon: "Batarang", vehicle: "Batmobile" };
6console.log(hero); // { name: 'Batman', weapon: 'Batarang', vehicle: 'Batmobile' }
7console.log(hero.constructor); // [Function: Object]
8console.log(hero.toString); // [Function: toString]
9console.log(hero.hasOwnProperty); // [Function: hasOwnProperty]
10
11// Object without constructor, prototype, ...
12const pureHero = Object.create(null);
13Object.assign(pureHero, { name: "Batman", weapon: "Batarang", vehicle: "Batmobile" });
14
15console.log(pureHero); // { name: 'Batman', weapon: 'Batarang', vehicle: 'Batmobile' }
16console.log(pureHero.__proto__); // undefined
17console.log(pureHero.constructor); // undefined
18console.log(pureHero.toString); // undefined
19console.log(pureHero.hasOwnProperty); // undefined

Contattaci.

Hai in mente un progetto e vorresti realizzarlo?
Sei interessato a migliorare le competenze del tuo team in ambito di programmazione e sviluppo?
Oppure vuoi semplicemente prendere prendere un caffรจ con noi e vedere la nostra collezione di Action Figure, allora scrivici tramite questo form.

Se, invece, vuoi far parte del team, guarda le nostre offerte di lavoro.

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