10 Tips & Tricks For New JavaScript Developers

Wanna develop your JavaScript coding skills and save time during coding? Check out these 10 Tips & Tricks which will simplify your code and make you a smart developer.

Sadman Sakib Mugdho
Webtips
5 min readAug 17, 2020

--

10 Tips & Tricks For New JavaScript Developers
Photo by Shahadat Rahman on Unsplash

Let’s get started.

1) Use let or const instead of var

You should use let or const when you define a variable. You might be asking, “Why should I do that?” Let’s see some examples of differences between let, const, and var then you will get your answer.

Here you can see that by declaring a variable using the var keyword, you can call it outside of the block where you have declared that variable.

But what if you use let keyword instead of var? Let’s see…

See? You can’t call a variable that has been declared using let outside of the declaring block. This will help your project will become larger and there will be hundreds of variables to work with.

Note: You can’t change the value of a variable that has been declared using the const keyword. const is the simplified version of the word Constant.

2) Spread Operator (Three dots Magic)

Are you thinking of making a single array by joining 2 arrays? And trying to do this by iterating through each element of an array and pushing them to a single array? Oh, Man! That’s an antique style. Let’s see some magic of spread operator…

See? Here three dots before the array2 is the spread operator. It spreads all the elements of an array and helps you to use these elements.

3) Destructuring Object

Are you thinking of destructuring an object? And trying to do this in this antique style?

What about this style?

The two curly brackets around name and heroOf, make the destructuring so easy and you can just declare their parent object. They will automatically search for the name and heroOf element in that object

4) Destructuring Array

Are you thinking of destructuring an array? Try this new style. It will save you time and you can do this in a few lines of code.

Those third brackets around firstFriend nextFriend and …restFriends are destructuring the array. “…” before the restFriends are spreading the elements from the index[2] to index[4].

5) Arrow Function

Are you bored of writing functions for one line of operation? You should try arrow functions.

It might look like declaring a variable. But this is the magic of arrow functions.Use const functionName = parameters = doOperations; for declaring a arrow function.

6) Default Parameter

Are you worried if a function will get all the necessary arguments when someone calls the function? Default Parameter will help you in these situations.

By putting a “=” after the parameter, you can write a default value for that parameter. If you give values when you call the function, the function will use your given values instead of these default values.

7) Template String

Are you angry while doing string concatenation? Putting all the “+”, “\n” and space stuff before every part of the string? You should use back-ticks instead of double or single quotes. Back-ticks are located before the “1” key in your keyboard.

You can write any valid JavaScript code inside of ${}. It will make all the concatenation stuff easier.

8) Map

Tired of writing for loops for getting the elements from an array and do operations with them? You should use the map method. Here is an example.

map method automatically goes through each element of an array and takes that element as an argument. Then it does operations and returns a new list of results.

9)Filter

Bored with writing “ if-else ” conditions for simple operations? Filter is there for you. It will check your given condition and return a new array of results.

In this case, it is checking all the elements if the element is bigger than 5. If the element is bigger than 5, it puts the element into a new array called results.

10) Find

Are you looking for something? There is a method in JavaScript called “find”. This method will help you to find if any element of an array fulfills your given condition, it will give you that. Otherwise, it will through an undefined.

In this case, the find method is searching for an element that is bigger than 5. When it gets “6”, it returns it and stops the operation. Remember, find will give you only the first element which fulfills your condition.

Was this helpful for you? Let me know in the comments below. And if you have extra tips and tricks in your mind, then why are you waiting for? Let me know your tips and tricks so that I can add to the list!!!

--

--