<?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: Mohit Kumar Toshniwal</title>
    <description>The latest articles on DEV Community by Mohit Kumar Toshniwal (@mohitkumartoshniwal).</description>
    <link>https://dev.to/mohitkumartoshniwal</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%2F975995%2F4b3d336f-a1ab-4dd3-81a2-86fdcbbb90fa.jpeg</url>
      <title>DEV Community: Mohit Kumar Toshniwal</title>
      <link>https://dev.to/mohitkumartoshniwal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohitkumartoshniwal"/>
    <language>en</language>
    <item>
      <title>Most used JavaScript methods I use in my day to day job</title>
      <dc:creator>Mohit Kumar Toshniwal</dc:creator>
      <pubDate>Sun, 20 Nov 2022 16:51:14 +0000</pubDate>
      <link>https://dev.to/mohitkumartoshniwal/most-used-javascript-methods-i-use-in-my-day-to-day-job-613</link>
      <guid>https://dev.to/mohitkumartoshniwal/most-used-javascript-methods-i-use-in-my-day-to-day-job-613</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This article is mainly about me walking you through some of the common JavaScript methods which I use in my day to day job. Its not an exhaustive list but covers a lot of methods which should be known I feel. I am not planning to explain all the methods deeply but would provide a cursory look for each method. Relevant links are provided for each method so you can further explore them in your free time.&lt;/p&gt;

&lt;p&gt;So, lets start&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" rel="noopener noreferrer"&gt;Array.map&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;I use map normally when I would like to convert an array of items(number, boolean, string,...) to some different representation. Let's take an example to understand it more clearly. &lt;/p&gt;

&lt;p&gt;Suppose, you have been given an array of dates in ISO format and you would like to compare the dates with each other. I feel the easiest way would be to convert the dates to a number and compare those numbers instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let dates=['2022-11-20T14:09:36.865Z', '2022-10-19T14:09:36.865Z', '2022-09-20T14:07:36.865Z']
dates.map((date)=&amp;gt; new Date(date).getTime()) // [1668953376865, 1666188576865, 1663682856865]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a side note, the returned value of the map operation is a new array with the transformation provided by you.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter" rel="noopener noreferrer"&gt;Array.filter&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Let's check out now filter function. Personally, I feel filter function should have been renamed to filterOut😅. In the initial days of learning JavaScript, this was the only method which I had to look repeatedly online.&lt;/p&gt;

&lt;p&gt;Anyways, filter will provide you a new array with items removed out of the array based on the condition you provide. A simple example would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers=[2, 23, 33, 4]
numbers.filter(number =&amp;gt; number &amp;gt; 20 ) // [23, 33]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce" rel="noopener noreferrer"&gt;Array.reduce&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Personally, I am okay with with writing more code instead of using reduce to make my code look compact. Its a personal preference, but if it makes sense that reduce is THE way to go for any specific problem, I don't shy away from using that as well. Enough with the monologue, I guess. So, what does reduce help us accomplish. Basically, the simplest answer is that you give it an array of items and it would return you a single value. The value can be anything based on the logic you provide.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [10, 22, 3, 4];

// 0 + 10 + 22 + 3 + 4
const initialValue = 0;
const sum = array.reduce(
  (accumulator, currentValue) =&amp;gt; accumulator + currentValue,
  initialValue
); // 39

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes" rel="noopener noreferrer"&gt;Array.includes&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Instead of using &lt;a href="//Array.prototype.indexOf()"&gt;indexOf&lt;/a&gt; and checking the result with specific numbers. My personal favourite of checking whether a value is present in the array is to use &lt;em&gt;includes&lt;/em&gt; instead. It will return you a Boolean value based on whether the value is present in the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3];
array.includes(2) // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only caveat is that it is case sensitive. So, "mohit" and "Mohit" are not the same value in the  👀 of &lt;em&gt;includes&lt;/em&gt;. So, maybe convert all your strings to lowercase before using &lt;em&gt;includes&lt;/em&gt; on them.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split" rel="noopener noreferrer"&gt;String.split&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now, suppose if you have a requirement to convert a string to an array and would like to do further processing on it using the array methods which we already discussed above, then *split *is your buddy here. You would need to give it a separator on the basis of which, its going to split the string into an array of strings.&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 = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' '); //  ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join" rel="noopener noreferrer"&gt;Array.join&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;join&lt;/em&gt; is the opposite of *split *. It will convert an array of strings to a new string with the separator which you provide.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const words=["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]
words.join(' '); //  "The quick brown fox jumps over the lazy dog."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice" rel="noopener noreferrer"&gt;Array.splice&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;If you would like to modify an existing array. Then &lt;em&gt;splice&lt;/em&gt; is the method which you are searching for. It would help you to add or remove element/elements from the array in-place. It would return elements which were removed from the array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const months = ['Jan', 'March', 'April', 'June'];
const removedItem = months.splice(1, 1); 
console.log(removedItem) // ["March"]
console.log(months) // ["Jan", "April", "June"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice" rel="noopener noreferrer"&gt;Array.slice&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now, suppose you would like to have a copy (shallow) of a selected portion of the array or a new (shallow) copy of the whole array, then slice can be used for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const animals = ['ant', 'bison', 'elephant'];
animals.slice(2) // ["elephant"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel="noopener noreferrer"&gt;Array.sort&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now, suppose if you would like to arrange the items in an array in ascending or descending order, then we can use &lt;em&gt;sort&lt;/em&gt; and provide the condition based on which the sorting will happen. The array will be sorted in-place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [10,3, 4, 22 ];
array.sort(( a, b)=&amp;gt; a-b); // [3, 4, 10, 22]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A word of caution, always make sure that your comparator function can return +ve value or -ve value or 0 based on your requirements. Basically, in the above example, I could have used a different comparator function, which might seem logical at first, but the sorting will not work on them and there are some other gotchas. If any confusion, I would highly suggest you to check out &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel="noopener noreferrer"&gt;mdn&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [10,3, 4, 22 ];
array.sort((a, b) =&amp;gt; a &amp;gt; b ? 1 : 0); // [10, 3, 4, 22]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned in the start of the article, these are just some of the most common methods which  I use daily. There are a lot of other methods which you can further explore. To name a few, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;reverse&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;some&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;every&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;*fill *&lt;/li&gt;
&lt;li&gt;&lt;em&gt;find&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;push&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;*pop *
and the list goes on.&lt;/li&gt;
&lt;/ul&gt;

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

</description>
      <category>llm</category>
    </item>
  </channel>
</rss>
