<?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[Function Declaration and Expression]]></title><description><![CDATA[Function Declaration and Expression]]></description><link>https://function-declaration-expression.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 11 Oct 2026 14:50:17 GMT</lastBuildDate><atom:link href="https://function-declaration-expression.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Function Declaration and Expression]]></title><description><![CDATA[🔹 Function Declaration
A function declaration is when you use the function keyword and give the function a name.
✅ Example:
function greet() {
  console.log("Hello, Priyanshu!");
}
greet(); // Output: Hello, Priyanshu!

Key points:

The function has...]]></description><link>https://function-declaration-expression.hashnode.dev/function-declaration-and-expression</link><guid isPermaLink="true">https://function-declaration-expression.hashnode.dev/function-declaration-and-expression</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[expressions]]></category><category><![CDATA[declaration]]></category><category><![CDATA[js]]></category><dc:creator><![CDATA[Priyanshu Vishwakarma]]></dc:creator><pubDate>Tue, 01 Jul 2025 18:30:00 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-function-declaration">🔹 Function Declaration</h2>
<p>A <strong>function declaration</strong> is when you use the <code>function</code> keyword and give the function a name.</p>
<p>✅ Example:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello, Priyanshu!"</span>);
}
greet(); <span class="hljs-comment">// Output: Hello, Priyanshu!</span>
</code></pre>
<h3 id="heading-key-points">Key points:</h3>
<ul>
<li><p>The function has a <strong>name</strong> (<code>greet</code>).</p>
</li>
<li><p>It can be called <strong>before or after</strong> its definition (because of <strong>hoisting</strong>).</p>
</li>
</ul>
<p>👉 Example of hoisting:</p>
<pre><code class="lang-javascript">sayHi(); <span class="hljs-comment">// Works, even though function is defined later</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHi</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hi there!"</span>);
}
</code></pre>
<hr />
<h2 id="heading-function-expression">🔹 Function Expression</h2>
<p>A <strong>function expression</strong> is when you assign a function to a variable.</p>
<p>✅ Example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> greet = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello, Priyanshu!"</span>);
};
greet(); <span class="hljs-comment">// Output: Hello, Priyanshu!</span>
</code></pre>
<h3 id="heading-key-points-1">Key points:</h3>
<ul>
<li><p>The function is stored in a variable (<code>greet</code>).</p>
</li>
<li><p>It can be <strong>anonymous</strong> (no name) or <strong>named</strong>.</p>
</li>
<li><p>It <strong>cannot be called before it is defined</strong> (not hoisted the same way).</p>
</li>
</ul>
<p>👉 Example:</p>
<pre><code class="lang-javascript">sayHi(); <span class="hljs-comment">// ❌ Error: Cannot access 'sayHi' before initialization</span>

<span class="hljs-keyword">const</span> sayHi = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hi there!"</span>);
};
</code></pre>
<hr />
<h2 id="heading-summary-easy-to-remember">🔹 Summary (Easy to Remember)</h2>
<ul>
<li><p><strong>Function Declaration</strong> → Starts with <code>function</code>, has a name, hoisted.</p>
</li>
<li><p><strong>Function Expression</strong> → Stored in a variable, not hoisted the same way.</p>
</li>
</ul>
]]></content:encoded></item></channel></rss>