Discussing the scope of var, let const

var

  • Function Scope: Variables declared with var are function-scoped, meaning they are only accessible within the function in which they are declared.
  • Hoisting: Variables declared with var are hoisted to the top of their containing function or global scope. This means that you can use the variable before it is declared, but it will have an initial value of undefined.
  • Re-declaration: You can re-declare a var variable within the same scope without any error.
    function exampleFunction() {
      if (true) {
          var x = 10;
      }
      console.log(x);
      // 10 (accessible outside the if block)
    }                
  
    function exampleFunction() {
      if (true) {
          let x = 10;
      }
      console.log(x);
      // ReferenceError: x is not defined 
      // (not accessible outside the if block)
    }            
  

let

  • Block Scope: Variables declared with let are block-scoped, meaning they are accessible only within the block (enclosed by {}) where they are defined, such as a loop or conditional statement.
  • Hoisting: Like var, let variables are hoisted, but they are not initialized. Attempting to use a let variable before its declaration results in a ReferenceError
  • No Re-declaration: You cannot re-declare a let variable within the same scope.

const

  • Block Scope: Variables declared with const are also block-scoped, just like let.
  • Hoisting: Like let, const variables are hoisted but not initialized, and attempting to use a const variable before its declaration results in a ReferenceError.
  • Immutable: const variables cannot be reassigned after they are initialized. However, if a const variable holds an object or array, you can still modify the properties or elements of that object or array.
    if (true) {
      const y = 20;
    }
    console.log(y); 
    // ReferenceError: y is not defined 
    // (not accessible outside the if block)
    
    const PI = 3.14159;
    PI = 3.14; 
    // TypeError: Assignment to constant variable.            
  

The use cases of null and undefined

null

  • Intentional Absence of Value: null is typically used when you want to explicitly indicate that a variable or object property has no assigned value, but you intend for it to have no value.
  • Clear Initialization: It can be used when initializing variables or properties to prepare them for later assignment.
  • As a Placeholder: In some cases, null is used as a placeholder for an object or value that will be assigned or created later.
  • Checking for Missing Values: You can use null when checking for the absence of a value in conditional statements.

undefined

  • Implicit Absence of Value: Usually indicates that a variable or object property has been declared but has not been assigned any value yet. It is often used as the default initial value for variables.
  • Function Parameters: When a function is called with fewer arguments than declared parameters, the extra parameters are undefined.
  • Object Properties: If you try to access an object property that doesn't exist, JavaScript returns undefined.
  • Uninitialized Variables: Variables declared but not explicitly assigned are automatically initialized to undefined.

What do you mean by REST API?

REST API, which stands for Representational State Transfer Application Programming Interface, is a set of architectural principles and constraints used for designing networked applications and web services. It is not a technology or a programming language but rather a style of building and interacting with web services. RESTful APIs are designed to be simple, scalable, and stateless, making them suitable for distributed systems, including the World Wide Web.