Some important note in Javascript

Md Moniruzzaman Sojol
4 min readMay 5, 2021

We should always use the modern JS syntax for clear code:

1.Comment in Javascript:

// this is a comment >right way//this is a comment > wrong way, because you should put a space after the slashes.

2.variables naming in Javascript: For variable names use lowerCamelCasing. which humans can read and also have a logic before the name of the variable. which will help to understand why it was named;

Right Way :

var myName;
var shoppingCart;

Wrong Way :

var myname;
var shoppingcart;
var 54Cart;

3.Declare a variable let or const not var:

when the variable is changable you can declare let;
like :
let age=23;
age=34;
console.log(age)// should be return 34;
const name='Tamim Iqbal Khan';console.log(name);// should be return Tamim Iqbal Khan

4.A function’s naming :

For function’s name use lowerCamelCasing. which humans can read and also have a logic before the name of the variable. which will help to understand why it was named as like variable ;

Like this :

function sayHello(){console.log("Hello")}

Not this :


function SayHello(){
console.log("Hello")}function Sayhello(){console.log("Hello")}function sayhello(){console.log("Hello")}

5.Try-Catch in Javascript :

If you start programming then you must hear the unavoidable name ERROR/Exceptions. And You should play with them and solve them. But how? That is Try-catch block.

Now, what is the Try-catch block in javascript/programming?

The try, catch blocks are used to handle exceptions (a type of an error). Before you learn about them, you need to know about the types of errors in programming.In programming there are two types of error thay are compile time error and run time errors.These errors that occur during runtime are called exceptions. Now, let’s see how you can handle these exceptions.

try{
//your main code is here;
}
catch(error){
//error body
}
it's kind of if-else statement i think;(don't take it seriously)
Note:
if the try block are successfully run the your code is safe. when any error occured on your main code then your catch block warn you.
The catch block handles the errors as per the catch statements

Can’t get anything? Don’t worry bro. Now we will see an example below. I think now try-catch is clear to you. Don’t it?

example code

if you run the above code you should get the result like below image

example code

Want to know more about try-catch? > go here.

6.Function with default parameters:

default function parameters allow initializing named parameters with default values if no values or undefined are passed into a function.

example:

here greet function have a parameter called “Hi”
so this function return “Hi”
but what if the greet function hasn’t any parameter
guess what happened?
if the default value of the message is not set the function throws an error.
So, to solve this error we can set a default value of the message that is “ Hello”.
So now if we have no parameter in greet function the function return “Hello”
we can pass any value as a default parameter like the number, string, boolean, or another function;

This is how works default parameter in a function;

7.Arrow functions in Javascript: in the javascript ES6 version we get many useful features like the arrow function. As a productive developers, we should write code clean and human-readable code. So, we use the arrow function in javascript now. It is a shorter version of the previously written function. it’s called the arrow function. it looks like the following image :

how arrow function looks like

Now we started to learn arrow function with some example:

One line of code and get magic :

Arrow function

Another example :

Now we will see how to write a multiline arrow function:

Example of multiline arrow function

Another Example of the multiline arrow function:

Arrow function example

--

--