<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohan Kumar Sah</title>
    <description>The latest articles on DEV Community by Mohan Kumar Sah (@neon010).</description>
    <link>https://dev.to/neon010</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F461122%2Fbea307ac-4dad-4236-b4f0-f111c1631655.jpg</url>
      <title>DEV Community: Mohan Kumar Sah</title>
      <link>https://dev.to/neon010</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neon010"/>
    <language>en</language>
    <item>
      <title>Detailed JavaScript Function Overview</title>
      <dc:creator>Mohan Kumar Sah</dc:creator>
      <pubDate>Wed, 29 Jun 2022 16:30:29 +0000</pubDate>
      <link>https://dev.to/neon010/detailed-javascript-function-overview-1i97</link>
      <guid>https://dev.to/neon010/detailed-javascript-function-overview-1i97</guid>
      <description>&lt;h2&gt;
  
  
  Intro to functions
&lt;/h2&gt;

&lt;p&gt;A function is a reusable code block that can be called whenever we need to run it. A function can called within the function(recursive function) or from anywhere in code.&lt;/p&gt;

&lt;p&gt;In JavaScript, We can create function in two ways&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Function declaration&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Function Expression&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  function declaration
&lt;/h2&gt;

&lt;p&gt;The function declaration also known as function statement defines a function with the specified parameters. A function created with function declaration require a name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Example of function declaration
function sum(a,b) {
   return a+b;
}
sum(2,5) // 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above function return the sum of two numbers a and b. Function parameters a and b passed to functions by value, so if the code within the body of a function assigns a completely new value to a parameter that was passed to the function, the change is not reflected globally or in the code which called that function. &lt;/p&gt;

&lt;p&gt;By default, functions return undefined. To return any other value, the function must have a return statement that specifies the value to return.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function declaration Hoisting
&lt;/h2&gt;

&lt;p&gt;Function declarations in JavaScript are hoisted to the top of the enclosing function or global scope. You can use the function before you declared it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;declarationHoisted();       // logs "hoisted function"

function declarationHoisted() {
  console.log('hoisted function');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Functions Scope
&lt;/h2&gt;

&lt;p&gt;A function defined in the global scope can access all variables defined in the global scope. A function defined inside another function can also access all variables defined in its parent function, and any other variables to which the parent function has access.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The following variables are defined in the global scope
let num1 = 20,
let  num2 = 3,
let  name = 'mohan';

// This function is defined in the global scope
function multiply() {
  return num1 * num2;
}

multiply(); // Returns 60

// A nested function example
function getScore() {
  let num1 = 2,
  let num2 = 3;

  function add() {
    return name + ' scored ' + (num1 + num2);
  }

  return add();
}

getScore(); // Returns "mohan scored 5"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function expression
&lt;/h2&gt;

&lt;p&gt;A function expression is very similar to and has almost the same syntax as a function declaration. The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Example of anonymous function expression
let sayHiAnonymous = function() {
  console.log("hello");
};
// Example of arrow function expression
let sayHiArrow = () =&amp;gt;{
  console.log("hello");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function expression hoisting
&lt;/h2&gt;

&lt;p&gt;Function expressions in JavaScript are not hoisted, unlike function declarations. You can't use function expressions before you create them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(notHoisted) // undefined
//  even though the variable name is hoisted, the definition isn't. so it's undefined.
notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function() {
   console.log('bar');
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function arguements and parameters
&lt;/h2&gt;

&lt;p&gt;The number of things passed in function is called as function arguements and the number of params that a function is called as function parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myFunc = function(a:any,b:any){
console.log(`number of things passed in function ${arguments.length}`);

console.log(`The number of params that function takes is ${myFunc.length}`);

if (arguments.length === myFunc.length) {
   console.log(`we have a match`);
} else {
   console.log(`No match`);
}
};
myFunc(2,3);  // "we have a match"

myFunc(2,3,4,5,6) // "No match"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
