Flashcards
JavaScript Intermediate Flashcards
JavaScript Intermediate Flashcards
Flashcard Deck
Card 1 of 34
Space
Flip Card
←→
Navigate
1const p = new Promise((resolve, reject) => {2 setTimeout(() => resolve("done"), 1000);3});4p.then(val => console.log(val)); // "done"1async function fetchData() {2 const res = await fetch("https://api.example.com/data");3 const json = await res.json();4 return json;5}1console.log(0 == "0"); // true (coercion)2console.log(0 === "0"); // false (strict)1function Animal(name) { this.name = name; }2Animal.prototype.speak = function() {3 console.log(this.name + " makes a noise.");4};5const a = new Animal("Dog");6a.speak(); // "Dog makes a noise."1function greet(greeting) {2 console.log(greeting + ", " + this.name);3}4const obj = { name: "Alice" };5greet.call(obj, "Hello"); // Hello, Alice6greet.apply(obj, ["Hi"]); // Hi, Alice7const fn = greet.bind(obj, "Hey");8fn(); // Hey, Alice1const nums = [1, 2, 3, 4];2const doubled = nums.map(n => n * 2);3console.log(doubled); // [2, 4, 6, 8]1const [a, b] = [10, 20];2const { name, age } = { name: "Ali", age: 30 };3console.log(a, b); // 10 204console.log(name, age); // Ali 301const arr1 = [1, 2, 3];2const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]3const obj1 = { a: 1 };4const obj2 = { ...obj1, b: 2 }; // { a: 1, b: 2 }1function sum(...nums) {2 return nums.reduce((acc, n) => acc + n, 0);3}4console.log(sum(1, 2, 3, 4)); // 101function* counter() {2 yield 1;3 yield 2;4 yield 3;5}6const gen = counter();7console.log(gen.next().value); // 18console.log(gen.next().value); // 21const id = Symbol("id");2const user = { [id]: 123, name: "Alice" };3console.log(user[id]); // 1231const val = null ?? "default"; // "default"2const zero = 0 ?? "default"; // 01const user = { address: { city: "Amman" } };2console.log(user?.address?.city); // "Amman"3console.log(user?.phone?.number); // undefined1const wm = new WeakMap();2let obj = {};3wm.set(obj, "secret");4console.log(wm.get(obj)); // "secret"1const a = [1, 2, 3].map(n => n * 2); // [2, 4, 6]2[1, 2, 3].forEach(n => console.log(n)); // undefined returned1const p1 = Promise.resolve(1);2const p2 = Promise.resolve(2);3Promise.all([p1, p2]).then(values => {4 console.log(values); // [1, 2]5});1const handler = {2 get(target, key) {3 return key in target ? target[key] : "N/A";4 }5};6const p = new Proxy({ name: "Ali" }, handler);7console.log(p.name); // "Ali"8console.log(p.age); // "N/A"1function debounce(fn, delay) {2 let timer;3 return (...args) => {4 clearTimeout(timer);5 timer = setTimeout(() => fn(...args), delay);6 };7}1function throttle(fn, limit) {2 let inThrottle;3 return (...args) => {4 if (!inThrottle) {5 fn(...args);6 inThrottle = true;7 setTimeout(() => (inThrottle = false), limit);8 }9 };10}1function memoize(fn) {2 const cache = new Map();3 return function(...args) {4 const key = JSON.stringify(args);5 if (cache.has(key)) return cache.get(key);6 const result = fn(...args);7 cache.set(key, result);8 return result;9 };10}1const add = a => b => a + b;2const add5 = add(5);3console.log(add5(3)); // 81const counter = (() => {2 let count = 0;3 return {4 increment: () => ++count,5 getCount: () => count,6 };7})();8counter.increment();9console.log(counter.getCount()); // 11(function() {2 const x = 10;3 console.log(x); // 104})();5// x is not accessible outside1const obj = { a: 1, b: { c: 2 } };2// Shallow copy3const shallow = { ...obj };4shallow.b.c = 99; // also mutates obj.b.c5
6// Deep copy7const deep = JSON.parse(JSON.stringify(obj));1// true = capturing phase, false (default) = bubbling phase2element.addEventListener("click", handler, true);1const range = {2 [Symbol.iterator]() {3 let n = 1;4 return { next: () => n <= 3 ? { value: n++, done: false } : { done: true } };5 }6};7for (const x of range) console.log(x); // 1 2 31console.log(false && doSomething()); // false, doSomething never called2console.log(true || doSomething()); // true, doSomething never called1function highlight(strings, ...vals) {2 return strings.reduce((acc, str, i) =>3 acc + str + (vals[i] ? `<b>${vals[i]}</b>` : ""), "");4}5const name = "Alice";6console.log(highlight`Hello ${name}!`); // Hello <b>Alice</b>!1function sum() {2 return Array.from(arguments).reduce((a, b) => a + b, 0);3}4console.log(sum(1, 2, 3)); // 6