Differences between var, let, and const?
✅ Scope
- Variables declared with
vardo not have block scope. They can be accessed outside blocks but not outside functions. - Variables declared with
lethave block scope, accessible only within the declared block. constdefines constants, must be initialized, accessible only in the declared block, and cannot be reassigned.
✅ Declaration Rules
- A variable name can only be declared once using one of these methods; otherwise, it throws an error.
✅ Difference when using with this
| Feature | var / let / const |
|---|---|
Can change this context |
✅ Yes |
First parameter is the object this refers to |
✅ Yes |
No object or undefined/null |
Defaults to global window |
| Parameter passing | apply uses array, call uses parameter list, bind supports multiple arguments |
| Execution method | apply/call execute immediately, bind returns a new function |
What is event delegation?
- Event delegation attaches events to a parent or ancestor element instead of individual child elements.
- When the event reaches the target element, it bubbles up and triggers the handler on the outer element.
Difference between debounce and throttle?
- Debounce: Executes only the last trigger. Even if the event keeps firing, it waits until
nseconds after the last event to execute. - Throttle: Controls the execution frequency. Fires every
nseconds during continuous event triggers.
How does frontend prevent SQL injection?
- Parameterized Queries: Use prepared statements or parameterized queries.
- Input Validation: Ensure user input follows expected format.
- Escape Special Characters: Escape characters when constructing SQL queries.
- Limit Permissions: Restrict user privileges to prevent unsafe operations.
- Whitelist Validation: Only allow expected values in input.
- Code Review: Regularly review code, especially DB-related parts.
Difference between HTTP and HTTPS
- HTTPS requires a certificate from a CA, which may cost money.
- HTTP transmits data in plaintext, while HTTPS uses SSL encryption.
- They use different connection methods and ports: HTTP uses 80, HTTPS uses 443.
- HTTP is stateless; HTTPS combines SSL + HTTP for encrypted, authenticated, secure communication.
Difference between created and mounted?
createdruns beforemounted. DOM is not fully rendered, but API requests can be made early here.mountedis triggered when the DOM has been rendered, so it’s suitable for manipulating DOM elements.
Explain the event loop mechanism
🔁 What is the Event Loop?
JavaScript is single-threaded. In addition to the call stack, it relies on task queues to control asynchronous code execution order.
This whole process is called the Event Loop.
🧠 Core Concepts
- Only one event loop in a single thread.
- Multiple task queues:
- Macro Tasks
- Micro Tasks
⏱ Execution Order
Macro Task ➝ Clear all Micro Tasks ➝ Next Macro Task ➝ Clear all Micro Tasks…
- Execute a macro task (like the whole script)
- Execute all micro tasks created during this macro task
- If micro tasks generate more micro tasks, execute them too
- Begin the next macro task loop
🧩 Examples of Macro Tasks
scriptsetTimeoutsetIntervalsetImmediate(Node.js)I/O operationsUI rendering
🧬 Examples of Micro Tasks
process.nextTick(Node.js, higher priority)Promise.then / catch / finallyasync / awaitMutationObserver
Explain the CSS box model
The CSS box model includes IE and standard W3C models.
- In standard W3C box model,
widthonly includes content.box-sizing: content-box(default). - In IE box model,
widthincludes content + padding + border.box-sizing: border-box. box-sizing: padding-boxincludes left/right padding + width.
Differences between Vue 2 and Vue 3
- Two-way binding:
- Vue 2: uses
Object.defineProperty()(ES5), which only watches individual properties. - Vue 3: uses
Proxy(ES6), which can observe entire objects and arrays.
- Vue 2: uses
- Lifecycle hooks:
- Vue 2:
beforeCreate,created,beforeMount,mounted, etc. - Vue 3:
setup,onBeforeMount,onMounted, etc.
- Vue 2:
- Vue 2 requires a root tag; Vue 3 allows multiple root tags via Fragment.
- API:
- Vue 2: Options API (functions and data handled separately).
- Vue 3: Composition API (related code grouped together).
- Slots:
- Named slot: Vue 2 uses
slot="", Vue 3 usesv-slot="". - Scoped slot: Vue 2 uses
slot-scope="data", Vue 3 uses#dataor#default="{data}".
- Named slot: Vue 2 uses