<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by codesplash on Medium]]></title>
        <description><![CDATA[Stories by codesplash on Medium]]></description>
        <link>https://medium.com/@codesplash?source=rss-1878418870f3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*z6MQepiqfKQAKOS_O8NyWQ.jpeg</url>
            <title>Stories by codesplash on Medium</title>
            <link>https://medium.com/@codesplash?source=rss-1878418870f3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 18:28:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@codesplash/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Currying in JavaScript]]></title>
            <link>https://codesplash.medium.com/currying-in-javascript-57b5b52a983b?source=rss-1878418870f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/57b5b52a983b</guid>
            <category><![CDATA[mern-stack]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[mern]]></category>
            <dc:creator><![CDATA[codesplash]]></dc:creator>
            <pubDate>Sun, 30 Nov 2025 12:04:09 GMT</pubDate>
            <atom:updated>2025-11-30T12:05:38.728Z</atom:updated>
            <content:encoded><![CDATA[<h3>#9 JavaScript Series</h3><blockquote>Currying is a process that allows to transform a function with multiple arguments into a sequence of nested functions.</blockquote><pre>f(a, b, c) ⇒ f(a)(b)(c)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6osN3PlfGxvrvpaK_njpfg.png" /></figure><p>We simply wrap a function inside another. The parent function takes the first argument and returns another function that takes the next argument. This keeps on repeating till the number of arguments ends. The function that receives the last argument returns the expected result.</p><h3>⭐ Simple Function</h3><pre>const add = (a, b, c) =&gt; {<br>  return a + b + c;<br>};<br><br>console.log(add(1, 2, 3)); // O/P = 6</pre><h3>⭐ Curried Function</h3><pre>const addCurry = (a) =&gt; {       // it takes one argument<br>  return (b) =&gt; {<br>    return (c) =&gt; {<br>      return a + b + c;<br>    };<br>  };<br>};<br><br>console.log(addCurry(1)(2)(3)); // O/P = 6</pre><p>If we do the below code referring the above code:</p><pre>const sum1 = addCurry(1);<br>console.log(sum1);</pre><h3>What will be the output:</h3><p>O/P will be:</p><pre>(b) =&gt; {<br>  return (c) =&gt; {<br>    return a + b + c;<br>  };<br>}</pre><p>Since we passed only one argument, it returns the other two functions expecting the remaining 2 arguments.</p><h3>⭐ Advantages of Currying</h3><h3>1️⃣ Reduces chance of error</h3><p>By dividing the logic into smaller &amp; manageable functions that handle one responsibility.</p><h3>2️⃣ Avoids passing same variable multiple times</h3><p>Example:</p><pre>function mult(x) {<br>  return function (y) {<br>    return x * y;<br>  };<br>}<br><br>const multBy10 = mult(10); // partially applied<br>console.log(multBy10(5)); // O/P = 50<br>console.log(multBy10(6)); // O/P = 60</pre><p>Now, <strong><em>multBy10 </em></strong>behaves as a completely new function &amp; can be reused.</p><h3>Uses of Currying:</h3><h3>⭐ 1. Implement DoOprtn (+ — / * ) (a)(b)</h3><p>Any operation passed should give that operation:</p><pre>function DoOprtn(op) {<br>  return (a) =&gt; {<br>    return (b) =&gt; {<br>      if (op === &quot;+&quot;) return a + b;<br>      else if (op === &quot;-&quot;) return a - b;<br>      else if (op === &quot;*&quot;) return a * b;<br>      else return a / b;<br>    };<br>  };<br>}<br><br>const add = DoOprtn(&quot;+&quot;)(5)(3);<br>const divide = DoOprtn(&quot;/&quot;)(9)(3);<br>console.log(add, divide); // O/P = 8, 3</pre><h3>⭐ 2. Currying Function That Takes Infinite Arguments (e.g. Perform +)</h3><p>We can’t do direct currying, we have to use recursion.</p><pre>const sum = (a) =&gt; {<br>  return (b) =&gt; {<br>    if (b) {<br>      return sum(a + b);<br>    } else {<br>      return a;<br>    }<br>  };<br>};<br><br>console.log(sum(1)(2)(3)(4)(5)()); // O/P = 15</pre><h3>One-liner version:</h3><pre>const sum = a =&gt; b =&gt; (b ? sum(a + b) : a);<br>console.log(sum(1)(2)(3)(4)(5)()); // O/P = 15</pre><p><strong>Note:</strong><br> The last empty argument <strong>() is required</strong> because this pattern is using <strong>curried function</strong> to accumulate values until you <em>stop</em> by passing a falsy value (here: undefined).</p><h3>⭐ Currying vs Partial Application</h3><h3>Partial application:</h3><p>Fixing some arguments of a function while leaving the other arguments open.</p><p>Example:</p><pre>function addOne(one) {<br>  return function (a, b, c) {<br>    return a + b + c + one;<br>  };<br>}<br><br>const addMeWith1= addOne(1);        // Fixed 1<br>console.log(addMeWith1(5, 4, 3));   // O/P = 13<br>console.log(addMeWith1(1, 2, 3));   // O/P = 7<br>console.log(addMeWith1(2, 4, 8));   // O/P = 15</pre><h3>NOTE:</h3><p>Partial application is <strong>not</strong> currying.</p><ul><li>In partial application → it expects 3 arguments &amp; has only <strong>2 nested functions</strong></li><li>In currying → it expects 3 arguments with <strong>3 nested functions</strong></li></ul><h3>⭐ DOM Manipulation Using Currying</h3><pre>&lt;h1 id=&quot;header&quot;&gt;update me&lt;/h1&gt;</pre><p>Now update it using currying:</p><h3>Approach</h3><ol><li>Take first argument &quot;ID&quot; which needs to be updated → can be reused</li><li>Second function &quot;content&quot; → dynamic content</li></ol><h3>Code:</h3><pre>function updateHeader(ID) {<br>  return function (content) {<br>    document.querySelector(&quot;#&quot; + ID).textContent = content;<br>  };<br>}<br><br>const updateEachTime = updateHeader(&quot;header&quot;);<br>updateEachTime(&quot;update me is now Deepjyoti Das&quot;);</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57b5b52a983b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Closures in JavaScript]]></title>
            <link>https://codesplash.medium.com/closures-in-javascript-516c0bb044f4?source=rss-1878418870f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/516c0bb044f4</guid>
            <category><![CDATA[closure-in-javascript]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[interview-preparation]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[codesplash]]></dc:creator>
            <pubDate>Sun, 21 Apr 2024 18:58:25 GMT</pubDate>
            <atom:updated>2024-04-21T18:58:25.571Z</atom:updated>
            <content:encoded><![CDATA[<h3>#8 JavaScript Series</h3><blockquote>A closure is the combination of a function bundled together with references to its surrounding state (the lexical environment).</blockquote><p>A closure gives access to an outer function’s scope from an inner function. In JavaScript, closures are created every lime a function is created, at function creation time.</p><pre>function x(){<br>     var a = 7;<br>     function y(){<br>         console.log(a); <br>     } <br>     return y;<br>}<br>var z = x(); <br>z(); //0P=7</pre><p>Here, y function is returned from the outer function xbefore being executed. In some languages, the local variables within a function exist for just the duration of that function’s execution. But in JavaScript due to function forming closures, a variable is still accessible once x finished executing.<br>The instance of y maintains a reference to its lexical environment, within which the variable a exists. For this reason, when z is invoked, the variable a remains available for use, and a=7 is passed to console.log().</p><h3>Uses of closures:</h3><blockquote><strong>Encapsulation and Information Hiding:</strong> They allow for private variables and methods, enhancing data encapsulation and hiding implementation details.<br><strong>Module Design Patterns:</strong> Closures are fundamental for implementing module design patterns, enabling the creation of private and public methods and variables within a module.<br><strong>Currying</strong>: Functional programming techniques like currying heavily rely on closures to create functions that remember their lexical environments.<br><strong>Functions like once</strong>: Closures are used to create functions like once, which ensure that a function is executed only once, caching its result for subsequent calls.<br><strong>setTimeout</strong>: Closures are utilized with setTimeout to maintain access to the surrounding lexical scope even after the outer function has finished execution.<br><strong>Memoization</strong>: Closures are used in memoization techniques to cache the results of expensive function calls, improving performance by avoiding redundant computations.</blockquote><blockquote>Q) Why are closures important?<br>=&gt; They allow for information hiding and encapsulation. They also allow for private variables and methods, which can be useful in certain scenarios.</blockquote><blockquote>Q) Print output.</blockquote><pre>function out(){<br>     var a = 10;<br>     function in(){ <br>         console.log(a);<br>     }<br>     return in; <br>} <br>out()();</pre><pre>10</pre><blockquote>Q) Does closures garbage collected?<br>=&gt; Closures themselves are not directly garbage collected, but the variables they capture are subject to garbage collection when there are no more references to them. Gabage collected but smartly.</blockquote><pre>function a(){<br>    var x=10;<br>    var z=20;<br>    return function b(){ <br>        console.log(x);<br>     }<br>}<br>var y = a();</pre><blockquote>Here z is not being used by the closure function b, so after returning b, z will be smartly garbage collected but not x.</blockquote><h3>Disadvantages of closures:</h3><blockquote><strong>Memory Consumption</strong>: Closures can consume memory, especially if many closures are created, potentially leading to increased memory usage.<br><strong>Memory Leaks</strong>: Improper handling of closures can result in memory leaks, where variables captured by closures are not released properly even when they’re no longer needed, leading to memory bloat over time. Proper management of closures and their associated variables is essential to avoid memory leaks.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=516c0bb044f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scope, Scope chain, and Lexical Environment]]></title>
            <link>https://codesplash.medium.com/scope-scope-chain-and-lexical-environment-1cbf9d41e8b8?source=rss-1878418870f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cbf9d41e8b8</guid>
            <category><![CDATA[scope-chains]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[codesplash]]></dc:creator>
            <pubDate>Sat, 20 Apr 2024 19:27:41 GMT</pubDate>
            <atom:updated>2024-04-20T19:27:41.390Z</atom:updated>
            <content:encoded><![CDATA[<h3>#7 JavaScript Series</h3><h3>Scope:</h3><blockquote>It refers to the region of the code where a particular variable or a function is accessible. It defines the context in which variables and functions are declared and accessed.</blockquote><h3>Types:</h3><blockquote><strong>Global Scope:</strong> variables declared outside any function or block, which are accessible throughout the entire Program.</blockquote><blockquote><strong>Local scope:</strong> variables declared inside a function or block, which are accessible within that function or block.</blockquote><pre>let num = 5; // Global Scope variable<br><br>function add(a, b) {<br>    let e = 3; // Local scope variable<br>    return a + b + e;<br>}<br><br>console.log(num);<br>console.log(add(5, 6)); </pre><h3>Lexical Environment:</h3><blockquote>It is the local memory along with the lexical environment of its parent.</blockquote><blockquote>A lexical environment is a structure that holds the association between identifiers (like variable and function names) and their corresponding variables or functions within the code. It also keeps track of the scope chain.</blockquote><p><strong>Components</strong>:</p><ol><li><strong>Environment Record</strong>: Stores the identifiers and their values.</li><li><strong>Reference to outer environment</strong>: Links to the lexical environment of the outer scope, forming the scope chain</li></ol><pre>var a=3; <br>function print(){<br> console.log(a);<br>} <br>print(); // Output: 3</pre><p>Here, a &amp; print are lexically present in the global scope. when print() will be called, print will search for a for console print but, it will not find in its lexical environment, so, it will search to its outer scope i.e. global scope. If in the global scope, there is ‘a’ variable declared it will print that.</p><p>Let’s understand lexical environment &amp; scope chain with following code and call stack:</p><pre>var b=10; // b is on global scope<br><br>function a(){ // a is on global scope<br>  c();<br>  function c(){ // c is lexically present inside a<br>     console.log(b);<br>  }<br>}<br><br>a();</pre><figure><img alt="call stack" src="https://cdn-images-1.medium.com/max/539/1*9uqrDtuVjGUVsj_1aQO5rA.png" /></figure><blockquote>In the call-stack, b anda() are in the global Lexical space. a() has the c() as a part of Lexical environment. c()has nothing in its lexical scope. When a()is called it also calls c() which will find b in the order of following lexical scope<br> c() → a() → GEC(Global Eexcution Context)</blockquote><h3>Scope chain:</h3><blockquote>The scope chain is a series of nested lexical environments. It determines the order in which the JavaScript looks up variables and functions.</blockquote><blockquote><strong>Working: </strong><br>When a variable is accessed, the engine first looks in the current scope’s environment record. If not found, it looks in the outer environment referenced by the scope chain. This process continues until the variable is found or the global scope is reached.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1cbf9d41e8b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Arrow Function vs Normal Function]]></title>
            <link>https://codesplash.medium.com/arrow-function-vs-normal-function-3068599d2623?source=rss-1878418870f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/3068599d2623</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[codesplash]]></dc:creator>
            <pubDate>Sat, 20 Apr 2024 18:47:01 GMT</pubDate>
            <atom:updated>2024-04-20T18:47:01.087Z</atom:updated>
            <content:encoded><![CDATA[<h3>#6 JavaScript Series</h3><h3>Syntax</h3><p>Normal Function</p><pre>function square (num) {<br>  return num*num;<br>}</pre><p>Arrow Function</p><pre>const square = (num) =&gt; {<br>  return num*num;<br>}<br><br>// If there is only one argument, parentheses are optional<br>// If there is only one operation performed, &#39;return&#39; and &#39;{}&#39; are optional<br><br>const square = num =&gt; num * num;</pre><h3>‘arguments ’ Keyword</h3><p>Normal Function</p><pre>function hello () {<br>  console.log(arguments);<br>}<br>hello (&quot;Deep&quot;); // Output = Deep</pre><p>Arrow Function</p><pre>const hello = () =&gt; console.log(arguments);<br><br>hello (&quot;Deep&quot;); // Output = Error: arguments is not defined</pre><h3>‘This’ Keyword</h3><pre>let user = {<br>      name: &quot;Deep&quot;<br>      f1:()=&gt; console.log(this.name) // with Arrow function,<br>//this points to the global object<br><br>      f2(){ console.log(this.name) } // with Normal function, <br>//this points to the caller function                                   <br>}<br>user.f1(); // Output: empty field<br>user.f2(); //Output:Deep</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3068599d2623" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Var vs Let vs Const in JavaScript]]></title>
            <link>https://codesplash.medium.com/var-vs-let-vs-const-in-javascript-6e80cb1f0c57?source=rss-1878418870f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e80cb1f0c57</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[codesplash]]></dc:creator>
            <pubDate>Fri, 19 Apr 2024 17:59:06 GMT</pubDate>
            <atom:updated>2024-04-19T17:59:06.198Z</atom:updated>
            <content:encoded><![CDATA[<h3>#5 JavaScript Series</h3><h3>On the Basis of Scope:</h3><blockquote>var</blockquote><blockquote><strong>var</strong> is function scoped &amp; Hoisted to the top of its function or global scope.</blockquote><blockquote><strong>Can</strong> be reassigned and redeclared within its scope.</blockquote><blockquote><strong>If </strong>declared within a block (e.g., if statement), it’s still visible outside the block.</blockquote><p>Example:</p><pre>function example() {<br>    if (true) {<br>        var x = 10;<br>        console.log(x); // Outputs: 10<br>    }<br>    console.log(x); // Outputs: 10<br>}<br>example();</pre><blockquote>let</blockquote><blockquote><strong>Scope: Block-scoped</strong></blockquote><blockquote><strong>Hoisted</strong> to the top of its block, but not initialized.</blockquote><blockquote><strong>Cannot </strong>be redeclared within the same scope.</blockquote><blockquote><strong>Can</strong> be reassigned within its scope.</blockquote><p>Example:</p><pre>function example() {<br>    if (true) {<br>        let y = 20;<br>        console.log(y); // Outputs: 20<br>    }<br>    console.log(y); // Throws ReferenceError: y is not defined<br>}<br>example();</pre><blockquote>const</blockquote><blockquote><strong>Scope: Block-scoped</strong></blockquote><blockquote><strong>Hoisted</strong> to the top of its block, but not initialized.</blockquote><blockquote><strong>Cannot</strong> be reassigned after declaration.</blockquote><p>Example:</p><pre>function example() {<br>    const z = 30;<br>    z = 40; // Throws TypeError: Assignment to constant variable<br>}<br>example();</pre><blockquote>While a variable declared with <strong>const</strong> cannot be reassigned to a new value, if it holds a reference to an object, the content of that object can be modified.</blockquote><p>Example:</p><pre>const arr = [1, 2, 3];<br>arr.push(4);<br>console.log(arr); // Outputs: [1, 2, 3, 4]</pre><h3>Variable Shadowing:</h3><blockquote>Variable shadowing occurs when a variable declared within a certain scope has the same name as a variable declared in an outer scope, thereby hiding the outer variable within that scope.</blockquote><p>Example:</p><pre>function example() {<br>    let a = 10;<br>    if (true) {<br>        let a = 20; // Inner &#39;a&#39; shadows outer &#39;a&#39;<br>        console.log(a); // Outputs: 20<br>    }<br>    console.log(a); // Outputs: 10 (outer &#39;a&#39; remains unchanged)<br>}<br>example();</pre><blockquote>If both variables were declared using <strong>var </strong>instead of <strong>let</strong>, due to function scoping, both would print &quot;20&quot; since the inner &#39;a&#39; would change the outer &#39;a&#39;, affecting both within the function scope.</blockquote><h3>Illegal shadowing:</h3><blockquote>Making <strong><em>let</em></strong> variables <strong><em>var.</em></strong></blockquote><blockquote>Making <strong><em>const</em></strong> variables <strong><em>var</em></strong>.</blockquote><pre>function example() {<br>    let a = 20;<br>    const b = 40;<br>    if (true) {<br>        var a = 30; // Illegal shadowing of &#39;a&#39;<br>        var b = 50; // Illegal shadowing of &#39;b&#39;<br>    }<br>}<br>example();</pre><h3>Declaration:</h3><blockquote><strong>var </strong>can be redeclared within the same scope, with the last declaration taking precedence.</blockquote><blockquote><strong>let</strong> and <strong>const </strong>cannot be redeclared within the same scope; attempting to do so will result in a syntax error.</blockquote><p>Example:</p><pre>var a = 20;<br>var a = 50; // Valid: &#39;a&#39; is redeclared<br><br>let b = 30;<br>let b = 40; // SyntaxError: Identifier &#39;b&#39; has already been declared<br><br>const c = 40;<br>const c = 50; // SyntaxError: Identifier &#39;c&#39; has already been declared</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e80cb1f0c57" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[IIFE (Immediately-Invoked Function Expression in JavaScript)]]></title>
            <link>https://codesplash.medium.com/iife-immediately-invoked-function-expression-in-javascript-67393b30d856?source=rss-1878418870f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/67393b30d856</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[codesplash]]></dc:creator>
            <pubDate>Wed, 17 Apr 2024 06:46:48 GMT</pubDate>
            <atom:updated>2024-04-17T06:49:59.805Z</atom:updated>
            <content:encoded><![CDATA[<h3>#4 JavaScript Series</h3><blockquote>It is a JavaScript function that runs as soon as it is defined. It’s a design pattern also known as a self-executing anonymous function.</blockquote><p>Here’s an example:</p><pre>(<br>function () {<br> console.log(&quot;Hello&quot;);<br>}<br>)();</pre><p>You can also assign it to a variable for later use.</p><p>Usage:<br>- Avoids hoisting issues.<br>- Prevents global namespace pollution.<br>- Useful for creating modules.</p><p>@ Guess the Output</p><pre>(<br> function (x) {<br>     return (function(y){<br>         console.log(x+y);<br>     }<br>     )(2)<br> }<br>)(1);</pre><pre>Output: 3<br><br>Explanation:<br>--------------<br>The outermost parentheses are used to wrap the entire function expression. This is a common practice to ensure the function is treated as an expression and not a declaration.<br><br>The outer function is immediately invoked with the argument 1. So, x inside the function becomes 1.<br><br>Inside the outer function, there&#39;s another immediately invoked function expression (function(y) { console.log(x + y); })(2). This inner function is immediately invoked with the argument 2.<br><br>Inside the inner function, y becomes 2. Then it calculates x + y, where x is from the outer function and y is from the inner function. So, x + y becomes 1 + 2, which is 3.<br><br>Finally, console.log(x + y) prints 3.</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67393b30d856" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[First Class Function:]]></title>
            <link>https://codesplash.medium.com/first-class-function-90db4777aba8?source=rss-1878418870f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/90db4777aba8</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[codesplash]]></dc:creator>
            <pubDate>Wed, 17 Apr 2024 06:31:47 GMT</pubDate>
            <atom:updated>2024-04-17T06:31:47.216Z</atom:updated>
            <content:encoded><![CDATA[<h3>#3 Javascript Series</h3><blockquote>In JavaScript, functions are treated as first-class citizens, which means that functions can be used like any other value, such as being passed as an argument, assigned to a variable, or returned as a result of a function.</blockquote><h3>A function assignment to a variable</h3><pre>function add (a,b){<br>    return a+b;<br>}<br>const sum = add;<br>console.log(sum(2,3));</pre><pre>Output: 5</pre><h3>A function passed as an argument to another function</h3><pre>function greet (name, fn){<br>     fn (name);<br>}<br>greet (&quot;Deep&quot;, (nm)=&gt;{<br>     console.log(`Hello, ${nm}`);<br>});</pre><pre>Output: Hello Deep</pre><h3>A function returning function as a result</h3><pre>function HOF(){<br>    return function(){<br>         console.log(&quot;Hello&quot;);<br>    }<br>}<br>const LOF = HOF();<br>LOF();</pre><pre>Output: Hello</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90db4777aba8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hoisting in JavaScript]]></title>
            <link>https://codesplash.medium.com/hoisting-in-javascript-894a925b0fd3?source=rss-1878418870f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/894a925b0fd3</guid>
            <category><![CDATA[hoisting]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[codesplash]]></dc:creator>
            <pubDate>Tue, 16 Apr 2024 19:46:58 GMT</pubDate>
            <atom:updated>2024-04-16T19:52:46.839Z</atom:updated>
            <content:encoded><![CDATA[<h3>#2 JavaScript Series</h3><blockquote>“Hoisting is a JavaScript mechanism where all variables and function declarations are moved to the top of their scope before code execution.”</blockquote><p>If we print variable before initialization, for<strong><em> /var/let/const</em></strong> it will work differently.</p><h3>Hoisting with “var”:</h3><pre>console.log(x);<br>var x = 7;</pre><blockquote>During memory allocation</blockquote><pre>x: undefined</pre><blockquote>During code execution</blockquote><p>When execution starts, it will encounter first x need to be printed. Since, x has undefined<strong> </strong>value, so it prints <strong>undefined</strong>, thereafter X will be assigned value 7.</p><h3>Hoisting with “let” &amp; “const”:</h3><pre>console.log(x);<br>let x = 7;<br><br>console.log(y);<br>let y= 9;</pre><blockquote><em>With let</em><strong><em> &amp; </em></strong>const <strong><em>Uncaught Reference Error</em></strong><em> is occurred</em></blockquote><blockquote>let &amp; const are hoisted in <strong>Temporal Dead-Zone</strong> (The period between declaration &amp; initialization of variable of let &amp; const)</blockquote><p><strong>@Guess the output</strong></p><pre>function abc (){<br>    console.log (a); <br>    console.log (b); <br>    console.log (c);<br>    var a = 5; <br>    let b = 6;<br>    const c = 7; <br>}<br>abc();</pre><pre>Undefined<br>Reference Error: can&#39;t access &#39;b&#39; before initialization</pre><h3>Hoisting with function (Named function):</h3><pre>Hello ();<br>function Hello()<br>    console.log (&quot;Hello&quot;);<br>}</pre><pre>Hello</pre><p>During memory allocation phase <strong>Hello</strong> is completely assigned the whole function body. So, it works normally with Named function.</p><h3>Hoisting with Arrow function:</h3><pre>Hello ();<br>const Hello=()=&gt;{<br>    console.log (&quot;Hello&quot;);<br>}</pre><pre>undefined</pre><p>It now behaves as a variable not as a function. So, it will print undefined. During <strong>memory allocation phase</strong> Hello will be filled with <strong>undefined</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=894a925b0fd3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fruit Into Baskets | C++ | Sliding Window | Map | Easy explanation | Leetcode]]></title>
            <link>https://codesplash.medium.com/fruit-into-baskets-c-sliding-window-map-easy-explanation-leetcode-aed0c83c8d27?source=rss-1878418870f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/aed0c83c8d27</guid>
            <category><![CDATA[sliding-windows]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[cpp]]></category>
            <dc:creator><![CDATA[codesplash]]></dc:creator>
            <pubDate>Sat, 30 Mar 2024 11:20:57 GMT</pubDate>
            <atom:updated>2024-03-30T11:20:57.431Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="fruit-into-baskets" src="https://cdn-images-1.medium.com/max/480/1*CSi8HPLu7tFEATIr5aLkOA.jpeg" /></figure><h3>Intuition</h3><blockquote><em>We will keep plucking one fruit while traversing from left to right and put it into one type of basket (map) between the two baskets. If at any point we need more than two baskets, we will start removing that type of fruit until one basket is freed up.</em></blockquote><h3>Why Sliding Window?</h3><blockquote><em>Maximum fruit insert in k (2 size basket) and it can be from any subarray.</em></blockquote><h3>Approach</h3><blockquote><em>=&gt; Take </em><em>map as two basket<br>=&gt; </em><em>i &amp; </em><em>j for traversing<br>=&gt; we will keep moving with </em><em>j for plucking &amp; </em><em>i for removing fruits<br>from basket(</em><em>map)<br>=&gt; If basket size is </em><em>&lt; 2, count the maximum fruit in the basket with </em><em>j-i+1</em></blockquote><h3>Complexity</h3><blockquote><em>Time complexity: O (N)</em></blockquote><blockquote><em>Space complexity: O (1)-&gt; Only keeping 2 types of fruit in the map at any point of time.</em></blockquote><h3>Code</h3><pre>class Solution {<br>public:<br>    int totalFruit(vector&lt;int&gt;&amp; fruits) {<br>        map&lt;int,int&gt; m;<br>        int i=0,j=0;<br>        int ans=0;<br>        while(j&lt;fruits.size()){<br>            m[fruits[j]]++;<br>            while(m.size()&gt;2){<br>                m[fruits[i]]--;<br>                if(m[fruits[i]]==0)m.erase(fruits[i]);<br>                i++;<br>            }<br>            ans=max(ans,j-i+1);<br>            j++;<br>        }<br>        return ans;<br>    }<br>};</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aed0c83c8d27" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Working | Execution Context of JavaScript]]></title>
            <link>https://codesplash.medium.com/javascript-working-4f5bdf3ae9b7?source=rss-1878418870f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f5bdf3ae9b7</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[javascript-interview]]></category>
            <dc:creator><![CDATA[codesplash]]></dc:creator>
            <pubDate>Tue, 27 Feb 2024 10:55:44 GMT</pubDate>
            <atom:updated>2024-02-27T10:58:44.719Z</atom:updated>
            <content:encoded><![CDATA[<h3>#1 Javascript Series Started</h3><h3>Execution context</h3><blockquote><strong>“Everything in Javascript happens inside an execution context”</strong></blockquote><blockquote>It can be thought of as an environment where the Javascript code is evaluated and executed.</blockquote><h3><strong>Two components of JavaScript:</strong></h3><blockquote>Variable Environment (Memory)<br>Thread of Execution (Code)</blockquote><figure><img alt="two component of javascript execution context" src="https://cdn-images-1.medium.com/max/1024/1*_qA5bCp8Ck6EWErFWq6MaA.png" /><figcaption>two components of javascript execution context</figcaption></figure><h3><strong>Types of Execution Context:</strong></h3><ol><li><strong>Global Execution Context (GEC):<br></strong>The first environment that is created on the first execution of JS code. In the browser, the entire web page is a global context.</li></ol><pre>var a=2;<br>console.log(a);</pre><ol><li><strong>Function Execution content (FEC):<br></strong>Every time a function is called, a new execution context is created for that function. <br>This context includes the function’s Parameters, local variables, and a reference to the outer execution context, which forms a chain called “scope chain” and is pushed onto “call Stack. Once the function execution is completed then that function context is popped off.</li></ol><h3>2-Phase Code Execution</h3><p>Take the below code as an example to understand</p><pre>let a=5;<br>function sum(a,b){<br>    return a+b;<br>}<br>console.log(sum(a,4));</pre><blockquote>Phase-1: Creation Phase (Memory-Code)</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DY-sdj5Y296gSUcnJ0qxpw.png" /><figcaption>Javascript code-memory creation phase</figcaption></figure><p>In this phase-1 javascript allocates variables with <em>undefined </em>and functions with the entire code. In the call stack, GEC is pushed.</p><blockquote>Phase-2: Code Execution Phase</blockquote><p>Now, thread execution happens and all the variables get their actual values which were assigned to them. As a function is invoked, a brand new execution context for that function is created in the code part and this cycle repeats until the call stack is empty.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ipRSvfw8zl1UD6GTYtBIbw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XnPre2g_-SQ6Ro0aukvAng.png" /></figure><p>At last, the console will print 9. The GEC will also be popped off from the stack.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f5bdf3ae9b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>