How to solve the problem of JavaScript

Md Mehedy Hassan
5 min readMay 6, 2021

Problem summary

1. Error handling, “try…catch”

2. Error Object

3. Checking a Type

4. Bad comments

5. Good comments

6. JavaScript Declarations are Hoisted

7. Block level declarations

8. No Redeclaration

9. Types of Values

10. Default parameters

1. Error handling, “try…catch”

Many times we get errors due to out mistake or not giving code or some other reason and this catch error is done to handle that error. If for some reason an error occurs then if we want to see that error then we can see the error by using the following command line. An error can be checked here and it will be caught. We can handle any error with this catch. Again, if we want to show the visors to the errors. Or we can send the message to the user without any error.

Example:

try {
// code...
} catch (err) {
// error handling
}

2. Error Object

It is often the case that users do not give us the information we want to get from them. Many times they do not give willingly. Sometimes they forget to give the information. That’s when we use the of the error object. Then we can alert them. And we can tell them what to do or what to make of mistakes. If they do not provide the information we asked for name or age or occupation, they will be alerted.

Example:

try {   ulala } 
catch (err) {
alert(err.name);
alert(err.message);
alert(err.stack);
}

3. Checking a Type

There are many types of JavaScript. We use many of them. Today we will know how many typeOf JavaScript. Some JavaScript type name are Undefined, Null, Boolean, Number, String, Symbol, Function object. It is very important to know these in order to work in JavaScript. We will learn how to find out these types.

Example:

consloe.log(typeof(undefined));
// output: undefined
consloe.log(typeof(null))
// output: object
consloe.log(typeof(true))
// output: boolean
console.log(typeof(7));
// output: number
cosole.log(typeof("Mehedy");
// output: string

4. Bad comments

We use comments when we have to put an information next to the code but only in the form of a note to remember. We always want some information to be remembered but if we do not write it in the form of comments, then it will show us the error. There will be trouble in our code. For this we use a lot of comments. Now we will see how bed commands are used. This // use to bad comments. The reason we call these bad comments is because the code doesn’t look like clean.

Example:

// friend age 
const friendAge = [21, 26, 18, 25, 30]
console.log(friendAge[2])
// output: 18

5. Good comments

These comments are called good comments to see if the code looks cleaner. And if you select these and comment, it looks very smooth.
These comments are commonly used / ** * / through these. Suddenly if we want to delete a code or if we want to clean something to check then our code is lost. But we can also check if we don’t comment on those codes. There will be no problem.

Example:

/**  
* Returns x raised to the n-th power.
*
* @param {number} x The number to raise.
* @param {number} n The power, must be a natural number.
* @return {number} x raised to the n-th power.
*/
function pow(x, n) {
...
}

6. JavaScript Declarations are Hoisted

In JavaScript, a variable can be declared after it has been used. In other words; a variable can be used before it has been declared. Variables defined with let and const are hoisted to the top of the block but not initialized. The block of code is aware of the variable, but it cannot be used until it has been declared. JavaScript only hoists declarations, not initializations.

Example:

var tomato = 29;
const mango = 31;
let banana = 50;
console.log(tomato + " , " + mango + " and " + banana);
// output: 29 , 31 and 50

7. Block level declarations

The new ES6 specification allows for block-level variable declarations by introducing the new ‘let’ and ‘const’ identifiers. why we use block declaration? Because when a block is declared, it only works in the distribution of a function.

‘let’ declarations

If we declare a variable lat, then the value of that variable can be changed. If you make a variable changed through this, you will not get any error. In this case, the output will show the value that will be the last.

Example:

let mango = 10;
mango = 15;
mango = 5
console.log("Mango one Kg" , mango , "taka.")
// output: Mango one Kg 5 taka.

‘const’ declarations

If we declare a variable const, then the value of that variable can not be changed. If you make a variable changed through this, you will get error. In this case, if you give a chance, the error will show.

Example:

const mango = 10;
mango = 15;
mango = 5
console.log("Mango one Kg" , mango , "taka.")
// output: error mango ^ = 15;

8. No Redeclaration

If a variable is declared at two different variables, it will give an error. For example, if var and let are used and the name of the variable is used the same then error will show. Suppose we first declare a var variable called mango. Then I re-declared that ;et variable called mango and then it will show us the error. Because the variable called mango is already dealt with, he can’t get into the new one.

Example:

var mango = 30;
let mango = 25
console.log(mango)
// output: error let ^ mango = 25;

Again if we put it in the bar first and then put it in a function then we will be able to use Late will not show any error.

Example:

var count = 30;// Does not throw an error
if (condition) {
let count = 40;
// more code
}

9. Types of Values

We know about many values. In fact, it is shown once again which are called which values.

Undefined

If I want to see the output without putting any values, it will show undefined.

Null

This is used if we set the initial value of a value to null. That means there will be nothing but empty.

Boolean

Boolean’s values ​​are of two types such as true and false. We can declare by setting these two types of values.

Number

If we insert a number into a variable, then that variable will be called a number. E.g. 2, 3, 7, 10. If we type like this ‘2’, ‘3’ Then this type variable called by string.

String

If we put a variable ‘’, “” ,``among these, it will be called string. E.g. ‘hello’, “hello”, `hello`;

10. Default parameters

If we set the default value of one of the parameters in advance. So even if the user does not set that value, we can set it as the default value. A lot of times we have to set a value that we can think of by default. This means that if someone does not add a value, then the value we are using as the default will go straight to the value.

Example:

function fruit(mango, banana = 5) {
return mango + banana;
}
console.log(fruit(5, 2));
// output: 10
console.log(fruit(5));
// output: 5

--

--