<?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: ashwani3011</title>
    <description>The latest articles on DEV Community by ashwani3011 (@ashwani3011).</description>
    <link>https://dev.to/ashwani3011</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%2F838577%2F674ba6b0-edb3-48b3-a2c1-5d2b8d2d257e.jpeg</url>
      <title>DEV Community: ashwani3011</title>
      <link>https://dev.to/ashwani3011</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashwani3011"/>
    <language>en</language>
    <item>
      <title>Destructuring and Spread Operators: Why and When to Use?</title>
      <dc:creator>ashwani3011</dc:creator>
      <pubDate>Sat, 15 Jul 2023 14:37:57 +0000</pubDate>
      <link>https://dev.to/ashwani3011/destructuring-and-spread-operators-why-and-when-to-use-2h4g</link>
      <guid>https://dev.to/ashwani3011/destructuring-and-spread-operators-why-and-when-to-use-2h4g</guid>
      <description>&lt;p&gt;Imagine you're a detective trying to solve a crime. You've just received a list of suspects and their corresponding alibis from your informant. However, the list is a mess and it's difficult to find the information you need.&lt;/p&gt;

&lt;p&gt;That's where destructuring and spread operators come in. With these tools, we can quickly extract the relevant information from the list and work with it in a more organized and efficient way.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⭐ Destructuring
&lt;/h2&gt;

&lt;p&gt;Destructuring allows us to extract values from arrays and objects and assign them to variables in a concise syntax.&lt;/p&gt;

&lt;h3&gt;
  
  
  Destructuring an array:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const suspects = ['Alice', 'Bob', 'Mallory'];
const [firstSuspect, secondSuspect] = suspects;
console.log(firstSuspect); // 'Alice'
console.log(secondSuspect); // 'Bob'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we were able to extract the first &amp;amp; second elements of the suspects array and assign them to variables using destructuring.&lt;/p&gt;

&lt;h3&gt;
  
  
  Destructuring an object:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const suspects = {
 'Alice': 'at the library',
 'Bob': 'at the gym',
 'Eve': 'at home',
 'Mallory': 'at the park'
};
const { Alice, Bob, Eve } = suspects;
console.log(Alice); // 'at the library'
console.log(Bob); // 'at the gym'
console.log(Eve); // 'at home'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ⭐ Spread Operator
&lt;/h2&gt;

&lt;p&gt;The spread operator allows us to add or spread out elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add elements to an array:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const suspects = ['Alice', 'Bob', 'Eve', 'Mallory'];
const newSuspects = ['Frank', 'Gina', ...suspects];
console.log(newSuspects); // ['Frank', 'Gina', 'Alice', 'Bob', 'Eve', 'Mallory']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, we used the spread operator to add the elements from the suspects array to the newSuspects array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Spread out elements in an array:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const suspects = ['Alice', 'Bob', 'Eve', 'Mallory'];
console.log(...suspects); // 'Alice', 'Bob', 'Eve', 'Mallory'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ⭐ When to Use
&lt;/h2&gt;

&lt;p&gt;1️⃣ When we want to extract multiple properties from an object: Instead of manually selecting each property, we can destructure them into separate variables.&lt;/p&gt;

&lt;p&gt;2️⃣ When we want to set default values: If a value is not present in the object, destructuring allows us to set a default value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { Alice, Bob = "No evidence, but doubtful" } = suspects;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3️⃣ When we want to pass object properties as function parameters: Destructuring can be used to unpack object properties directly into function parameters.&lt;/p&gt;

&lt;p&gt;4️⃣ When we want to clone an object or array: The spread operator allows us to easily create a copy of an object or array.&lt;/p&gt;

&lt;p&gt;5️⃣ When we want to merge objects or arrays: We can use the spread operator to combine multiple objects or arrays into one.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⭐ Why to Use
&lt;/h2&gt;

&lt;p&gt;1️⃣ It simplifies the syntax when dealing with data stored in objects and arrays.&lt;/p&gt;

&lt;p&gt;2️⃣ Destructuring reduces the amount of code needed to extract data from objects and arrays.&lt;/p&gt;

&lt;p&gt;3️⃣ Spread operator helps to make our code more readable and maintainable.&lt;/p&gt;

&lt;p&gt;4️⃣ Spread operator provides a simple syntax for combining and copying data structures.&lt;/p&gt;

&lt;p&gt;5️⃣ Spread operator provides an easy way to expand elements in iterable objects.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Can you explain the meaning of '^1.2.3' in a package.json file?</title>
      <dc:creator>ashwani3011</dc:creator>
      <pubDate>Sun, 09 Jul 2023 13:23:38 +0000</pubDate>
      <link>https://dev.to/ashwani3011/can-you-explain-the-meaning-of-123-in-a-packagejson-file-4l38</link>
      <guid>https://dev.to/ashwani3011/can-you-explain-the-meaning-of-123-in-a-packagejson-file-4l38</guid>
      <description>&lt;p&gt;In this post, let's dive into npm versioning together and uncover its significance!&lt;/p&gt;

&lt;p&gt;🌟 npm Versioning Basics:&lt;/p&gt;

&lt;p&gt;1️⃣ Major Version (X.y.z): Signifies significant changes, often incompatible with previous versions.&lt;/p&gt;

&lt;p&gt;2️⃣ Minor Version (x.Y.z): Introduces new features while maintaining backward compatibility.&lt;/p&gt;

&lt;p&gt;3️⃣ Patch Version (x.y.Z): Reserved for bug fixes and minor updates, with no new features.&lt;/p&gt;

&lt;p&gt;🌟 Symbols:&lt;/p&gt;

&lt;p&gt;1️⃣ Tilde (~):&lt;/p&gt;

&lt;p&gt;~ allows patch updates and compatible bug fixes.&lt;br&gt;
Example: ~1.2.3 includes 1.2.4, 1.2.9, but not 1.3.0.&lt;/p&gt;

&lt;p&gt;2️⃣ Caret (^):&lt;/p&gt;

&lt;p&gt;^ includes minor and patch updates, including new features.&lt;br&gt;
Example: ^1.2.3 includes 1.2.4, 1.3.0, but not 2.0.0.&lt;/p&gt;

&lt;p&gt;🤔 So, how can we use this knowledge in our projects?&lt;/p&gt;

&lt;p&gt;1️⃣ If you're developing an open-source library:&lt;/p&gt;

&lt;p&gt;🌟 Increment the major version for breaking changes and major updates.&lt;/p&gt;

&lt;p&gt;🌟 Increment the minor version for introducing new features and enhancements while maintaining backward compatibility.&lt;/p&gt;

&lt;p&gt;🌟 Increment the patch version for bug fixes and minor updates that don't introduce new features.&lt;/p&gt;

&lt;p&gt;2️⃣ When using someone else's library.&lt;/p&gt;

&lt;p&gt;🌟 Use the ~ (tilde) symbol to allow patch updates and compatible bug fixes.&lt;/p&gt;

&lt;p&gt;🌟 Use the ^ (caret) symbol to allow minor and patch updates, including new features.&lt;/p&gt;

&lt;p&gt;🌟 Specify an exact version (without symbol) when you want to be precise about the dependency version used.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Console.time() and Console.timeEnd()</title>
      <dc:creator>ashwani3011</dc:creator>
      <pubDate>Fri, 29 Apr 2022 09:20:57 +0000</pubDate>
      <link>https://dev.to/ashwani3011/consoletime-and-consoletimeend-4p3f</link>
      <guid>https://dev.to/ashwani3011/consoletime-and-consoletimeend-4p3f</guid>
      <description>&lt;p&gt;When people use web applications, they want them to deliver fast and efficient performance. As a result, speed has become one of the top metrics that people use to assess the quality of an application.&lt;/p&gt;

&lt;p&gt;So, as a developer when we start working on a real-life application, then it's not only about achieving the desired result, it's also about how performant our code is?&lt;/p&gt;

&lt;p&gt;Generally there are multiple ways a work can be done and it becomes important to take an informed decision while opting for a particular solution.&lt;/p&gt;

&lt;p&gt;⭐ The console object's &lt;strong&gt;time()&lt;/strong&gt; and &lt;strong&gt;timeEnd()&lt;/strong&gt; methods can be used to analyse the performance of a piece of code.&lt;/p&gt;

&lt;p&gt;First we call &lt;strong&gt;console.time()&lt;/strong&gt; by providing a string argument, then the code that we want to test, then we call **console.timeEnd() **with the same string argument. That's all we need to do, it will now show us the time it took to run this piece of code in our browser console.&lt;/p&gt;

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

&lt;p&gt;Let's swap two variables in two different ways, and let's find which one is more time-efficient.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Destructuring assignment&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;let a = 3;
let b = 4;
console.time("Destructuring_assignment");
[a, b] = [b, a];
console.timeEnd("Destructuring_assignment");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;O/P&lt;/strong&gt; - &lt;code&gt;Destructuring_assignment: 0.017333984375 ms&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Temporary 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;let a = 3;
let b = 4;
let temp;
console.time("Temporary_variable");
temp = a;
a = b;
b = temp;
console.timeEnd("Temporary_variable");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;O/P&lt;/strong&gt; - &lt;code&gt;Temporary_variable: 0.008056640625 ms&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can see that by using these techniques, we can quickly obtain the elapsed time in milliseconds, which will aid us in identifying the bottleneck in our code and refactoring it.&lt;/p&gt;

&lt;p&gt;⛔Note: In this post, I'm talking about the solution by only considering the time factor. There are other factors also that need to be considered while working on a project.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Cookies, Local Storage &amp; Session Storage</title>
      <dc:creator>ashwani3011</dc:creator>
      <pubDate>Sun, 24 Apr 2022 07:45:41 +0000</pubDate>
      <link>https://dev.to/ashwani3011/cookies-local-storage-session-storage-587a</link>
      <guid>https://dev.to/ashwani3011/cookies-local-storage-session-storage-587a</guid>
      <description>&lt;p&gt;These are storage methods provided by the browser, to store information on the user's browser.&lt;/p&gt;

&lt;p&gt;Now let's see what's the difference between them and how can we use them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Local storage
&lt;/h2&gt;

&lt;p&gt;➡️ &lt;strong&gt;Capacity&lt;/strong&gt; - 10MB&lt;br&gt;
➡️ &lt;strong&gt;Expiry&lt;/strong&gt; - Never&lt;br&gt;
➡️ &lt;strong&gt;Data accessibility&lt;/strong&gt; - Any tab of a browser&lt;/p&gt;

&lt;p&gt;➡️ &lt;strong&gt;Syntax&lt;/strong&gt; -&lt;/p&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Storing data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;localStorage.setItem('name', 'Ashwani')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;// Here the first parameter is the name and the second parameter is the value, both need to be in string format&lt;/p&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;Getting Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;localStorage.getItem('name')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;// We need to specify the key for which we want to get the data&lt;/p&gt;

&lt;p&gt;3️⃣ &lt;strong&gt;Removing Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;localStorage.removeItem('name')&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Session Storage
&lt;/h2&gt;

&lt;p&gt;➡️ &lt;strong&gt;Capacity&lt;/strong&gt; - 5MB&lt;br&gt;
➡️ &lt;strong&gt;Expiry&lt;/strong&gt; - On tab close&lt;br&gt;
➡️ &lt;strong&gt;Data accessibility&lt;/strong&gt; - Same tab&lt;/p&gt;

&lt;p&gt;➡️ &lt;strong&gt;Syntax&lt;/strong&gt; - Exact same methods as localStorage&lt;/p&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Storing data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sessionStorage.setItem('name', 'Ashwani')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;Getting Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sessionStorage.getItem('name')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3️⃣ &lt;strong&gt;Removing Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sessionStorage.removeItem('name')&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Cookies
&lt;/h2&gt;

&lt;p&gt;➡️ &lt;strong&gt;Capacity&lt;/strong&gt; - 4KB&lt;br&gt;
➡️ &lt;strong&gt;Expiry&lt;/strong&gt; - Manually set&lt;br&gt;
➡️ &lt;strong&gt;Data accessibility&lt;/strong&gt; - Any tab of a browser&lt;/p&gt;

&lt;p&gt;➡️ &lt;strong&gt;Syntax&lt;/strong&gt; -&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;document.cookie&lt;/strong&gt; is used both for setting and getting the data.&lt;/p&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Storing data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;document.cookie = 'name = Ashwani'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;⬇️ &lt;strong&gt;Manually setting expiry&lt;/strong&gt; - we can use the expiry or max-age property.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;document.cookie = "lastName = jha ; max-age = 10"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;Getting Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(document.cookie)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;// There's not any way to get a particular cookie data.&lt;/p&gt;

&lt;p&gt;⭐ As we now know that all these three are methods to store data in the browser, but with cookie we can store data on the server as well.&lt;/p&gt;

&lt;p&gt;Now, let's conclude this -&lt;/p&gt;

&lt;p&gt;Most of the time we prefer local storage or session storage due to its simple syntax and large capacity, we use cookie when we need data on the server (e.g. authentication).&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Function Currying in JavaScript</title>
      <dc:creator>ashwani3011</dc:creator>
      <pubDate>Tue, 29 Mar 2022 07:30:43 +0000</pubDate>
      <link>https://dev.to/ashwani3011/function-currying-in-javascript-47k1</link>
      <guid>https://dev.to/ashwani3011/function-currying-in-javascript-47k1</guid>
      <description>&lt;p&gt;With the rapid rise of functional programming in JavaScript, a lot of attention has been given to the curried function. So in this post, let's discuss this.&lt;/p&gt;

&lt;p&gt;⬇️ What is Function Currying ❓&lt;/p&gt;

&lt;p&gt;Currying is a transformation that can be applied to functions to allow them to take one less argument than previously.&lt;/p&gt;

&lt;p&gt;Okay okay, let's simplify this&lt;/p&gt;

&lt;p&gt;Currying is when a function, rather than taking all arguments at once, takes the first one and returns a new function, which then takes the second one and returns a new function, and so on until all arguments are completed.&lt;/p&gt;

&lt;p&gt;⬇️ Uses and Need of Function Currying ❓&lt;/p&gt;

&lt;p&gt;➡️ It helps us to avoid passing the same variable again and again.&lt;/p&gt;

&lt;p&gt;➡️ It separates our function into several smaller functions, each of which can handle a single duty. This ensures that our function is error-free and free of side effects.&lt;/p&gt;

&lt;p&gt;➡️ It is used in functional programming to create a higher-order function.&lt;/p&gt;

&lt;p&gt;➡️ It is extremely useful in event handling.&lt;/p&gt;

&lt;p&gt;⬇️ How can we do Function Currying ❓&lt;/p&gt;

&lt;p&gt;There are multiple ways by which we can do function currying, in this post we will discuss one of that methods.&lt;/p&gt;

&lt;p&gt;⭐ Function Currying using Closures&lt;/p&gt;

&lt;p&gt;function add (a) {&lt;br&gt;
return function (b) {&lt;br&gt;
console.log ( a + b );&lt;br&gt;
};&lt;br&gt;
}&lt;br&gt;
add(2)(5);&lt;/p&gt;

&lt;p&gt;Confused 😕❓, Let's discuss&lt;/p&gt;

&lt;p&gt;Here, the result of add(a) is the wrapper function(b).&lt;/p&gt;

&lt;p&gt;When it's called add(2), the argument is saved in the Lexical Environment, and a new wrapper is returned function(b).&lt;/p&gt;

&lt;p&gt;Then this wrapper is called with ‘5’ as an argument, this wrapper will already have access to ‘a’ due to closure, which is currently equal to ‘2’, so we will get ‘7’ printed on the console.&lt;/p&gt;

&lt;p&gt;Currying is a difficult concept to grasp. But, with time and practice, we can certainly get the hang of it.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
      <category>interview</category>
    </item>
  </channel>
</rss>
