JavaScript Life-Saviour Liners
Hi everyone, in this article, we will witness some of the one-liners of Javascript that can literally save your time while coding as an IT professional or as a student. So without any further delay, let’s get started, and always remember…
Laziness is a programmers main virtue
Reverse
One can easily do the reverse of a string in javascript with the help of split(), reverse(), and join() methods.
Average
Finding an average is not a tedious task, but as programmers are always a step ahead in laziness, let's look at a better way to find an average using a reduce() method…
Unique Elements
Whenever there is a list of people or anything, and we want to extract the unique elements from that list/array, we can use SET to get the same…
Conditionally Add Property
Whenever we work with objects we might get into a situation where we have to add a property to an object based on a certain condition/s. We can deal with this situation in the following way…
Nullish coalescing (??)
In the case where we want to check NULL or UNDEFINED, and if either of these is getting returned, then by default we want to return something else, then we can use nullish coalescing. Here is an example of how to use…
Comparing Arrays
If we need to compare 2 arrays by each and every element, we can use every() method from javascript. Let us see how…
Comparing Objects
Comparing two objects are never that easy, but if we have objects that are not nested, we can use every() method to compare them (even more than two at once).
Deep Copy
A deep copy of an object is a copy whose properties do not share the same references as those of the source object from which the copy was made. So, if we make changes to the source object, those will not get reflected in the newly copied object.
Is Object an Array
If we want a given object is a type of an Array, we can implement the following code…
Only Index Needed
Whenever we loop through an array using map or forEach, and our task is dependent on its index only, we can skip naming the value/element by using underscore _ in place of its name…
Inverting Keys and Values
Objects contain key and value pairs. These can easily be interchanged using the fromEntries() and entries() methods.
Conclusion
There are a lot of one-liners available in the javascript, but these are the ones that I personally use on a regular basis if not daily. Javascript is one of the most used programming languages in the world, and I hope, these one/two-liners will surely help you one or another day. Also, I will try to update this blog if something new comes up and it qualifies to be in this blog.
Thanks for Reading!!!