<?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>Forem: Nishant Ranjan</title>
    <description>The latest articles on Forem by Nishant Ranjan (@nishu_2811).</description>
    <link>https://forem.com/nishu_2811</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%2F310007%2Fceab2586-40cd-485b-bb1d-2d4f00beda0e.jpg</url>
      <title>Forem: Nishant Ranjan</title>
      <link>https://forem.com/nishu_2811</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nishu_2811"/>
    <language>en</language>
    <item>
      <title>What happens when you run Javascript code?</title>
      <dc:creator>Nishant Ranjan</dc:creator>
      <pubDate>Sat, 21 Jan 2023 14:56:35 +0000</pubDate>
      <link>https://forem.com/nishu_2811/what-happens-when-you-run-javascript-code-3781</link>
      <guid>https://forem.com/nishu_2811/what-happens-when-you-run-javascript-code-3781</guid>
      <description>&lt;p&gt;Hello again 👋,&lt;/p&gt;

&lt;p&gt;There are a lot of things that happen behind the scenes inside the JavaScript engine.&lt;/p&gt;

&lt;p&gt;Remember in the last blog I discussed that everything in JavaScript happens inside an Execution Context.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://dev.to/nishu_2811/how-javascript-works-execution-context-21ef"&gt;How Javascript works &amp;amp; execution context?&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and JavaScript isn't possible without this beautiful Execution Context.&lt;/p&gt;

&lt;p&gt;So what happens when you run a JavaScript program?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Yes, you are right &lt;strong&gt;&lt;em&gt;An execution context is created.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's now understand how this beautiful execution context is created with the help of a JavaScript program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. var n=2;
2. function square (num){
3.   var ans = num * num;
4.   return ans;
5. }
6.
7. var square2 = square(n);
8. var square3 = square(4);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let us see how this whole JavaScript code is run behind the scenes.&lt;/p&gt;

&lt;p&gt;When you run this whole code, &lt;strong&gt;A global execution context is created&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This execution context is created in two phases.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
The first phase is known as the creation phase. This is also known as the memory creation phase. This is the critical phase. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the first phase, JavaScript will allocate memory to all the variables and functions.&lt;/p&gt;

&lt;p&gt;As soon as JS encounters this line 1, it will allocate memory to n.&lt;/p&gt;

&lt;p&gt;Now the question is how will it allocate?&lt;/p&gt;

&lt;p&gt;It will reserve a memory space here for n. Similarly, now JavaScript goes to line 2. It allocates some memory to the square. &lt;/p&gt;

&lt;p&gt;Let me tell you what it stores.&lt;/p&gt;

&lt;p&gt;When it allocates memory to n, it stores a special value. Which is known as undefined.&lt;/p&gt;

&lt;p&gt;In the first phase and in the case of functions, it literally stores the whole code of the function inside this memory space.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bYv3QwVP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idk72hnsv08lfbiic1p4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bYv3QwVP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idk72hnsv08lfbiic1p4.png" alt="Memory allocation" width="880" height="768"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and now it will also allocate memory to square2 and square4, it will again store undefined for them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Let us see what happens in the second phase, which is the code execution phase.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The second phase is the code execution phase&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's see how this code is executed after the memory allocation.&lt;/p&gt;

&lt;p&gt;JavaScript once again runs through this whole JS program line by line and it executes the code now. So this is the point when all these functions and every calculation in the program are done.&lt;/p&gt;

&lt;p&gt;as soon as it encounters the first line, n=2, it actually places the '2' inside the 'n'. Till now, the value of n was undefined. Now in the second phase of the creation of Execution Context, &lt;em&gt;i.e. the Code execution phase,&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;After finishing line one, it goes to line two and sees that We have nothing to do here. So line number two to five, there is nothing to execute literally. It moves to line number 6.&lt;/p&gt;

&lt;p&gt;Here, we invoke a function. Whenever a function is invoked you know a mini program invoked.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Altogether new execution context is created inside the global execution context.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And It will be similar to the first phase of Global execution. Memory creation and code execution. See the below image and you will understand the whole thing. This is very very important to understand how this execution context works and Javascript code runs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SpYgSRvd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ficjwkoqocxjpcqufhpy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SpYgSRvd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ficjwkoqocxjpcqufhpy.png" alt="execution context created to run the function" width="880" height="785"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One more thing to understand here when the whole function is executed, &lt;strong&gt;_this whole execution context, for that instance of that function, will be deleted.&lt;br&gt;
_&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So now there won't be the execution context as soon as we return the value.&lt;/p&gt;

&lt;p&gt;In line number 7 we are again invoking a function, right, and just the difference is, we are now passing in 4 over here as an argument directly. That's it.&lt;/p&gt;

&lt;p&gt;Again, a brand new execution context will be created, the same process will happen.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
But don't you think that all this is too much to manage for the JS engine?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Execution context is created one by one, inside one, and all these things are very tough to manage, right? And suppose there was a function invocation inside the function, so what would have happened is, you would have created an execution context INSIDE an execution context over here, and maybe again an invocation so it can go to any deep level right?&lt;br&gt;
So it is very difficult for the JS engine to manage, and it does it very beautifully.&lt;/p&gt;

&lt;p&gt;It handles everything to manage this execution context creation, deletion, and control, It manages a stack.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is known as the call stack.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It has its own call stack. At the bottom of the stack, we have our global execution context. That means, whenever any JS program is run, this call stack is populated with this Global execution context. This whole execution context is pushed inside this stack.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember this. Whenever a function is invoked, or a new execution context is created.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's all for today. I will explain more execution context in upcoming blogs with some visual examples.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How Javascript works &amp; execution context?</title>
      <dc:creator>Nishant Ranjan</dc:creator>
      <pubDate>Mon, 16 Jan 2023 17:03:07 +0000</pubDate>
      <link>https://forem.com/nishu_2811/how-javascript-works-execution-context-21ef</link>
      <guid>https://forem.com/nishu_2811/how-javascript-works-execution-context-21ef</guid>
      <description>&lt;p&gt;Hello 👋 , In this blog, I am going to cover &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How Javascript works and how the code is executed?&lt;/li&gt;
&lt;li&gt;Is Javascript synchronous or asynchronous?&lt;/li&gt;
&lt;li&gt;Is Javascript single-threaded or Multithreaded?&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Everything in Javascript happens inside an &lt;em&gt;execution context&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can assume this execution context to be a big box or a container in which the whole JavaScript code is executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgl9je41e7alyqsvs9ngy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgl9je41e7alyqsvs9ngy.png" alt="Global Execution context" width="800" height="652"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So execution context is like a big box and it has two components in it. The first component is memory component. This is the place where all the variables and functions are stored as key-value pairs. The memory component is also known as &lt;em&gt;Variable environment&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;As you can see in the image, the Key value, if suppose we have a variable 'a' which is equivalent to 10, will be stored in memory and similarly, functions are stored over here in this memory component.&lt;/p&gt;

&lt;p&gt;The second component of this execution context is the code component. This is the place where code is executed one line at a time. The code component is also known as &lt;em&gt;Thread of Execution&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Thread of execution is like a thread in which the whole code is executed one line at a time.&lt;/p&gt;

&lt;p&gt;so here's the time for another core fundamental.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;JavaScript is a synchronous single-threaded language.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What does it mean?&lt;br&gt;
JavaScript can only execute one command at a time and when I say synchronous single-threaded that means that JavaScript can only execute one command at a time and in a specific order. That means it can only go to the next line once the current line has been finished executing.&lt;/p&gt;

&lt;p&gt;I know that you must be thinking then, we have heard of something known as AJAX, right? A Stands for Asynchronous, so what does that mean? Don't worry I will explain this in upcoming blogs.&lt;/p&gt;

&lt;p&gt;For now, just remember &lt;strong&gt;JavaScript is not possible without this beautiful execution context.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>llm</category>
      <category>ai</category>
      <category>discuss</category>
    </item>
    <item>
      <title>What is WebdriverIO?</title>
      <dc:creator>Nishant Ranjan</dc:creator>
      <pubDate>Sun, 05 Apr 2020 22:12:03 +0000</pubDate>
      <link>https://forem.com/nishu_2811/what-is-webdriverio-2g69</link>
      <guid>https://forem.com/nishu_2811/what-is-webdriverio-2g69</guid>
      <description>&lt;p&gt;WebdriverIO is a custom implementation for selenium's W3C webdriver API. It is written in Javascript and packaged into 'npm' and runs on Node.js.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--co5LdVu9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/grantnorwood.com/app/uploads/2017/07/webdriver-io-logo.png%3Fw%3D1680%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--co5LdVu9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/grantnorwood.com/app/uploads/2017/07/webdriver-io-logo.png%3Fw%3D1680%26ssl%3D1" alt="WebdriverIO" width="880" height="463"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Main Features of WebdriverIO:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;WebdriverIO is a good automation tool which can automate both web applications and native mobile Apps.&lt;/li&gt;
&lt;li&gt;It has integrated test runner which helps us to write asynchronous commands in a synchronous way so that we don’t need to care about how to handle a Promise to avoid racing conditions.&lt;/li&gt;
&lt;li&gt;It has 'wdio setup wizard' which makes our project setup very easy.&lt;/li&gt;
&lt;li&gt;We can write our own javascript functions test.&lt;/li&gt;
&lt;li&gt;The test runner also comes with a variety of hooks that allow us to interfere into the test process in order to e.g. take screenshots if an error occurs or modify the test procedure according to a previous test result.&lt;/li&gt;
&lt;li&gt;WebdriverIO services will be helpful to integrate our test to third party tools like 'Appium'.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Installation steps:
&lt;/h3&gt;

&lt;p&gt;To install webdriverIO, you will need node to be installed in your system. Please go through below mentioned post to install it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nishantranjan.in/Install%20nodes/"&gt;Install node in mac and windows&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setup your project
Before installing dependencies, we will need to initialize a new NPM project. This will allow us to use the CLI to install dependencies in your project.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir webdriverio-test &amp;amp;&amp;amp; cd webdriverio-test
$ npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The -y will answer 'yes' to all the prompts, giving you a standard NPM project. Feel free to omit the -y if you'd like to specify your own project details.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install WebdriverIO CLI
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm i --save-dev @wdio/cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Generate configuration file
Next, we will generate a configuration file to store our WebdriverIO settings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To do that, just run the configuration utility:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npx wdio config -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! The configurator will install all required packages for you and create a config file called wdio.conf.js.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nishantranjan.in/run%20test%20in%20webdriverIO/"&gt;&lt;strong&gt;In next post we will run our first spec using webdriverIO.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading. Please share your thoughts.&lt;/p&gt;

</description>
      <category>2020</category>
      <category>automation</category>
      <category>webdriverio</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
