<?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"><channel><title><![CDATA[functions in javascript]]></title><description><![CDATA[functions in javascript]]></description><link>https://functions-in-javascript-and-react.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Sep 2026 13:36:05 GMT</lastBuildDate><atom:link href="https://functions-in-javascript-and-react.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Mastering JavaScript Functions: From Basics to Advanced with Real-World Applications]]></title><description><![CDATA[Functions are the heart of JavaScript and the building blocks of modern applications. Whether you're just starting out or aiming to become a React developer at a top company, understanding how functions work—from scratch to advanced—is essential.
In ...]]></description><link>https://functions-in-javascript-and-react.hashnode.dev/mastering-javascript-functions-from-basics-to-advanced-with-real-world-applications</link><guid isPermaLink="true">https://functions-in-javascript-and-react.hashnode.dev/mastering-javascript-functions-from-basics-to-advanced-with-real-world-applications</guid><category><![CDATA[Functional Programming]]></category><category><![CDATA[functions]]></category><category><![CDATA[function in js]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[React Native]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[krishan]]></dc:creator><pubDate>Sun, 04 May 2025 10:44:02 GMT</pubDate><content:encoded><![CDATA[<p>Functions are the heart of JavaScript and the building blocks of modern applications. Whether you're just starting out or aiming to become a React developer at a top company, understanding how functions work—<strong>from scratch to advanced</strong>—is essential.</p>
<p>In this guide, we’ll cover JavaScript functions step-by-step, from beginner to pro, with real-world usage, React integrations, and internal concepts.</p>
<hr />
<h2 id="heading-step-1-what-is-a-function">🔹 Step 1: What is a Function?</h2>
<p>A function is a reusable block of code designed to perform a particular task.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-string">`Hello, <span class="hljs-subst">${name}</span>`</span>;
}

<span class="hljs-built_in">console</span>.log(greet(<span class="hljs-string">'Alice'</span>)); <span class="hljs-comment">// Hello, Alice</span>
</code></pre>
<p>📌 <strong>Why use functions?</strong></p>
<ul>
<li><p>Reusability</p>
</li>
<li><p>Better structure</p>
</li>
<li><p>DRY (Don’t Repeat Yourself)</p>
</li>
</ul>
<hr />
<h2 id="heading-step-2-function-expressions-amp-arrow-functions">🔹 Step 2: Function Expressions &amp; Arrow Functions</h2>
<p><strong>Function Expression:</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> add = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-keyword">return</span> a + b;
};
</code></pre>
<p><strong>Arrow Function:</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> multiply = <span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a * b;
</code></pre>
<p>✅ Arrow functions are cleaner and great for callbacks, but they do <strong>not</strong> bind their own <code>this</code>.</p>
<hr />
<h2 id="heading-step-3-higher-order-functions-hofs">🔹 Step 3: Higher-Order Functions (HOFs)</h2>
<p>A function that takes another function as an argument or returns one.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">withLogger</span>(<span class="hljs-params">fn</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Arguments:"</span>, args);
    <span class="hljs-keyword">return</span> fn(...args);
  };
}

<span class="hljs-keyword">const</span> add = <span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a + b;
<span class="hljs-keyword">const</span> loggedAdd = withLogger(add);

<span class="hljs-built_in">console</span>.log(loggedAdd(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>)); <span class="hljs-comment">// Logs args, returns 5</span>
</code></pre>
<p>📌 Used in:</p>
<ul>
<li><p><a target="_blank" href="http://Array.map"><code>Array.map</code></a><code>()</code>, <code>Array.filter()</code></p>
</li>
<li><p>Middleware in Redux</p>
</li>
<li><p>Enhancing components in React</p>
</li>
</ul>
<hr />
<h2 id="heading-step-4-closures-the-secret-sauce">🔹 Step 4: Closures (The Secret Sauce 🔐)</h2>
<p>Closures give access to an outer function’s scope even after the outer function has returned.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">counter</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    count++;
    <span class="hljs-keyword">return</span> count;
  };
}

<span class="hljs-keyword">const</span> increment = counter();
<span class="hljs-built_in">console</span>.log(increment()); <span class="hljs-comment">// 1</span>
<span class="hljs-built_in">console</span>.log(increment()); <span class="hljs-comment">// 2</span>
</code></pre>
<p>✅ <strong>Used in React:</strong> for state updates like:</p>
<pre><code class="lang-js">setCount(<span class="hljs-function"><span class="hljs-params">prev</span> =&gt;</span> prev + <span class="hljs-number">1</span>);
</code></pre>
<hr />
<h2 id="heading-step-5-function-currying">🔹 Step 5: Function Currying</h2>
<p>Transforming a function with multiple arguments into a sequence of functions.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiply</span>(<span class="hljs-params">a</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">b</span>) </span>{
    <span class="hljs-keyword">return</span> a * b;
  };
}

<span class="hljs-keyword">const</span> double = multiply(<span class="hljs-number">2</span>);
<span class="hljs-built_in">console</span>.log(double(<span class="hljs-number">5</span>)); <span class="hljs-comment">// 10</span>
</code></pre>
<p>✅ Useful in functional programming, partial application, and React HOFs.</p>
<hr />
<h2 id="heading-step-6-call-apply-and-bind">🔹 Step 6: <code>call</code>, <code>apply</code>, and <code>bind</code></h2>
<p>These control function context (<code>this</code>).</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> user = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">"Tom"</span>,
};

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">role</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">this</span>.name}</span> is a <span class="hljs-subst">${role}</span>`</span>;
}

<span class="hljs-built_in">console</span>.log(greet.call(user, <span class="hljs-string">"Engineer"</span>)); <span class="hljs-comment">// Tom is an Engineer</span>
</code></pre>
<p>🔸 <code>call</code> – calls immediately with context<br />🔸 <code>apply</code> – like <code>call</code> but with an array of arguments<br />🔸 <code>bind</code> – returns a new function with <code>this</code> bound</p>
<hr />
<h2 id="heading-step-7-composition-in-react">🔹 Step 7: Composition in React</h2>
<p>Functions as building blocks of components:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Button</span>(<span class="hljs-params">{ children }</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>{children}<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Button</span>&gt;</span>Click Me!<span class="hljs-tag">&lt;/<span class="hljs-name">Button</span>&gt;</span></span>;
}
</code></pre>
<p>✅ Composition lets you build flexible, reusable components instead of relying on inheritance.</p>
<hr />
<h2 id="heading-final-thoughts">🎯 Final Thoughts</h2>
<p>Mastering functions means understanding:</p>
<ul>
<li><p>Basic declarations</p>
</li>
<li><p>Scope and closures</p>
</li>
<li><p>Function expressions vs. declarations</p>
</li>
<li><p><code>call</code>, <code>apply</code>, <code>bind</code></p>
</li>
<li><p>Currying</p>
</li>
<li><p>Composition in React</p>
</li>
</ul>
<p>Functions are everywhere—from handling events in the DOM to managing complex UI in React.</p>
<hr />
<h2 id="heading-practice-idea">🧠 Practice Idea</h2>
<p>Build a <strong>function utility library</strong> like:</p>
<ul>
<li><p>debounce</p>
</li>
<li><p>throttle</p>
</li>
<li><p>memoize</p>
</li>
</ul>
<p>These are heavily used in real-world front-end performance tuning.</p>
<hr />
<h3 id="heading-written-by-krishan-follow-me-on-twitterhttpsxcomkrishancodes-and-githubhttpsgithubcomkrish868496-and-linkedinhttpslinkedincominkrishan86">✍️ Written by Krishan — follow me on <a target="_blank" href="https://x.com/KrishanCodes">Twitter</a> and <a target="_blank" href="https://github.com/krish868496">GitHub</a> and <a target="_blank" href="https://linkedin.com/in/krishan86">Linkedin</a></h3>
<p>💡 Check out the current code examples and notes repo: <a target="_blank" href="https://github.com/krish868496/Frontend-Elevate/tree/main/week-3/Day-5">GitHub</a></p>
<p>📝 Read more on my blog: <a target="_blank" href="https://hashnode.com/@krishancode">Hashnode</a></p>
]]></content:encoded></item></channel></rss>