JavaScript and React Study Guide (Q1–
Q22)
Q1. Explain JavaScript Architecture in detail. (Also differentiate between
window and document objects.)
JavaScript Architecture:
JavaScript is single-threaded and uses an Event Loop to manage execution.
- Call Stack: Where functions are executed one by one.
- Web APIs: Browser-provided features like setTimeout, DOM, etc.
- Callback Queue: Stores functions waiting to run.
- Event Loop: Moves tasks from the queue to the call stack when it's free.
Window vs Document:
- window: Represents the browser window. It is the global object in browsers.
- document: Represents the webpage (DOM) loaded in the window.
Example:
[Link]([Link]); // Width of the browser window
[Link]([Link]); // Title of the webpage
Q2. Explain var, let, and const with examples. When should we use each?
- var: Function-scoped, can be re-declared and updated.
- let: Block-scoped, can be updated but not re-declared in the same block.
- const: Block-scoped, cannot be updated or re-declared.
Example:
var x = 10;
let y = 20;
const z = 30;
Use:
- var: Avoid using in modern JS.
- let: When the value will change.
- const: When the value stays the same.