DEV Community

Mozibur Rahman
Mozibur Rahman

Posted on

JavaScript (Part-2)

Function

We use a function for performing operations. Sometimes we have to perform similar operations in many places of code. A function's code can be used several times, so we don't need to write repetitive code. To declare a function, we use the function keyword, and to call a function we use functionName();

//function declaration
function functionName() {
  console.log("Hello!");
}
//function calling
functionName(); //"Hello!"
Enter fullscreen mode Exit fullscreen mode

Function with Parameters

Function basic structure is first function keyword, then the name of the function, then parameters between parentheses. If there is no parameter, it is empty. If there is more than one parameter then we have to put a comma between them. Finally function body between curly braces.

function name(parameter1,parameter2) {
  //body
}
Enter fullscreen mode Exit fullscreen mode

Below there is a add function, that takes two parameters (a and b) and add them.

function name(a,b) {
   let sum = a+b; 
   console.log(sum);
}

add(5,6); // 11
Enter fullscreen mode Exit fullscreen mode

Function with Default Value

If a function takes one or more parameters, but during function calls, if a parameter is not passed, then the value of the parameter becomes undefined. So, to avoid this we can set a default value. If no value is passed for the parameter, it will use the default value.

function square(n){
   console.log(n*n);
}

square(); // undefined
square(2); // 4

Enter fullscreen mode Exit fullscreen mode

But if we set default value.

function square(n = 3){
   console.log(n*n);
}

square(); // 9
square(2); // 4

Enter fullscreen mode Exit fullscreen mode

Arrow Function

An arrow function expression is a compact version of a traditional function. If a function doesn't have a name identifier, it is called an anonymous function. Every arrow function is an anonymous function. Example of Arrow Function.

()=> {
   //function body
   return;
}

//If there is a single statement
()=> console.log("Hi!");

// If there is a single parameter
msg => console.log(msg);

if there are multiple parameters
(a,b) => console.log(a+b);
Enter fullscreen mode Exit fullscreen mode

Spread Operator

Spread operator refers to ellipsis of three dots(...). We used the spread operator when we need to use all elements of an array or object.

let fruit = ['Apple','Banana','Orange'];
let moreFruit = ['Pine-Apple','Plam', ...fruit];

// Object Example
let obj1 = { p : 1, q : 2 };
let obj2 = { r : 3 };
let obj3 = {...obj1, ...obj2};

console.log(obj3); // {p: 1, q: 2, r: 3}
Enter fullscreen mode Exit fullscreen mode

Use in Math Function.

let value = [12,33,42,65,36,78];
Math.max(...value); // 78
Enter fullscreen mode Exit fullscreen mode

Rest Parameters

In javascript, we can call a function with any number of arguments, and there will be no error because of it. But if the number of arguments is fixed then the arguments without parameters will be lost. Rest parameter is used to store the value of all remaining arguments. It allows us to use an indefinite number of parameters as an array. The rest parameter has to be the last parameter and it is prefixed with the three-dots ( ...).

function checkingRestParameter(a,b,...args) {
   console.log(a);
   console.log(b);
   console.log(args);
}

checkingRestParameter(1,2,3,4,5,6);
//output
//1
//2
//[3,4,5,6]
Enter fullscreen mode Exit fullscreen mode

Global Scope

If a variable is declared out of any function and then it becomes a global variable. We can access and modify a global variable from anywhere. Its scope is global.

let num = 10;
console.log(num); // 10

function modifyNum() {
    num = 30;
}

//Before function call
console.log(num); // 10

modifyNum();

//Before function call
console.log(num); // 30
Enter fullscreen mode Exit fullscreen mode

Local Scope

If a variable is declared inside a function, then it can only be accessed within that function. A local variable cannot be accessed or modified outside the function declaration. The scope of A local variable is called local scope or function scope.


function modifyNum() {
    let num = 10;
    console.log(num); // 10
    num = 30;
    console.log(num); // 30
}


modifyNum(); // 10 30

console.log(num); // Throws Error because num is only accessible within the function.
Enter fullscreen mode Exit fullscreen mode

Block scope

Block scope is the scope of a variable within the curly braces that it has been declared. A block can be a function, an if statement, or a loop. let is block-scoped and var is function scoped.

function checkingScope() {
    if(true){
        var x =10;
    }
    //x can be accessible because it is function scoped.
    console.log(x); // 10
}
Enter fullscreen mode Exit fullscreen mode

Now an example of block scope.

function checkingScope() {
    if(true){
        let x =10;
    }
    //x cannot be accessible because it is block scoped.
    console.log(x); // error
}
Enter fullscreen mode Exit fullscreen mode

Error Handling 'try...catch'

We use the try-catch statement to handle errors that may occur in a block of code without stopping the program. In the try statement, we write the code that needs to be tested during execution. If an error occurs code that we write in the catch block will execute. If no error is thrown, the catch block is skipped.

try {
    //Code-block that needs to be checked.
}
catch(err) {
    // Code block that will be execute if exception is thrown.
}

try {
    checkingFunction();
}
catch(error) {
    console.log(error.message);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)