{"id":1657,"date":"2026-04-08T10:14:11","date_gmt":"2026-04-08T10:14:11","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=1657"},"modified":"2026-04-08T10:14:12","modified_gmt":"2026-04-08T10:14:12","slug":"javascript-quick-recap","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/javascript-quick-recap\/","title":{"rendered":"JavaScript &#8211; Quick Recap"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Summary Quick Revision<\/h3>\n\n\n\n<p><strong>use strict<\/strong><\/p>\n\n\n\n<p>In JavaScript,\u00a0<strong><code>\"use strict\"<\/code><\/strong>\u00a0is\u00a0a directive introduced in ECMAScript 5 (ES5) that enables\u00a0<strong>Strict Mode<\/strong>, a restricted variant of JavaScript.<\/p>\n\n\n\n<p><strong>Closures<\/strong><br>A closure is a function that retains access to variables from its outer scope after that outer function has returned, enabling private state and encapsulation.<\/p>\n\n\n\n<p><strong>IIFE<\/strong><br>An Immediately Invoked Function Expression runs as soon as it is defined and creates a private scope to avoid polluting the global namespace.<\/p>\n\n\n\n<p><strong>call, apply and bind<\/strong><br><code>call<\/code> invokes a function with a specified <code>this<\/code> and individual arguments; <code>apply<\/code> invokes with a specified <code>this<\/code> and an array of arguments; <code>bind<\/code> returns a new function with <code>this<\/code> (and optionally arguments) permanently bound.<\/p>\n\n\n\n<p><strong>this<\/strong><br>The value of <code>this<\/code> depends on how a function is called: object method, standalone function, arrow function (lexical <code>this<\/code>), constructor, or event handler.<\/p>\n\n\n\n<p><strong>Hoisting<\/strong><br>Function declarations and <code>var<\/code> declarations are hoisted to the top of their scope; <code>let<\/code> and <code>const<\/code> are not accessible before their declaration (temporal dead zone).<\/p>\n\n\n\n<p><strong>Temporal Dead Zone<\/strong><br>The period between entering scope and the <code>let<\/code>\/<code>const<\/code> declaration where accessing the variable throws a ReferenceError.<\/p>\n\n\n\n<p><strong>Event Propagation<\/strong><br>Events travel through three phases: capturing, target, and bubbling. Handlers can run during capture or bubble and can stop propagation.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Capturing Phase (Trickling):<\/strong>\u00a0The event starts from the\u00a0<code>window<\/code>\u00a0and travels downward through ancestors until it reaches the target element.<\/li>\n\n\n\n<li><strong>Target Phase:<\/strong>\u00a0The event reaches the actual element that was interacted with (e.g., a button).<\/li>\n\n\n\n<li><strong>Bubbling Phase:<\/strong>\u00a0The event &#8220;bubbles&#8221; back up from the target element through its ancestors to the\u00a0<code>window<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Function Types &#8211; Ways function can be defined<\/strong><br>Function declarations, function expressions, arrow functions, generator functions, and async functions each have different hoisting, scoping, and <code>this<\/code> behavior.<\/p>\n\n\n\n<p><strong>Array Methods<\/strong><br>Mutating methods: <code>push<\/code>, <code>pop<\/code>, <code>splice<\/code>; non\u2011mutating: <code>map<\/code>, <code>filter<\/code>, <code>reduce<\/code>, <code>slice<\/code>; modern helpers: <code>flat<\/code>, <code>at<\/code>, <code>with<\/code>.<\/p>\n\n\n\n<p><strong>DOM Methods<\/strong><br>Selection, creation, attribute manipulation, event handling, and utilities such as <code>getBoundingClientRect<\/code> and <code>scrollIntoView<\/code>.<\/p>\n\n\n\n<p><strong>Promises vs Observables and RxJS<\/strong><br>Promises produce a single future value and execute immediately; Observables produce streams, execute on subscription, and can be cancelled. RxJS operators like <code>map<\/code>, <code>filter<\/code>, <code>switchMap<\/code>, and <code>mergeMap<\/code> transform and control streams.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Tricky Coding Questions<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code>console.log(a); var a = 10; # undefined<\/code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><code>console.log(b); let b = 10; # <span style=\"background-color: initial; font-family: inherit; font-size: inherit; text-align: initial; color: initial;\">ReferenceError<\/span><\/code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(function() {\nvar x = y = 5;\n})();\nconsole.log(typeof x); # undefine\nconsole.log(typeof y); # number<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>for (var i = 0; i &lt; 3; i++) {\n  setTimeout(() => console.log(i), 1000);\n}\n# Output: 333<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>setTimeout(() => console.log('timeout'), 0);\nPromise.resolve().then(() => console.log('promise'));\n# Execution Order: Promise, setTimeout<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(0 == '0'); # true\nconsole.log(0 === '0'); # false\nconsole.log(10 + 2 + \"9\") # 129\nconsole.log(NAN === NAN) # false<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># How to access variable x (which is outside function) in function test?\nx = 10;\nfunction test(){\n  let x = 20;\n  console.log(this.x); # 10, accessed global variable x using this.\n  console.log(x); # 20, local variable x\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>let x=1;\ny = x++; # At this point: x=1, y=1\nconsole.log(x,y); # 2, 1 \n# Note: ++ operator first assigns existing value, then increases by one, so assign 1 to y and becomes 2 in next console.log statement)<\/code><\/pre>\n\n\n\n<p><strong>Hoisting and Scoping (The &#8220;Undefined&#8221; Trap):<\/strong><\/p>\n\n\n\n<p>What is the output of\u00a0<code>console.log(a); var a = 10;<\/code>?<\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<p><strong>Answer:<\/strong> <code>undefined<\/code>. The declaration <code>var a<\/code> is hoisted to the top and initialized as undefined, but the assignment 10 stays in place.<\/p>\n\n\n\n<p><strong>Temporal Dead Zone:<\/strong><\/p>\n\n\n\n<p>What happens if you run\u00a0<code>console.log(b); let b = 10;<\/code>?<\/p>\n\n\n\n<p>\u00a0<strong>Answer:<\/strong> <code>ReferenceError<\/code>. Unlike\u00a0<code>var<\/code>,\u00a0<code>let<\/code>\u00a0and\u00a0<code>const<\/code>\u00a0variables are hoisted but not initialized, placing them in a &#8220;temporal dead zone&#8221; until the declaration is reached.<\/p>\n\n\n\n<p><strong>Shadowing in Functions:<\/strong><\/p>\n\n\n\n<p>What is the output of this IIFE?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(function() {\nvar x = y = 5;\n})();\nconsole.log(typeof x);\nconsole.log(typeof y);<\/code><\/pre>\n\n\n\n<p><strong>Answer:<\/strong>\u00a0<code>undefined<\/code>\u00a0and\u00a0<code>number<\/code>.\u00a0<code>y<\/code>\u00a0becomes a global variable because it is not declared with\u00a0<code>var<\/code>, while\u00a0<code>x<\/code>\u00a0is local to the function scope.<\/p>\n\n\n\n<p><strong>The Loop Closure Quirk:<\/strong><\/p>\n\n\n\n<p>What does this code print?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (var i = 0; i &lt; 3; i++) {\n  setTimeout(() => console.log(i), 1000);\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Answer:<\/strong>\u00a0<code>3 3 3<\/code>. Because\u00a0<code>var<\/code>\u00a0is function-scoped, all callbacks share the same\u00a0<code>i<\/code>, which is\u00a0<code>3<\/code>\u00a0by the time they run. Using\u00a0<code>let<\/code>\u00a0fixes this by creating a new binding for each iteration.<\/p>\n\n\n\n<p><strong>Microtasks vs. Macrotasks &#8211; Asynchronous Execution:<\/strong><\/p>\n\n\n\n<p>In what order will &#8220;timeout&#8221; and &#8220;promise&#8221; print?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>setTimeout(() => console.log('timeout'), 0);\nPromise.resolve().then(() => console.log('promise'));\n<\/code><\/pre>\n\n\n\n<p><strong>Answer:<\/strong>\u00a0&#8220;promise&#8221; then &#8220;timeout&#8221;. The\u00a0<strong>Event Loop<\/strong>\u00a0prioritizes the microtask queue (Promises) over the macrotask queue (setTimeout).\u00a0<\/p>\n\n\n\n<p><strong>Type Coercion and Comparison<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Why does\u00a0<code>0 == '0'<\/code>\u00a0return\u00a0<code>true<\/code>, but\u00a0<code>0 === '0'<\/code>\u00a0return\u00a0<code>false<\/code>?<\/li>\n<\/ol>\n\n\n\n<p><strong>Answer:<\/strong>\u00a0<code>==<\/code>\u00a0performs\u00a0<strong>implicit type coercion<\/strong>\u00a0to make types match before comparing, while\u00a0<code>===<\/code>\u00a0checks both value and type.<\/p>\n\n\n\n<p>2. What is the result of\u00a0<code>10 + 2 + \"9\"<\/code>?<\/p>\n\n\n\n<p><strong>Answer:<\/strong>\u00a0<code>\"129\"<\/code>. JavaScript adds the first two numbers (<code>12<\/code>) and then concatenates the result with the string\u00a0<code>\"9\"<\/code>.<\/p>\n\n\n\n<p>3. What is the result of\u00a0<code>console.log(NaN === NaN)<\/code>?<\/p>\n\n\n\n<p><strong>Answer:<\/strong>\u00a0<code>false<\/code>.\u00a0<code>NaN<\/code>\u00a0(Not-a-Number) is the only value in JavaScript that is not equal to itself.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Detailed Notes and Examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">use strict<\/h3>\n\n\n\n<p>In JavaScript,\u00a0<strong><code>\"use strict\"<\/code><\/strong>\u00a0is\u00a0a directive introduced in ECMAScript 5 (ES5) that enables\u00a0<strong>Strict Mode<\/strong>, a restricted variant of JavaScript. It is designed to catch common coding mistakes, improve security, and allow JavaScript engines to perform better optimizations by eliminating some of the language&#8217;s &#8220;sloppy&#8221; behaviors.\u00a0<\/p>\n\n\n\n<p><strong>How to Use It<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Global Scope:<\/strong>\u00a0Place\u00a0<code>\"use strict\";<\/code>\u00a0at the very top of a script file to apply it to all code in that file.<\/li>\n\n\n\n<li><strong>Function Scope:<\/strong>\u00a0Place it inside the body of a specific function to apply strict mode only within that function&#8217;s scope.<\/li>\n\n\n\n<li><strong>Automatic Activation:<\/strong>\u00a0Strict mode is\u00a0<strong>automatically enabled<\/strong>\u00a0in\u00a0JavaScript Modules\u00a0(ES6 modules) and within\u00a0<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Strict_mode\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript Classes<\/a>.\u00a0<\/li>\n<\/ul>\n\n\n\n<p><strong>Key Changes and Rules<\/strong><\/p>\n\n\n\n<p>Strict mode changes previously accepted &#8220;bad syntax&#8221; into real errors:\u00a0<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>No Undeclared Variables:<\/strong>\u00a0You cannot use a variable without declaring it (e.g.,\u00a0<code>x = 10<\/code>\u00a0will throw a\u00a0<code>ReferenceError<\/code>\u00a0instead of creating a global variable).<\/li>\n\n\n\n<li><strong>Unique Parameter Names:<\/strong>\u00a0Functions cannot have duplicate parameter names (e.g.,\u00a0<code>function sum(a, a, b)<\/code>\u00a0is not allowed).<\/li>\n\n\n\n<li><strong>Secure\u00a0<code>this<\/code>:<\/strong>\u00a0In regular functions, if\u00a0<code>this<\/code>\u00a0is not set by the call, it defaults to\u00a0<code>undefined<\/code>\u00a0rather than the global object (like\u00a0<code>window<\/code>).<\/li>\n\n\n\n<li><strong>Restricted\u00a0<code>eval<\/code>:<\/strong>\u00a0Variables declared inside an\u00a0<code>eval()<\/code>\u00a0call no longer &#8220;leak&#8221; into the surrounding scope.<\/li>\n\n\n\n<li><strong>Read-Only Protections:<\/strong>\u00a0Writing to a read-only property or deleting an undeletable property (like\u00a0<code>Object.prototype<\/code>) throws an error instead of failing silently.<\/li>\n\n\n\n<li><strong>Forbidden Keywords:<\/strong>\u00a0Certain words reserved for future versions (e.g.,\u00a0<code>implements<\/code>,\u00a0<code>interface<\/code>,\u00a0<code>public<\/code>,\u00a0<code>private<\/code>) cannot be used as variable names.\u00a0<\/li>\n<\/ul>\n\n\n\n<p><strong>Why Use It?<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Bug Prevention:<\/strong>\u00a0It catches typos (like mistyped variable names) and structural mistakes early by throwing immediate errors.<\/li>\n\n\n\n<li><strong>Performance:<\/strong>\u00a0Engines can optimize strict mode code more effectively because it follows predictable rules.<\/li>\n\n\n\n<li><strong>Security:<\/strong>\u00a0It prevents access to potentially dangerous features and helps avoid accidental global variable pollution.<\/li>\n\n\n\n<li><strong>Modern Standards:<\/strong>\u00a0Using it ensures your code is compatible with modern JavaScript features like modules and classes, which rely on strict mode behavior by default.\u00a0<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Closures<\/h3>\n\n\n\n<p><strong>Explanation<\/strong><br>A closure is formed when an inner function retains access to variables from its outer function even after the outer function has finished executing. This enables private variables and persistent state.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function counter() {\n  let count = 0; \/\/ private\n  return function () {\n    count++;\n    return count;\n  };\n}\n\nconst increment = counter();\nconsole.log(increment()); \/\/ 1\nconsole.log(increment()); \/\/ 2\nconsole.log(increment()); \/\/ 3\n<\/code><\/pre>\n\n\n\n<p><strong>Pattern<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function createUser(name) {\n  let score = 0;\n  return {\n    getName() { return name; },\n    getScore() { return score; },\n    increment() { score++; return score; }\n  };\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">IIFE<\/h3>\n\n\n\n<p><strong>Explanation<\/strong><br>An IIFE executes immediately and creates a private scope, preventing variables from leaking into the global scope.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(function () {\n  const secret = 'hidden';\n  console.log('IIFE executed');\n})();\n<\/code><\/pre>\n\n\n\n<p><strong>Use cases<\/strong><br>Initialization code, module-like encapsulation in older environments, avoiding global collisions.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">call, apply and bind<\/h3>\n\n\n\n<p><strong>call<\/strong> \u2014 invoke immediately with individual arguments.<br><strong>apply<\/strong> \u2014 invoke immediately with arguments provided as an array.<br><strong>bind<\/strong> \u2014 return a new function with <code>this<\/code> and optional arguments bound; does not invoke.<\/p>\n\n\n\n<p><strong>Examples<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function greet(greeting, punctuation) {\n  console.log(`${greeting}, ${this.name}${punctuation}`);\n}\nconst user = { name: \"Lok\" };\ngreet.call(user, \"Hello\", \"!\"); \/\/ Hello, Lok!\n\nfunction sum(a, b, c) { return a + b + c; }\nconst numbers = &#91;1, 2, 3];\nconsole.log(sum.apply(null, numbers)); \/\/ 6\n\nconst module = { x: 42, getX() { return this.x; } };\nconst unboundGetX = module.getX;\nconst boundGetX = unboundGetX.bind(module);\nconsole.log(boundGetX()); \/\/ 42\n\nfunction multiply(a, b) { return a * b; }\nconst double = multiply.bind(null, 2);\nconsole.log(double(5)); \/\/ 10\n<\/code><\/pre>\n\n\n\n<p><strong>When to use<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>call<\/code> for immediate invocation with known args.<\/li>\n\n\n\n<li><code>apply<\/code> when args are in an array or array-like.<\/li>\n\n\n\n<li><code>bind<\/code> for callbacks or partial application where <code>this<\/code> must be preserved.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">this Keyword<\/h3>\n\n\n\n<p><strong>Contexts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Object method: <code>this<\/code> is the object.<\/li>\n\n\n\n<li>Standalone function: <code>this<\/code> is <code>undefined<\/code> in strict mode, global in non-strict.<\/li>\n\n\n\n<li>Arrow function: <code>this<\/code> is inherited from the lexical scope.<\/li>\n\n\n\n<li>Constructor: <code>this<\/code> is the new instance.<\/li>\n\n\n\n<li>Event handler: <code>this<\/code> is the element that received the event (unless arrow function used).<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const obj = { name: 'Lok', sayName() { console.log(this.name); } };\nobj.sayName(); \/\/ Lok\n\nfunction showThis() {\n  'use strict';\n  console.log(this); \/\/ undefined\n}\nshowThis();\n\nconst person = {\n  name: 'Lok',\n  regular: function() { console.log(this.name); }, \/\/ Lok\n  arrow: () =&gt; { console.log(this); } \/\/ lexical this\n};\nperson.regular();\nperson.arrow();\n\nfunction Person(name) { this.name = name; }\nconst p = new Person('Lok');\nconsole.log(p.name); \/\/ Lok\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Hoisting<\/h3>\n\n\n\n<p><strong>Explanation<\/strong><br>Hoisting moves declarations to the top of their scope at runtime. Function declarations and <code>var<\/code> declarations are hoisted; <code>let<\/code> and <code>const<\/code> are not accessible before their declaration.<\/p>\n\n\n\n<p><strong>Examples<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Function declaration hoisted\nsayHi(); \/\/ works\nfunction sayHi() { console.log('Hi'); }\n\n\/\/ var hoisted but undefined until assignment\nconsole.log(x); \/\/ undefined\nvar x = 5;\n\n\/\/ let\/const are not accessible before declaration\nconsole.log(y); \/\/ ReferenceError if uncommented\nlet y = 10;\n<\/code><\/pre>\n\n\n\n<p><strong>Key points<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Function declarations can be called before they appear in code.<\/li>\n\n\n\n<li><code>var<\/code> variables exist before their assignment but have value <code>undefined<\/code>.<\/li>\n\n\n\n<li><code>let<\/code> and <code>const<\/code> are in the temporal dead zone until declared.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Temporal Dead Zone<\/h3>\n\n\n\n<p><strong>Explanation<\/strong><br>The temporal dead zone (TDZ) is the time between entering a scope and the <code>let<\/code>\/<code>const<\/code> declaration. Accessing the variable in the TDZ throws a ReferenceError.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \/\/ console.log(a); \/\/ ReferenceError: Cannot access 'a' before initialization\n  let a = 2;\n  console.log(a); \/\/ 2\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Practical note<\/strong><br>Use <code>let<\/code> and <code>const<\/code> to avoid accidental use of uninitialized variables; be aware that they are not hoisted in the same usable way as <code>var<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Function Types<\/h3>\n\n\n\n<p><strong>Function Declaration<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function sum(a, b) { return a + b; } \/\/ hoisted\n<\/code><\/pre>\n\n\n\n<p><strong>Function Expression<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const sum = function(a, b) { return a + b; }; \/\/ not hoisted\nconst factorial = function fac(n) { return n &lt;= 1 ? 1 : n * fac(n - 1); };\n<\/code><\/pre>\n\n\n\n<p><strong>Arrow Function<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const multiply = (a, b) =&gt; a * b; \/\/ lexical this, no arguments object\n<\/code><\/pre>\n\n\n\n<p><strong>Generator Function<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function* numberGen() {\n  yield 1;\n  yield 2;\n}\nconst gen = numberGen();\nconsole.log(gen.next().value); \/\/ 1\n<\/code><\/pre>\n\n\n\n<p><strong>Async Function<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async function fetchData(url) {\n  const res = await fetch(url);\n  return res.json();\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Array Methods<\/h3>\n\n\n\n<p><strong>Mutating<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, 2, 3];\narr.push(4);\narr.pop();\narr.splice(1, 1, 99);\n<\/code><\/pre>\n\n\n\n<p><strong>Non\u2011mutating<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const nums = &#91;1, 2, 3, 4];\nconst doubled = nums.map(x =&gt; x * 2);\nconst evens = nums.filter(x =&gt; x % 2 === 0);\nconst sum = nums.reduce((acc, cur) =&gt; acc + cur, 0);\n<\/code><\/pre>\n\n\n\n<p><strong>Searching and reordering<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nums.includes(2);\nnums.find(x =&gt; x &gt; 2);\nnums.sort();\nnums.reverse();\nnums.join('-');\n<\/code><\/pre>\n\n\n\n<p><strong>Modern helpers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const nested = &#91;1, &#91;2, 3], &#91;4, &#91;5]]];\nnested.flat(2); \/\/ &#91;1,2,3,4,5]\n\nconst arr2 = &#91;1, 2, 3];\nconst updated = arr2.with(1, 99); \/\/ &#91;1,99,3] non-mutating\nconsole.log(arr2.at(-1)); \/\/ 3\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">DOM Methods<\/h3>\n\n\n\n<p><strong>Selection<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>document.getElementById('myId');\ndocument.querySelector('.myClass');\ndocument.querySelectorAll('p');\n<\/code><\/pre>\n\n\n\n<p><strong>Creation and manipulation<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const div = document.createElement('div');\ndiv.textContent = 'Hello';\ndocument.body.appendChild(div);\ndiv.remove();\n<\/code><\/pre>\n\n\n\n<p><strong>Attributes and classes<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>div.setAttribute('id', 'newDiv');\ndiv.classList.add('highlight');\ndiv.classList.toggle('active');\ndiv.getAttribute('id');\ndiv.removeAttribute('id');\n<\/code><\/pre>\n\n\n\n<p><strong>Events<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function onClick(e) { console.log('clicked', e.target); }\ndiv.addEventListener('click', onClick);\ndiv.removeEventListener('click', onClick);\n<\/code><\/pre>\n\n\n\n<p><strong>Utilities<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const rect = div.getBoundingClientRect();\ndiv.scrollIntoView({ behavior: 'smooth' });\ndiv.closest('section');\ndiv.matches('.highlight');\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Event Propagation<\/h3>\n\n\n\n<p><strong>Phases<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Capturing phase: event travels from the window down to the target.<\/li>\n\n\n\n<li>Target phase: event reaches the target element.<\/li>\n\n\n\n<li>Bubbling phase: event bubbles up from the target to the window.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=\"outer\"&gt;\n  &lt;button id=\"btn\"&gt;Click&lt;\/button&gt;\n&lt;\/div&gt;\n&lt;script&gt;\n  const outer = document.getElementById('outer');\n  const btn = document.getElementById('btn');\n\n  \/\/ Capture listener\n  outer.addEventListener('click', () =&gt; console.log('outer capture'), true);\n\n  \/\/ Bubble listener\n  outer.addEventListener('click', () =&gt; console.log('outer bubble'), false);\n\n  btn.addEventListener('click', (e) =&gt; {\n    console.log('button clicked');\n    \/\/ e.stopPropagation(); \/\/ stops further propagation\n  });\n&lt;\/script&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>Stopping propagation<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>e.stopPropagation()<\/code> prevents the event from reaching other listeners in later phases.<\/li>\n\n\n\n<li><code>e.stopImmediatePropagation()<\/code> prevents other listeners on the same element from running.<\/li>\n<\/ul>\n\n\n\n<p><strong>Use cases<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prevent duplicate handling when parent and child both listen for the same event.<\/li>\n\n\n\n<li>Implement delegated event handling by listening on a parent and using <code>event.target<\/code>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Promises vs Observables and RxJS<\/h3>\n\n\n\n<p><strong>Promise example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const myPromise = new Promise((resolve, reject) =&gt; {\n  setTimeout(() =&gt; resolve('done'), 1000);\n});\nmyPromise.then(result =&gt; console.log(result)).catch(err =&gt; console.error(err));\n<\/code><\/pre>\n\n\n\n<p><strong>Observable example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Observable } from 'rxjs';\n\nconst myObservable = new Observable(observer =&gt; {\n  observer.next('first');\n  setTimeout(() =&gt; {\n    observer.next('second');\n    observer.complete();\n  }, 500);\n});\n\nconst subscription = myObservable.subscribe({\n  next: value =&gt; console.log('value:', value),\n  error: err =&gt; console.error(err),\n  complete: () =&gt; console.log('completed')\n});\n\n\/\/ subscription.unsubscribe(); \/\/ cancel if needed\n<\/code><\/pre>\n\n\n\n<p><strong>BehaviorSubject example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { BehaviorSubject } from 'rxjs';\nconst loggedIn$ = new BehaviorSubject(false);\nloggedIn$.next(true);\nloggedIn$.subscribe(value =&gt; console.log('loggedIn:', value));\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">RxJS Operators Examples<\/h3>\n\n\n\n<p><strong>map<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { of } from 'rxjs';\nimport { map } from 'rxjs\/operators';\nof(1,2,3).pipe(map(x =&gt; x * 2)).subscribe(console.log); \/\/ 2,4,6\n<\/code><\/pre>\n\n\n\n<p><strong>filter<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { filter } from 'rxjs\/operators';\nof(1,2,3,4).pipe(filter(x =&gt; x % 2 === 0)).subscribe(console.log); \/\/ 2,4\n<\/code><\/pre>\n\n\n\n<p><strong>switchMap<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { fromEvent, of } from 'rxjs';\nimport { debounceTime, switchMap } from 'rxjs\/operators';\nconst input = document.querySelector('input');\nfromEvent(input, 'input').pipe(\n  debounceTime(300),\n  switchMap(e =&gt; of(`results for ${e.target.value}`))\n).subscribe(console.log);\n<\/code><\/pre>\n\n\n\n<p><strong>mergeMap<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { mergeMap } from 'rxjs\/operators';\nof(1,2,3).pipe(mergeMap(x =&gt; of(x * 10))).subscribe(console.log); \/\/ 10,20,30\n<\/code><\/pre>\n\n\n\n<p><strong>take and takeUntil<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { interval, Subject } from 'rxjs';\nimport { takeUntil } from 'rxjs\/operators';\nconst stop$ = new Subject();\ninterval(1000).pipe(takeUntil(stop$)).subscribe(console.log);\nsetTimeout(() =&gt; stop$.next(), 5000); \/\/ stop after 5s\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This revision adds hoisting, temporal dead zone, and event propagation to the earlier coverage of closures, IIFEs, function invocation methods, <code>this<\/code>, function types, array methods, DOM APIs, Promises, Observables, and RxJS operators. Use the summary for quick revision and the detailed sections for examples and deeper understanding.<\/p>\n\n\n\n<p>If you want, I can convert this into a printable one\u2011page cheat sheet, flashcards for study, or a small demo page with runnable examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary Quick Revision use strict In JavaScript,\u00a0&#8220;use strict&#8221;\u00a0is\u00a0a directive introduced in ECMAScript 5 (ES5) that enables\u00a0Strict Mode, a restricted variant of JavaScript. ClosuresA closure is a function that retains access&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[322,2],"tags":[335,333,100,332,334,336],"class_list":["post-1657","post","type-post","status-publish","format-standard","hentry","category-interview-questions","category-javascript","tag-important-js-concepts","tag-js-cheetsheet","tag-js-interview-question","tag-js-notes","tag-js-revision","tag-js-tricky-questions","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=1657"}],"version-history":[{"count":47,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1657\/revisions"}],"predecessor-version":[{"id":1704,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1657\/revisions\/1704"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=1657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=1657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=1657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}