<?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: Navin Mishra</title>
    <description>The latest articles on DEV Community by Navin Mishra (@navinmishra1717).</description>
    <link>https://dev.to/navinmishra1717</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%2F876847%2F2bf9cba3-3243-470b-97dd-da26e2fc0520.jpeg</url>
      <title>DEV Community: Navin Mishra</title>
      <link>https://dev.to/navinmishra1717</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/navinmishra1717"/>
    <language>en</language>
    <item>
      <title>7 best practices for arrays in javascript</title>
      <dc:creator>Navin Mishra</dc:creator>
      <pubDate>Wed, 12 Apr 2023 10:31:33 +0000</pubDate>
      <link>https://dev.to/navinmishra1717/7-best-practices-for-arrays-in-javascript-9ge</link>
      <guid>https://dev.to/navinmishra1717/7-best-practices-for-arrays-in-javascript-9ge</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IAPjQfrJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dy0egbmwh9vb26lok166.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IAPjQfrJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dy0egbmwh9vb26lok166.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Arrays are a fundamental data structure in JavaScript, and are used in nearly every application. But working with arrays can be messy and inefficient if you don't use the right methods and techniques. In this post, we'll explore some array methods and techniques that will help you write cleaner, more efficient code.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. forEach : The Gentle Giant of Array Methods
&lt;/h2&gt;

&lt;p&gt;When it comes to iterating over an array, the forEach() method is the gentle giant. It quietly steps through each element in the array, without making a fuss. It doesn't care if you mutate the array or not. It just wants to be helpful.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  2. map : The Transformer
&lt;/h2&gt;

&lt;p&gt;The map() method transforms each element in an array into a new element. It's like a magical machine that takes in old junk and spits out shiny new things.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  3. filter : The Cleaner
&lt;/h2&gt;

&lt;p&gt;The filter() method is like a cleaning crew for your array. It filters out elements that don't belong, leaving behind only the good stuff. &lt;br&gt;
&lt;em&gt;&lt;strong&gt;Bonus&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
some() method checks if at least one element in an array satisfies a condition and returns boolean value. &lt;strong&gt;And&lt;/strong&gt;, find() method returns the first element in an array that satisfies a provided testing function. It returns &lt;strong&gt;undefined&lt;/strong&gt; if no element satisfies the condition&lt;/p&gt;

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

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

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

&lt;h2&gt;
  
  
  4. reduce : The Calculator
&lt;/h2&gt;

&lt;p&gt;The reduce() method is like a calculator for your array. It takes in an array of numbers and crunches them down into a single number.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  5. spread operator : The Expander
&lt;/h2&gt;

&lt;p&gt;The spread operator is like a can of compressed air that expands when you use it. It expands an array into individual elements, making it easy to pass them around as separate arguments.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  6. destructuring : The Unpacker
&lt;/h2&gt;

&lt;p&gt;Destructuring is like a reverse present wrapper. It unpacks the contents of an array and assigns them to individual variables.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  7. deduplication : The Remover
&lt;/h2&gt;

&lt;p&gt;Deduplication is the process of removing duplicate values from an array. The &lt;strong&gt;Set&lt;/strong&gt; constructor is used to create a new set object, which automatically removes duplicates.&lt;/p&gt;

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




&lt;h4&gt;
  
  
  &lt;strong&gt;Finally&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading&lt;/strong&gt;. I Hope you find this article useful. Please leave a comment for mistakes so that i can improve in my next articles. And follow for more quality articles. &lt;em&gt;Happy reading!!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>8 Javascript useful techniques</title>
      <dc:creator>Navin Mishra</dc:creator>
      <pubDate>Wed, 15 Mar 2023 06:14:54 +0000</pubDate>
      <link>https://dev.to/navinmishra1717/10-javascript-useful-techniques-m1g</link>
      <guid>https://dev.to/navinmishra1717/10-javascript-useful-techniques-m1g</guid>
      <description>&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%2Fucarecdn.com%2F01292099-b782-4b74-a05e-f902be3feecd%2F" 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%2Fucarecdn.com%2F01292099-b782-4b74-a05e-f902be3feecd%2F" alt="js cover"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  This article collects some common javascript techniques for clean code and better development efficiency
&lt;/h6&gt;

&lt;h2&gt;
  
  
  1. Converting string to number.
&lt;/h2&gt;

&lt;p&gt;We might be using Number() and parseInt() to convert string to number. But we can use "+" as well:&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = '123';
const num = +str
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Converting number to string
&lt;/h2&gt;

&lt;p&gt;There is another method to convert number to string.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const num = 123;
const str = ""+num
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But considering readibility and performance, String() and template string are recommended. &lt;/p&gt;

&lt;h2&gt;
  
  
  3. Ternary operator
&lt;/h2&gt;

&lt;p&gt;The ternary operator is a shorthand way of writing an if-else statement in JavaScript. It is represented by the ? and : symbols, and has the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;condition ? expression1 : expression2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example using if/else statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num = 5;
let message;

if (num &amp;gt; 0) {
  message = "Positive";
} else {
  message = "Non-positive";
}
console.log(message); // Output: "Positive"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same code can be written using the ternary operator as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num = 5;
let message = (num &amp;gt; 0) ? "Positive" : "Non-positive";
console.log(message); // Output: "Positive"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Use object instead of "switch"
&lt;/h2&gt;

&lt;p&gt;We often use switch to handle different things, but have you ever thought of using an object to greatly simplify your code? Let me explain in code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const n = 1
let result;
switch (n) {
  case 1:
    result = 'res-1'
    break
  case 2:
    result = 'res-2'
    break
  case 3:
    result = 'res-3'
    break  
  // ...There are a lot more
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We only need to use object map to achieve our goal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const n = 1
const nMap = {
  1: 'res-1',
  2: 'res-2',
  3: 'res-3'
}
const result = nMap[ n ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Use the "includes" method instead of multiple "if"
&lt;/h2&gt;

&lt;p&gt;We often writes code like this but multiple conditions makes the logic more confusing.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const n = 1
if (n === 1 || n === 2 || n === 3 || n === 4 || n === 5) {
  // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's use "includes" method to make our code extra clean and maintainable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const n = 1
const conditions = [ 1, 2, 3, 4, 5 ] // You just need to add new numbers here
if (conditions.includes(n)) {
  // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Use the default parameters of the ES6 function
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const func = (name) =&amp;gt; {
  name = name || 'magic'
  console.log(name)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's use like this instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const func = (name = 'magic') =&amp;gt; {
  console.log(name)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. Improve variable logging using console.log
&lt;/h2&gt;

&lt;p&gt;Instead of using like 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 str = "magic"
console.log("str:", str);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use like this to improve readability.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = "magic"
console.log({str});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  This will print like an object with key as "str" and value as "magic".
&lt;/h5&gt;

&lt;h2&gt;
  
  
  8. Use of nullish coalescing operator with numbers
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;The Nullish Coalescing Operator (??)&lt;/strong&gt;&lt;/em&gt; is a logical operator that accepts two values and returns the second value if the first one is null or undefined and otherwise returns the first value.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num = 0;
let num2;

console.log(num || 1); // 1
console.log(num2 || 2); // 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's use '??' operator now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num = 0;
let num2;

console.log(num ?? 1); // 0
console.log(num2 ?? 2); // 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;Hope you find these techniques useful and able to make your code cleaner. Happy reading!!&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

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