Javascript. Small recipes to improve (II)
Compilation of tips and recipes to increase your skills by developing with Javascript
Continuing with the compilation of the Javascript recipes that I publish periodically on Linkedin on Wednesdays, here I leave the ones that I have uploaded these last two months. So, if you missed any or just want to keep refreshing your knowledge you can have them at hand.
Remember that the first part of this article can be found here:
To me the truth is that writing them serves me so much to reinforce some parts of one of my favorite languages and to be aware of the main novelties that appear.
So, as always, let’s do it!
Cloning objects
A recipe where I summarize the 3 main ways we have to clone an object in Javascript.
Be careful with the use of the “spread operator”, because as you see it produces what is known as a “shallow clone”, that is, it copies the objects of the object to be cloned by reference!

ES2020
As time goes! We are already in 2020 and with it, a new ECMA specification for Javascript with some very interesting news that little by little also begin to reach browsers.
In this recipe I summarize the main features of ECMAScript 2020 so you don’t miss any:

Nullish coalescing operator
As you have seen in the summary of the main novelties of ES2020, one of them is the appearance of the “nullish coalescing operator”, which allows us to quickly check if a variable is null, and, in that case, set a value by default.
In the recipe you have some examples with the value obtained when using it:

The Proxy object in Javascript
Surely a great unknown. The Javascript Proxy object allows us to define custom behaviors for elementary operations of objects or functions.
In the example of the recipe you can see how it can be used to control the data that is entered in an array. Useful, right?

Basically, by controlling the get function we can create a new push function that checks the type of the value to be added so that, by doing
proxyForFoo.push (4)
we obtain that function and the verification is carried out.
Pure functions
Now that functional programming is living a second youth, it is worth reviewing what a pure function is and what elements cause it to lose that adjective.
Remember that the benefit of working whenever possible with pure functions is that we will always have the security of obtaining the same result in each invocation without external factors that alter it or without causing chain effects within our application.

Spread Operator
One of the most used features of ES6 and the one that has contributed the most (in my opinion) to make the code more readable.
Its uses are multiple, from cloning objects to adding new elements to objects and arrays. Very useful, both he and his twin brother the “rest” operator.

This
The variable this in Javascript is probably one of the biggest headaches we can face working with this language, especially if we are still learning.
That is why I prepared this summary of the values that it adopts within an object depending on how we invoke the methods. I know that there are not all the cases but I think it is a good approximation to be taking fluency when identifying what value this famous variable will have.

Variable hoisting
The typical question of an interview that never hurts to know and that allows us to use variables in Javascript that appear later in the code. Don’t get caught in the next interview!

Final thoughts
As you can see, Javascript has many syntactic tricks to make the experience of developing in this language much more pleasant. That’s why I always answer who asks me about which library or framework to learn:
Do you know Javascript well?
I think that knowing the language is essential before going to work on a layer above, hence I prepare these mini recipes every Wednesday to reinforce my knowledge.
Remember that if you also want to deepen you have the fantastic free online book “You don’t know Javascript:”
Do you want to read more articles like this?
If you liked this article I encourage you to subscribe to the newsletter that I send every Sunday with similar publications to this and more recommended content: 👇👇👇