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

21

Checking if a key exists in an object

2021-02-22

1// JS Tip of the Day
2// Checking if a key exists in an object
3
4const hero = { name: 'Batman', weapon: 'Batarang' };
5
6// hasOwnProperty
7console.log(hero.hasOwnProperty("vehicle")); // false
8// in operator
9console.log('name' in hero); // true
10// Get elements (brackets style)
11console.log(hero["vehicle"]); // undefined
12// Get elements (object style)
13console.log(hero.vehicle); // undefined
14
20

Convert a String to a Number

2021-02-19

1// JS Tip of the Day
2// Convert a String to a Number
3
4let intStr = '42';
5let floatStr = '42.24';
6let nanStr = 'Hitchhiker';
7
8// with Number() constructor
9console.log(new Number(intStr)); // Number {42}
10console.log(new Number(floatStr)); // Number {42.24}
11console.log(new Number(nanStr)); // Number {NaN}
12
13// with parseInt function
14console.log(parseInt(intStr)); // 42
15console.log(parseInt(floatStr)); // 42
16console.log(parseInt(nanStr)); // NaN
17
18// with parseFloat function
19console.log(parseFloat(intStr)); // 42
20console.log(parseFloat(floatStr)); // 42.24
21console.log(parseFloat(nanStr)); // NaN
22
23// with Math.floor or ceil methods
24console.log(Math.floor(intStr)); // 42
25console.log(Math.floor(floatStr)); // 42
26console.log(Math.floor(nanStr)); // NaN
27
28// with Multiply by 1 
29console.log(intStr * 1); // 42
30console.log(floatStr * 1); // 42.24
31console.log(nanStr * 1); // NaN
32console.log('42' * 1); // 42
33console.log('42.21' * 1); // 42.24
34
35// with Unary operator
36console.log(+intStr); // 42
37console.log(+floatStr); // 42.24
38console.log(+nanStr); // NaN
39console.log(+'42'); // 42
40console.log(+'42.24'); // 42.24
19

Custom Events with detail information object

2021-02-18

1// JS Tip of the Day
2// Custom Events with detail information object
3
4// Create Custom Event with detail object
5const customEvent = new CustomEvent('batsignal', { detail: { hero: 'Batman!' } });
6
7// Add a valid event listener
8document.addEventListener('batsignal', (event) => console.log(event.detail.hero));
9
10// Dispatch the Custom Event
11document.dispatchEvent(customEvent);
12
18

Temporal Dead Zone

2021-02-17

1// JS Tip of the Day
2// Temporal Dead Zone
3
4let hero = 'Superman';
5
6// ...
7
8if (hero) { // Enter in a new scope
9  // 💀 We are inside the Temporal Dead Zone for hero 💀 
10  
11  /*
12	let declaration is block scoped and it isn't afflicted by Hoisting but live under the TDZ!
13       We cannot use it before its block declaration or we trigger a ReferenceError
14  */
15  console.log(hero); // ReferenceError 
16  
17  // 💀 We are inside the Temporal Dead Zone for hero 💀 
18
19  let hero; // TDZ is ending and hero è undefined
20  /*
21	hero is also declared inside this scope with let and it create a block scope!
22  */
23  
24  hero = 'Batman';
25  console.log('Batman'); // Batman
26}
27
28console.log(hero); // Superman;
17

Quick ways to empty an Array

2021-02-16

1// JS Tip of the Day
2// Quick ways to empty an Array
3
4let heroes = ['Batman', 'Superman', 'Wonder Woman'];
5
6// Assigning a new Array - This catch an error if you declare with const
7heroes = [];
8
9// Setting lentgh to zero
10heroes.length = 0;
11

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.