JavaScript_Advanced_CheatSheet_Final
JavaScript_Advanced_CheatSheet_Final
1. Scope
What it means:
Types of Scope:
Example:
let a = 10;
function test() {
let b = 20;
if (true) {
let c = 30;
console.log(a, b, c); //
2. Hoisting
What it means:
Example:
console.log(x); // undefined
var x = 10;
foo(); // Works
Notes:
- var is hoisted (but value is undefined)
3. Closures
What it means:
A closure is when a function remembers and uses variables from its outer function even after the outer
Example:
function outer() {
let count = 0;
count++;
console.log(count);
};
counter(); // 1
counter(); // 2
4. Event Loop
What it means:
Execution Flow:
Example:
console.log("Start");
console.log("End");
Output:
Start
End
Inside timeout
5. Streams (Node.js)
What it means:
Streams allow you to read/write data in chunks (efficient for large files).
Types:
- Readable
- Writable
- Duplex
- Transform
Example:
const fs = require('fs');
console.log('Chunk:', chunk.toString());
});
6. Buffer (Node.js)
What it means:
Example:
console.log(buffer.toString()); // Hello
What it means:
fs is Nodes module to read/write files.
Example:
const fs = require('fs');
console.log(data);
});
console.log('File written!');
});