<?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: Evan Loria</title>
    <description>The latest articles on DEV Community by Evan Loria (@evanloria4).</description>
    <link>https://dev.to/evanloria4</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%2F1906370%2F4d1f64a3-e5b9-41ef-97ff-dfa6acb857d4.png</url>
      <title>DEV Community: Evan Loria</title>
      <link>https://dev.to/evanloria4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/evanloria4"/>
    <language>en</language>
    <item>
      <title>Optimizing Top K Frequent Elements in JavaScript Using a Max-Heap</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Wed, 09 Apr 2025 17:59:08 +0000</pubDate>
      <link>https://dev.to/evanloria4/optimizing-the-top-k-elements-problem-using-a-max-heap-387b</link>
      <guid>https://dev.to/evanloria4/optimizing-the-top-k-elements-problem-using-a-max-heap-387b</guid>
      <description>&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;The Top K Elements algorithm helps find the k most frequent elements in a list. A naive solution involves storing all elements and their frequencies as tuples in an array, sorting the array by frequencies in decreasing order, and then returning the first k elements. While this works fine for small datasets, it becomes inefficient as the dataset grows. We can optimize this approach by using a binary heap, which will allow us to store only the top k elements and significantly improve the time complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Naive Solution Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a frequency map (using an object) to store the elements and their frequencies.&lt;/li&gt;
&lt;li&gt;Convert the frequency map into an array of [element, frequency] pairs.&lt;/li&gt;
&lt;li&gt;Sort the array in decreasing order based on the frequency of elements.&lt;/li&gt;
&lt;li&gt;Slice the array to extract the top k elements.&lt;/li&gt;
&lt;li&gt;Return the elements by mapping over the sliced array and retrieving only the elements (ignoring their frequencies).
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;naiveTopK&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Create an object of element frequencies&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;freqMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elem&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;freqMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;freqMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// Convert frequencies object into an array of tuples&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;freqArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;freqMap&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// Sort the frequenciesArr in descending order&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;freqArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="c1"&gt;// Slice and map over the top k elements to return top k occurring&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sortedArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Efficiency Issues of the Naive Solution
&lt;/h2&gt;

&lt;p&gt;The time complexity of the naive solution is O(n log n), meaning that as the size of the dataset (n) increases, the execution time increases at an accelerating rate. The primary issue with this approach is that it requires storing and sorting the entire array of elements, which becomes inefficient with larger datasets.&lt;/p&gt;

&lt;p&gt;A binary heap is a binary tree data structure that satisfies the heap property. In a max-heap, each node's value is greater than or equal to the values of its children, while in a min-heap, each node's value is less than or equal to the values of its children. By using a binary heap to store only the top k most frequent elements, we can significantly improve time complexity. This optimized solution reduces the complexity to O(n log k), where k is the number of elements we need to track, leading to better performance with larger datasets.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1t8318f7tsivqe8q28t2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1t8318f7tsivqe8q28t2.png" alt="Heaps" width="596" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a MaxHeap Class
&lt;/h2&gt;

&lt;p&gt;We will implement a MaxHeap class to efficiently store the top k elements of the list. The class will include several methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;size: Returns the current number of elements in the heap&lt;/li&gt;
&lt;li&gt;push: Adds a new element to the heap&lt;/li&gt;
&lt;li&gt;pop: Removes the root (largest element) of the heap and returns it&lt;/li&gt;
&lt;li&gt;removeLast: Removes the last element in the heap array (assumed to be the least frequent due to insertion order)&lt;/li&gt;
&lt;li&gt;_heapifyUp: Balances the heap when new elements are added, ensuring the max-heap property is maintained&lt;/li&gt;
&lt;li&gt;_heapifyDown: Balances the heap when the root is removed, ensuring the max-heap property is maintained
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create a MaxHeap class&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MaxHeap&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Ensure the heap is balanced&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_heapifyUp&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;removeLast&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// Store the old root to be returned&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="c1"&gt;// Reassign the root to the last element to maintain the heap structure&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="c1"&gt;// Remove the last element from the heap since it is now a duplicate&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;// Ensure the heap is balanced&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_heapifyDown&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// Ensure the max heap maintains balance by making sure element is added in the correct place&lt;/span&gt;
  &lt;span class="nf"&gt;_heapifyUp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Start with the last element (this is where new elements are inserted)&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// While the index is greater than 0&lt;/span&gt;
    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Find the parent index&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parentIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// If parent is greater than child the heap property is satisfied and we can stop&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;parentIndex&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Flip the parent and child elements&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;parentIndex&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;parentIndex&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
        &lt;span class="c1"&gt;// Set the index to the parent index&lt;/span&gt;
        &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;parentIndex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// Keep balance of the heap when the root is removed&lt;/span&gt;
  &lt;span class="nf"&gt;_heapifyDown&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;parentIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;// While there is at least one child&lt;/span&gt;
    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;parentIndex&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// We assume the largest element index is at the front&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;largestElemIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;parentIndex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;leftIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;parentIndex&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rightIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;parentIndex&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="c1"&gt;// Check if left child frequency is larger than the current largest&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;leftIndex&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;largestElemIndex&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Reassign the current largestElemIndex&lt;/span&gt;
        &lt;span class="nx"&gt;largestElemIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;leftIndex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="c1"&gt;// Check if right child exists and is larger than the current largest&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rightIndex&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;rightIndex&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;largestElemIndex&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Reassign the current largestElemIndex&lt;/span&gt;
        &lt;span class="nx"&gt;largestElemIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rightIndex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="c1"&gt;// If largest is not the current node (one of the children was larger than the parent), swap and continue&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;largestElemIndex&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;parentIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Swap the current largest with the parent&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;parentIndex&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;largestElemIndex&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;largestElemIndex&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;parentIndex&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
        &lt;span class="c1"&gt;// Reassign the parentIndex to the largestElemIndex&lt;/span&gt;
        &lt;span class="nx"&gt;parentIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;largestElemIndex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// No swap needed, heap is in valid state&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Optimized Solution
&lt;/h2&gt;

&lt;p&gt;Once our MaxHeap class is configured, we can create an instance of the class in order to store our top k elements. This prevents us from having to eventually sort every element in the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;optimizedTopK&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Create an object of element frequencies&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;freqMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;elem&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Add the elements to freqMap or increase the frequency count&lt;/span&gt;
    &lt;span class="nx"&gt;freqMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;freqMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// Create an instance of MaxHeap to store the most frequent elements&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;heap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MaxHeap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="c1"&gt;// Add the tuples to the heap&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;freq&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;freqMap&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;freq&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="c1"&gt;// Check if the heap's size exceeds the desired number of top elements (k)&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeLast&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// Return the most frequent elements from the heap&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;optimizedTopK&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: ['1', '2']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  _heapifyUp and _heapifyDown
&lt;/h2&gt;

&lt;p&gt;These methods ensure that the max-heap property is maintained, meaning each parent element is greater than or equal to its children, whenever an element is added or removed from the heap.&lt;/p&gt;

&lt;p&gt;The _heapifyUp method is called whenever a new element is added to the heap. It starts from the newly added element and moves upwards, comparing its frequency with its parent's frequency. If the new element's frequency is greater than its parent's, the two elements are swapped. This process continues until the max-heap property is satisfied, meaning the parent element is greater than or equal to the child.&lt;/p&gt;

&lt;p&gt;The _heapifyDown method is used when the root is removed from the heap. In this solution, we never remove the root, so _heapifyDown isn’t called. However, if we were using pop()—for example, in a variation of this algorithm or a min-heap version—we’d rely on _heapifyDown to maintain heap order. It checks if either of the two child nodes has a greater frequency than the parent. If one of the child's frequencies is greater than the parent's, the elements are swapped, and the process repeats until the heap property is restored.&lt;/p&gt;

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

&lt;p&gt;The naive solution to the Top K Elements problem works fine for smaller datasets, but as the dataset grows, sorting the entire list becomes inefficient. By implementing a max-heap, we can avoid sorting the entire list and instead focus on maintaining just the top k elements. This optimization reduces our time complexity from O(n log n) to O(n log k), significantly improving performance for larger datasets.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>toyproblem</category>
    </item>
    <item>
      <title>node-cron: Automation in node.js</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Sun, 09 Feb 2025 19:14:21 +0000</pubDate>
      <link>https://dev.to/evanloria4/node-cron-automation-in-nodejs-4949</link>
      <guid>https://dev.to/evanloria4/node-cron-automation-in-nodejs-4949</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Automation is vital in a Node.js environment. The NPM node-cron package allows developers to schedule tasks to run at regular intervals, automating anything from database backups to daily reports. This is achieved using cron syntax, a standard format used to define the schedule in which a job should run. Automating tasks within an application saves time, increases efficiency, and removes human error from repetitive or tedious tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Cases
&lt;/h2&gt;

&lt;p&gt;node-cron is great for scheduling jobs that need to be run at regular intervals. These tasks may include system maintenance, data processing and management, or removing temporary files to save disk space and system memory. Any job that needs to run routinely can be done so reliably with node-cron.&lt;/p&gt;

&lt;h2&gt;
  
  
  Crontab and Cron Syntax
&lt;/h2&gt;

&lt;p&gt;Crontab, which is short for 'cron table', is a file used in Unix-like systems to store scheduled tasks. node-cron uses the same syntax for defining the intervals for application tasks. Cron syntax is a string of 5 date/time fields that tell the system when the jobs should be executed. &lt;/p&gt;

&lt;h3&gt;
  
  
  Fields
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;minute: 1-59&lt;/li&gt;
&lt;li&gt;hour: 0-23&lt;/li&gt;
&lt;li&gt;day of month: 1-31&lt;/li&gt;
&lt;li&gt;month: 1-12&lt;/li&gt;
&lt;li&gt;day of week: 0-7 (Sunday is 0 and 7)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;node-cron offers an optional seconds field that is placed at the beginning of the cron expression. This is not standard for implementations of crontab syntax.&lt;br&gt;
An asterisk (*) is used to represent any possible value of the field. For example, the following cron expression would run a task every minute:&lt;br&gt;
&lt;code&gt;* * * * *&lt;/code&gt;&lt;br&gt;
This cron expression runs a task everyday at 12:00 pm:&lt;br&gt;
&lt;code&gt;0 12 * * *&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Rather than using numbers to represent the month and day of the week, these fields can be spelled out or abbreviated.&lt;br&gt;
These fields are not case sensitive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;* * * january MON&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Every monday in january&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;* * * jan monday&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Every monday in janaury&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition, more than one option can be given to a field by supplying a comma separated list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0,15 12,1 * * MON,WED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
&lt;span class="nx"&gt;Every&lt;/span&gt; &lt;span class="nx"&gt;Mon&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;Wed&lt;/span&gt; &lt;span class="nx"&gt;at&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fav43xfr7idrepbpstrb4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fav43xfr7idrepbpstrb4.png" alt="Cron Syntax" width="310" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Ranges and Steps
&lt;/h2&gt;

&lt;p&gt;node-cron allows for ranges and steps to be applied to the fields. Ranges are applied using a dash (-) and steps are supplied with a slash (/) followed by the step to increment by. For example, to run a task only during the work week (Monday to Friday), you could use MON-FRI in the day of the week field. Similarly, to run a task every minute from minute 30 through minute 45 of an hour, use the expression '30-45 * * * *'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Every&lt;/span&gt; &lt;span class="nx"&gt;minute&lt;/span&gt; &lt;span class="nx"&gt;between&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="nx"&gt;Mon&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Fri&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0-30 12 * * MON-FRI&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
&lt;span class="nx"&gt;At&lt;/span&gt; &lt;span class="nx"&gt;midnight&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;st&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;every&lt;/span&gt; &lt;span class="nx"&gt;month&lt;/span&gt; &lt;span class="nx"&gt;January&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;June&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0 0 1 1-6 *&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;Every&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="nx"&gt;minutes&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;Monday&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*/5 * * * Mon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;Every&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="nx"&gt;months&lt;/span&gt; &lt;span class="nx"&gt;at&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="nx"&gt;pm&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0 12 * */2 *&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Steps can also be applied to ranges&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Every&lt;/span&gt; &lt;span class="nx"&gt;other&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;every&lt;/span&gt; &lt;span class="nx"&gt;other&lt;/span&gt; &lt;span class="nx"&gt;month&lt;/span&gt; &lt;span class="nx"&gt;between&lt;/span&gt; &lt;span class="nx"&gt;Jan&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Aug&lt;/span&gt; &lt;span class="nx"&gt;every&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="nx"&gt;hours&lt;/span&gt; &lt;span class="nx"&gt;between&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="nx"&gt;am&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="nx"&gt;pm&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0 6-12/3 * jan-aug/2 */2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hash Operator (#)
&lt;/h3&gt;

&lt;p&gt;The hash operator is used to schedule a task on the nth occurrence of a day of the week within a given month.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0 0 * * 1#3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// At midnight the 3rd Monday of every month&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;* * * 11 thu#4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// The 4th Thursday of November&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Scheduling Tasks
&lt;/h2&gt;

&lt;p&gt;Once you understand cron syntax, you are able to schedule tasks to run within your Node.js application. Install the package using NPM.&lt;br&gt;
&lt;code&gt;npm install node-cron&lt;/code&gt;&lt;br&gt;
Import cron into the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cron&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node-cron&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// OR&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cron&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node-cron&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you've installed and imported cron into your project use the schedule method to schedule the interval on which a task should run. &lt;br&gt;
The schedule method takes two arguments, and an optional third argument: the cron expression string, and the function to execute. This example will execute a function to log 'Hello' to the console every 10 minutes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cron&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node-cron&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;cron&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*/10 * * * *&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Options Paramter
&lt;/h3&gt;

&lt;p&gt;The schedule method also accepts an options object. This parameter can set if the task is scheduled or not, and also tell node-cron what timezone to use to schedule tasks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;scheduled: boolean to set if the created task is scheduled. Defaults to true&lt;/li&gt;
&lt;li&gt;timezone: string of the timezone to schedule tasks. See &lt;a href="https://www.iana.org/time-zones" rel="noopener noreferrer"&gt;IANA Timezone Database&lt;/a&gt; for possible values&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Task Methods
&lt;/h2&gt;

&lt;p&gt;If you set a task equal to the invocation of the schedule method, you are able to use methods of that task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cron&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;* * * * *&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;taskFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;task.start() starts the scheduled task&lt;/li&gt;
&lt;li&gt;task.stop() stops the scheduled task (must be restarted to run)&lt;/li&gt;
&lt;li&gt;task.validate(cronExpression) validates a given cron expression&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Scheduling Tasks
&lt;/h2&gt;

&lt;p&gt;In order for the application to run tasks, the server must read the file that the tasks are defined in. This means to import (or require) your task file in your server file. Alternatively, you could define your tasks directly on your server's app.js or index.js file. It is generally considered good practice to separate your tasks into a different file. This allows for better organization and maintainability. &lt;/p&gt;

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

&lt;p&gt;node-cron provides a way for scheduling tasks within Node.js applications. By understanding cron syntax and utilizing the available options, you can improve efficiency, and save valuable time. &lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/node-cron" rel="noopener noreferrer"&gt;node-cron npm&lt;/a&gt;&lt;br&gt;
&lt;a href="https://nodecron.com/docs/" rel="noopener noreferrer"&gt;node-cron docs&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Node's GoogleGenerativeAI: Incorporating AI Technology In javaScript</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Sun, 26 Jan 2025 18:18:04 +0000</pubDate>
      <link>https://dev.to/evanloria4/nodes-googlegenerativeai-incorporating-ai-technology-in-javascript-5he9</link>
      <guid>https://dev.to/evanloria4/nodes-googlegenerativeai-incorporating-ai-technology-in-javascript-5he9</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The world has seen a lot of advancements in the AI world over the last several years. With artificial intelligence becoming more commonplace, developers must find ways to incorporate AI into their applications. Gemini offers an easy way for javasScript developers to build with AI through node's GoogleGenerativeAI package. Developers can access Gemini models, which are developed by Google's DeepMind, in order to create exciting features using AI. Other packages are available for those using Python or GO, and Gemini also has a RESTful API. This article will talk about the improvements Gemini's newest models bring to the table, and how to get started with the GoogleGenerativeAI package using node.&lt;/p&gt;

&lt;h2&gt;
  
  
  Major Developments
&lt;/h2&gt;

&lt;p&gt;A major development the Gemini 1.5 Flash model brings is the amount of context tokens that can be handled in a single request. In the past, these types of models were limited by the amount of text, or tokens, that could be processed at a time. The generative models that have been created over the last few years only had the ability to process 8,000 tokens at once. While this number had improved with the advancements in AI technology, it still remained a limiting factor. Today, Gemini 1.5 Flash is capable of processing up to 1 million tokens at a time. The pro version (Gemini 1.5 Pro) can process up to 2 million. This makes Gemini capable of processing large amounts of information at once, while still maintaining a very high accuracy rate. You can read more about Gemini's advancements in AI world and what they mean &lt;a href="https://ai.google.dev/gemini-api/docs/long-context" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;The first thing you will need to do in order to use the GoogleGenerativeAI package is crate a Gemini API key. &lt;br&gt;
This is a quick and painless process.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;a href="https://aistudio.google.com/prompts/new_chat?gad_source=1&amp;amp;gclid=Cj0KCQiA19e8BhCVARIsALpFMgHW5rkmPhw-P8zQm7ZYoRWyiZz93K-kjI8P0K0HlCVjQ8lVR-oPn0oaAmvbEALw_wcB" rel="noopener noreferrer"&gt;Google AI Studio&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Click the 'Get API Key' button at the top left&lt;/li&gt;
&lt;li&gt;Click the 'Create API Key' button&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once you've accessed an API key you'll need to install the package with node.&lt;br&gt;
&lt;code&gt;npm install @google/generative-ai&lt;/code&gt;&lt;br&gt;
Once you've done all this you're ready to start developing with AI!&lt;/p&gt;
&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;Import the package into the file you wish to use it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;GoogleGenerativeAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@google/generative-ai&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="c1"&gt;// OR&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;GoogleGenerativeAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@google/generative-ai&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an instance of GoogleGenerativeAI while passing in your API key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;genAI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GoogleGenerativeAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YOUR_API_KEY&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the getGenerativeAI method and pass in and object for the model you want to use. There are a number of models available, this example uses the Gemini 1.5 Flash model. &lt;a href="https://ai.google.dev/pricing#1_5flash" rel="noopener noreferrer"&gt;Gemini Models&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;genAI&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getGenerativeModel&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gemini-1.5-flash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the model is setup, you can use the AI to generate text, respond to images, extract information from videos, and more.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration and System Instructions
&lt;/h2&gt;

&lt;p&gt;You can optionally provide the model with a configuration and system instructions. The configuration is applied on a generationConfig property within the generateContent method invocation. Some configuration options include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;responseSchema: Output schema of the generated text&lt;/li&gt;
&lt;li&gt;candidateCount: (integer) Number of responses to return&lt;/li&gt;
&lt;li&gt;temperature: (number) Controls the randomness of the output&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See more generationConfig properties &lt;a href="https://ai.google.dev/api/generate-content#v1beta.GenerationConfig" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
Providing system instructions can help improve the response by providing additional context to the AI. In addition, the model will generate more custom responses, and be able to better suit the user's needs. The system instructions are provided when the model is initialized.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;genAI&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getGenerativeModel&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gemini-1.5-flash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;systemInstruction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You are extremely friendly, always use your manners.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Text Generation
&lt;/h2&gt;

&lt;p&gt;Text can be generated in a number of ways using the package. The simplest way is to only provide the model with text, but there are more exciting and complex ways text can  be generated. You can provide the model with an image along with the text in order to have the AI react to an image. Here is a simple example of a request that generates a response using only text. &lt;br&gt;
The model setup is not included in this code block, but it is still part of the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tell me something about the moon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Log the response &lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A prompt string is passed into the model's generateContent method. Once the response is returned, the response can be accessed on the response properties text method. The result of this response was: 'The Moon's surface is covered in a layer of fine dust called regolith, formed by billions of years of micrometeorite impacts.  This dust is incredibly fine and clings to everything, posing challenges for astronauts and lunar equipment.' Pretty cool huh? This is a super simple example, but there are more possibilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Text Streaming and Chatting
&lt;/h2&gt;

&lt;p&gt;The model waits for the entire response text to be generated before returning the response. Obviously right? If you didn't want to wait on the entire response to be generated, you can use text streaming to receive a faster response by not waiting on the entire result. This can be achieved using the streamGenerateContent method.&lt;br&gt;
Here is an example from the Gemini API docs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;GoogleGenerativeAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@google/generative-ai&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;genAI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GoogleGenerativeAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GEMINI_API_KEY&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;genAI&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getGenerativeModel&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gemini-1.5-flash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Explain how AI works&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateContentStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunkText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunkText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The package also offers the capability for keeping track of conversation. This helps users solve multistep problems by 'allowing users to step incrementally toward answers.' This is a relatively advanced feature of the Gemini API. More can be read about creating a chat and other text generation capabilities on the &lt;a href="https://ai.google.dev/gemini-api/docs/text-generation?lang=node" rel="noopener noreferrer"&gt;Gemini API Docs&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;The GoogleGenerativeAi package makes it easy for javaScript developers to incorporate AI technologies into their applications. The package has numerous capabilities in AI generation, including text, video, and images. Gemini's ability to process a large amount of text at a time is a huge development in AI generation. With node's GoogleGenerativeAI, developers are able to include advanced AI technologies in their projects in a much easier way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/@google/generative-ai" rel="noopener noreferrer"&gt;NPM&lt;/a&gt;&lt;br&gt;
&lt;a href="https://deepmind.google/about/" rel="noopener noreferrer"&gt;DeepMind&lt;/a&gt;&lt;br&gt;
&lt;a href="https://ai.google.dev/gemini-api/docs/long-context" rel="noopener noreferrer"&gt;Gemini Long Context&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>gemini</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Material UI: React Component Library</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Mon, 13 Jan 2025 05:32:46 +0000</pubDate>
      <link>https://dev.to/evanloria4/material-ui-react-component-library-5355</link>
      <guid>https://dev.to/evanloria4/material-ui-react-component-library-5355</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Material UI, or MUI, is an open source React component library used for building and designing user interface. MUI allows applications to be built quickly by offering out-of-the-box components. The library allows developers to provide a custom theme for their application, as well as customization options for individual components. This article will show how to create a custom theme, import components for use, and demonstrate the use of the stack component. &lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Material UI can be installed using npm or yarn. React and react-dom are peer-dependencies, so you must install these packages as well in order to use MUI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @mui/material
yarn add @mui/material
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Components
&lt;/h2&gt;

&lt;p&gt;Once MUI is installed along with its peer dependencies, the components can be imported and used to build your application. Import the component at the top of the file you want to use it. The below code block shows how to import the Button, Dialog, and List component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@mui/material/Button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Dialog&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@mui/material/Dialog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;List&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@mui/material/Button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Components can also be imported using destructuring.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Dialog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;List&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@mui/material&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the component is imported it's available to be used as is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Dialog&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;List&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Customization
&lt;/h2&gt;

&lt;p&gt;MUI offers properties for each component that can be used for customization. Properties for each component are available on the MUI docs under the Component API tab. &lt;br&gt;
&lt;a href="https://mui.com/material-ui/getting-started/" rel="noopener noreferrer"&gt;Material UI Docs&lt;/a&gt;&lt;br&gt;
Properties are assigned inside of the opening tag of the component. Here's an example of assigning property values when working with the button component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;variant&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;black&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;variant&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;contained&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;large&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;variant&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;outlined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;disabled&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F740tzg6zm59i840ko9qj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F740tzg6zm59i840ko9qj.png" alt="MUI Buttons" width="661" height="89"&gt;&lt;/a&gt;&lt;br&gt;
All components have an sx property available to them, which is used to for system overrides as well as other CSS styles that are not direct properties of a component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt;
  &lt;span class="nx"&gt;variant&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;contained&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="nx"&gt;sx&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; 
  &lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;purple&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;white&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;}}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fqkfynr2ybcw8sfu5v1hl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fqkfynr2ybcw8sfu5v1hl.png" alt="Button" width="661" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Themes
&lt;/h2&gt;

&lt;p&gt;MUI offers the ability for users to create custom themes. This feature enables users to maintain consistency between like components across the application, as well as the ability to create a custom color palette. Import the createTheme function and ThemeProvider component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createTheme&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                         
         &lt;span class="nx"&gt;ThemeProvider&lt;/span&gt;                                    
       &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@mui/material/styles&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The createTheme function accepts an object argument, with the properties equal to the element for the styles to be applied to. Set a variable equal to the invocation of the createTheme component on your custom theme object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createTheme&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
&lt;span class="na"&gt;components&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="na"&gt;MuiButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;styleOverrides&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The components property is used to define which MUI component will be effected by the styles. The styleOverrides property is used to override the styles of components. In this case, we are overriding the styles of Button root components. Once you are happy with the theme you have created, you need to wrap the component you want the theme to apply to in the ThemeProvider tag, and supply the theme variable name to the theme property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ThemeProvider&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;YourComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; 
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ThemProvider&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also use CSS classes of components to define style overrides. The classes for each component exist on the components API within the MUI docs. We can put a '&amp;amp;.Mui-disabled' (must be in quotes) property on our root property object to define the styles of any disabled Button component. &lt;br&gt;
This is just one way to define styles for components. I recommend looking into the documentation for custom themes on the Material UI docs, as well as the docs on creating custom palettes.&lt;br&gt;
&lt;a href="https://mui.com/material-ui/customization/theming/?srsltid=AfmBOorMi53iayLB80VbVTWVGG2DyotQBsYoKYYho6ZxO6Nxy-YS9DC_" rel="noopener noreferrer"&gt;Themes&lt;/a&gt;&lt;br&gt;
&lt;a href="https://mui.com/material-ui/customization/palette/?srsltid=AfmBOop5hpqaeSJiC871PpCdSod-AFiG2WpZz2fsN8RHGsY4PdFY88F6" rel="noopener noreferrer"&gt;Palette&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Containers
&lt;/h2&gt;

&lt;p&gt;Lastly, MUI offers different containers for grouping components to be displayed on the page. One of those components is Stack, which can be used to group elements vertically or horizontally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@mui/material&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Stack&lt;/span&gt; &lt;span class="nx"&gt;spacing&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;One&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Item&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Two&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Item&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Three&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Item&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Stack&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fkemuibqkznfg640j245g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fkemuibqkznfg640j245g.png" alt="Vertically Stack" width="422" height="244"&gt;&lt;/a&gt;&lt;br&gt;
The spacing property determines how far apart the elements will be from each other. The default direction of the stack is vertical. To stack the elements in a row, set the direction property equal to "row".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@mui/material&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Stack&lt;/span&gt; &lt;span class="nx"&gt;direction&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;row&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;spacing&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;One&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Item&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Two&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Item&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Three&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Item&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Stack&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fkiny6l4pakhez495jxbj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fkiny6l4pakhez495jxbj.png" alt="Stack row" width="621" height="78"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more complex, two-dimensional designs, MUI offers the Grid component. You can read the documentation &lt;a href="https://mui.com/material-ui/react-grid2/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;Material UI emphasizes speed and efficiency in enabling developers to build and customize web applications. Their are many more, ready-to-use, well-documented components and customization options available to developers using Material UI. This article only scratches the surface of what Material UI has to offer.&lt;/p&gt;

&lt;p&gt;Sources: &lt;br&gt;
&lt;a href="https://mui.com/material-ui/getting-started/" rel="noopener noreferrer"&gt;Material UI Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://mui.com/material-ui/react-button/" rel="noopener noreferrer"&gt;MUI Button&lt;/a&gt;&lt;br&gt;
&lt;a href="https://mui.com/material-ui/customization/theming/?srsltid=AfmBOorMi53iayLB80VbVTWVGG2DyotQBsYoKYYho6ZxO6Nxy-YS9DC_" rel="noopener noreferrer"&gt;MUI Themes&lt;/a&gt;&lt;br&gt;
&lt;a href="https://mui.com/material-ui/react-stack/?srsltid=AfmBOor-7Z5qa7vmfIJKIflODmyz-67kLmVTVddWleLZ7zB4mSR290cT" rel="noopener noreferrer"&gt;MUI Stack&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>mui</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Internet of Things: The Benefits and Challenges of Connected Devices</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Mon, 02 Dec 2024 07:30:59 +0000</pubDate>
      <link>https://dev.to/evanloria4/internet-of-things-the-benefits-and-challenges-of-connected-devices-1oe4</link>
      <guid>https://dev.to/evanloria4/internet-of-things-the-benefits-and-challenges-of-connected-devices-1oe4</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Internet of Things (IoT) is a network of devices that are connected to each other via the internet, and the technology that allows these devices to communicate with each other. These devices automate our lives, and reduce the need for human intervention in certain tasks. These devices contain technology that can collect, send, and receive data. IoT networks provide a number of benefits to our everyday lives, but they also introduce challenges due to the amount of interconnected devices and data points within a network. &lt;br&gt;
&lt;a href="https://media2.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%2Fusggu8jhio78896ucwhq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fusggu8jhio78896ucwhq.jpeg" alt="IoT Devices" width="311" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  IoT Devices
&lt;/h2&gt;

&lt;p&gt;An IoT device a device that is connected to the internet, and has the ability to exchange 'data with other devices or systems.'(&lt;a href="https://www.softwareag.com/en_corporate/resources/iot/article/iot-device.html" rel="noopener noreferrer"&gt;What is an IoT Device&lt;/a&gt;) These devices use sensors to collect information, which can then be sent to an IoT platform. Once the data is analyzed by the platform, the necessary actions can be triggered to indicate that a task needs to be done, or something was detected within a device. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9541qfmisrjxsgowrhru.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9541qfmisrjxsgowrhru.png" alt="IoT Platform" width="275" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  IoT Platform
&lt;/h2&gt;

&lt;p&gt;IoT devices use sensors to collect data about the device itself, or the environment of a device. These sensors are continuously transmitting data to applications known as IoT platforms. An IoT platform is the core of a network. A platform allows the devices of a network to communicate, send data, and receive commands. The type of platform a network wants to use depends on the the purpose, or functionality, of the devices of a network. You can read more about IoT platforms and the different types &lt;a href="https://danielelizalde.com/iot-platform/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  IoT Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Data is collected by the sensors within a device&lt;/li&gt;
&lt;li&gt;Information is sent to the IoT platform&lt;/li&gt;
&lt;li&gt;Platform transforms the data so that it can be analyzed&lt;/li&gt;
&lt;li&gt;Data is analyzed using algorithms&lt;/li&gt;
&lt;li&gt;Actions can be triggered if necessary based on the analysis of the data received. 
&lt;img src="https://media2.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%2Fxwgfiih0s1rfbdeut67g.jpeg" alt="Benefits" width="317" height="159"&gt;
## IoT Benefits
IoT networks have the ability to make our lives simpler. These networks offer a number of benefits to us everyday, such as:&lt;/li&gt;
&lt;li&gt;Easy Accessibility:
Because IoT networks store all the data in a central location, any device connected to the network is able to access any data that was produced by another device on the network. &lt;/li&gt;
&lt;li&gt;Improved Communication:
Data is stored in a structured way within a network, which allows information to be received more quickly.&lt;/li&gt;
&lt;li&gt;Saving Time and Money:
By providing real-time analysis, networks are able to catch problems as they occur. This eliminates the need for human-intervention when detecting issues, and can ultimately save money by catching issues early on. &lt;/li&gt;
&lt;li&gt;Process Automation:
IoT networks reduce the need for human-intervention in certain tasks.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Some other benefits of IoT networks are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better Business Decisions&lt;/li&gt;
&lt;li&gt;Productive Analysis&lt;/li&gt;
&lt;li&gt;Labor Reduction&lt;/li&gt;
&lt;li&gt;Performance Monitoring&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fs8a8ik1buc5wdndrlbbg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fs8a8ik1buc5wdndrlbbg.png" alt="IoT Security" width="251" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges
&lt;/h2&gt;

&lt;p&gt;The use of IoT networks has a number of upsides in many aspects of our lives, however, the existence of these large networks do not come without a few challenges. &lt;/p&gt;

&lt;h5&gt;
  
  
  Security
&lt;/h5&gt;

&lt;p&gt;As a the number of devices within a network grows, so does the risk of a cyber attack. Because of the number of devices and amount of data within a network, the attack surface of a network can become extremely large. An attacker can access information through a handful of different entry points. &lt;br&gt;
It is important for these networks to remain secure because of the amount of information they hold. &lt;/p&gt;

&lt;h5&gt;
  
  
  Data Management
&lt;/h5&gt;

&lt;p&gt;The shear volume of data within a network can be complex to manage. An IoT network deals with a large amount of data, coming from a number of different devices. A single network can be dealing with any number of different data types. This is a big reason why data needs to be efficiently stored.&lt;/p&gt;

&lt;h5&gt;
  
  
  Mass Corruption
&lt;/h5&gt;

&lt;p&gt;While it is beneficial for data to be stored in a central location for all devices to access, it also introduces the risk of mass corruption on a network. If a bug is introduced to a network device, it could wind up affecting every device within the network.&lt;/p&gt;

&lt;h5&gt;
  
  
  Compatibility
&lt;/h5&gt;

&lt;p&gt;Devices from different manufacturers can exist on the same network. Because there is not a standard for IoT compatibility, there is a possibility some devices are not compatible with each other. &lt;/p&gt;

&lt;h5&gt;
  
  
  Job Displacement
&lt;/h5&gt;

&lt;p&gt;Because IoT networks remove the need for human-intervention by automating tasks, some jobs can be replaced by devices. This is especially true for lower-skilled jobs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fxolmh7xu7se1jyqinc5z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxolmh7xu7se1jyqinc5z.png" alt="Iot Fields" width="334" height="151"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  IoT Examples In Different Fields
&lt;/h2&gt;

&lt;p&gt;IoT devices are used across every type of field throughout the world. Networks are able to offer different kinds of benefits depending on the purpose of the devices. &lt;/p&gt;

&lt;h5&gt;
  
  
  Healthcare
&lt;/h5&gt;

&lt;p&gt;A heart monitor that is able to relay information about a patient's heart. The data received from this device could be used to detect a heart attack or other heart diseases.&lt;/p&gt;

&lt;h5&gt;
  
  
  Manufacturing
&lt;/h5&gt;

&lt;p&gt;A machine with a sensor that collects data about the environment within the machine. The data collected by the sensor could determine if a machine is working properly, or if it is due for maintenance. This could be a huge factor in saving a company time and money. &lt;/p&gt;

&lt;h5&gt;
  
  
  Agriculture
&lt;/h5&gt;

&lt;p&gt;An irrigation system that can be automated to water plants under certain conditions. A device could detect the moisture of the soil and trigger the water to be sent at a certain level. Also, a user could tell water to be sent at different intervals with minimal intervention. &lt;/p&gt;

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

&lt;p&gt;IoT is a network of devices connected by the internet which have the ability to collect, send, and receive data. These interconnected devices automate our lives, offering many benefits, as well as introducing challenges in order to protect information, and efficiently manage data. IoT will continue to become more advanced in the coming years with the advancements in machine learning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://aws.amazon.com/what-is/iot/#:~:text=The%20term%20IoT%2C%20or%20Internet,as%20between%20the%20devices%20themselves" rel="noopener noreferrer"&gt;Amazon Web Services&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.ibm.com/topics/internet-of-things" rel="noopener noreferrer"&gt;IBM&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.cloudflare.com/learning/cloud/what-is-the-cloud/" rel="noopener noreferrer"&gt;CloudFlare&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.techtarget.com/iotagenda/definition/Internet-of-Things-IoT" rel="noopener noreferrer"&gt;TechTarget&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.cbtnuggets.com/blog/technology/networking/seven-examples-of-iot-in-everyday-life" rel="noopener noreferrer"&gt;cbtNuggets&lt;/a&gt;&lt;br&gt;
&lt;a href="https://builtin.com/internet-things" rel="noopener noreferrer"&gt;BuiltIn&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.softwareag.com/en_corporate/resources/iot/article/iot-device.html" rel="noopener noreferrer"&gt;SoftwareAg&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Intro to Brain.js, Creating Neural Networks in javaScript</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Mon, 18 Nov 2024 07:50:49 +0000</pubDate>
      <link>https://dev.to/evanloria4/intro-to-brainjs-creating-neural-networks-in-javascript-g6d</link>
      <guid>https://dev.to/evanloria4/intro-to-brainjs-creating-neural-networks-in-javascript-g6d</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Brain.js is a javaScript library that is used to create neural networks using javaScript. The Brain.js library makes it easier to understand neural networks by supplying methods that hide all the complex computations that are done inside of a network. I will talk more specifically about what a neural network is, the methods Brain supplies, and how the library simplifies the process of creating a neural network. &lt;/p&gt;

&lt;h2&gt;
  
  
  Neural Network
&lt;/h2&gt;

&lt;p&gt;A neural network is a machine learning algorithm that uses a series of connected nodes in order to process data and makes decisions. This type of algorithm is inspired by how the human brain makes decisions using neurons that fire information to each other. Networks have three different types of layers, which contain nodes that pass information to and from each other. Functions are run on the input passed into nodes in order to determine whether or not a node should continue to pass along the information it received. You can find a more in depth explanation of neural networks and their nodes &lt;a href="https://dev.to/evanloria4/neural-networks-5cmd"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Brain.js
&lt;/h2&gt;

&lt;p&gt;Brain.js makes it possible to create neural networks using javaScript, and helps users to understand how neural networks work in the process. The library can be installed using npm.&lt;br&gt;
&lt;code&gt;npm install Brain.js&lt;/code&gt;&lt;br&gt;
Once installed, a neural network can be created in the same way a new javaScript class is created: Snippet from &lt;a href="https://github.com/BrainJS/brain.js?tab=readme-ov-file#Installation-and-Usage" rel="noopener noreferrer"&gt;Brain.js Documentation&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;brain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NeuralNetwork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;brain.NeuralNetwork takes in an optional hash table, which tells the network how to behave. &lt;br&gt;
A few optional properties to supply the network:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;inputSize: The number of input nodes&lt;/li&gt;
&lt;li&gt;outputSize: The number of output nodes&lt;/li&gt;
&lt;li&gt;learningRate: Controls the step size during training
iterations: The max number of training sessions in a network&lt;/li&gt;
&lt;li&gt;activation: The activation function used in network training.
&lt;a href="https://www.wikiwand.com/en/articles/Activation_function" rel="noopener noreferrer"&gt;Activation Functions&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;hiddenLayers: An array of numbers that specifies the number of hidden layers, and number of nodes on each layer.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;inputSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;outputSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;learningRate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;
&lt;span class="na"&gt;activationFunction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sigmoid&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;hiddenLayers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The properties have default values if they are not defined. &lt;/p&gt;
&lt;h2&gt;
  
  
  Training a Network
&lt;/h2&gt;

&lt;p&gt;Networks have a train() method to train a network what an output should be when given a certain input. train() takes in an array of data for the network to learn from. Each training pattern in the array has an input and an output property. Input and output can be either an array of numbers 0 through 1, or a hash table of numbers 0 through 1. &lt;br&gt;
Let's look at an example from &lt;a href="https://www.w3schools.com/ai/ai_brainjs.asp" rel="noopener noreferrer"&gt;W3 Schools&lt;/a&gt; that trains a network to output either 1 or 0 based on an input array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;brain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NeuralNetwork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;network&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;train&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The input property tells the network what is being supplied. The output property tells the network what the output should be when given the corresponding input. &lt;br&gt;
The output of train() is a hash table containing information of how the training went.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// W3 Schools Example&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.0039139985510105032&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// training error&lt;/span&gt;
  &lt;span class="nx"&gt;iterations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;406&lt;/span&gt;       &lt;span class="c1"&gt;// training iterations&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Brain.js run()
&lt;/h2&gt;

&lt;p&gt;Once a network is trained, the run() method can be used to output a networks prediction when a certain value is passed in. Using the same trained network from above, we can pass in an array to the run method, and the array will output its prediction based on what it learned during the training. The return value of run() is a hash table with properties that correspond to the possible outputs. The values of these properties are a number that represent how likely it is that that property is the output that should be supplied with the current input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// W3 Schools Example&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;network&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; 
&lt;span class="cm"&gt;/* result will be: 
{"zero":0.06577067077159882,"one":0.9343253374099731}
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The network tells us that there is a 93% chance that the output of the input [1,0] is 1. We are also able to use object property access on the result just like we would with a normal javaScript object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// W3 Schools Example&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;zero&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// 0.06577067077159882&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// 0.9343253374099731&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Other NeuralNetwork Methods
&lt;/h2&gt;

&lt;h4&gt;
  
  
  network.forecast(input, count)
&lt;/h4&gt;

&lt;p&gt;Returns an array of predictions for the input. Count is how many predictions are given.&lt;/p&gt;

&lt;h4&gt;
  
  
  network.toJSON()
&lt;/h4&gt;

&lt;p&gt;Returns the network converted into JSON format.The network can then be saved to a file or moved into a database.&lt;/p&gt;

&lt;h4&gt;
  
  
  network.fromJSON()
&lt;/h4&gt;

&lt;p&gt;Unstringifies a network so that it can be used for predictions. &lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Information
&lt;/h2&gt;

&lt;p&gt;This blog only mentions the very basic capabilities of the Brain.js library. Brain.js is capable of creating different types of neural networks (RNNTimeStep, LSMTimestep, GRUTimeout) which have unique behaviors. The library also allows a network to be trained asynchronously with certain network types by using trainAsync() in place of train(). If you find this post interesting, I encourage you to look at the Brain.js documentation, provided in the sources below, to get a deeper understanding of the library's capabilities. &lt;/p&gt;

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

&lt;p&gt;Brain.js simplifies the complex topic of neural networks and machine learning. Networks are able to be created using the 'new' keyword, and then can be interacted with using methods supplied by the library. By hiding all the complexities of neural networks behind the scenes, a user can more easily understand how the complex algorithms work. &lt;/p&gt;

&lt;h2&gt;
  
  
  Sources:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/BrainJS/brain.js?tab=readme-ov-file#Installation-and-Usage" rel="noopener noreferrer"&gt;Brain.js Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/@karo.skibinska/brain-js-playing-with-javascript-for-neural-networks-6004637d5ff1" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/ai/ai_brainjs.asp" rel="noopener noreferrer"&gt;W3 Schools&lt;/a&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>ai</category>
      <category>brainjs</category>
    </item>
    <item>
      <title>Promises: The Ability to Write Asynchronous Code Using Javascript</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Mon, 11 Nov 2024 10:05:18 +0000</pubDate>
      <link>https://dev.to/evanloria4/promises-the-ability-to-write-asynchronous-code-using-javascript-36a2</link>
      <guid>https://dev.to/evanloria4/promises-the-ability-to-write-asynchronous-code-using-javascript-36a2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;By nature, Javascript is a synchronous, single-threaded programming language. This means that operations are run one at a time, in the order they were called. Web applications need the ability to perform multiple operations at the same time. Using synchronous code, a program would have to wait for an operation to complete before the program could continue running. For this reason, Javascript offers a way for developers to create asynchronous operations that perform in the background, allowing code to continue running before an operation is completed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous Callbacks
&lt;/h2&gt;

&lt;p&gt;A callback is a function that is passed into another function to be called at the appropriate time. Before the introduction of promises in Javascript ES6, callbacks were used to handle asynchronous operations. Because Javascript uses an event loop to track the order in which functions should be executed, functions are able to execute in a different order in which they were called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addNextNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;addOne&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;addOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;addNextNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The addNextNumber function relies on the return of the addOne function. The addOne function finishes executing, and then the addNextNumber function is able to complete its operation.&lt;/p&gt;

&lt;p&gt;However, if we need to pass multiple callbacks into one another our code can become difficult to understand. When callbacks become overly nested it is called callback hell, or the pyramid of doom.&lt;/p&gt;

&lt;p&gt;Example From &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing" 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 javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doStep1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;init&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doStep2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;init&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doStep3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;init&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doOperation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;doStep1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;doStep2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;doStep3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`result: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;result3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;doOperation&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see how nesting callbacks can become difficult to read when callback functions take in another callback. Especially when dealing with more complicated callbacks that fetch data from a server. &lt;/p&gt;

&lt;h2&gt;
  
  
  Promises
&lt;/h2&gt;

&lt;p&gt;A promise is a unique object that is returned by an asynchronous function that represents the eventual success or failure of an asynchronous operation. Promises are the foundation of asynchronous Javascript. Promise objects contain two properties: state and result: &lt;/p&gt;

&lt;h4&gt;
  
  
  State Property Value Options
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Pending: The function that created the promise has not finished executing &lt;/li&gt;
&lt;li&gt;Fulfilled: The asynchronous function succeeded&lt;/li&gt;
&lt;li&gt;Rejected: The asynchronous function failed&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Result Property Value Options
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The value of the successful result&lt;/li&gt;
&lt;li&gt;The value of the failed result&lt;/li&gt;
&lt;li&gt;Undefined &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is worth noting that the above properties are not code-accessible. We can only view them using a debugger tool.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch" rel="noopener noreferrer"&gt;Fetch API&lt;/a&gt; is what javascript uses to make requests to a server. It replaced &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest" rel="noopener noreferrer"&gt;XMLHttpRequest&lt;/a&gt;, which uses callbacks. A request is made by calling fetch(), which can take in an object, or a string url of a resource to send a request to. The return value of fetch() is a promise object. &lt;/p&gt;

&lt;h3&gt;
  
  
  Promise Handlers
&lt;/h3&gt;

&lt;p&gt;The promise object supplies methods to deal with the result of a promise. We will be talking about the .then() and .catch() handlers, but you can find other promise handlers &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#instance_methods" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  .then() Handler
&lt;/h4&gt;

&lt;p&gt;.then() takes in two callback arguments: one for the case of a fulfilled promise, and one for the rejected promise. This handler is capable of returning a promise, returning a value, or throwing an error. &lt;br&gt;
Let's set a variable equal to the promise returned by invoking fetch()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ourPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://example.com/post&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can attach then() to our promise, and once the promise is fulfilled or rejected it will perform the corresponding callback argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fulfilledCB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Promise was successful&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rejectedCB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Promise failed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;ourPromise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fulfilledCB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rejectedCB&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If our promise is returned successfully, our fulfilledCB will run. If the promise was rejected, then our rejectedCB will run. We can optionally only pass in a callback for the value we are interested in, setting the unwanted value's callback to null.&lt;/p&gt;

&lt;h3&gt;
  
  
  Chaining Promises
&lt;/h3&gt;

&lt;p&gt;Once a response object is received using fetch(), we need to use another function to access the data of the response. We can do this using the json() method to transform our promise into a json object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ourPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://example.com/post&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;ourPromise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use our first .then() to transform the promise  into json, and the second to access the data within our response object.&lt;/p&gt;

&lt;h4&gt;
  
  
  .catch()Handler
&lt;/h4&gt;

&lt;p&gt;You may have noticed that in the above code snippet we did not provide a callback to handle rejected promises. When using promise chaining, we attach the .catch handler to the end of our chain. This handler will take in a callback to deal with any promise that was rejected at any point in the chain. .catch() can also be used with only one .then() rather than passing in an error callback to .then().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ourPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://example.com/post&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;ourPromise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;One of your promises was rejected&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;.catch() offers an extremely simple way to handle errors when dealing with asynchronous code. &lt;/p&gt;

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

&lt;p&gt;In order for our programs to run as efficiently as possible, it is imperative that our code have the ability to run asynchronously. Long-running synchronous code would cause a program to become unresponsive while it waited on the return of an operation. Promises offer an elegant solution to writing asynchronous code by improving code readability, simplifying error handling, and allowing developers to avoid callback hell.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises" rel="noopener noreferrer"&gt;MDN Promises&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing" rel="noopener noreferrer"&gt;MDN Introduction to Asynchronous Code&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.freecodecamp.org/news/synchronous-vs-asynchronous-in-javascript/" rel="noopener noreferrer"&gt;Free Code Camp Asynchronous Javascript&lt;/a&gt;&lt;br&gt;
&lt;a href="https://blog.greenroots.info/javascript-promises-explain-like-i-am-five" rel="noopener noreferrer"&gt;Green Roots Promises Explained Like I Am Five&lt;/a&gt;&lt;br&gt;
&lt;a href="https://blog.greenroots.info/javascript-promise-chain-the-art-of-handling-promises?source=more_series_bottom_blogs" rel="noopener noreferrer"&gt;Green Roots Handling Promises&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/jps27cse/exploring-asynchronous-javascript-callbacks-promises-and-asyncawait-16k6#:~:text=Callbacks%3A%20The%20Original%20Asynchronous%20Pattern,handle%20asynchronous%20operations%20in%20JavaScript."&gt;Dev Community Callbacks, Promises, and Async/Wait&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

</description>
      <category>es6</category>
      <category>learning</category>
      <category>api</category>
    </item>
    <item>
      <title>Dynamic Link Library Basics</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Mon, 07 Oct 2024 13:53:18 +0000</pubDate>
      <link>https://dev.to/evanloria4/dynamic-link-libraries-22je</link>
      <guid>https://dev.to/evanloria4/dynamic-link-libraries-22je</guid>
      <description>&lt;h2&gt;
  
  
  What are Dynamic Link Libraries
&lt;/h2&gt;

&lt;p&gt;Dynamic Link Libraries (DLLs) were created by Microsoft, and are usually written using C++. A dynamic link library is a type of file that allows resources, such as functions and other types of data, to be shared across multiple applications. They offer a number of benefits to Microsoft’s  programs, but they can also induce errors within programs.&lt;/p&gt;

&lt;h2&gt;
  
  
  What They Do
&lt;/h2&gt;

&lt;p&gt;DLLs offer multiple benefits for Microsoft’s applications to run. DLLs reduce the size of files, which in turn improves the performance of the program. Because several programs are able to use a DLL at the same time, the same block of code is able to be used by multiple programs.&lt;/p&gt;

&lt;p&gt;In addition, the DLL code is able to be updated independently from the other programs, and the programs will have access to the updated code. This is much easier than having to update code in each individual program. &lt;/p&gt;

&lt;p&gt;However, there are dangers to be aware of when using DLLs across multiple programs. Updates can cause a program's code to break due to compatibility issues. DLLs can also leave systems vulnerable to malware attacks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Static Library
&lt;/h2&gt;

&lt;p&gt;'Static libraries refer to a set of routines, external functions, and variables compiled and linked into an executable file at compile time.' (Vijay Kanade, What Is a Dynamic Link Library (DLL)? Meaning, Types, and Advantages) These types of libraries cause files to become larger because it appears as if the library code is part of the program itself. These libraries are more beneficial when creating a smaller library, and they are able to cut down the time is takes to execute a function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1ulhgs8cmbm1ygxtj880.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1ulhgs8cmbm1ygxtj880.png" alt="Static vs Dynamic" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamic Library Differences
&lt;/h2&gt;

&lt;p&gt;Unlike static libraries, dynamic libraries are only loaded to memory at the execution of a function within the library. The DLL is referenced within the memory of a computer when a program is started. Once the program requests the use of a function, then the library is loaded into memory. This library is better served when creating larger files and programs. &lt;/p&gt;

&lt;h2&gt;
  
  
  Steps For DLL
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;When a program needs a function:
The program searches for the DLL in memory. If it is not found, then the library is loaded into memory, and the resources added to the memory space of the program.&lt;/li&gt;
&lt;li&gt;Link to DLL:
A function is called to link the DLL to the program. A dynamic link method is then used to link the library and the program. A dynamic link being used means that only a portion of the DLL file will be loaded into the program memory. This reduces file size, but increases the time it takes to run the program.&lt;/li&gt;
&lt;li&gt;Once the first two steps are completed the program has access to all the resources within the DLL. &lt;/li&gt;
&lt;li&gt;The DLL is offloaded from the computer's memory once the program has finished using it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;p&gt;Code Reuse: Dynamic libraries enable programs to reuse code. This eliminates duplicate code, enabling programs with matching functionality to use the same block of code. &lt;br&gt;
Better Performance: Because DLL's control the size of files, and only load the DLL into memory when needed, the program can better execute due to the reduced loading time.&lt;br&gt;
Encourages Modularity: Modularity is beneficial to creating programs because it allows component's to focus on smaller tasks. DLLs encourage modularity by allowing each component full access to the resources within. &lt;/p&gt;

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

&lt;p&gt;DLLs offer many benefits to programs and operating systems in general. These libraries play a huge role in the overall performance of a computer’s programs. Although the use of DLLs is not a perfect way to enhance a program's efficiency, by limiting file size, encouraging modularity, and eliminating duplicate code DLLs allow for more efficient program execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries" rel="noopener noreferrer"&gt;Learn Microsoft&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/@mishqat_abid/dll-files-basics-dll-hijacking-81a6e2519215" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.spiceworks.com/tech/tech-general/articles/what-is-dll/#_001" rel="noopener noreferrer"&gt;Spice Works&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Neural Networks</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Mon, 30 Sep 2024 12:38:00 +0000</pubDate>
      <link>https://dev.to/evanloria4/neural-networks-5cmd</link>
      <guid>https://dev.to/evanloria4/neural-networks-5cmd</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Machine learning is the process in which artificial intelligence uses data and algorithms to learn over time. Models are able to run multiple simulations, changing how much weight a particular variable holds on a decision, and then compares the result to the expected result. Neural networks are a subclass of machine learning. Rather than making decisions based on the data the system receives, neural networks are able to create new variables to factor into their decision making. This gives neural networks the ability to attempt to give answers to more complex problems. &lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Structure
&lt;/h2&gt;

&lt;p&gt;Neural networks are designed to make decisions like the human brain. They use nodes/neurons that pass information to each other at different intensities, and then decide whether or not to discard that information. These networks are made up of layers that consist of nodes that pass data to each other. The strength of the connection between two nodes is called the connection weight. This will determine how much impact the data coming from a node will have on the decision of the node receiving the information. Larger 'weights' have more of an impact on a node's decision making. Weights are initially randomly assigned, but will changed overtime as the system learns which data is more important.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fbb03x4wnx81nj5jw0g6z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbb03x4wnx81nj5jw0g6z.png" alt="Layers" width="278" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Input Layer
&lt;/h2&gt;

&lt;p&gt;First, the data is passed into the nodes/neurons within the input layer. "Each neuron corresponds to a feature, and its value represents the feature’s value" (Sarita, 'Basic Understanding of Neural Network Structure'). Each node then passes the weighted sum value to every node on the next layer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hidden Layer
&lt;/h2&gt;

&lt;p&gt;The hidden layer is the the next layer within the network which receives the weighted sum from the input layer. The weighted sum is calculated by multiplying the output of the node by the connection weight between the two nodes exchanging data, and then adding all the resulting values together.&lt;/p&gt;

&lt;p&gt;In the example below, the weighted sum is calculated by:&lt;br&gt;
&lt;code&gt;Value1 = 160 Weight1 = 0.35&lt;br&gt;
Value2 = 55 Weight2 = 0.2&lt;br&gt;
(Value1 * Weight1) + (Value2 * Weight2) = 67&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.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%2F0js8zye1r7fkyxm9gubf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0js8zye1r7fkyxm9gubf.png" alt="Weighted Sum" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://deeplizard.com/lesson/ddd2drizla" rel="noopener noreferrer"&gt;Deep Learning Dictionary - Lightweight Crash Course&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The bias of the node will then be added to the weighted sum before being passed into an activator function. The bias is a constant that "is used to offset the result. It helps the models to shift the activation function towards the positive or negative side."(Turing, 'What Is the Necessity of Bias in Neural Networks?)&lt;br&gt;
The value of bias will change as the system learns &lt;/p&gt;

&lt;p&gt;A network may have multiple hidden layers depending on how complex it is. In these cases, each hidden layer is in charge of learning about a separate aspect of the data. The layers are broken up into components that perform smaller tasks. &lt;/p&gt;

&lt;h2&gt;
  
  
  Activator Functions
&lt;/h2&gt;

&lt;p&gt;Activator functions are required to make networks non-linear, allowing data to be passed in all directions to different nodes, and then backwards through the network. The purpose of an activator function is to tell a node whether or not to send data to the next layer (activate). Different activators will perform different transformations on node output, and have different return values. If the activator function does not tell the node to fire, then it will not output any information to the next layer. &lt;br&gt;
A few popular functions are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sigmoid&lt;/li&gt;
&lt;li&gt;TanH&lt;/li&gt;
&lt;li&gt;ReLu&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F5sp8ltt8lf5x02eo9omj.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5sp8ltt8lf5x02eo9omj.jpeg" alt="Activators" width="432" height="117"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Output Layer
&lt;/h2&gt;

&lt;p&gt;The final hidden layer passes data to the output layer, which is where the final decision/output of the model will be made. The same calculations performed within the hidden layers are also performed here. The weighted sum is calculated and the bias is added. That number is then passed into an activator function that tells the node whether or not to fire. &lt;/p&gt;

&lt;h2&gt;
  
  
  Backward Propagation
&lt;/h2&gt;

&lt;p&gt;This is where the model is able to learn from the mistakes it made. The data is passed backwards through the system, and evaluated to see where mistakes were made. The system changes the weights and biases over the nodes, and runs the simulation again. By changing the weights,  the system is able to learn which variables are more impactful when making a decision. &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Neural networks are complex machines, with the ability to analyze data, compare results, and manipulate conditions in order to become better decision makers. The system is composed of three types of layers, which are a collection of nodes that pass data to each other. The hidden and output layers perform calculations that decide whether or not information should continue to be passed through the network. A huge characteristic of these networks is the ability to pass data backwards, which gives it the ability to learn and improve. &lt;/p&gt;

&lt;p&gt;Sources: &lt;br&gt;
&lt;a href="https://aws.amazon.com/what-is/neural-network/#:~:text=A%20neural%20network%20is%20a,that%20resembles%20the%20human%20brain." rel="noopener noreferrer"&gt;AWS Neural Network&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.ibm.com/topics/neural-networks#:~:text=Every%20neural%20network%20consists%20of,own%20associated%20weight%20and%20threshold." rel="noopener noreferrer"&gt;IBM Neural Network&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/@sarita_68521/basic-understanding-of-neural-network-structure-eecc8f149a23" rel="noopener noreferrer"&gt;Basic Understanding&lt;/a&gt;&lt;br&gt;
&lt;a href="https://bipartisanpolicy.org/blog/ai-101/#:~:text=Machine%20learning%20(ML)%20is%20a,all%20AI%20is%20machine%20learning." rel="noopener noreferrer"&gt;AI, Machine Learning, Neural Networks, and Deep Learning&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.v7labs.com/blog/neural-networks-activation-functions#h1" rel="noopener noreferrer"&gt;Function Activation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>ai</category>
    </item>
    <item>
      <title>Malware: Detection, Collection, and Analysis</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Mon, 23 Sep 2024 11:45:47 +0000</pubDate>
      <link>https://dev.to/evanloria4/malware-detection-collection-and-analysis-1o68</link>
      <guid>https://dev.to/evanloria4/malware-detection-collection-and-analysis-1o68</guid>
      <description>&lt;h2&gt;
  
  
  What is Malware
&lt;/h2&gt;

&lt;p&gt;Malicious software, or malware, is software that is specifically designed to disrupt, damage, or gain unauthorized access to a computer system. The existence of malware requires users and companies to secure their systems, in order to prevent data leaks. Researchers are constantly working with live malware and analyzing behavior in order to develop new techniques in combatting attacks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Malware Classification
&lt;/h2&gt;

&lt;p&gt;A user can become infiltrated by malware in a number of different ways. Attackers will use different methods to trick a user into downloading different types of malware. Malware is often disguised as a regular link or file, and will be downloaded when a user clicks on the element. It is even possible for a user to become infected without clicking on anything if they visit an infected website. Common types of malware include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Worms: Software that spreads through a network by replicating itself&lt;/li&gt;
&lt;li&gt;Trojans: Software that disguises itself as desirable code&lt;/li&gt;
&lt;li&gt;Spyware: Software that secretly collects user activity&lt;/li&gt;
&lt;li&gt;Adware: Software that displays unwanted ads&lt;/li&gt;
&lt;li&gt;Ransomware: Software that locks a user out of their environment until a ransom is paid&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F57wmdc04i7ncvk2ckx4x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F57wmdc04i7ncvk2ckx4x.png" alt="Types of Malware" width="504" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Detection and Collection of Malware
&lt;/h2&gt;

&lt;p&gt;The ability to detect malware within a network or server is a key skill in protecting user data. If a server has been compromised, the software will leave signs of its presence, these signs are known as indicators of compromise(IOCs). Some IOCs include unusual traffic, an increase in the number of incorrect logins, or files existing in the incorrect locations. &lt;/p&gt;

&lt;p&gt;One technique to detect attacks is a malware honeypot. Honeypots are designed to look like a regular application, and invite attacks onto the system in a secure environment that does not actually allow access to the server. This allows for the detection of malware because the honeypot is hidden, so any traffic on the honeypot is likely malicious.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Flrcb93pjyp9wmkbienxr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Flrcb93pjyp9wmkbienxr.jpg" alt="Honeypots" width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Malware Analysis
&lt;/h2&gt;

&lt;p&gt;Malware can be analyzed statically, without running the code, by looking at the name of a file, the address the file came from, and other signs a file may be malicious.  &lt;/p&gt;

&lt;p&gt;On the other hand, dynamic analysis views the software while it is running. Honeypots are a great dynamic analysis tool. If a hacker commits an attack on a honeypot, their activity is able to monitored, this can be used to learn valuable information on what type of attack is being attempted, and the best way to respond.&lt;/p&gt;

&lt;p&gt;Another technique used for data analysis is sandboxing. Sandboxing refers to the practice of downloading malware into a totally separate environment from the server, where the malware's behavior can be studied. "Security researchers use sandboxing when analyzing malware and many advanced anti-malware products use it to determine whether or not suspicious files are truly malicious based on their behavior." (Marc Laliberte, &lt;strong&gt;&lt;u&gt;The Difference Between Sandboxing, Honeypots &amp;amp; Security Deception&lt;/u&gt;&lt;/strong&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  Malware Data Sets
&lt;/h2&gt;

&lt;p&gt;There are databases available that contain samples of known types of malware and their features. The existence of these datasets offer the ability to test methods to detect malware. Some reputable malware repositories include theZoo, InQuest, and malwareBizaar.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why It's Important
&lt;/h2&gt;

&lt;p&gt;The detection and analysis of malware within a system is pivotal to protecting user data. It's believed that there are "more than 1 billion malware programs" in existence, with thousands more created each day.(Darren Craft, &lt;strong&gt;&lt;u&gt;Malware Statistics &amp;amp; Facts: Frequency, Impact &amp;amp; Cost&lt;/u&gt;&lt;/strong&gt;) With the constant development of new malware, researchers must also develop new ways to detect, analyze, and prevent malware attacks. &lt;/p&gt;

&lt;p&gt;Sources: &lt;br&gt;
&lt;a href="https://paperswithcode.com/task/malware-classification#:~:text=Malware%20Classification%20is%20the%20process,on%20how%20they%20are%20extracted." rel="noopener noreferrer"&gt;Malware Classification&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.crowdstrike.com/cybersecurity-101/malware/types-of-malware/" rel="noopener noreferrer"&gt;Types of Malware&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.crowdstrike.com/cybersecurity-101/malware/malware-detection/" rel="noopener noreferrer"&gt;Malware Detection Techniques&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.crowdstrike.com/cybersecurity-101/indicators-of-compromise/" rel="noopener noreferrer"&gt;Indicators of Compromise&lt;/a&gt;&lt;br&gt;
&lt;a href="https://usa.kaspersky.com/resource-center/threats/what-is-a-honeypot" rel="noopener noreferrer"&gt;Honeypots&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.crowdstrike.com/cybersecurity-101/malware/malware-analysis/" rel="noopener noreferrer"&gt;Malware Analysis&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.darkreading.com/endpoint-security/the-difference-between-sandboxing-honeypots-security-deception" rel="noopener noreferrer"&gt;Honeypots &amp;amp; Sandboxes&lt;/a&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>software</category>
    </item>
    <item>
      <title>Why We Need Higher-Order Functions</title>
      <dc:creator>Evan Loria</dc:creator>
      <pubDate>Fri, 09 Aug 2024 17:30:03 +0000</pubDate>
      <link>https://dev.to/evanloria4/why-we-need-higher-order-functions-2k4m</link>
      <guid>https://dev.to/evanloria4/why-we-need-higher-order-functions-2k4m</guid>
      <description>&lt;h2&gt;
  
  
  What is a Higher Order Function?
&lt;/h2&gt;

&lt;p&gt;Higher Order Functions are functions that are able to take in a function as an argument, or return a function. These characteristics allow developers to produce the desired result in a much more readable way. This article will give some examples of well known higher-order functions and the benefits they provide.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions as Arguments &amp;amp; Return Values
&lt;/h2&gt;

&lt;p&gt;The ability to pass a function as an argument, or return a function call is a huge benefit to developers. This is a key way to make code more neat and readable. As Joan Ayebola put it in her article 'What are Higher Order Functions in JavaScript? Explained With Examples' It also allows for 'dynamic behavior' within our code.&lt;/p&gt;

&lt;p&gt;// Function as an argument &lt;br&gt;
&lt;a href="https://media2.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%2Fp6o6plv3lbwb2wqcahtu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fp6o6plv3lbwb2wqcahtu.png" alt="Function as an Argument" width="729" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the higher order function is called:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The function performs its code (in this case it's logging to the console)&lt;/li&gt;
&lt;li&gt;The callback function is invoked and performs its tasks&lt;/li&gt;
&lt;li&gt;Expect 'DO SOMETHING' and 'I was passed as an argument' to log to the console&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;// Function as a return value&lt;br&gt;
&lt;a href="https://media2.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%2Fvgub4o5wnhwy7gbmkquc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvgub4o5wnhwy7gbmkquc.png" alt="Image description" width="792" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When greet_message is called:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The function calls the greet function with 'Evan' passed in&lt;/li&gt;
&lt;li&gt;greet() returns a function that logs a greeting to the console&lt;/li&gt;
&lt;li&gt;Expect 'Hi!! Evan, Welcome To Operation Spark!' to log to the console&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Useful Higher-Order Functions
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ftof3vqo5qm22kwwu4816.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftof3vqo5qm22kwwu4816.jpeg" alt="HOFs" width="369" height="136"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;.filter() Method&lt;/li&gt;
&lt;li&gt;.map() Method&lt;/li&gt;
&lt;li&gt;.forEach() Method&lt;/li&gt;
&lt;li&gt;.reduce() Method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these is an array method that takes a function as an argument. &lt;/p&gt;

&lt;h2&gt;
  
  
  .filter()
&lt;/h2&gt;

&lt;p&gt;Iterates over an array and returns a new array of all the elements that returned a truthy value when passed through the function argument. Does not mutate the elements. The .filter() method takes in a parameter representing the current element being passed over.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F10h95t0g13zl34xmybev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F10h95t0g13zl34xmybev.png" alt=".filter()" width="800" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  .map()
&lt;/h2&gt;

&lt;p&gt;Iterates over an array and returns a new array of the result of invoking the function argument onto each element in the array. The .map() method takes in a parameter representing the current element being passed over.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fgar2d153sgy03fpr3yms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgar2d153sgy03fpr3yms.png" alt=".map()" width="800" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  .reduce() Method
&lt;/h2&gt;

&lt;p&gt;.reduce() can be used in many different ways. It iterates over an array and returns a single value. The function argument of .reduce() takes in 2 parameters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;accumulator: The value to be returned at the end of the function call&lt;/li&gt;
&lt;li&gt;current: The current index being passed into the function &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;.reduce() also has an optional third parameter seed. Seed defines the value in which accumulator will start at. If no seed is given, the accumulator value begins at the first element of the array, and the function begins iterating at the second element. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fxvz8g7qnpl08seu3azww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxvz8g7qnpl08seu3azww.png" alt=".reduce()" width="800" height="248"&gt;&lt;/a&gt;&lt;br&gt;
Other possible uses for .reduce()&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set the seed as an empty array and push desired elements onto it&lt;/li&gt;
&lt;li&gt;Assign key-value pairs to an object&lt;/li&gt;
&lt;li&gt;Concatenate to a string&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  .forEach()
&lt;/h2&gt;

&lt;p&gt;Iterates over an array and mutates each element. A key charatceristic to note about the .forEach() method is that it has no return value. Therefore, unlike the other 3 methods, you cannot initialize a variable to the value of invoking .forEach(), because the return value is undefined. Just like .map() and .filter(), .forEach() takes a parameter representing the current element being passed over.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F6ssvhowws5cau45mlh0o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6ssvhowws5cau45mlh0o.png" alt=".forEach()" width="504" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;While higher-order functions may be a little confusing at first, they really make developers lives a lot simpler. Without the ability to return functions, or pass in functions as arguments; our code would become a lot less readable very quickly. &lt;/p&gt;

&lt;h2&gt;
  
  
  TIP
&lt;/h2&gt;

&lt;p&gt;It may be a good idea to look at the native code for all of these methods. The more you understand about how these functions work, the easier they will be.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F37cnepy7lu07xrsfrj2t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F37cnepy7lu07xrsfrj2t.jpg" alt="meme" width="500" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://www.freecodecamp.org/news/higher-order-functions-explained/#:%7E:text=JavaScript%20offers%20a%20powerful%20feature,even%20return%20functions%20as%20results" rel="noopener noreferrer"&gt;https://www.freecodecamp.org/news/higher-order-functions-explained/#:~:text=JavaScript%20offers%20a%20powerful%20feature,even%20return%20functions%20as%20results&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
