<?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: Sanket Jadhav</title>
    <description>The latest articles on DEV Community by Sanket Jadhav (@sanket4120).</description>
    <link>https://dev.to/sanket4120</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%2F544323%2Fa9476d7d-9385-4061-bfad-ae386f5bcde4.png</url>
      <title>DEV Community: Sanket Jadhav</title>
      <link>https://dev.to/sanket4120</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanket4120"/>
    <language>en</language>
    <item>
      <title>Prototype &amp; prototype inheritance</title>
      <dc:creator>Sanket Jadhav</dc:creator>
      <pubDate>Sat, 24 Sep 2022 18:24:36 +0000</pubDate>
      <link>https://dev.to/sanket4120/prototype-prototype-inheritance-1mh8</link>
      <guid>https://dev.to/sanket4120/prototype-prototype-inheritance-1mh8</guid>
      <description>&lt;p&gt;demo post&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JavaScript Closures</title>
      <dc:creator>Sanket Jadhav</dc:creator>
      <pubDate>Sat, 24 Sep 2022 18:22:21 +0000</pubDate>
      <link>https://dev.to/sanket4120/javascript-closures-2pkp</link>
      <guid>https://dev.to/sanket4120/javascript-closures-2pkp</guid>
      <description>&lt;p&gt;What is closure?&lt;/p&gt;

&lt;p&gt;A closure is a function that accesses the scope of its outer function even after the outer function returns. This means that a closure can remember and access its outer function's variables and arguments even after the function has finished.closure is a function that references variables in its outer scope from its inner scope. The closure keeps the outer scope inside its inner scope.&lt;/p&gt;

&lt;p&gt;example-&lt;/p&gt;

&lt;p&gt;function OuterFunction() {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var extern variable = 100;

function InnerFunction() {
    alert(externalVariable);
}

return InnerFunction;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
var innerFunc = OuterFunction();&lt;/p&gt;

&lt;p&gt;innerFunc();&lt;/p&gt;

&lt;p&gt;In the example above, return InnerFunction; returns InnerFunction from OuterFunction when you call OuterFunction(). The innerFunc variable only refers to InnerFunction(), not OuterFunction(). as the call innerFunc() it can still access the externalVariable that is declared in OuterFunction(). This is called closure.&lt;br&gt;
One important property of closures is that external variables can retain their state between multiple calls. Remember that the inner function does not keep a separate copy of the outer variables, but refers to the outer variables, that is, the value of the outer variables will change if you change it using the inner function.&lt;/p&gt;

&lt;p&gt;What is lexical scope?&lt;/p&gt;

&lt;p&gt;Lexical scope in JavaScript refers to the availability of variables, functions, and objects based on their physical location in the source code. Lexical scope defines the scope of a variable according to the position of the variable declared in the source code.&lt;br&gt;
In this example:&lt;/p&gt;

&lt;p&gt;let name = 'John';&lt;/p&gt;

&lt;p&gt;function greeting() { &lt;br&gt;
    let message = 'Hi';&lt;br&gt;
    console.log(message + ' '+ name);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;The variable name is a global variable. It is accessible from anywhere, including the greeting() function.&lt;br&gt;
The message variable is a local variable that is only accessible within the greeting() function.&lt;br&gt;
If we access the message variable outside the greeting() function, you will get an error.&lt;/p&gt;

&lt;p&gt;So the Js engine uses a scope to manage variable accessibility.&lt;/p&gt;

&lt;p&gt;According to the lexical scope, scopes can be nested and an inner function can access variables declared in its outer scope.&lt;br&gt;
example-&lt;/p&gt;

&lt;p&gt;function greeting() &lt;br&gt;
{&lt;br&gt;
    let message = 'Hi';&lt;/p&gt;

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

sayHi();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;greeting();&lt;/p&gt;

&lt;p&gt;The greeting() function makes a local variable named message and a function named sayHi().&lt;/p&gt;

&lt;p&gt;SayHi() is an internal function that is only available in the body of the greeting() function.&lt;/p&gt;

&lt;p&gt;The sayHi() function can access external function variables, such as the message variable of the hello() function.&lt;/p&gt;

&lt;p&gt;Inside the greet() function, we call the sayHi() function to display the Hello message.&lt;/p&gt;

&lt;p&gt;Take a look at this code snippet:&lt;/p&gt;

&lt;p&gt;function person() {&lt;br&gt;
  let name = 'Peter';&lt;/p&gt;

&lt;p&gt;return function displayName() {&lt;br&gt;
    console.log(name);&lt;br&gt;
  };&lt;br&gt;
}&lt;br&gt;
let peter = person();&lt;br&gt;
Peter(); // prints 'Peter'&lt;br&gt;
When the person function is invoked, the JavaScript engine creates a new execution context and lexical environment for the function. After this function is finished, it returns the displayName function and assigns it to the peter variable.&lt;br&gt;
the lexical environment will look like this:&lt;/p&gt;

&lt;p&gt;personLexicalEnvironment = {&lt;br&gt;
  EnvironmentRecord: {&lt;br&gt;
    name: 'Peter',&lt;br&gt;
    displayName: &lt;br&gt;
  }&lt;br&gt;
  external: &lt;br&gt;
}&lt;br&gt;
When a person's function completes, its execution context is removed from the stack. But its lexical environment is still in memory because its lexical environment is referenced by the lexical environment of its internal displayName function. So its variables are still available in memory.&lt;/p&gt;

&lt;p&gt;Please note that when a personLexicalEnvironment is created, the JavaScript engine attaches the personLexicalEnvironment to all function definitions in that lexical environment. So that later, if any of the internal functions are called, the JavaScript engine can set the external lexical environment to the lexical environment attached to that function definition.&lt;/p&gt;

&lt;p&gt;When the peter function (which is actually a reference to the displayName function) is executed, the Js engine makes a new execution context and lexical environment for that function.&lt;/p&gt;

&lt;p&gt;The  lexical environment will be- &lt;/p&gt;

&lt;p&gt;displayNameLexicalEnvironment = {&lt;br&gt;
  EnvironmentRecord: {&lt;/p&gt;

&lt;p&gt;}&lt;br&gt;
  outer: &lt;br&gt;
}&lt;br&gt;
Since there is no variable in the displayName function, its environment record will be empty. During the execution of this function, the JavaScript engine tries to find the variable name in the lexical environment of the function.&lt;/p&gt;

&lt;p&gt;Since there are no variables in the lexical environment of the displayName function, it looks in the external lexical environment, i.e. the lexical environment of the person function, which is still in memory. The JavaScript engine finds the variable and prints its name to the console.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Call Stack in JavaScript</title>
      <dc:creator>Sanket Jadhav</dc:creator>
      <pubDate>Sat, 24 Sep 2022 18:19:26 +0000</pubDate>
      <link>https://dev.to/sanket4120/call-stack-in-javascript-1dah</link>
      <guid>https://dev.to/sanket4120/call-stack-in-javascript-1dah</guid>
      <description>&lt;p&gt;What is a call stack ?&lt;br&gt;
A call stack is a mechanism for an interpreter (such as a JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions - which function is currently running and which functions are being called from that function, etc. — MDN Web Docs&lt;/p&gt;

&lt;p&gt;The call stack keeps record of the all functions to be executed. When a function is called , it is added on to top of the call stack. When the function returns, it is removed or removed from the call stack. Any asynchronous functions (fetch, setTimeout, async, etc.) are moved to the event queue (more on that later).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O8eEuTo0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tav3urf2rxm73uc5y4sr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O8eEuTo0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tav3urf2rxm73uc5y4sr.png" alt="Image description" width="284" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How does the call stack work?&lt;br&gt;
As JavaScript is single-threaded. This means that it has only one call stack and can only handle one command at a time. The call stack follows the LIFO (Last In, First Out) principle, which means that it always processes the call at the top of the stack first.&lt;/p&gt;

&lt;p&gt;When a function is called, it is added to the stack. When a function calls another function, it is added to the calling function.&lt;/p&gt;

&lt;p&gt;Callback queue&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G0x1kneG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7gavyusq1k77ypzagz2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G0x1kneG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7gavyusq1k77ypzagz2.png" alt="Image description" width="731" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the browser is done with the timer (or any other API which it provides for JS), it doesn't transfer the codes to be executed back to Javascript immediately. When the browser finishes, it adds  the code in a callback queue. As the name implies, it is a queue containing some functions or codes which would be called back at a later time.&lt;/p&gt;

&lt;p&gt;The callback which was passed as an argument to setTimeout is written in JavaScript. So the JavaScript interpreter needs to run the code, which means it needs to use the call stack, which again means we have to wait for the call stack to be empty to make the callback.&lt;/p&gt;

&lt;p&gt;The setTimeout call triggers the Web API execution, which adds the callback to the callback queue. The event loop then takes the callbacks from the queue and adds them to the stack once it's empty.&lt;br&gt;
Event Loop: This is followed by an Event Loop which runs continuously and checks the main stack if it has any frames to execute, if not it checks the callback queue, if the callback queue has codes to execute then it pushes the message from it to the main stack for execution .&lt;/p&gt;

&lt;p&gt;Job Queue: In addition to the Callback Queue, browsers have introduced one more queue, which is the "Job Queue", reserved only for the new Promise() function. So when you use promises in your code, you add a .then() method, which is a callback method.&lt;/p&gt;

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