JavaScript logical questions

Basic JavaScript question for interview

                                   **Here, we Have JavaScript Logical Question**

Question no:1

function car(){
    console.log(model);
    console.log(color);
     var model = 2015;
    let color = red;
}
 car()

The Output will be Undefined & Error. in the first line the output is undefined & in second line the output is error.

why we are getting undefined in first line of console?

Here the JavaScript Hosting rule is applied

Hoisting rule:-JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code. Hoisting allows functions to be safely used in code before they are declared.

when ever we are declaring any variable in hoisting it allow us to use that variable before declaring it, so in JavaScript hoisting that is allowed that why in the first line it was giving undefined instead of error.
if we just type model =2015 on the above line of console.log(model); it will work.

why we are getting error on second line?

So, Basically hoisting rule is not applied on literals or constant because Variables defined with let and const are hoisted to the top of the block, but not initialized. Meaning: The block of code is aware of the variable, but it cannot be used until it has been declared. Using a let variable before it is declared will result in a ReferenceError. if we write let color = red; on the top of console.log(model); line it will work.