# JavaScript Interview 1

**JavaScript Fundamentals**

1. **What is the difference between var, let, and const in JavaScript?**  
    In JavaScript, var, let, and const are used to declare variables, but they have key differences in terms of scope, hoisting, and mutability.  
    i) var  
    Scope - Function-scoped (i.e., only accessible within the function where it was declared).  
    Hoisting - Hoisted to the top of its scope and initialized as undefined.  
    Redeclaration - Allowed (you can declare the same variable multiple times in the same scope). Reassignment - Allowed.  
    ii) let  
    Scope - Block-scoped (i.e., only accessible within the block {} where it was declared).  
    Hoisting - Hoisted but not initialized (accessing it before declaration results in a ReferenceError). Redeclaration - Not allowed in the same scope.  
    Reassignment - Allowed.  
    iii) const  
    Scope - Block-scoped (same as let).  
    Hoisting - Hoisted but not initialized.  
    Redeclaration - Not allowed.  
    Reassignment - Not allowed (must be initialized when declared and cannot be changed).
    
2. **What are the different data types in JavaScript?**  
    JavaScript has **8 main data types**, categorized into **primitive** and **non-primitive (reference)** types.  
    **i) Primitive Data Types (Immutable) -** String, Number, BigInt, Boolean, Undefined, NULL, NaN.  
    Store **single, immutable values**.  
    Compared **by value**.  
    Stored **directly** in memory (stack).
    
    ```javascript
    let x = 10;
    let y = x;  // Copying the value
    x = 20;     
    
    console.log(x); // 20
    console.log(y); // 10 (remains unchanged)
    ```
    
    **ii) non-primitive (reference) -** Objects, Array, Function.  
    Store collections of values or functions.  
    Compared by reference (memory address).  
    Stored in heap memory, and variables hold a reference to the location.
    
    ```javascript
    let obj1 = { name: "Alice" };
    let obj2 = obj1;  // Copying reference
    
    obj1.name = "Bob";  
    
    console.log(obj1.name); // Bob
    console.log(obj2.name); // Bob (both point to the same object)
    ```
    
3. **What are the types of errors in JavaScript?**  
    JavaScript errors can be categorized into three main types: Syntax Errors, Runtime Errors, and Logical Errors. These errors occur due to different reasons and affect how the code executes.  
    **i) Syntax Errors (Parsing Errors) -**  
    Occur when JavaScript fails to understand the code due to incorrect syntax. These errors prevent the script from running.
    
    ```javascript
    console.log("Hello World"  // Missing closing parenthesis
    ```
    
    **ii) Runtime Errors (Reference & Type Errors)**  
    a) Reference Error - Happens when trying to access an undefined variable or function.  
    b) Type Error - Occurs when trying to perform an invalid operation on a data type.
    
    **iii) Logical Errors (Bugs)**  
    The most difficult to detect because the code runs without errors, but the output is incorrect. Caused by mistakes in logic.
    
    ```javascript
    let num = 10;
    if (num = 5) {  // Mistakenly used '=' instead of '==='
      console.log("Number is 5");
    }
    ```
    
    Other common JavaScript errors,  
    i) SyntaxError  
    `if (true { console.log("Error"); }`  
    (Missing parenthesis)  
    ii) ReferenceError  
    `console.log(undeclaredVar);`  
    (Using an undefined variable)  
    iii) TypeError  
    `"hello".push("!");`  
    (String does not have push())  
    iv) RangeError  
    `new Array(999999999999999);`  
    (Array size is too large)  
    v) URIError  
    `decodeURIComponent("%");`  
    (Malformed URI)  
    vi) EvalError  
    Rarely occurs,  
    related to eval() misuse
