<?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: Saurabh sharma</title>
    <description>The latest articles on DEV Community by Saurabh sharma (@saurabh2412).</description>
    <link>https://dev.to/saurabh2412</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%2F523121%2Fe4da62bf-25b5-46d2-93a8-3c0d74d19d1f.jpeg</url>
      <title>DEV Community: Saurabh sharma</title>
      <link>https://dev.to/saurabh2412</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saurabh2412"/>
    <language>en</language>
    <item>
      <title>bind(), call() &amp; apply() in JS</title>
      <dc:creator>Saurabh sharma</dc:creator>
      <pubDate>Wed, 15 Sep 2021 04:22:01 +0000</pubDate>
      <link>https://dev.to/saurabh2412/bind-call-apply-in-js-9ne</link>
      <guid>https://dev.to/saurabh2412/bind-call-apply-in-js-9ne</guid>
      <description>&lt;p&gt;In this you will get a deep knowledge about how call(), bind() and apply works.&lt;/p&gt;

&lt;p&gt;These methods are available to every function in JavaScript. which are all used to change the scope of what this is equal to inside of a function or a method. Let's see the following examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;bind()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;We will start with a simple example of an object that has a method on it. With bind(), we can tell the JavaScript engine where to look for this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = {
  name: "Saurabh",
  myFunction: function () {
    return(`Hello ${this.name}`)
  }
}

function greetPerson() {
  console.log(this.myFunction())
}

const bindPerson = greetPerson.bind(person)

bindPerson();
//Expected Output :- Hello Saurabh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Things to notice here are : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;bind()&lt;/code&gt; creates bindPerson, and a copy of the greetPerson function.&lt;/li&gt;
&lt;li&gt;bindPerson when called will have it's this variable pointing to the person object&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;call()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;call()&lt;/code&gt; calls a function with a given this value and arguments provided individually.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;call()&lt;/code&gt; essentially does the same thing as &lt;code&gt;bind()&lt;/code&gt; except that &lt;code&gt;call()&lt;/code&gt; actually executes the function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Saurabh = {
  name: "Saurabh",
  myFunction: function () {
    return(`Hi, I'm ${this.name}`)
  }
}

function callingFunction(age, hobby) {
  console.log(`${this.myFunction()}, my age is ${age} and I 
  like to ${hobby}`);
}

callingFunction.call(Saurabh, 26, 'reading');

// Expected Output :- Hi, I'm Saurabh, my age is 26 and I like reading

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;apply()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;call()&lt;/code&gt; and &lt;code&gt;apply()&lt;/code&gt; do the exact same thing except that &lt;code&gt;call()&lt;/code&gt; expects all parameters to be passed in individually, but &lt;code&gt;apply()&lt;/code&gt; expects all additional parameters to be passed as an array.&lt;br&gt;
so this what our existing sample code will look like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Saurabh = {
  name: "Saurabh",
  myFunction: function () {
    return(`Hi, I'm ${this.name}`)
  }
}

function applyingFunction(age, hobby) {
  console.log(`${this.myFunction()}, my age is ${age} and I like 
  ${hobby}`)
}

applyingFunction.apply(Saurabh, ``[26, 'Reading']``)

// Expected Output :- Hi, I'm Saurabh, my age is 26 and I like Reading
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope you find it useful and learned something new from it.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How does JavaScript treats its functions and variable environment..?</title>
      <dc:creator>Saurabh sharma</dc:creator>
      <pubDate>Tue, 14 Sep 2021 14:02:07 +0000</pubDate>
      <link>https://dev.to/saurabh2412/how-does-javascript-treats-its-functions-and-variable-environment-5e6d</link>
      <guid>https://dev.to/saurabh2412/how-does-javascript-treats-its-functions-and-variable-environment-5e6d</guid>
      <description>&lt;p&gt;Before starting it let's check out code snip.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 1;
a();
console.log(x);

function a() {
  var x = 10;
  console.log(x);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can actively participate and with me to understand the workflow of the program(assuming that you are having basic knowledge about how to write a function in html and JavaScript file and how to run that set of file.) and how things works behind the scene..&lt;br&gt;
after running the code go to browser inspector and there you can check out the result in console tab and source tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y_5847hV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjgwklchelviw39wor56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y_5847hV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjgwklchelviw39wor56.png" alt="executed result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now lets understand the flow step by step&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xTEix3wL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbavwe14swmywmrc056g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xTEix3wL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbavwe14swmywmrc056g.png" alt="debugger1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Over here browser engine creates a execution context for the program. It has been pushed into the call stack and the control is at line 1&lt;br&gt;
Before going further lets have a walk through on Execution context and call stack.&lt;br&gt;
The execution context : &lt;br&gt;
It is an internal JavaScript concept. Here JavaScript engine use to track the execution of code. The execution context is divided in two components&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Memory Phase : In memory phase, javascript allocates memory to the variables and functions. For the variable, it allocates the keyword 'undefined' and for the functions, it allocates its function body itself as the memory&lt;/li&gt;
&lt;li&gt;Code Execution Phase: In code execution phase javascript executes the program lines in order. 
This is what global execution context is.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Call Stack : &lt;br&gt;
The JavaScript Engine creates a stack data structure where function execution contexts are placed on creation — when the function is called or invoked. This data structure itself is called the Call Stack.&lt;/p&gt;

&lt;p&gt;going further in our program the control is on line 1. Javascript allocated undefined to the variable 'X' and to the function a() it has allocated its function body.&lt;/p&gt;

&lt;p&gt;after debugging line 2 we got to know that 'X' variable is considered as global variable and its value is 1.&lt;/p&gt;

&lt;p&gt;Now let's put the debugger at line 6.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6YYa2FmP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbjp3rhwjbzuigqbu3qi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6YYa2FmP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbjp3rhwjbzuigqbu3qi.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here javascript creates new execution context for function a. This is been pushed to the call stack and then the control is now given to function which is at line number 5. As you can visualize we have a variable declared in function so a new local memory and global memory gets created and it will only considered for the function a.&lt;br&gt;
let's check line no 7. here javascript looks for variable 'X' in local memory and allocates the value 10.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--20oqYOJW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mol4jw6ys0rrwekoubel.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--20oqYOJW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mol4jw6ys0rrwekoubel.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;as you can notice that we have 1 more variable as 'X' in global scope&lt;/p&gt;

&lt;p&gt;for examine we have to put the debugger at line 3&lt;/p&gt;

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

&lt;p&gt;You can notice that in the call stack the execution context for function a() just got popped out and now the control is in line 3 back to the global execution context. Javascript ran the remaining program. It looked for variable 'X' in global memory and allocates the value '1' to variable 'X'. It now completed the global execution context in the call stack. And now the global execution context just got popped out of the stack. The call stack is now empty.&lt;/p&gt;

&lt;p&gt;I Hope you find it useful and learned something new from it.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Cookies or JWT..? 🤔</title>
      <dc:creator>Saurabh sharma</dc:creator>
      <pubDate>Wed, 08 Sep 2021 05:49:55 +0000</pubDate>
      <link>https://dev.to/saurabh2412/cookies-or-jwt-o2i</link>
      <guid>https://dev.to/saurabh2412/cookies-or-jwt-o2i</guid>
      <description>&lt;h2&gt;
  
  
  Cookie Based Authentication
&lt;/h2&gt;

&lt;p&gt;First let us focus on what are cookies..?&lt;br&gt;
Cookies are text formatted files with small piece of data included in it like username and password that are used to identify your system. Such cookies know as HTTP cookies and are used to identify specific user.&lt;/p&gt;

&lt;p&gt;Now u might be thinking what are this HTTP cookies??&lt;br&gt;
lets dive into it. HTTP cookies are built specifically for web browsers to track and save each users information about there sessions(sessions are the time you spent on that particular site).Cookies are created to identify you when you visit a new website. The web server stores the website data and sends a short stream to identify info of a web browser.&lt;br&gt;
If a user returns to that site in the future, the web browser returns that data to the web server in the form of a cookie. This is when your browser will send it back to the server to recall data from your previous sessions.&lt;/p&gt;

&lt;p&gt;To put it general words, cookies are more likely getting a ticket,&lt;br&gt;
'You wanna a access the service' you pay for it and in return you get token or id for reference. This referral token needs to be stored locally in browser and should get triggered while making service calls which helps server to identify the browser or user info.&lt;br&gt;
If in case you leave or return to the system this cookies helps server to serve the user at last where user has ended his last session.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fcc00ddh8xphfspi20gzj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fcc00ddh8xphfspi20gzj.jpg" alt="Cookie based athentication"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The client sends a login request(with credentials) to the server from his device.&lt;/li&gt;
&lt;li&gt;The server then validates the credentials. If the credentials are correct login is successful, the web server will create a session in the database.&lt;/li&gt;
&lt;li&gt;This session ID is stored in a cookie on the user's browser and while the user is logged in the cookie is sent with every subsequent request.&lt;/li&gt;
&lt;li&gt;At each request the server takes a look at the cookie to read the session id. If it matches the data stored in its memory it sends the response back.&lt;/li&gt;
&lt;li&gt;If in case the logout request takes place the server will make the cookie expire by deleting it from the database.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Token Based Authentication
&lt;/h2&gt;

&lt;p&gt;Now lets us have a look in Token-Based-Authentication:&lt;br&gt;
Here, we store the user's data on the client side itself. This is achieved with JSON Web Token(JWT). It is a standard and a secure way of transmitting information between client and server in the form of JSON.&lt;br&gt;
now lets dive into JWT.&lt;br&gt;
JWT has three forms of data and is separated by dot(.)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First section is know as header it consist of algorithm and type. &lt;/li&gt;
&lt;li&gt;Second section consist of payload and it consist of user information.&lt;/li&gt;
&lt;li&gt;Third section consist of encrypted signature.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is what sample token looks like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.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%2Fyx7qtmla4uo2032tt4sd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fyx7qtmla4uo2032tt4sd.jpg" alt="JWT Based Authentication"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The user submits login credentials.&lt;/li&gt;
&lt;li&gt;On receiving the request the server verifies the credentials and generates an encrypted JWT with a secret and sends back to the client.&lt;/li&gt;
&lt;li&gt;On client side the browser stores the token locally(in local storage, session storage or cookie storage).&lt;/li&gt;
&lt;li&gt;On future requests the token is added to the authorization header and then its server duty to decode the token before proceeding the request this check is basically know as middleware authentication.&lt;/li&gt;
&lt;li&gt;If in case the logout request takes place the token gets deleted without any server interaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Differences between JSON Web Tokens and Session Cookies..?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cryptographic Signature: JSON web tokens have cryptographic signatures, and that’s not the case with session cookies.&lt;/li&gt;
&lt;li&gt;Stateless: JSON web tokens are stateless because tokens are stored at client-side in local storage, rather than in the server memory.&lt;/li&gt;
&lt;li&gt;Scalability: As session cookies are stored in the server’s memory, it has the potential of using a lot more resources if the website or app sees a lot of traffic though JSON web tokens are stateless, they can potentially save on server resources in many cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fzxv5093klvjlt4msiqqs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fzxv5093klvjlt4msiqqs.png" alt="Differences"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;JSON web tokens and session cookies both offer secure user authentication, but they have key differences that make them suitable in varying situations. Hope you find it useful and learned something new from it.&lt;/p&gt;

</description>
      <category>authentication</category>
      <category>jwt</category>
      <category>cookies</category>
    </item>
  </channel>
</rss>
