Sitemap

Javascript Practices

2 min readSep 17, 2026

--

1. Event Loop (Most Important)

console.log("1");
setTimeout(() => console.log("2"), 0);
Promise.resolve().then(() => console.log("3"));
console.log("4");

Output

1
4
3
2

Reason

  1. Sync code → 1
  2. setTimeout goes to macrotask
  3. Promise goes to microtask
  4. Sync → 4
  5. Microtask → 3
  6. Macrotask → 2

2. var Closure Bug

for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}

Output

3
3
3

Because var has function scope and all callbacks share the same i.

Fix

for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}

Output:

0
1
2

3. this in Arrow vs Normal Function

const obj = {
name: "Rita",

normal() {
console.log(this.name);
},

arrow: () => {
console.log(this.name);
}
};

obj.normal(); //Rita
obj.arrow(); //undefined

Reason

Normal function → this = obj

Arrow function → lexical this (global/module)

4. Object Reference

const a = { x: 1 };
const b = a;

b.x = 100;

Get Rita Mahato’s stories in your inbox

Join Medium for free to get updates from this writer.

console.log(a.x); //100

*Objects are stored by reference.

5. Spread Operator (Tricky)

const a = { x: 1, y: { z: 2 } };
const b = { ...a };
b.y.z = 99;
console.log(a.y.z); //99

Reason

Spread creates a shallow copy.

Only the first level is copied.

6. map(parseInt)

console.log(["1", "2", "3"].map(parseInt));  //[1, NaN, NaN]

Reason

map passes (value, index).

parseInt("1", 0);
parseInt("2", 1);
parseInt("3", 2);

Radix 1 and 2 make the last two invalid.

7. Promise Chain

Promise.resolve(5)
.then(x => x * 2)
.then(x => {
console.log(x); //10
return x + 1;
})
.then(console.log); //11

Each .then() receives the previous return value.

8. Nullish vs OR

console.log(0 || 100); //100
console.log(0 ?? 100); //0

| Operator | Checks | | — -| — -| | || | Falsy values | | ?? | Only null & undefined |

9. Hoisting

console.log(a); //undefined

var a = 5;

function test() {
console.log(a); //undefined
var a = 10;
}

test();

Inside test, local var a is hoisted.

function test() {
var a;
console.log(a);
a = 10;
}

10. Async/Await Order

async function demo() {
console.log(“A”);

await Promise.resolve();

console.log(“B”);
}

console.log(“C”);
demo();
console.log(“D”);

Output

C
A
D
B

await pauses the async function and schedules the remaining code as a microtask.

--

--

Rita Mahato
Rita Mahato

Written by Rita Mahato

I am here for learning on diff tech news, and also providing my writing on my areas topic.I have been around 3 years in IT sector .