|
4 | 4 | 1. [Is JavaScript a dynamically typed language or a statically typed language?](#q1-is-javascript-a-dynamically-typed-language-or-a-statically-typed-language)
|
5 | 5 | 2. [What are the different datatypes in JavaScript?](#q2-what-are-the-different-datatypes-in-javascript-most-asked)
|
6 | 6 | 3. [What is Hoisting in JavaScript?](#q3-what-is-hoisting-in-javascript-most-asked)
|
| 7 | +4. [What is Temporal Dead Zone?](#q4-what-is-temporal-dead-zone) |
| 8 | +5. [What are the differences between `let`, `var`, and `const`?](#q5-differences-between-let-var-and-const) |
7 | 9 |
|
| 10 | + |
8 | 11 | ### Q1. Is JavaScript a dynamically typed language or a statically typed language?
|
9 | 12 | JavaScript is a dynamically typed language. It means all type checks are done at runtime (when the program is executing). So, we can just assign anything to the variable and it works fine.
|
10 | 13 |
|
@@ -40,5 +43,40 @@ console.log(a); // "Hello";
|
40 | 43 | - In other scripting/server side languages, variables or functions must be declared before using it.
|
41 | 44 | - In javascript, variables and functions can be used before declaring it. The javascript compiler moves all the declarations of variables and functions on top. so there will not be any error. This is called hoisting.
|
42 | 45 | #### 👉 Interview Tip: Mention buzz word 'temporal dead zone' for let & const in above answer so that interviewer will ask What is temporal dead zone. 😉
|
| 46 | +### What are the various things hoisted in javascript ? |
| 47 | + - Function declarations: Fully hoisted. |
| 48 | + - var - Hoisted |
| 49 | + - Arrow functions: Not hoisted |
| 50 | + - Anonymous Function expressions: Not hoisted |
| 51 | + - let and const - Hoisted but not initialized. (Temporal dead zone). |
| 52 | + - class declarations - Hoisted but not initialized. |
| 53 | +## |
| 54 | +[Back to Top](#javascript-basics) |
| 55 | + |
| 56 | +### Q4. What is temporal dead zone ? |
| 57 | +- It is a specific time period in the execution of javascript code where the variables declared with let and const exists but cannot be accessed until the value is assigned. |
| 58 | +- Any attempt to access them result in reference errors. |
| 59 | +``` |
| 60 | +function somemethod() { |
| 61 | + console.log(counter1); // undefined |
| 62 | + console.log(counter2); // ReferenceError |
| 63 | + var counter1 = 1; |
| 64 | + let counter2 = 2; |
| 65 | + } |
| 66 | +``` |
| 67 | +## |
| 68 | +[Back to Top](#javascript-basics) |
| 69 | + |
| 70 | +### Q5. What are the differences let, var and const ? (Most asked) |
| 71 | +** Scope: ** |
| 72 | +- Variables declared with var are function scoped.( available through out the function where its declared ) or global scoped( if defined outside the function ). |
| 73 | +- Variables declared with let and const are block scoped. |
| 74 | +** Reassignment: ** |
| 75 | +- var and let can be reassigned. |
| 76 | +- const cannot be reassigned. |
| 77 | +** Hoisting: ** |
| 78 | +- var gets hoisted and initialized with undefined. |
| 79 | +- let and const - gets hoisted to the top of the scope but does not get assigned any value.(temporal dead zone) |
| 80 | + |
43 | 81 | ##
|
44 | 82 | [Back to Top](#javascript-basics)
|
0 commit comments