10 Magical JavaScript Tips for Every Web Developer

This article will cover 10 JavaScript tips that web developers can use to save time and money.

Although I don’t always enjoy being taught, I am always willing to learn.

Winston Churchill

Flatten the array

This tip will allow you to flatten an array with many nested elements by using Infinity in Flat.

var array = [123, 500, [1, 2, [34, 56, 67, [234, 1245], 900]], 845, [30257]]//flatten array of array
array.flat(Infinity)// output:
// [123, 500, 1, 2, 34, 56, 67, 234, 1245, 900, 845, 30257]

Simple Exchange Variables

The third variable temp will swap the variables. This tip will help you to use destructuring to swap variables.

//example 1var a = 6;
var b = 7;[a,b] = [b,a]console.log(a,b) // 7 6

Sort alphabetically

Programming can be difficult when it comes to sorting strings alphabetically. This tip will help you save time and cut down on your programming efforts.

//sort alphabeticallyfunction alphabetSort(arr)
{
return arr.sort((a, b) => a.localeCompare(b));
}let array = ["d", "c", "b", "a"]
console.log(alphabetSort(array)) // ["a", "b", "c", "d"]

Create a range of numbers

Let’s say you need to generate a number within a certain range. You will first use the loop. This tip will help you save valuable time and make it easy.

let Start = 1000, End = 1500;
//Generating
[...new Array(End + 1).keys()].slice(Start);Array.from({length: End - Start + 1}, (_,i) => Start + i) // [1000, 1001 .... 1500]

Leave the Array empty

This tip will help you save time when emptying an array. This is the JavaScript length method that I’ll show you to empty an array.

let array = ["A", "B", "C", "D", "E", "F"]array.length=0
console.log(array) // []

Use isString

This tip will help you check if data or values are in string format. This is useful when you need to verify the data type of data that has been requested from the server.

const isString = vaue => typeof value === 'string';isString('JavaScript'); // true
isString(345); // false
isString(true); // false

Check Null

Sometimes, programming can require us to verify that a result or piece of data is valid.

const CheckNull= value => value === null || value === undefined;CheckNull(null) // true
CheckNull() // true
CheckNull(123) // false
CheckNull("J") // false

Combine Arrays into One

This tip is useful when you want to combine two arrays of any size. This can be done using the JavaScript concate technique.

//Example Code
let arr1 = ["JavaScript", "Python", "C++"]
let arr2 = ["Dart", "Java", "C#"]const mergeArr = arr1.concat(arr2)
console.log(mergeArr) // ["JavaScript", "Python", "C++", "Dart", "Java", "C#"]

>> Backend Developer Roadmap

Performance Quick Calculation

This tip is most used by me to calculate the performance my JavaScript program.

const starttime = performance.now();
//some program
const endtime = performance.now();const totaltime = startTime - endTime
console.log("function takes "+totaltime +" milisecond");

Eliminate duplicates

It is likely that you will encounter arrays with duplicate data. To get rid of them, use a loop method. This tip will allow you to remove duplicates quickly and without any loop.

const ReDuplicates = array => [...new Set(array)];console.log(ReDuplicates([200,200,300,300,400,500,600,600])) // [200,300,400,600]

Last Thoughts

These tips are useful and enjoyable to me. Share your JavaScript tips with others if you have one.

Share this article with friends if you found it useful.

Also Check:

Happy Coding….☺️

About us: Codelivly is a platform designed to help newbie developer to find the proper guide and connect to training from basics to advance .

2 thoughts on “10 Magical JavaScript Tips for Every Web Developer”

Leave a Comment