Here is a interesting fact, do you know which is the worlds most hated and most loved programming language .......? π
(anyways) You are right it is β€ JAVASCRIPT β€
Someone who is working in javascript from a certain period of time can easily describe the advantages of using javascript over some another languages like PHP and Python.
for ex.
π°π°π°
- Async nature of javascript
- Interpreted instead of Compiled
- Supports Promises and Closures and many more.
But, every thing comes with it's own cost. If someone tried coding in javascript without understanding the execution context and behavior of javascript, then (I think) it's the end game.
For avoiding this kinds of situation, here is the list of best practices, which anyone can follow to avoid the future mistakes.
π one thing, I want to mention is, this may not be worlds best article for best practices, but I will definitely make it as good as possible.
π³ Understand Scoping of Variables :
Understanding the scoping of variables is pretty important while coding. Variables declared with let are specifically blocked scope. Whereas, if you used var then you can hoist it anywhere in your code, but using var should be avoided as much as possible (suggested by many !).
But, I escalate this suggestion only when there is nothings seems working or scenarios where, I have to manipulate the global variables based of the events like
- If exception occurs print log and terminate programme using
process.exit()
etc...
π’ Getting Known To This :
I often find the behavior of the this is pretty confusing in javascript for me. Coming from the background of java and
PHP, I assumed that I will definitely feel at home if I used it but, then I was pretty confused when getting started with the React or ES6. After several weird things, I left it and then approached the programming in javascript using functional
approach.
It is still pretty confusing for me to know how this works, but if you wanna use it then know it perfectly.
πCreate Pure Functions :
This is the principal everyone should follow in their programming paradigm. Creating pure functions and causing less side effects on the external world, makes the function more reusable and robust. If function is no longer making any side effects on the global context or the data present outside the body of the function, then it can be easily swapped out, reused and manipulated with ease
π· Never Trust API Response Blindly:
Handling the API responses blindly can easily lead us to the end game or late night forceful fixes. Always handle the API response with null coalescing operators or default initialization of the variables.
for ex .
Know more about Null Coalescing Operators Here
πͺ Use IIEF Whenever Possible :
IIEF stands for " Immediately Invoking Function Expression". It's comes pretty handy if you want to make your isolated from the global context. Take a look at here
IIEF keeps the variable inside the context and avoids the hoisting.
π Use Promises Instead of Callback Only Approach :
Using Promises is pretty standard practice now. But, anyone using callback-only approach must take a look at here.
Introducing promises cured the some major deficiencies in the javascript. for ex
- Inversion of Control trust loss
etc.
Handle promises gracefully and enjoy the javascripting
π΅ Final Thoughts π΅
Using javascript with modern practices could be the experience which any programmer never wanna leave, but it can be nightmare for someone who tries to get rid of the basic and directly jump into the advanced stuff.
Thanks for reading π
Top comments (6)
JS is not async by nature. Without using service workers, it is single-threaded.
JS is also not interpreted (usually). It is JIT compiled in all modern browsers.
I'd also argue that understanding JS grammar is important to writing good code. Not understanding expressions, for instance, leads to slapping semicolons everywhere until they hold no meaning aside from, "It runs."
But, all this said, the other suggestions are fairly good. I think using IIFE more than once is getting a bit too clever, but it does help prevent scope pollution from
var
andfunction
unnusual circumstances.These days JITted languages are also considered "interpreted". Strictly speaking, almost no language is interpreted anymore these days, so playing semantics doesn't really help anyone there.
Liked your reply !
That's more of a downside, really. Javascripts Async-Model has been the source of much headache and only recently with
async
/await
it seems like it's reached a level where most programmers are comfortable with it.This applies to both PHP and Python too, as well as a whole lot of other scripting languages. And it's not even an advantage in the first place; it's a trade-off. Interpreted javascript will never be as fast as well-written C code, and even JIT-Compilers tend to only beat idiomatic C code in very special cases.
Promises are not really a language-feature; they could easily be implemented as a library on top of any language that supports some of objects and uses callbacks for asynchronous code.
As for Closures, javascript sadly stores values separately for each closure, so a whole bunch of techniques that use closures with shared state doesn't work.
This is the one category where no other language can beat javascript in how ridiculous it is.
function
declarations get hoistet, butclass
declarations don't.var
is function-scoped, whilelet
is block-scoped.<script>
scripts share a scope across an HTML document, but<script type="module">
scripts have their own scope.Once you understand all the implications of these differences, they become mostly noise, but for someone learning the language, that's unnecessarily complicated.
Yikes! Don't do that! Blocks
{}
exist for a good reason, and thanks tolet
, you probably won't have to wrap things into functions, specially if you're using strict mode.Better yet: use
async
/await
. Unlike callbacks, promises are way over-engineered and way too complicated. They can do a lot for you, but they can also do a lot to ruin your day. Even if you think you've understand how they work in detail, the next programmer maintaining your code might not.Oh no! There is a bug in
console.log
! It randomly removes"c"
from a string!"Darius Kinacde"
became"Darius Kinade"
!!!It's toooooooooooooo savage ππ
but, your πobservation skills are on fire π₯π₯π₯π₯