Because nothing is fool-proof to a talented fool.
Thereās always one isnāt there? That junior dev who writes things like if (x === undefined)
, or that one package that pollutes the global scope with loads of garbage you donāt need. Or maybe itās a bit of self-sabotage ā you spend ages debugging some obscure issue and cursing whoever caused it, only to find that actually it was you who screwed up š
Despite the incredible progress the JavaScript ecosystem has made over the last decade, thereās still a million ways some careless code can ruin your day.
If youāre a front-end developer youāll know exactly what Iām talking about, especially if you ever have to work outside of a framework. Thereās only so much a coding standards guide can do ā it canāt cover for carelessness or a lack of understanding of how a language works.
Or can itā¦
The following programming pattern shields your code from almost every sloppy mistake made elsewhere in your app, including imported libraries, and it does it in just two lines ā short enough for anyone to remember:
;(function(window, undefined) {
// Do stuff here
})(this);
Doesnāt look like much, does it? Its simplicity is deceptive though, thereās actually quite a lot to unpack. Letās dive in.
You may not know about these tips 7 Simple JavaScript Tips for Optimizing Your Code
How to start writing less error-prone code in JavaScript - Everything in JavaScript is an object!ā. We said that this assertion is false. Many things in JavaScript can behave like an object, but that doesnāt mean itās the object. We can say we have three types of objects (objects, functions and arrays) in JavaScript.
This week, I had to construct a date selector form in React that looks like this. As you can see, the user interface includes the surrounding month and date options in addition to the selected date. This means that I need to not only keep track of the current date, but I also need a way to conveniently figure out how many days are in the selected month.
To paraphrase the title of an old computer science textbook, āAlgorithms + Data = Programs.ā The first step in learning a programming language such as JavaScript is to learn what types of data the language can work with. The second step is to learn how to store that data in variables. In this article Iāll discuss the different types of data you can work with in a JavaScript program and how to create and use variables to store and manipulate that data.
Professor JavaScript is a JavaScript online learning courses YouTube Channel. Students can learn how to develop codes with JavaScript from basic to advanced levels through the online courses in this YouTube channel.