<?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: Sandeep Nautiyal </title>
    <description>The latest articles on DEV Community by Sandeep Nautiyal  (@sandeepnautiyal).</description>
    <link>https://dev.to/sandeepnautiyal</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%2F647757%2Fc9c0f50f-9385-4b07-94ad-9973bb9e483f.jpg</url>
      <title>DEV Community: Sandeep Nautiyal </title>
      <link>https://dev.to/sandeepnautiyal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sandeepnautiyal"/>
    <language>en</language>
    <item>
      <title>Understanding Lexical Scope and Closure in JavaScript</title>
      <dc:creator>Sandeep Nautiyal </dc:creator>
      <pubDate>Mon, 29 Apr 2024 15:20:17 +0000</pubDate>
      <link>https://dev.to/sandeepnautiyal/understanding-lexical-scope-and-closure-in-javascript-4p94</link>
      <guid>https://dev.to/sandeepnautiyal/understanding-lexical-scope-and-closure-in-javascript-4p94</guid>
      <description>&lt;h2&gt;
  
  
  Intro:
&lt;/h2&gt;

&lt;p&gt;In programming, scoping refers to the accessibility of variables and functions within different parts of your code. It essentially defines where you can use a certain variable or function and where you can't. &lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical scope:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Lexical scope&lt;/strong&gt; is a specific way of determining this accessibility based on the physical location of the code.&lt;/p&gt;

&lt;p&gt;To put it simply scoping means which variable or functions can be accessed in which part of your code understanding this will help you to write better code. Js Uses lexical scoping during execution to check if the variable we are calling in our function or if the function we are trying to call is even accessible or not. there are three types of scope in js &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Global Scope.&lt;/li&gt;
&lt;li&gt;Local Scope.&lt;/li&gt;
&lt;li&gt;Block Scope.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Global Scope.
&lt;/h2&gt;

&lt;p&gt;Variables and functions declared outside of any function or a block {} are in global scope and can be accessed by any method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 10;
function sum(b){
return a+b;
}
console.log(sum(20))
//print's 30 as a is declared in global scope it can be accessible by any Method.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Local Scope
&lt;/h2&gt;

&lt;p&gt;If a variable is being declared inside a function then it can only be accessed inside that function that is a local scope or a functional scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum(b){
     var a = 10;
 // will give you 10 as it will first check for 
 // local context where it will get it's value 
 // then it will be printed
     console.log(a)
     return a+b;
}
sum(10)
console.log(a) // it will give you an err as console.log will check for the variable in global context as it is not inside the function.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Block Scope
&lt;/h2&gt;

&lt;p&gt;Now let's talk about block Scope in ES6 two new keywords were introduced &lt;strong&gt;const&lt;/strong&gt; and &lt;strong&gt;let&lt;/strong&gt; these two goes one step further in scoping as these two are block scope what I mean by that is if you declare any variable with these two keywords inside a block {} they will be discoverable inside that block only.&lt;/p&gt;

&lt;p&gt;to give you a better understanding here is a code snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(true){
    var a = 10;
    let b = 20;
// this will print 10 20
    console.log(a,b);
}
// throws err saying b not defined.
console.log(a,b)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this will help you understand better what Scopes are in js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scope Accessibility Summary:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Global Scope:&lt;/strong&gt; Variables and functions declared outside of any function are accessible from anywhere in the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Local Scope (Function Scope):&lt;/strong&gt; Variables declared within a function are only accessible within that function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Block Scope (let &amp;amp; const):&lt;/strong&gt; Variables declared with let or const within a block (like if statements or loops) are only accessible within that block.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closure
&lt;/h2&gt;

&lt;p&gt;In JavaScript, closures allow inner functions to access variables from their enclosing function's scope, even after the outer function has finished executing. This happens because the inner function remembers the reference to the outer function's variables at the time it's created. It's like the inner function "closes over" the outer function's scope, hence the term "closure."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createUserInfo(name) {
  let userName = name; // Local variable to `createUserInfo`

  function greetUser() {
    console.log("Hello, " + userName + "!");
  }

  return greetUser;
}

const greetJohn = createUserInfo("Sandeep");
greetJohn(); // Output: Hello, Sandeep!

const greetJane = createUserInfo("Aarush");
greetJane(); // Output: Hello, Aarush!

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;createUserInfo&lt;/strong&gt; takes a name as input and creates a local variable userName.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;inner function&lt;/strong&gt; greetUser is defined that uses userName.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;outer function&lt;/strong&gt; returns the greetUser function, effectively hiding the userName variable.&lt;/p&gt;

&lt;p&gt;Now, calling createUserInfo with different names ("John" and "Jane") creates separate closures. Each closure remembers its own private copy of userName. This way, you can greet different users without worrying about variable conflicts.&lt;/p&gt;

&lt;p&gt;And there you have it! By mastering lexical scope and closures, you'll be well on your way to crafting clean, maintainable, and organized JavaScript code. These concepts empower you to safeguard variables from accidental changes, establish privacy within functions, and build modular, reusable code blocks.&lt;/p&gt;

&lt;p&gt;So next time when you met an err with a variable you know exactly what went wrong.....&lt;/p&gt;

&lt;p&gt;Hope this helps! 😁😁&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Polyfills Explained: Making Modern JavaScript Work Everywhere 🤯🤯</title>
      <dc:creator>Sandeep Nautiyal </dc:creator>
      <pubDate>Fri, 26 Apr 2024 18:52:51 +0000</pubDate>
      <link>https://dev.to/sandeepnautiyal/polyfills-explained-making-modern-javascript-work-everywhere-34lc</link>
      <guid>https://dev.to/sandeepnautiyal/polyfills-explained-making-modern-javascript-work-everywhere-34lc</guid>
      <description>&lt;p&gt;Ever written code that works flawlessly on your machine, only to have it crumble when a user with an older browser tries to access it? That's where polyfills come in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Polyfills
&lt;/h2&gt;

&lt;p&gt;In simpler terms, a polyfill is a piece of code that fills the gaps between modern JavaScript features and older browsers that don't support them yet. Imagine it like a translator, taking modern code and converting it into something older browsers can understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Case for Polyfills
&lt;/h2&gt;

&lt;p&gt;Let's say you built a fantastic web app that uses the reduce method to efficiently calculate the sum of numbers in an array. But what if someone using an outdated browser tries to access it? Boom! 💥💥 Your app might malfunction because that browser doesn't recognize reduce. Here's where polyfills come to the rescue!&lt;/p&gt;

&lt;h2&gt;
  
  
  Polyfills to the Rescue!
&lt;/h2&gt;

&lt;p&gt;Polyfills mimic the behavior of new features for browsers that lack them. They essentially act as translators, converting modern code into a language older browsers can understand. This ensures your app functions smoothly for everyone, regardless of their browser version.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting Polyfills into Action: A Simple Example
&lt;/h2&gt;

&lt;p&gt;Remember our reduce method example? We can use a polyfill to ensure it works even in older browsers. Here's a simplified version of how it might look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!Array.prototype.reduce) {
  Array.prototype.reduce = function (callback, initialValue) {
    // console.log(this,"arr");
    let accumulator = initialValue !== undefined ? initialValue : this[0]; // Use initialValue or first element as accumulator

    for (
      let index = initialValue !== undefined ? 0 : 1;
      index &amp;lt; this.length;
      index++
    ) {
      accumulator = callback(accumulator, this[index], index, this); // Pass all 4 arguments to callback
    }

    return accumulator;
  };
}
const numbers = [1, 2, 3, 4];

const sum = numbers.reduce((acc, curr) =&amp;gt; acc + curr, 0); // Initial value of 0
console.log(sum); // Output: 10

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  How Does the Polyfill Work?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Conditional Check:&lt;/strong&gt; The code first checks if the reduce method already exists on the Array.&lt;a href="https://dev.to/aarushnautiyal/prototypal-inheritance-in-js-whats-that--41bg"&gt;prototype&lt;/a&gt; object. If not, it proceeds to define the reduce method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accumulator and Loop:&lt;/strong&gt; The reduce method takes two arguments: a callback function and an optional initial value. It sets up an accumulator variable to store the ongoing result and iterates through the array using a for loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Callback Execution:&lt;/strong&gt; Inside the loop, the callback function is called for each element in the array, passing four arguments: the current accumulator value, the current element, the current element's index, and the entire array itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Returning the Result:&lt;/strong&gt; After iterating through all elements, the final value of the accumulator is returned, representing the accumulated result of the callback function applied to each element.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Power of Polyfills
&lt;/h2&gt;

&lt;p&gt;By incorporating polyfills, you ensure your web app functions flawlessly for a wider audience, regardless of their browser version. It's a win-win situation: you get to leverage the latest JavaScript features while maintaining compatibility with older browsers. Polyfills are a valuable tool for any developer who wants to create future-proof and user-friendly web applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, the next time you're coding with modern JavaScript features, remember the power of polyfills. They'll help you bridge the gap between cutting-edge development and real-world user experience!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Prototypal Inheritance in JS 🤔🤔 what's that ?</title>
      <dc:creator>Sandeep Nautiyal </dc:creator>
      <pubDate>Thu, 25 Apr 2024 18:55:16 +0000</pubDate>
      <link>https://dev.to/sandeepnautiyal/prototypal-inheritance-in-js-whats-that--41bg</link>
      <guid>https://dev.to/sandeepnautiyal/prototypal-inheritance-in-js-whats-that--41bg</guid>
      <description>&lt;h2&gt;
  
  
  Inheritance in General
&lt;/h2&gt;

&lt;p&gt;Before jumping into prototypal inheritance, let's talk about inheritance in general. Inheritance refers to the ability of an object (child) to inherit properties and methods from another object (parent).  This is a fundamental concept in object-oriented programming (OOP).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy:&lt;/strong&gt;  A classic example of inheritance is a child inheriting traits from its parents.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript and Inheritance:
&lt;/h2&gt;

&lt;p&gt;While many OOP languages use classes for inheritance, JavaScript takes a different approach: &lt;strong&gt;prototypal inheritance&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prototypal Inheritance
&lt;/h2&gt;

&lt;p&gt;In JavaScript, instead of relying on classes, inheritance is achieved through prototypes. A prototype is a special object that serves as a template for creating new objects. It defines the properties and methods that will be inherited by these new objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's understand this with some code:&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;// Define an Employee prototype (the master chef) using a constructor function
function Employee(empId, name, department) {
  this.empId = empId;
  this.name = name;
  this.department = department;
}

// Add a greet method to the prototype (a signature recipe)
Employee.prototype.greet = function() {
  console.log(`Hello, ${this.name} Welcome to xyz`);
};

// Create new Employee objects (the apprentices) using the Employee constructor
const emp1 = new Employee(30, "Alice", "QA");
const emp2 = new Employee(25, "Bob", "IT");

emp1.greet(); // Output: "Hello, Alice Welcome to xyz"
emp2.greet(); // Output: "Hello, Bob Welcome to xyz"

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fun Fact:&lt;/strong&gt;  The class syntax we use in JavaScript is actually syntactic sugar over a more fundamental concept – prototypes. Under the hood, classes create constructor functions and prototypes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyy6z30rmakbnn2ngm0ur.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyy6z30rmakbnn2ngm0ur.png" alt="Image description" width="578" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Everything in JavaScript is Connected!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's a mind-blowing secret: almost everything in JavaScript is connected to a prototype! You can verify this in your console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 10;
console.log(a.__proto__); // This will show you a whole object – that's the prototype!

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Power of Prototypal Inheritance
&lt;/h2&gt;

&lt;p&gt;One of the benefits of prototypal inheritance is its flexibility.  Unlike class-based inheritance, you can modify prototypes even after objects have been created. This allows you to add new methods or properties to existing objects dynamically.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: Adding a Custom Method to the Array Prototype
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.myReduce = function() {
  // Implement your custom reduce functionality here
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; While prototypal inheritance offers flexibility, it can sometimes lead to less structured code compared to traditional class-based inheritance. However, with a good understanding of prototypes and the prototype chain, you can become a master of object-oriented programming in JavaScript!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In the next article, we'll explore how to implement your own reduce method using prototypal inheritance, Until then, happy coding!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope this helps! 😁😁&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What are Classes and How to Use Them in JavaScript ? 🤔🤔</title>
      <dc:creator>Sandeep Nautiyal </dc:creator>
      <pubDate>Wed, 24 Apr 2024 06:33:02 +0000</pubDate>
      <link>https://dev.to/sandeepnautiyal/what-are-classes-and-how-to-use-them-in-javascript--3hp9</link>
      <guid>https://dev.to/sandeepnautiyal/what-are-classes-and-how-to-use-them-in-javascript--3hp9</guid>
      <description>&lt;p&gt;JavaScript, prior to ES6 (ECMAScript 2015), relied on constructor functions to mimic object-oriented concepts. These functions act as blueprints for creating objects, but lack some of the features and structure offered by modern classes.&lt;br&gt;
Let's see a basic implementation of a constructor function for reference...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Person(name) {
  this.name = name;
  this.greet = function() {
    console.log("Hello, my name is " + this.name);
  };
}
const person1 = new Person("Sandeep");

person1.greet(); // Output: "Hello, my name is Sandeep"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the question is if we already have something like this why to introduce something new, why to introduce classes, well thing is these functions have some limitations for example:-&lt;/p&gt;

&lt;p&gt;No Inheritance: While you can mimic inheritance patterns using prototypes, it's less straightforward compared to classes.&lt;br&gt;
Verbosity: Defining methods within the constructor function can lead to repetitive code, especially for complex objects.&lt;/p&gt;

&lt;p&gt;that is why Classes were introduced in ES6 to  addressed these shortcomings by providing a more structured and concise way to define objects and their behaviour.&lt;/p&gt;

&lt;p&gt;Well Now enough with why they were introduced and let's jump into how to use them properly.&lt;/p&gt;

&lt;p&gt;To create a class all you have to do is use a keyword to define it which is called (wait for it, wait for it, drum roll please 🥁 🥁) "class" lol 🤣🤣. Here is a code snippet of a class in action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person{
    constructor(name){
        this.name=name;
    }
    greet(){
        return `Hello, my name is ${this.name}`
    }
}

const sandeep = new Person("sandeep");

console.log(sandeep.greet()); 
//Output:'Hello, my name is sandeep'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are some key benefits of using classes in JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reusability&lt;/strong&gt;: Classes allow you to create multiple objects with the same structure and functionality, reducing code duplication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Maintainability&lt;/strong&gt;: Code is easier to understand and modify when it's organised into classes. Each class encapsulates its own data and logic, making it more self-contained.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Object-Oriented Patterns&lt;/strong&gt;: Classes facilitate the use of common object-oriented design patterns like inheritance and polymorphism ✨&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now your next question will be "Sandeep, we got it, why classes were introduced and how to implement them, but where to implement them? Well, that's something we will discuss in the next article. Until then, happy coding!"&lt;/p&gt;

&lt;p&gt;I hope this helps! 😁😁&lt;/p&gt;

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