DEV Community

Cover image for JavaScript Tips and Tricks
Adnan Latif
Adnan Latif

Posted on • Originally published at Medium

1

JavaScript Tips and Tricks

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript UI Library for Surveys and Forms

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

View demo

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay