0% found this document useful (0 votes)
2 views4 pages

JavaScript_Advanced_CheatSheet_Final

This cheat sheet covers advanced JavaScript concepts including scope, hoisting, closures, the event loop, streams, buffers, and the file system in Node.js. It explains the types of scope, the behavior of hoisting, the functionality of closures, and how the event loop manages asynchronous tasks. Additionally, it provides examples for each concept to illustrate their usage in code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

JavaScript_Advanced_CheatSheet_Final

This cheat sheet covers advanced JavaScript concepts including scope, hoisting, closures, the event loop, streams, buffers, and the file system in Node.js. It explains the types of scope, the behavior of hoisting, the functionality of closures, and how the event loop manages asynchronous tasks. Additionally, it provides examples for each concept to illustrate their usage in code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JavaScript Advanced Concepts Cheat Sheet

1. Scope

What it means:

Scope decides where variables can be accessed.

Types of Scope:

- Global Scope: Available everywhere.

- Function Scope: Available only inside a function.

- Block Scope (let, const): Available only inside {}

Example:

let a = 10;

function test() {

let b = 20;

if (true) {

let c = 30;

console.log(a, b, c); //

2. Hoisting

What it means:

JS moves variable and function declarations to the top before execution.

Example:

console.log(x); // undefined

var x = 10;

foo(); // Works

function foo() { console.log("Hello"); }

Notes:
- var is hoisted (but value is undefined)

- let/const are not accessible before declaration

- Functions are hoisted fully

3. Closures

What it means:

A closure is when a function remembers and uses variables from its outer function even after the outer

function has finished.

Example:

function outer() {

let count = 0;

return function inner() {

count++;

console.log(count);

};

const counter = outer();

counter(); // 1

counter(); // 2

4. Event Loop

What it means:

JavaScript handles async tasks using the event loop.

Execution Flow:

1. Executes synchronous code

2. Moves async code (setTimeout, fetch) to queue

3. After sync is done, handles queued async code

Example:

console.log("Start");

setTimeout(() => { console.log("Inside timeout"); }, 0);

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');

const readStream = fs.createReadStream('file.txt');

readStream.on('data', chunk => {

console.log('Chunk:', chunk.toString());

});

6. Buffer (Node.js)

What it means:

Buffers store binary data temporarily used with streams.

Example:

const buffer = Buffer.from('Hello');

console.log(buffer); // <Buffer 48 65 6c 6c 6f>

console.log(buffer.toString()); // Hello

7. File System (Node.js)

What it means:
fs is Nodes module to read/write files.

Example:

const fs = require('fs');

fs.readFile('file.txt', 'utf8', (err, data) => {

if (err) throw err;

console.log(data);

});

fs.writeFile('new.txt', 'Hello!', err => {

if (err) throw err;

console.log('File written!');

});

You might also like