JavaScript Tips and Tricks
As you know, JavaScript is the number one programming language in the world, the language of the web, of mobile hybrid apps (like React Native), of the server side (like NodeJS ) and has many other implementations. It’s also the starting point for many new developers to the world of programming, as it can be used to display a simple alert in the web browser but also to control a robot (using nodebot, or nodruino). The developers who master JavaScript and write organized and performant code have become the most sought after in the job market.
In this article, I’ll share a set of JavaScript tips, tricks and best practices that should be known by all JavaScript developers regardless of their browser/engine or the SSJS (Server Side JavaScript) interpreter.
*use *=== *instead of *==
The == (or !=) operator performs an automatic type conversion if needed. The === (or !==) operator will not perform any conversion. It compares the value and the type, which could be considered faster than ==.
undefined, null, 0, false, NaN, ‘’(empty string) are all falsy.
Find max and min from an array
The traditional way to find min and max is loop through each element of array and find min and max. Below code snippet is showing to find min and max from array using spread operator.
Merging two arrays
Repeating string multiple times
String to Number conversion
Multiple variable assignments
Multiple condition check
Remove duplicates from an array
Sum the values of an array
Convert a string to an array
Remove falsey values from an array
Alternatives of for loop
OR Short circuit
AND Short circuit
Exponent power
Count function parameters
Assign default function parameter
Check if key exists in the object
Object property assignment
Assigning object value property to a variable
Removing multiple properties from an object
Here are my two bits of contribution in JavaScript community. I hope these code snippets will help someone to learn some tips and tricks in JavaScript.
Top comments (0)