Answer of the JavaScript Questions

Discuss the scope of 'var', 'let', and 'const'

In JavaScript, we can declare variables in 3 ways using var, let, and const. Now, lets take a look each one of them and their scope.

'var': Variable declared with bar can only accessible within the function they are defined in. This is means var is function scoped. var declared variables can be reassigned and re-declared. Let me give an example to understand this better.
Example:

                  
    function fun1(){
      if(10 > 5){
          console.log(10);
          var a = 6;
      }
      console.log(a); //output will be 6 
      because a is function scoped
  }
  fun1();
                  
                

In this example we declared a variable using var inside a function if-else condition now we can access 'a' from anywhere inside that function. We can also access 'a' from outside the function because var variables are hoisted to the global scope.

Now, what is hoisting?
In simple words hoisting means you can define a variable before its declaration. Lets take another example to understand hoisting better. Hoisting Example:

                    
    // Hoisting Example 1
    console.log(number);
    
    var number = 23;
    
    // Hoisting Example 2
    number2 = 36;
    
    console.log(number2);
    
    var number2;
                    
                  

In Example 1, we console log a variable named as number but we didn't declare or initialized the number variable before console log it. In this case output will be undefined because we didn't initialized any value for number variable before console.log(number)
In Example 2, we first initialized a variable number2 and put 36 as value then we console log the number2 after that we declare the variable. In this case output will be 36 because hoisting put every var variable declaration at the top of the scop that's why it's printing 36 even when we are declaring the variable number2 after console.log

'let': Variables declared with let is block scoped and hoisting doesn't work for let declared variables.
Block Scoped: Variable declared with let are only accessible inside the block they are defined in. In other way we can say that let declared variables can only accessible within the curly braces they are defined. Let's take a simple example for better understanding.

                        
    Example 1:
    function printSomething() {
      if (56 > 20) {
        //we can access number variable inside this scop only
        let number = 23;
        console.log(number);
      }
      //   if we try to access number variable here it will give an error of not defined
      console.log(number);
    }
    printSomething();
                        
                      

let declared variables doesn't support hoisting. This means we can't define a variable before declaration. Let's take another example.
Example 2:

                          
  /* we are trying to console log
  the variable before it's declaration
  it will give an ReferenceError:
  (Cannot access 'username' before initialization) */
  console.log(username);
  let username = "Shahin";
                          
                        

'const': Variables declared as const are in the block scope. As like let, variables declared with const are block-scoped, meaning they are limited to the nearest enclosing block. Also variables declared with const can't be reassigned and re-declared. Let's take some examples.
Example 1:

                            
  const nam = 'amar nam mofiz';
  // it will throw an TypeError
  //that means we can not reassign nam variable 
  nam = 'ami kintu mofiz na';
                            
                          

As like let declared variables const variables also do not support hoisting. This means we can not define a const variable before it's declaration.
Example 2:

                              
  // it will throw an ReferenceError:
  //Cannot access 'nameKi' before initialization
  console.log(nameKi);
  const nameKi = 'nam dia kam ki?';
                              
                            
Tell us the use cases of 'null' and 'undefined'

In JavaScript, null and undefined are two separate values that have different applications:

Null: null is a value that can be explicitly assigned to a variable this means it is the intentional absence of the value. It is one of the primitive value of the javascript. When we define a variable to null that means we are saying this variable is empty. Null is used most of the time when we intentionally want to set a value to empty or we want to check a condition using null. Let's take an example to understand the use case of null.

Example 1:

            
      /* here we are checking vairable
      'mokkel'  is null or not
      */
      let mokkel = 'akkel';
      if(mokkel === null){
          console.log('Mokkel er akkel nai');
      }
      else{
          console.log('Mokkel er akkel ache');
      }
            
          

Undefined: A variable in JavaScript that has been declared but not assigned a value automaticly takes the value "undefined."
If we decalre a varibale but do not assign any value the default value will be 'undefined' also if we write a function that takes a parameter if we call the function and do not pass any value in argument then the default value will be 'undefined'. Let's look at the example below:

Example 1:

            
    let abc;
    console.log(abc); //output will be undefined 
    /*abc will be undefined because we didn't
    assign any value for abc
    */
    function printMyName(name){
        console.log(`My name is ${name}`); 
    }
    printMyName(); 
    /*name will be undefined because we didn't
    pass any name in argument
    */
            
          

It's important to note both 'null' and 'undefined' looks like they are doing the same thing but there are some major differences.
Null is used to describe a intentional absence of value or to clear a value, whereas undefined is often used to represent an unintentional or accidental absence of value or a missing value. Now Let's compare null and undefined using double qual & triple equal operator for better understanding.
Example 2:

            
    /* comparing null and undefined
    using double equal operator
    */
    if(null == undefined){
        console.log('true');
        //output will be true
    }
    else{
        console.log('false');
    }
    /* comparing null and undefined
    using triple equal operator
    */
    if(null === undefined){
        console.log('true');
    }else{
        console.log('false');
        //output will be false
    }
            
          

When we are comparing with double equals it's giving us out true but when we are comparing with triple equal operator output is false because null is equal to undefined but not identical.

What do you mean by REST API?

REST API: REST API stands for Represntarional State Transfer Application Programming Interface. REST is an architectural style that defines a set of constraints to be used for creating web services. REST is not a Programming Laguage neither a Library. It's a set of guidelines and principles for creating web services.

How REST API Works?
REST API uses stateless communication, treats everything as a resource, and uses GET, POST, PUT, and DELETE HTTP methods. Each resources has an unique URL, and data can be displayed in various way, including JSON.

There are 4 HTTP Request method for REST API:

  1. GET: This method send request to server to get all data as response. It also can be used for getting specific data by using endpoints.
  2. POST: This method request server to create a new data.
  3. PUT/PATCH: This method request server to update an existing data.
  4. PUT/DELETE: This method request server to delete data you requested for.
  5. Here is an example below of how REST API data looks in json format:

                
    [
    {
      "id": 1,
      "title": "Ki Khobor Mia Vi",
      "author": "Mofiz Mia"
    },
    {
      "id": 2,
      "title": "Aj Achi Kal Nai",
      "author": "Motin Vi"
    }
    ]