Tip of the day - JavaScript

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

13

Value to Boolean Conversion

2021-02-10 - JavaScript

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
#JStipoftheday
12

Exponential Operator

2021-02-09 - JavaScript

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
#JStipoftheday
11

String occurrences quick replacement

2021-02-08 - JavaScript

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); // 🦇 🤝 🕷️ 🤝  🐜
#JStipoftheday
10

Prettify JSON Stringify

2021-02-05 - JavaScript

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*/
#JStipoftheday
9

Create pure Object

2021-02-04 - JavaScript

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