| name | js-mastery |
| description | Comprehensive JavaScript reference covering 33+ essential concepts every developer should know. From fundamentals like primitives and closures to advanced patterns like async/await and functional programming. Use when explaining JS concepts, debugging JavaScript issues, or teaching JavaScript fundamentals. |
| version | 1 |
| author | Antigravity |
🧠 JavaScript Mastery
[!IMPORTANT]
This skill MUST be executed strictly under the Omni-Architect Agent Protocol v1.0.
All tool executions, code modifications, and communications MUST adhere to the 13 core protocols.
33+ essential JavaScript concepts every developer should know, inspired by 33-js-concepts.
When to Use This Skill
Use this skill when:
- Explaining JavaScript concepts
- Debugging tricky JS behavior
- Teaching JavaScript fundamentals
- Reviewing code for JS best practices
- Understanding language quirks
1. Fundamentals
1.1 Primitive Types
JavaScript has 7 primitive types:
const str = "hello";
const num = 42;
const float = 3.14;
const big = 9007199254740991n;
const bool = true;
let undef;
const empty = null;
const sym = Symbol("description");
Key points:
- Primitives are immutable
- Passed by value
typeof null === "object" is a historical bug
1.2 Type Coercion
JavaScript implicitly converts types:
"5" + 3;
"5" - 3;
Boolean("");
Boolean("hello");
Boolean(0);
Boolean([]);
"5" == 5;
"5" === 5;
Falsy values (8 total):
false, 0, -0, 0n, "", null, undefined, NaN
1.3 Equality Operators
null == undefined;
"1" == 1;
null === undefined;
"1" === 1;
Object.is(NaN, NaN);
Object.is(-0, 0);
Rule: Always use === unless you have a specific reason not to.
2. Scope & Closures
2.1 Scope Types
var globalVar = "global";
function outer() {
var functionVar = "function";
if (true) {
let blockVar = "block";
const alsoBlock = "block";
var notBlock = "function";
}
}
2.2 Closures
A closure is a function that remembers its lexical scope:
function createCounter() {
let count = 0;
return {
increment() {
return ++count;
},
decrement() {
return --count;
},
getCount() {
return count;
},
};
}
const counter = createCounter();
counter.increment();
counter.increment();
counter.getCount();
Common use cases:
- Data privacy (module pattern)
- Function factories
- Partial application
- Memoization
2.3 var vs let vs const
var x = 1;
var x = 2;
let y = 1;
const z = 1;
const obj = { a: 1 };
obj.a = 2;
obj.b = 3;
3. Functions & Execution
3.1 Call Stack
function first() {
console.log("first start");
second();
console.log("first end");
}
function second() {
console.log("second");
}
first();
Stack overflow example:
function infinite() {
infinite();
}
infinite();
3.2 Hoisting
console.log(a);
var a = 5;
console.log(b);
let b = 5;
sayHi();
function sayHi() {
console.log("Hi!");
}
sayBye();
var sayBye = function () {
console.log("Bye!");
};
3.3 this Keyword
console.log(this);
const obj = {
name: "Alice",
greet() {
console.log(this.name);
},
};
const obj2 = {
name: "Bob",
greet: () => {
console.log(this.name);
},
};
function greet() {
console.log(this.name);
}
greet.call({ name: "Charlie" });
greet.apply({ name: "Diana" });
const bound = greet.bind({ name: "Eve" });
bound();
4. Event Loop & Async
4.1 Event Loop
console.log("1");
setTimeout(() => console.log("2"), 0);
Promise.resolve().then(() => console.log("3"));
console.log("4");
Execution order:
- Synchronous code (call stack)
- Microtasks (Promise callbacks, queueMicrotask)
- Macrotasks (setTimeout, setInterval, I/O)
4.2 Callbacks
function fetchData(callback) {
setTimeout(() => {
callback(null, { data: "result" });
}, 1000);
}
fetchData((error, result) => {
if (error) {
console.error(error);
return;
}
console.log(result);
});
getData((data) => {
processData(data, (processed) => {
saveData(processed, (saved) => {
notify(saved, () => {
});
});
});
});
4.3 Promises
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Success!");
}, 1000);
});
promise
.then((result) => console.log(result))
.catch((error) => console.error(error))
.finally(() => console.log("Done"));
Promise.all([p1, p2, p3]);
Promise.allSettled([p1, p2]);
Promise.race([p1, p2]);
Promise.any([p1, p2]);
4.4 async/await
async function fetchUserData(userId) {
try {
const response = await fetch(`/api/users/${userId}`);
if (!response.ok) throw new Error("Failed to fetch");
const user = await response.json();
return user;
} catch (error) {
console.error("Error:", error);
throw error;
}
}
async function fetchAll() {
const [users, posts] = await Promise.all([
fetch("/api/users"),
fetch("/api/posts"),
]);
return { users, posts };
}
5. Functional Programming
5.1 Higher-Order Functions
Functions that take or return functions:
const numbers = [1, 2, 3];
const doubled = numbers.map((n) => n * 2);
function multiply(a) {
return function (b) {
return a * b;
};
}
const double = multiply(2);
double(5);
5.2 Pure Functions
function add(a, b) {
return a + b;
}
let total = 0;
function addToTotal(value) {
total += value;
return total;
}
function getDiscount(price) {
return price * globalDiscountRate;
}
5.3 map, filter, reduce
const users = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 35 },
];
const names = users.map((u) => u.name);
const adults = users.filter((u) => u.age >= 30);
const totalAge = users.reduce((sum, u) => sum + u.age, 0);
const result = users
.filter((u) => u.age >= 30)
.map((u) => u.name)
.join(", ");
5.4 Currying & Composition
const curry = (fn) => {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
}
return (...moreArgs) => curried(...args, ...moreArgs);
};
};
const add = curry((a, b, c) => a + b + c);
add(1)(2)(3);
add(1, 2)(3);
add(1)(2, 3);
const compose =
(...fns) =>
(x) =>
fns.reduceRight((acc, fn) => fn(acc), x);
const pipe =
(...fns) =>
(x) =>
fns.reduce((acc, fn) => fn(acc), x);
const addOne = (x) => x + 1;
const double = (x) => x * 2;
const addThenDouble = compose(double, addOne);
addThenDouble(5);
const doubleThenAdd = pipe(double, addOne);
doubleThenAdd(5);
6. Objects & Prototypes
6.1 Prototypal Inheritance
const animal = {
speak() {
console.log("Some sound");
},
};
const dog = Object.create(animal);
dog.bark = function () {
console.log("Woof!");
};
dog.speak();
dog.bark();
class Animal {
speak() {
console.log("Some sound");
}
}
class Dog extends Animal {
bark() {
console.log("Woof!");
}
}
6.2 Object Methods
const obj = { a: 1, b: 2 };
Object.keys(obj);
Object.values(obj);
Object.entries(obj);
const copy = { ...obj };
const copy2 = Object.assign({}, obj);
const frozen = Object.freeze({ x: 1 });
frozen.x = 2;
const sealed = Object.seal({ x: 1 });
sealed.x = 2;
sealed.y = 3;
delete sealed.x;
7. Modern JavaScript (ES6+)
7.1 Destructuring
const [first, second, ...rest] = [1, 2, 3, 4, 5];
const { name, age, city = "Unknown" } = { name: "Alice", age: 25 };
const { name: userName } = { name: "Bob" };
const {
address: { street },
} = { address: { street: "123 Main" } };
7.2 Spread & Rest
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];
const obj1 = { a: 1 };
const obj2 = { ...obj1, b: 2 };
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
sum(1, 2, 3, 4);
7.3 Modules
export const PI = 3.14159;
export function square(x) {
return x * x;
}
export default class Calculator {}
import Calculator, { PI, square } from "./math.js";
import * as math from "./math.js";
const module = await import("./dynamic.js");
7.4 Optional Chaining & Nullish Coalescing
const user = { address: { city: "NYC" } };
const city = user?.address?.city;
const zip = user?.address?.zip;
const fn = user?.getName?.();
const value = null ?? "default";
const zero = 0 ?? "default";
const empty = "" ?? "default";
const value2 = 0 || "default";
Quick Reference Card
| Concept | Key Point |
|---|
== vs === | Always use === |
var vs let | Prefer let/const |
| Closures | Function + lexical scope |
this | Depends on how function is called |
| Event loop | Microtasks before macrotasks |
| Pure functions | Same input → same output |
| Prototypes | __proto__ → prototype chain |
?? vs || | ?? only checks null/undefined |
Resources