10 Useful Javascript Tips That Make Your Code Better

More or less everyone writes code but not everyone knows how to code looks better.
Every programmer dreams of being able to write better code with less effort and less time. This is only possible when using some tips that make your code simple. Here I will discuss some tips that you may like.

1.Self Invoked Function

In javascript, self invoked function is also known as self create function.
Function expressions will automatically take effect if followed by the expression (). You cannot self-request the declaration of an activity.

This is a function that executes automatically after you create it. Have a look below-

2.Create an object constructor

A constructor is a function that creates an example of a class commonly known as an “object”. A constructor is useful when you want to create multiple similar objects with the same properties and methods. The code creates objects instead of them. Check an example below-

3.Use Semicolons for line termination

It is a good practice to use semicolons to finish the line. You won’t be warned if you forget it, because in most cases it will be inserted by a JavaScript parser.


var burger
var cheese

Will be this

var burger;
var cheese;

4.Using Loops

In the loop, the statement needs to be written only once and the loop will be executed multiple times as shown in the image below. A loop is a sequence of instructions that is repeated until a certain condition is reached.

5.Usages of the Spread Operator

We can use the spread operator in iterations like strings or arrays and it puts the contents of the iterators into separate elements.

let greet = ['Hello', 'World']; console.log(greet); // Without spread operator console.log(...greet); // Using spread operator

If we will run this code we will see this

 ['Hello', 'World'] Hello World 

You must have noticed that in the second case (including the spread operator), the content of the greeting list was expanded and dropped out of the array.

6.Usage of Comma Operator

The comma evaluates each operation of the operator (,) and returns the value of the last operand. Comma also divided key-value pairs.

7.Converting a string to an array

JavaScript strings can be split () and converted to a character array using arrays. () From the function.

var myArray = [12 , 222 , 1000 ];
myArray.length = 0; // myArray will be equal to [].

8.Avoid negative indexes in arrays

Languages ​​that use multiple arrays as real multi-arrays — arrays of arrays — cannot have a negative index. Make sure that the arguments passed in the spliceare not negative.

9.Empty an array

An empty array means it has a zero array of length. It has no elements. When you create empty zero arrays without specifying values ​​for their elements, they default to values ​​like zero – 0 for integer arrays.

var myArray = [12 , 222 , 1000 ];
myArray.length = 0; // myArray will be equal to [].

10.Use logical AND/ OR for conditions

Logical operators:

&& means AND
|| means OR

Logical OR functions can be used to set a default value for arguments. These are typically used with Boolean (logical) values.


I hope you have learned something from this article and find some useful. Have a great day.

Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *