Function Expression vs Function Declaration in JavaScript?

JavascriptWeb DevelopmentFront End Technology

<h2 style="">Function Declaration</h2><p style="">The &ldquo;function&rdquo; keyword declares a function in JavaScript. To define a function in JavaScript use the &ldquo;function&rdquo; keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.</p><p style="">Here&rsquo;s an example &minus;</p><pre class="result notranslate">function sayHello(name, age) { &nbsp; &nbsp;document.write (name + &quot; is &quot; + age + &quot; years old.&quot;); }</pre><h2 style="">Function Expression</h2><p style="">Function Expression should not start with the keyword &ldquo;function&rdquo;. Functions defined can be named or anonymous.</p><p style="">Here are the examples &minus;</p><pre class="result notranslate">//anonymous function expression var a = function() { &nbsp; &nbsp;return 5; }</pre><p style="">Or</p><pre class="result notranslate">//named function expression var a = function bar() { &nbsp; &nbsp;return 5; }</pre>
Updated on 12-Jun-2020 11:30:56