DEV Community

Mohammad Naimur Rahman
Mohammad Naimur Rahman

Posted on

Some Issues in JS

Hello, Dear Dev Community
Greetings!

Let's start to talk about JS function in a brief.

aa. Functions are mainly two types.

  1. Built-in functions like toString(), indexOf(), map() and so many....

  2. User defined functions

Built-in functions are the functions which do not need to declare/ define but can be used from anywhere of the by calling them only.

User defined functions are the functions which is defined by the user and can be used by calling them.

User defined function details:

  1. The name of the function which should be followed by the keyword "function".
  2. A list of parameters to the function, enclosed in parentheses () and separated by commas.
  3. The JavaScript statements that define the function, enclosed in curly brackets {...}.

For example
function addition(number1, number2) {
return number1 + number2;
}
addition(10, 20);
// output should be 30

bb. Function expression:
function expression is almost like variable declaration where the function name is optional.
const addition = function(number1, number2 ) {
return (number1 + number2)
}
var x = addition(10, 20);
// output should be 30

cc. Now let's see the arrow function

const addition = (number1, number2) => {
return (number1 + number2);
}

addition(10, 20);
// output should be 30

dd. Block Scope

The block is delimited by a pair of braces ("curly brackets")

If a variable is can only be accessible inside the {} is called block scope. Like

const addition = () => {
let a = 10;
let b = 20;
let result = a + b;
console.log(result);
}

addition();
// output should be 30
Here the variable a, b or result can not be accessed out side the unction addition declaration.

ee. Error Handling with Try and Catch
If any error occurs then it is difficult to find where the error happened. To solve this kind of problem JavaScript has brought to us the try and catch method.

Try and Catch method has two main blocks

try {
// some code
}
catch(error) {
// Error handling
}
Built-in errors, the error object has two main properties: error.name which shows the error name;
error.message which shows the error message.

try {
blabla; // error, variable is not defined!
} catch (err) {
alert(err.name); // ReferenceError
alert(err.message); // blabla is not defined
}

ff. Now let's talk about spread operator which is introduced in ES6.

It looks something like this
var newValue = [...value];
All the values of value variable is copied to newValue.

With the help of spread operator the more values or variable can be add.
var value = ["Asmin", "Jara", "Asif"];
var newValue = [...value, "Rahin", "Jubin"]
the output of the newValue is // [""Asmin", "Jara", "Asif", "Rahin", "Jubin"];

Beside this spread operator can be used over object.

Top comments (0)