Convert Fahrenheit to Celsius with JavaScript

The United States is one of the last bodies that refuses to implement the Celsius temperature standard. Why? Because we’re arrogant and feel like we don’t need to change. With that said, if you code for users outside the US, it’s important to provide localized weather data to users. Let’s took at how you can convert between Fahrenheit and Celsius. Fahrenheit to Celsius The formula to convert Fahrenheit to Celsius is: °C = 5/9 x (°F - 32) The following function converts Fahrenheit to Celsius: function convertFahrenheitToCelsius(degrees) { return Math.floor(5 / 9 * (degrees - 32)); } Celsius to Fahrenheit The formula to convert Celsius to Fahrenheit is: °F = (°C... more →
Posted in: JavaScript

How to Internationalize Numbers with JavaScript

Presenting numbers in a readable format takes many forms, from visual charts to simply adding punctuation. Those punctuation, however, are different based on internationalization. Some countries use , for decimal, while others use .. Worried about having to code for all this madness? Don’t — JavaScript provides a method do the hard work for you! The Number primitive has a toLocaleString method to do the basic formatting for you: const price = 16601.91; // Basic decimal format, no providing locale // Uses locale provided by browser since none defined price.toLocaleString(); // "16,601.91" // Provide a specific locale price.toLocaleString('de-DE'); // "16.601,91" // Formatting... more →
Posted in: JavaScript

How to Extend Prototypes with JavaScript

One of the ideological sticking points of the first JavaScript framework was was extending prototypes vs. wrapping functions. Frameworks like MooTools and Prototype extended prototypes while jQuery and other smaller frameworks did not. Each had their benefits, but ultimately all these years later I still believe that the ability to extend native prototypes is a massive feature of JavaScript. Let’s check out how easy it is to empower every instance of a primitive by extending prototypes! Every JavaScript native, like Number, String, Array, Object, etc. has a prototype. Every method on a prototype is inherited by every instance of that object. For example, we can provide every `Array... more →
Posted in: JavaScript

Skip or Only Run a Test with JavaScript Mocha

Whenever I start to feel anxiety about a big change I’m making, I start writing more unit tests. I’ll write down my fear and then write a test that attacks, and eventually relaxes, that fear. There are two actions that I’ve been frequently using with test writing: skipping all but one test or single tests. Skip a Test Oftentimes I will create tests with empty bodies so that I don’t forget to write them. To skip a test which is incomplete or known to fail, you can use xit: xit('does the thing I want', () ={ }); Once the test is complete or ready to be applied, you can change xit back to it. Run a Single Test To run only a single test with the Mocha test framework,... more →
Posted in: JavaScript

JavaScript Event.defaultPrevented

Whether you started with the old on_____ property or addEventListener, you know that events drive user experiences in modern JavaScript. If you’ve worked with events, you know that preventDefault() and stopPropagation() are frequently used to handle events. One thing you probably didn’t know: there’s a defaultPrevented proptery on events! Consider the following block of code: // Specific to a link const link = document.querySelector('#my-link'); link.addEventListener('click', e =e.preventDefault()); // A larger document scope document.addEventListener('click', documentClickHandler); function documentClickHandler(event) { if (event.defaultPrevented) {// Using the property... more →
Posted in: JavaScript

JavaScript String replaceAll

Replacing a substring of text within a larger string has always been misleading in JavaScript. I wrote Replace All Occurrences of a String in JavaScript years ago and it’s still one of my most read articles. The confusion lies in that replace only replaces the first occurrence of a substring, not all occurrences. For example: 'yayayayayaya'.replace('ya', 'na'); // nayayayayaya To replace all instances of a substring, you’ve needed to use a regular expression: 'yayayayayaya'.replace(/ya/g, 'na'); // nananananana Using regular expressions is certainly powerful but let’s be honest — oftentimes we simply want to replace all instances of a simple substring that shouldn’t... more →
Posted in: JavaScript

Immediately Executing setInterval with JavaScript

Employing setInterval for condition polling has really been useful over the years. Whether polling on the client or server sides, being reactive to specific conditions helps to improve user experience. One task I recently needed to complete required that my setInterval immediately execute and then continue executing. The conventional and best way to immediately call a function at the beginning of a setInterval is to actually call the function before the initial setInterval` is called: myFunction(); setInterval(myFunction, 1000); // Every second If you truly want to isolate the function call to the setInterval, you can use this trick of self-executing function that returns itself: // Use... more →
Posted in: JavaScript
1 2 3 4 5 52