function test() {
var age = 25;
console.log(age);
}age
cannot be accessed outside
test()
.
Block Scope
let
and
const
are block-scoped.
if (true) {
let x = 10;
const y = 20;
console.log(x, y);
}x
and
y
cannot be accessed outside the
if
block.
18. What is strict mode ("use strict")?
Strict mode enables a stricter set of JavaScript rules and helps catch certain programming mistakes.
Example:
"use strict";
x = 10;
This produces a
ReferenceError
because
x
was not declared.
Without strict mode, older JavaScript behavior could create a global variable in some situations.
Benefits:
Catches common mistakes
Prevents accidental global variables
Makes some unsafe operations throw errors
Helps write cleaner code
19. What are comments in JavaScript?
Comments are text ignored by the JavaScript engine. They are used to explain code or temporarily disable code.
Single-Line Comment:
// This is a comment
console.log("Hello");
Multi-Line Comment:
/*
This is a
multi-line comment
*/
console.log("Hello");
Why Use Comments?
✅ Explain complex logic
✅ Improve code readability
✅ Help other developers understand the code
✅ Document important decisions
20. What are JavaScript modules?
Modules allow you to split JavaScript code into separate, reusable files.
They help organize large applications and prevent unnecessary global variables.
Export:
// math.js
export function add(a, b) {
return a + b;
}
Import:
// app.js
import { add } from "./math.js";
console.log(add(10, 20));
Types of Exports:
Named exports
Default exports
Default Export:
export default function greet() {
console.log("Hello");
}Important Interview Point:
ES Modules use
import
and
export
. They are the standard module system for modern JavaScript.
❤️ Double Tap For Part 3