<?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: Debarshi Bhattacharjee</title>
    <description>The latest articles on DEV Community by Debarshi Bhattacharjee (@devdebarshi).</description>
    <link>https://dev.to/devdebarshi</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%2F508636%2Fc3734be9-29c0-4852-acc7-377d0df39e55.jpeg</url>
      <title>DEV Community: Debarshi Bhattacharjee</title>
      <link>https://dev.to/devdebarshi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devdebarshi"/>
    <language>en</language>
    <item>
      <title>JavaScript Basics-Closures</title>
      <dc:creator>Debarshi Bhattacharjee</dc:creator>
      <pubDate>Sun, 16 May 2021 09:16:45 +0000</pubDate>
      <link>https://dev.to/devdebarshi/javascript-basics-closures-hgp</link>
      <guid>https://dev.to/devdebarshi/javascript-basics-closures-hgp</guid>
      <description>&lt;p&gt;In the previous post, we learnt about of &lt;a href="https://dev.to/debarshi95/javascript-basics-scopes-hoisting-2lmj"&gt;JavaScript Basics- Scopes &amp;amp; Hoisting&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We saw how variables and functions in &lt;strong&gt;local/function scope and block scope&lt;/strong&gt; get restricted within that scope (they cannot be accessed outside the scope). &lt;/p&gt;

&lt;p&gt;In this post, we'll learn how the opposite is true, how we can have access to all variables and functions present in the parent scope, from the child scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some basics first&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical Scope in JS:
&lt;/h2&gt;

&lt;p&gt;Lexical scope is the ability of a child function where it has to access all variables present in its parent's scope. The child function is said to be lexically bound to the scope of the parent function. &lt;/p&gt;

&lt;p&gt;Example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Global Scope
var message="Hello JavaScript!"; // Can be accessed anywhere in 
                                    the program

function foo(){  // Function scope
  // Within the scope of foo

  var greet = "Hello World!";
  let number = 45;

  function baz(){ // Another Function Scope, lexically bound to 
                     the scope of foo and has access to all 
                     variables present in the scope of foo.


   console.log(greet); // Prints 'Hello World!'

   console.log(number); // Prints '45'

  }

baz(); // Called baz within the scope of foo

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

&lt;/div&gt;



&lt;p&gt;In the above example, the child function &lt;strong&gt;baz&lt;/strong&gt; declared within the scope of parent function &lt;strong&gt;foo&lt;/strong&gt; has access to all the variables declared in &lt;strong&gt;foo's scope.&lt;/strong&gt; This was possible as &lt;strong&gt;baz&lt;/strong&gt; was &lt;strong&gt;&lt;em&gt;lexically&lt;/em&gt;&lt;/strong&gt; bound to the scope of &lt;strong&gt;foo&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closures
&lt;/h2&gt;

&lt;p&gt;A closure is a feature in JavaScript in which a child function has access to the parent function’s scope &lt;strong&gt;even when the function is executed outside its lexical scope&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;According to MDN&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Any function that forms a &lt;strong&gt;closure&lt;/strong&gt; has access to three scopes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Its own scope — variables defined between its curly brackets&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parent scope-  variables and function defined in parent &lt;br&gt;
             function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Global scope - global variables and functions&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function outer(){
let count=0; // Declared in outer scope

   function increment(){ 
     count++;
     return count; // accessing count from parent scope 
   }
 return increment; // returning the increment method;
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here we have two functions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A parent function called &lt;strong&gt;outer&lt;/strong&gt; which has a &lt;br&gt;
variable &lt;strong&gt;count&lt;/strong&gt; assigned to 0, and returns an &lt;br&gt;
inner function &lt;strong&gt;increment&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A child function &lt;strong&gt;increment&lt;/strong&gt; which is &lt;strong&gt;lexically scoped&lt;/strong&gt; and &lt;br&gt;
forms a &lt;strong&gt;closure&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As the child function forms a &lt;strong&gt;closure&lt;/strong&gt; it has &lt;br&gt;
access to all three scopes - its own scope, parent &lt;br&gt;
scope and the global scope.  &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We now invoke the parent function &lt;strong&gt;outer&lt;/strong&gt; and &lt;strong&gt;store the returned function to variable x&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Calling in global scope

const x = outer(); // assigned the function increment to 
                          variable x

console.log(x); // Prints the returned function increment

console.log(x()); // invoked the function increment for the first                 
                  // Prints 1;

console.log(x()); // invoked the function increment second time, 
                  // Prints 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here's what's happens now :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We invoked the function &lt;strong&gt;outer&lt;/strong&gt; which initialized the variable &lt;br&gt;
&lt;strong&gt;count&lt;/strong&gt; to 0 and assigned the returned function &lt;strong&gt;increment&lt;/strong&gt; &lt;br&gt;
to variable &lt;strong&gt;x&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Logging &lt;strong&gt;x&lt;/strong&gt; to console, prints the body of returned function &lt;br&gt;
&lt;strong&gt;increment&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We now invoke and log &lt;strong&gt;x&lt;/strong&gt; for the first time, it increases the &lt;br&gt;
value of &lt;strong&gt;count&lt;/strong&gt; form parent scope by 1 and returns it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Invoking &lt;strong&gt;x&lt;/strong&gt; for the second time, increases the previous value &lt;br&gt;
of &lt;strong&gt;count&lt;/strong&gt; again by 1 and returns it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the above example, we saw that even though the child function &lt;strong&gt;increment&lt;/strong&gt; was executed outside its parent's scope, it was able to remember the value of the variable &lt;strong&gt;count&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;increased its previous value&lt;/em&gt;&lt;/strong&gt; exactly by 1 one each call. This was possible because the function &lt;strong&gt;increment&lt;/strong&gt; had formed a &lt;strong&gt;&lt;em&gt;closure&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion:
&lt;/h4&gt;

&lt;p&gt;Closures are one of those tricky concepts in JavaScript that are difficult to grasp at first. But once you understand them, you realize that things could not have been any other way.&lt;/p&gt;

&lt;h4&gt;
  
  
  Resources
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;MDN&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript Basics-  Scopes &amp; Hoisting</title>
      <dc:creator>Debarshi Bhattacharjee</dc:creator>
      <pubDate>Mon, 12 Apr 2021 09:04:57 +0000</pubDate>
      <link>https://dev.to/devdebarshi/javascript-basics-scopes-hoisting-2lmj</link>
      <guid>https://dev.to/devdebarshi/javascript-basics-scopes-hoisting-2lmj</guid>
      <description>&lt;p&gt;If you're a JS beginner, you must have heard these words.&lt;/p&gt;

&lt;p&gt;Let's get in depth to understand more about these concepts in JS.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Scope in JS?
&lt;/h2&gt;

&lt;p&gt;A scope in JS is a context in which values and expressions are &lt;strong&gt;&lt;em&gt;visible&lt;/em&gt;&lt;/strong&gt; or can be referenced.&lt;br&gt;
In simple words, Scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. &lt;/p&gt;
&lt;h3&gt;
  
  
  Types of scopes in JS:
&lt;/h3&gt;

&lt;p&gt;JavaScript has 3 types of scopes :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global Scope : Any variable, function, expression that is present globally or not declared inside any other function belongs to the global scope. Variables, functions that declared in global scope can accessed from anywhere in the program.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var foo=23; // declared in the global scope

function greet(){
console.log(foo);
} 

greet();// Prints '23'
console.log(foo)l //Prints '23'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Local or Function Scope : Variables and functions  that are enclosed in some other function become part of that function's local or function Scope. In other words, a local scope or function scope is associated with a function, when we declare variables and functions inside &lt;strong&gt;&lt;em&gt;another function&lt;/em&gt;&lt;/strong&gt;, they become part of the local/function scope and are &lt;strong&gt;&lt;em&gt;restricted&lt;/em&gt;&lt;/strong&gt; to that scope. Variables and function declared in function/local scope are &lt;strong&gt;&lt;em&gt;accessible within that scope&lt;/em&gt;&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Declared in Global Scope
var foo=23;

function bar(){ 
// Function or Local Scope
   var message="Hello JavaScript"; // inside the local/function 
                                   scope of bar 
   console.log(message); // Prints 'Hello JavaScript' 
   console.log(foo); // Prints '23'
}

//Global Scope
console.log(message); // Uncaught ReferenceError: message is not 
                         defined

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

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Block Scope : Variables declared with let and const can block scoped. That is, they can be enclosed within a pair of curly braces(&lt;strong&gt;&lt;em&gt;blocks&lt;/em&gt;&lt;/strong&gt;). Any variable/function declared with let or const, inside a pair of curly braces are only available within the scope of the braces. It stands true for only for variables that declared with &lt;strong&gt;&lt;em&gt;let and const&lt;/em&gt;&lt;/strong&gt; and not with &lt;strong&gt;&lt;em&gt;var&lt;/em&gt;&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ //Block scope
  let message = 'Hello JavaScript'; // declared with let
  var name = 'Debarshi'; // declared with var
  console.log(message); // Prints 'Hello JavaScript'
}

//Global scope
console.log(name);// prints 'Debarshi'
console.log(message); // Uncaught ReferenceError: message is not 
                         defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Hoisting in JS:
&lt;/h2&gt;

&lt;p&gt;In JavaScript, &lt;strong&gt;&lt;em&gt;hoisting&lt;/em&gt;&lt;/strong&gt; is a concept in which a function or a variable can be used before declaration.&lt;/p&gt;
&lt;h3&gt;
  
  
  Hoisting with variables and functions declared with var keyword.
&lt;/h3&gt;

&lt;p&gt;Variables declared with var keyword gets hoisted to the top of the scope. If accessed before &lt;strong&gt;initialization&lt;/strong&gt; they result &lt;strong&gt;&lt;em&gt;undefined&lt;/em&gt;&lt;/strong&gt;.&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(baz); // Prints 'undefined'

console.log(greet); // Prints 'undefined

greet(); // Prints 'TypeError: greet is not a function'

var baz="Hello World!";

//anonymous function stored in a variable
var greet=function(){
    console.log("Hello JavaScript!");
}

greet(); // Prints 'Hello from JavaScript!'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Hoisting with variables and functions declared/assigned with &lt;strong&gt;let&lt;/strong&gt; and &lt;strong&gt;const&lt;/strong&gt; keyword
&lt;/h2&gt;

&lt;p&gt;Although variables and functions declared/assigned to &lt;strong&gt;&lt;em&gt;let&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;const&lt;/em&gt;&lt;/strong&gt; are also hoisted, but they cannot be accessed until they have been &lt;strong&gt;&lt;em&gt;initialized&lt;/em&gt;&lt;/strong&gt;.&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(a); // Prints 'ReferenceError: Cannot access 'a' 
                    before initialization'
console.log(name); // Prints 'ReferenceError: Cannot access 'm' 
                       before initialization'

doSomething(); // Prints 'ReferenceError: Cannot access 'm' before 
                   initialization' 

let a = 45; // Initialized
const name="Debarshi"; // Initialized

const doSomething = function (){
   console.log("Hello World!")
}

console.log(a) // Prints '45'

console.log(name) // Prints 'Debarshi'

doSomething(); // Prints 'Hello World!'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hoisting with functions:
&lt;/h3&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Functions declared with &lt;strong&gt;&lt;em&gt;function&lt;/em&gt;&lt;/strong&gt; keyword:&lt;br&gt;
Functions that are declared with the &lt;strong&gt;&lt;em&gt;function&lt;/em&gt;&lt;/strong&gt; keyword &lt;br&gt;
are also hoisted and can accessed from the top of the scope.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   foo(); // Prints 'Hello from foo!'

   function foo(){
      console.log("Hello from foo!");
    } 
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Anonymous functions, functions expression:&lt;/p&gt;

&lt;p&gt;As anonymous functions and function expressions are &lt;br&gt;
 assigned/stored to a &lt;strong&gt;&lt;em&gt;variable&lt;/em&gt;&lt;/strong&gt;, they behave same as &lt;br&gt;
 that of the variable &lt;strong&gt;&lt;em&gt;depending on the variable type they &lt;br&gt;
 are assigned to&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   baz(); // TypeError: baz is not a function
   foo();  // Uncaught ReferenceError: Cannot access 'foo' 
              before initialization

   var baz=function(){ // assigned to var
   console.log("Hello from Baz");
   }

  let foo=()=&amp;gt;{  assigned to let
   console.log("Hello from Foo");
  }

  baz(); // Prints 'Hello from Baz'
  foo(); // Prints 'Hello from Foo'
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Temporal Dead Zone in JS:
&lt;/h3&gt;

&lt;p&gt;let and const variables cannot be read/written until they &lt;br&gt;
 have been fully initialized, which happens when they are &lt;br&gt;
 declared (if no initial value is specified on declaration, &lt;br&gt;
 the variable is initialized with a value of undefined). &lt;br&gt;
  Accessing the variable before the initialization results in &lt;br&gt;
  a ReferenceError.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  { // TDZ starts at beginning of scope&lt;br&gt;
  console.log(bar); // undefined&lt;br&gt;
  console.log(foo); // ReferenceError&lt;br&gt;
  var bar = 1;&lt;br&gt;
  let foo = 2; // End of TDZ (for foo)&lt;br&gt;
 }&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  References:&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href=""&gt;MDN&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Functions in JavaScript</title>
      <dc:creator>Debarshi Bhattacharjee</dc:creator>
      <pubDate>Fri, 26 Mar 2021 12:02:39 +0000</pubDate>
      <link>https://dev.to/devdebarshi/fun-fun-functions-3fb</link>
      <guid>https://dev.to/devdebarshi/fun-fun-functions-3fb</guid>
      <description>&lt;p&gt;&lt;strong&gt;Functions&lt;/strong&gt; are one of the fundamental building blocks of any programming language. Functions are block of code enclosed in some brackets that perform a given task. To use a function, we must define it somewhere in the scope from which we can &lt;strong&gt;call/invoke&lt;/strong&gt; it.&lt;/p&gt;

&lt;p&gt;Let's start by &lt;strong&gt;declaring&lt;/strong&gt; a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function hello(){
console.log("Hello World");  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We just declared a function called &lt;strong&gt;hello&lt;/strong&gt;, in order to execute it, we need to &lt;strong&gt;&lt;em&gt;call&lt;/em&gt;&lt;/strong&gt; it.&lt;/p&gt;

&lt;h4&gt;
  
  
  What do you I mean by &lt;strong&gt;&lt;em&gt;call&lt;/em&gt;&lt;/strong&gt; the function?
&lt;/h4&gt;

&lt;p&gt;In all programming languages, whenever you need to execute a function, you have to &lt;strong&gt;&lt;em&gt;call&lt;/em&gt;&lt;/strong&gt; it. No matter how many functions you have in your code, it won't be executed until you have &lt;strong&gt;called&lt;/strong&gt; the function.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to call a function?
&lt;/h4&gt;

&lt;p&gt;It's simple, just add your &lt;strong&gt;&lt;em&gt;function name followed by an opening and closing brackets&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hello(); //Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Assigning functions to variables:
&lt;/h2&gt;

&lt;p&gt;In JavaScript, you can assign/store a function in a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var someVariable = function(){
console.log("My Function")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something, I didn't name the function.&lt;br&gt;
The function is an example of &lt;strong&gt;unnamed/anonymous&lt;/strong&gt; function.&lt;/p&gt;

&lt;p&gt;The above function can also be written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var justAnotherVariable= function myAnotherFunction(){
console.log("My another named Function");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To execute these functions you have to call them.&lt;/p&gt;

&lt;p&gt;Let's call myFunction&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;someVariable(); //My function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's call the second function. &lt;br&gt;
But wait how do we call it? &lt;br&gt;
The function has been assigned to a variable. &lt;br&gt;
Should we use the variable name or function name?&lt;/p&gt;

&lt;p&gt;In JavaScript, &lt;strong&gt;&lt;em&gt;whenever you assign a function to a variable, no matter if it is a named function or anonymous(unnamed) function, to call it, you have to use the variable name followed by an opening and closing brackets.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the example above &lt;strong&gt;someNamed&lt;/strong&gt; function has been stored in the variable &lt;strong&gt;justAnotherVariable&lt;/strong&gt;, in order to call it, &lt;strong&gt;&lt;em&gt;we have to use the variable name followed by parentheses.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;justAnotherVariable()  // My another named function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function Declaration &amp;amp; Function Expression
&lt;/h2&gt;

&lt;p&gt;Functions are declared by using the &lt;strong&gt;&lt;em&gt;function&lt;/em&gt;&lt;/strong&gt; keyword.&lt;br&gt;
Every function &lt;strong&gt;declaration&lt;/strong&gt; must begin with the &lt;strong&gt;&lt;em&gt;function&lt;/em&gt;&lt;/strong&gt; keyword.&lt;/p&gt;

&lt;p&gt;Let's declare a function &lt;strong&gt;&lt;em&gt;foo&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function foo() {
console.log("Hello from foo");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A declared function gets executed only when it is &lt;strong&gt;&lt;em&gt;called&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In order to execute &lt;strong&gt;foo&lt;/strong&gt;, we have to &lt;strong&gt;&lt;em&gt;call&lt;/em&gt;&lt;/strong&gt; it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo(); // Hello from foo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In JavaScript, functions can also be defined using an &lt;strong&gt;&lt;em&gt;expression&lt;/em&gt;&lt;/strong&gt; and function expressions can be &lt;strong&gt;&lt;em&gt;stored/assigned to a variable&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's declare a variable &lt;strong&gt;&lt;em&gt;baz&lt;/em&gt;&lt;/strong&gt; and store a function to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var baz = function () {
console.log("Hello from baz")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once a function expression has been assigned to variable, in order to &lt;strong&gt;&lt;em&gt;call&lt;/em&gt;&lt;/strong&gt; the function expression we have to use the &lt;strong&gt;variable&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;baz(); // Hello from baz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main difference between a function expression and a function declaration is the &lt;strong&gt;function name&lt;/strong&gt;, which can be &lt;strong&gt;&lt;em&gt;omitted&lt;/em&gt;&lt;/strong&gt; in function expressions to create anonymous functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Argument &amp;amp; Parameters
&lt;/h2&gt;

&lt;p&gt;When we declare a function, we can receive some addition information/values to the function. These additional information will be passed by us when we &lt;strong&gt;&lt;em&gt;call&lt;/em&gt;&lt;/strong&gt; the function.&lt;/p&gt;

&lt;p&gt;These values when passed while calling the function are called &lt;strong&gt;arguments&lt;/strong&gt; whereas in the function declaration they are called &lt;strong&gt;parameters&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;We receive two &lt;strong&gt;&lt;em&gt;parameters&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;a&lt;/strong&gt; and &lt;strong&gt;b&lt;/strong&gt; while declaring the function &lt;strong&gt;bar&lt;/strong&gt; and pass two &lt;strong&gt;&lt;em&gt;arguments&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;5&lt;/strong&gt; and &lt;strong&gt;15&lt;/strong&gt; when calling it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function bar(a, b){
return a+b;
}

var add = bar(5, 15); 
console.log(add); //15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  First Class Function, High Order Function and Callback
&lt;/h2&gt;

&lt;p&gt;JavaScript has a concept of &lt;strong&gt;First class function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A programming language is said to have &lt;strong&gt;First-class functions&lt;/strong&gt; when functions in that language are treated like any other variable. &lt;strong&gt;&lt;em&gt;In such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We have already seen the example of a function being assigned to a variable.&lt;/p&gt;

&lt;p&gt;Let's see an example of how a function can be passed as argument to another function and returned from a function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
Function passed to a function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function greet(){
console.log("Hello from greet")
}  

function doSomething(callback){
callback() // calling the passed function
}

doSomething() // Hello from greet

Calling the function doSomething  calls the callback function which is greet in our case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Function returned from a function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(num1,num2){
console.log("I'm number 1: "+num1);
console.log("I'm number 2: "+num2);
} 

function adder(){
return add;
}


var x = adder() 
x(10,20) //I'm number 1: 10
         //I'm number 2: 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we called the function &lt;strong&gt;adder&lt;/strong&gt; and stored the returned function &lt;strong&gt;add&lt;/strong&gt; to variable &lt;strong&gt;x&lt;/strong&gt;. Since, x is now storing a function, we &lt;strong&gt;call&lt;/strong&gt; it and pass arguments &lt;strong&gt;10&lt;/strong&gt; and &lt;strong&gt;20&lt;/strong&gt; respectively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Callback
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;Callback&lt;/strong&gt; is just an identifier for a function that is passed as argument to another function. In the above examples, the function &lt;strong&gt;&lt;em&gt;greet&lt;/em&gt;&lt;/strong&gt; is a callback. &lt;/p&gt;

&lt;h3&gt;
  
  
  High Order Function
&lt;/h3&gt;

&lt;p&gt;Any function that &lt;strong&gt;&lt;em&gt;takes a function as an argument or returns a function&lt;/em&gt;&lt;/strong&gt; is called a &lt;strong&gt;high order function&lt;/strong&gt;. In the examples, both the functions &lt;strong&gt;&lt;em&gt;doSomething&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;adder&lt;/em&gt;&lt;/strong&gt; are high order functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;Functions are the building blocks in JavaScript. Functions can perform complex operation, take input values, process and return values. In JavaScript, functions can also return a function from a function, store a function to a variable, pass a function to another function.&lt;/p&gt;

&lt;p&gt;If you liked the post then do share your feedback. &lt;br&gt;
I'd love to hear from you. Connect with me @ &lt;a href=""&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  References:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions"&gt;MDN&lt;/a&gt;&lt;/p&gt;

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