Tip of the day - JavaScript

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

73

How to detect localStorage updates between browser tabs or window

2021-05-05 - JavaScript

1// JS Tip of the Day
2// How to detect localStorage updates between browser tabs or window
3
4// Browser Tab or Window - http://example.com
5window.addEventListener('storage', (event) => { // or you may use window.onstorage
6  console.log(event.key, event.newValue, event.oldValue, event.url);
7  // Hero, Batman, null, http://example.com/heroes
8});
9
10
11// Another Browser Tab or Window - http://example.com/heroes
12localStorage.setItem('hero', 'Batman');
#JStipoftheday
72

Convert Binary string to text (with a secret and awesome message!)

2021-05-04 - JavaScript

1// JS Tip of the Day
2// Convert Binary string to text (with a secret and awesome message!)
3
4const awesomeBinaryMessage = "01001001 00100111 01101101 00100000 01101110 01101111 01110111 00100000 01101111 01100110 01100110 01101001 01100011 01101001 01100001 01101100 01101100 01111001 00100000 01100001 00100000 01000111 01101111 01101111 01100111 01101100 01100101 00100000 01000100 01100101 01110110 01100101 01101100 01101111 01110000 01100101 01110010 00100000 01000101 01111000 01110000 01100101 01110010 01110100 00100001";
5
6const awesomeMessage = awesomeBinaryMessage.split(' ')
7   .map(bin => {
8     return String.fromCharCode(parseInt(bin, 2)) // Binary char to Text char
9   }) 
10   .join('');
11
12// 🎉🎉🎉🎉🎉 You have to execute this snippet to read the message! 🎉🎉🎉🎉🎉
13console.log(awesomeMessage); 
#JStipoftheday
71

Two types of Interval

2021-05-03 - JavaScript

1// JS Tip of the Day
2// Two types of Interval
3
4// No consideration on the end of the execution
5const interval = setInterval(() => {
6 // ...
7}, 1000);
8
9// Sure that the execution has finished
10const myInterval = () => {
11  // ...
12  setTimeout(myInterval, 1000)
13}
14setTimeout(myInterval, 1000);
#JStipoftheday
70

How to extract a property from an object

2021-04-30 - JavaScript

1// JS Tip of the Day
2// How to extract a property from an object
3
4const hero = {
5  name: 'Batman',
6  weapon: 'Batarang',
7  vehicle: 'Batmobile',
8  partner: 'Robin'
9};
10
11const { vehicle, ...anotherHero } = hero;
12console.log(anotherHero); // { name: 'Batman',  weapon: 'Batarang', partner: 'Robin' }
#JStipoftheday
69

Hiding Characters of a String

2021-04-29 - JavaScript

1// JS Tip of the Day
2// Hiding Characters of a String
3
4function hideChars(str = '', chars = 0, obfuscator = '*') {
5    const maxChars = chars > str.length ? str.length : chars;  
6    const partialStr = str.slice(0, str.length - maxChars);
7    return partialStr.padEnd(str.length, obfuscator);
8}
9
10
11console.log(hideChars('My name is Batman',6)); // My name is ******
12
#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.