<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Difference between function declaration and function expression &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/difference-between-function-declaration-and-function-expression/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 21 Oct 2015 08:53:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Difference between function declaration and function expression &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Function Declaration And Function Expression</title>
		<link>https://www.sibeeshpassion.com/function-declaration-and-function-expression/</link>
					<comments>https://www.sibeeshpassion.com/function-declaration-and-function-expression/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 21 Oct 2015 00:59:00 +0000</pubDate>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Difference between function declaration and function expression]]></category>
		<category><![CDATA[Function Declaration And Function Expression]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=10821</guid>

					<description><![CDATA[In this post we will see the basic difference between function declaration and function expression in jQuery and JavaScript. Sometimes the developer&#8217;s getting confused between these two topics(Shhhh I ma saying about the starters like me). Basically both are same. Only difference is depends on how you handle or treat these two. We will see a basic demo here to ensure the difference between function declaration and expression. I hope you will like this. Here I am using jQuery 2.0.2 version, you can use whichever version you like. Background In my projects I am using both function declarations and function [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In this post we will see the basic difference between function declaration and function expression in jQuery and JavaScript. Sometimes the developer&#8217;s getting confused between these two topics(Shhhh I ma saying about the starters like me). Basically both are same. Only difference is depends on how you handle or treat these two. We will see a basic demo here to ensure the difference between function declaration and expression. I hope you will like this.</p>
<blockquote><p>Here I am using jQuery 2.0.2 version, you can use whichever version you like. </p></blockquote>
<p><strong>Background</strong></p>
<p>In my projects I am using both function declarations and function expressions. So I thought to highlight some basic difference between them, so that it may help someone. </p>
<p><strong>Basic Difference Between Function Declaration And Function Definition</strong></p>
<li>Function declarations are loaded first, that is before any codes</li>
<li>Function expressions are loaded when the interpreter reaches that particular line of code.</li>
<li>Basically we can call any code only after the declarations are loaded</li>
<p>We will try to find out this difference with a demo.</p>
<p><strong>Using the code</strong></p>
<p>First thing you need to do is create a page.</p>
<p>[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Function Declaration VS Expression &#8211; Sibeesh Passion&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
   Function Declaration VS Expression &#8211; Sibeesh Passion<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p>Then you need to include the script needed.</p>
<p>[js]<br />
&lt;script src=&quot;http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js&quot;&gt;&lt;/script&gt;<br />
[/js]</p>
<p>Now we will create a function declaration as follows. </p>
<p>[js]<br />
&lt;script&gt;<br />
        $(document).ready(function () {<br />
		   alert(myFunctionDeclaration());<br />
           function myFunctionDeclaration()<br />
		   {<br />
				return &#8216;myFunctionDeclaration is called&#8217;;<br />
		   }<br />
        });<br />
    &lt;/script&gt;<br />
[/js]</p>
<p>What will be the output of this?</p>
<div id="attachment_10822" style="width: 391px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-10822" src="http://sibeeshpassion.com/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression.png" alt="Function Declaration And Function Expression " width="381" height="231" class="size-full wp-image-10822" srcset="/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression.png 381w, /wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression-300x182.png 300w" sizes="(max-width: 381px) 100vw, 381px" /></a><p id="caption-attachment-10822" class="wp-caption-text">Function Declaration And Function Expression</p></div>
<p>Now we will change our script as follows. </p>
<p>[js]<br />
&lt;script&gt;<br />
        $(document).ready(function () {<br />
           function myFunctionDeclaration()<br />
		   {<br />
				return &#8216;myFunctionDeclaration is called&#8217;;<br />
		   }<br />
		   alert(myFunctionDeclaration());<br />
        });<br />
    &lt;/script&gt;<br />
[/js]</p>
<p>This will also returns the same output.</p>
<div id="attachment_10822" style="width: 391px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-10822" src="http://sibeeshpassion.com/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression.png" alt="Function Declaration And Function Expression " width="381" height="231" class="size-full wp-image-10822" srcset="/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression.png 381w, /wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression-300x182.png 300w" sizes="(max-width: 381px) 100vw, 381px" /></a><p id="caption-attachment-10822" class="wp-caption-text">Function Declaration And Function Expression</p></div>
<p>Now we will create a function expression as follows. </p>
<p>[js]<br />
&lt;script&gt;<br />
        $(document).ready(function () {<br />
		  alert(myFunctionExpression());<br />
		   var myFunctionExpression = function()<br />
		   {<br />
				return &#8216;myFunctionExpression is called&#8217;;<br />
		   }<br />
        });<br />
    &lt;/script&gt;<br />
[/js]</p>
<p>What may be the output of this? We will check it now. </p>
<div id="attachment_10823" style="width: 427px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression1.png"><img decoding="async" aria-describedby="caption-attachment-10823" src="http://sibeeshpassion.com/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression1.png" alt="Function Declaration And Function Expression " width="417" height="282" class="size-full wp-image-10823" srcset="/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression1.png 417w, /wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression1-300x203.png 300w, /wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression1-400x271.png 400w" sizes="(max-width: 417px) 100vw, 417px" /></a><p id="caption-attachment-10823" class="wp-caption-text">Function Declaration And Function Expression</p></div>
<p>As you can see, instead of giving an alert it is throwing an error <em>Uncaught TypeError: myFunctionExpression is not a function</em> in the console. It is just because the function expression is not yet loaded. You are trying to call that expression before it loads.</p>
<p>So what we can do to make it work? Simple, just change the script as follows.</p>
<p>[js]<br />
&lt;script&gt;<br />
        $(document).ready(function () {<br />
                   var myFunctionExpression = function()<br />
		   {<br />
				return &#8216;myFunctionExpression is called&#8217;;<br />
		   }<br />
		   alert(myFunctionExpression());<br />
        });<br />
    &lt;/script&gt;<br />
[/js]</p>
<p>Now you will get an alert as follows.</p>
<div id="attachment_10824" style="width: 397px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression2.png"><img decoding="async" aria-describedby="caption-attachment-10824" src="http://sibeeshpassion.com/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression2.png" alt="Function Declaration And Function Expression " width="387" height="207" class="size-full wp-image-10824" srcset="/wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression2.png 387w, /wp-content/uploads/2015/10/Function_Declaration_And_Function_Expression2-300x160.png 300w" sizes="(max-width: 387px) 100vw, 387px" /></a><p id="caption-attachment-10824" class="wp-caption-text">Function Declaration And Function Expression</p></div>
<p>That is all. I hope you know the difference between function declaration and function expression now. </p>
<p><strong>Complete Code</strong></p>
<p>[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Function Declaration VS Expression &#8211; Sibeesh Passion&lt;/title&gt;<br />
    &lt;script src=&quot;http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js&quot;&gt;&lt;/script&gt;<br />
    &lt;script&gt;<br />
        $(document).ready(function () {</p>
<p>		   var myFunctionExpression = function()<br />
		   {<br />
				return &#8216;myFunctionExpression is called&#8217;;<br />
		   }<br />
		   alert(myFunctionExpression());<br />
		   alert(myFunctionDeclaration());<br />
           function myFunctionDeclaration()<br />
		   {<br />
				return &#8216;myFunctionDeclaration is called&#8217;;<br />
		   }<br />
        });<br />
    &lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    Function Declaration VS Expression &#8211; Sibeesh Passion<br />
	&lt;div class=&quot;first&quot;&gt;&lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p>
<p>[/html]</p>
<p><strong>Conclusion</strong></p>
<p>Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<p><strong>Your turn. What do you think?</strong></p>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/function-declaration-and-function-expression/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
