<?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>Forem: Valentina Peric</title>
    <description>The latest articles on Forem by Valentina Peric (@valentinaperic).</description>
    <link>https://forem.com/valentinaperic</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%2F177561%2F3364e44e-b6b0-4958-949c-a81ef8872798.png</url>
      <title>Forem: Valentina Peric</title>
      <link>https://forem.com/valentinaperic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/valentinaperic"/>
    <language>en</language>
    <item>
      <title>Local Storage vs Session Storage in JavaScript</title>
      <dc:creator>Valentina Peric</dc:creator>
      <pubDate>Sun, 07 May 2023 22:43:48 +0000</pubDate>
      <link>https://forem.com/valentinaperic/local-storage-vs-session-storage-in-javascript-1p8h</link>
      <guid>https://forem.com/valentinaperic/local-storage-vs-session-storage-in-javascript-1p8h</guid>
      <description>&lt;p&gt;Let's dive into two fundamental web storage mechanisms in JavaScript: Local Storage and Session Storage. These two mechanisms are important for creating personalized user experiences by using data persistence. They both store data in the users browser in a way that cannot be read by a server. We are going to dive into both, explain its use cases, and the differences between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites 📝
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;A working knowledge of javascript&lt;/li&gt;
&lt;li&gt;A working knowledge of web browsers &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's Get Started ✨
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is Local Storage?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Local storage stores key-value pairs in the user's browser. This stored data is persisted even after the user closes their browser or when they shut down their device. This data can be cleared by the user manually clearing their browser cache or until the app clears the data. It has a storage capacity of about 5MB which is larger than what cookies can store. &lt;/p&gt;

&lt;p&gt;Local storage is great for storing global data that is going to be accessed often in your app like username, email, name, etc. It is also great for features like remember me which can help streamline a user's experience by skipping steps that they have already inputted before. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Session Storage?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Similar to local storage, session storage also stores key-value pairs in the user's browser. In fact, they are both objects that are part of the web storage API with the difference being that their data is handled differently. Instead of the data being persisted for multiple sessions, it is only available during the duration of its user's specific session. This means that when the user closes its browser or tab, the data gets cleared. This is great for multi-step processes like booking flights and hotels, shopping carts and user authentication.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Local Storage vs Session Storage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's compare the two. You know that they have differences in how their data is persisted. What other differences do they have? Firstly, the scope is different. Local storage has global scope across all tabs and windows while session storage is limited to its single tab or window. Local storage also has a larger capacity which makes sense given that it can persist data among multiple tabs and windows. &lt;/p&gt;

&lt;p&gt;This may even have an impact on performance when not used wisely. Given its large size, if used improperly, it can slow down your app, however, it does not make a huge difference. The most important thing is to choose which scenario is best for the specific use case of your app. Should the data be persisted for multiple sessions? Use local storage. If not, consider session storage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next Steps ✨
&lt;/h2&gt;

&lt;p&gt;How can you personalize the user experience of your app using local or session storage? Go take a deep dive into this! You can even take a look at how apps do this by looking into the Application tabs in dev tools shown below 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZeTR6ohZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0o2hzx3ut6elw73ajcw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZeTR6ohZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0o2hzx3ut6elw73ajcw.png" alt="screen shot of dev tools showing local and session storage" width="678" height="1018"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you used local or session storage before? What use cases have you used them in? What use cases are you considering? Let me know in the comments!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>localstorage</category>
    </item>
    <item>
      <title>The Heart of all Vue.js Applications: Vue Instance</title>
      <dc:creator>Valentina Peric</dc:creator>
      <pubDate>Sun, 07 May 2023 03:12:29 +0000</pubDate>
      <link>https://forem.com/valentinaperic/the-heart-of-all-vuejs-applications-vue-instance-2c3g</link>
      <guid>https://forem.com/valentinaperic/the-heart-of-all-vuejs-applications-vue-instance-2c3g</guid>
      <description>&lt;p&gt;Let's talk about the heart of all Vue.js applications. The motherboard. The part that is absolutely essential to running these applications: Vue Instance. What is it? How do we use it? In this article, I will help break down what this fundamental topic is and how they are used in Vue applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites 📝
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;A working knowledge of javascript&lt;/li&gt;
&lt;li&gt;A code editor (I recommend Visual Studio Code)&lt;/li&gt;
&lt;li&gt;A working knowledge of Vue.js (V2)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's Get Started ✨
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Please note:&lt;/strong&gt; This is in Vue.js V2. You will see in V3, the initialization is a bit different. You can read more about it &lt;a href="https://vueschool.io/articles/vuejs-tutorials/the-benefits-of-the-vue-3-app-initialization-code/"&gt;here&lt;/a&gt;. Back to our scheduled programming 👇,&lt;/p&gt;

&lt;p&gt;To put it simply, the Vue Instance is the root of every Vue application. It is used to connect the data, properties, and lifestyle hooks. Let's take a look at an example and go from there,&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;new&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;el&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;books&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Seed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;books&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;created&lt;/span&gt;&lt;span class="p"&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="nx"&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 world&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a Vue instance is created, an &lt;code&gt;options&lt;/code&gt; object is passed in. In the example above, this object contains the &lt;code&gt;el&lt;/code&gt; and &lt;code&gt;data&lt;/code&gt; property. &lt;code&gt;el&lt;/code&gt; represents a specific part of the DOM that the Vue instance will be mounted to, which in this case, is the &lt;code&gt;#app&lt;/code&gt;. Typically, you will see that &lt;code&gt;#app&lt;/code&gt; is the root of application. &lt;/p&gt;

&lt;p&gt;The second property is &lt;code&gt;data&lt;/code&gt;. This property passes in an object that contains reactive data. Any changes to that data will cause a rerender of its corresponding view in the DOM. This happens because of vue's reactivity system which consists of two-way databinding. &lt;/p&gt;

&lt;p&gt;In the example above, you will see that &lt;code&gt;books&lt;/code&gt; was passed in. This could be an array of objects holding data about books. If this app was a library system and a librarian added a book to the system, this array would be updated which would cause the &lt;code&gt;#app&lt;/code&gt; view to rerender. This creates a seamless user experience. &lt;/p&gt;

&lt;p&gt;The Vue instance also provides a set of lifestyle hooks. These hooks help with tasks like initialization and clean-up. Common hooks are &lt;code&gt;created&lt;/code&gt;, &lt;code&gt;mounted&lt;/code&gt;, &lt;code&gt;updated&lt;/code&gt;, and &lt;code&gt;destroyed&lt;/code&gt;. The above example shows the &lt;code&gt;created&lt;/code&gt; hook. This means that its function will get called when the Vue instance is created which in this case, &lt;code&gt;hello world&lt;/code&gt; will get printed out in the console. &lt;/p&gt;

&lt;h2&gt;
  
  
  Next Steps ✨
&lt;/h2&gt;

&lt;p&gt;So there you have it, the heart of all Vue applications. Do not forget to add this instance to your Vue app (I have! It will cause errors hehe) and you will see how it essential it is for building your app. Take a look at Vue projects or build your own to see it in action. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>vue</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Breaking down Promises in JavaScript</title>
      <dc:creator>Valentina Peric</dc:creator>
      <pubDate>Fri, 24 Mar 2023 19:50:56 +0000</pubDate>
      <link>https://forem.com/valentinaperic/breaking-down-promises-in-javascript-34a8</link>
      <guid>https://forem.com/valentinaperic/breaking-down-promises-in-javascript-34a8</guid>
      <description>&lt;p&gt;Promises became widely supported and popularized in JavaScript when ES6 was released in 2015. Although, fun fact, the term was proposed by Daniel P. Friedman back in 1975. By definition, a promise is an object that contains the results of the eventual completion or failure of an asynchronous operation. This article will break down what this all means. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites 📝
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;A working knowledge of javascript&lt;/li&gt;
&lt;li&gt;A code editor (I recommend Visual Studio Code)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's Get Started ✨
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Three States of Promises&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Promises have three different states depending on the operations that are being executed, &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Pending&lt;/em&gt;: the operation has not yet completed &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Fulfilled&lt;/em&gt;: the operation has completed successfully and the returned object is ready&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Rejected&lt;/em&gt;: The operation has failed &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Cool, makes sense. Now, how does this look like in 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;myPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="c1"&gt;//async operation&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;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;   &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the operation has completed successfully!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&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="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;"the operation has failed"); 
  }
});
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The promise constructor takes in one parameter which is the callback function. The callback function takes in two parameters which are the resolve and reject functions. While the asynchronous operation is executing, the promise is in a pending state. Once the operation of the promise has completed, the resolve function can be called to indicate the operation was successful, and the reject function if it has failed.  The promise object returned from calling the Promise constructor can then be used for chaining.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chaining Promises and Consumers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can chain promises by using consumers such as &lt;code&gt;.then()&lt;/code&gt;, &lt;code&gt;.catch()&lt;/code&gt;, and &lt;code&gt;.finally()&lt;/code&gt;. Let's go through each one,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.then()&lt;/code&gt; continues code execution and is invoked when a promise is either resolved or rejected. it takes in two arguments: the callback function of the fulfilled promise and the callback function for the rejected case.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.catch()&lt;/code&gt; is invoked when a promise is rejected. It is also invoked when some error has occurred during execution. Treated similar to the .then() but it does not take in an argument for when the promise is fulfilled. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;.finally()&lt;/code&gt; gets invoked when the promise has settled, regardless of success or failure. This consumer takes no arguments. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's see how they all work together by making a cappuccino.&lt;/strong&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="nx"&gt;boilWater&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;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;setTimeout&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;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;water has boiled.&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="mi"&gt;2000&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;brewCoffee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;water&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;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;setTimeout&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;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;coffee has been brewed.&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="mi"&gt;2000&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;frothMilk&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;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;setTimeout&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Milk has been frothed.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&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="nx"&gt;boilWater&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;water&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;brewCoffee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;water&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="nx"&gt;then&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;frothMilk&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="nx"&gt;then&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enjoy your cappuccino&lt;/span&gt;&lt;span class="dl"&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="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="nx"&gt;error&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="k"&gt;finally&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Continue coding!&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;In the example above, &lt;code&gt;boilWater&lt;/code&gt; is called first which returns a resolved promise that gets chained to the &lt;code&gt;brewCoffee&lt;/code&gt; function. The chaining continues and once &lt;code&gt;frothMilk&lt;/code&gt; returns a resolved promise, &lt;code&gt;enjoy your cappuccino&lt;/code&gt; will be logged out. If any promise gets rejected or any other error occurs in the execution, the catch will log out the error. The &lt;code&gt;finally&lt;/code&gt; consumer will get called regardless of success or failure. This is the routine that I have every morning :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Next Steps ✨
&lt;/h2&gt;

&lt;p&gt;You can see that promises can result in a lot of chaining and nested functions which can cause readability in your code to suffer. With async/await, a lot of this will be made simpler. You can read how &lt;a href="https://dev.to/valentinaperic/asyncawait-in-javascript-2i8c"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>promises</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Async/Await in Javascript</title>
      <dc:creator>Valentina Peric</dc:creator>
      <pubDate>Tue, 21 Mar 2023 23:18:49 +0000</pubDate>
      <link>https://forem.com/valentinaperic/asyncawait-in-javascript-2i8c</link>
      <guid>https://forem.com/valentinaperic/asyncawait-in-javascript-2i8c</guid>
      <description>&lt;p&gt;Reading codebases and seeing async/await in functions may look a little tricky. With this article, you will see how they work and notice that it is a clever, easier way to work with asynchronous functions. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites 📝
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;A working knowledge of promises (I will give a quick refresher, if you do not remember!) &lt;/li&gt;
&lt;li&gt;A working knowledge of try/catch &lt;/li&gt;
&lt;li&gt;A code editor (I recommend &lt;a href="https://visualstudio.microsoft.com/"&gt;Visual Studio Code&lt;/a&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's Get Started ✨
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;First, let's breakdown the &lt;em&gt;async&lt;/em&gt; keyword&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you see the async keyword before a function, this means that the function is going to return a promise. (here's the refresher!!!) A promise, in simple terms, is an object that contains the results of the eventual completion or failure of an asynchronous operation. Let's see it in action,&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;brewCoffee&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;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;setTimeout&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;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;coffee is done! enjoy :)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;try&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;coffee&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;promise&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coffee&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="nx"&gt;error&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="nx"&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;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&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="nx"&gt;brewCoffee&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;//output: coffee is done! enjoy :)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;brewCoffee&lt;/code&gt; starts with a promise that will resolve after 2 seconds. That promise will be assigned to the variable &lt;code&gt;coffee&lt;/code&gt; which will be printed out. If the promise is rejected, the error will be printed out in the catch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does &lt;em&gt;await&lt;/em&gt; play a role?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;await&lt;/code&gt; keyword will pause the execution of the function so the promise can resolve.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How they both work together&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;await&lt;/code&gt; keyword is declared in the &lt;code&gt;async&lt;/code&gt; function. Often, you will see &lt;code&gt;async/await&lt;/code&gt; in API calls. This will make sure the data is correctly fetched before other lines of code are called. For example,&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchCappiccunoRecipe&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;response&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.sampleapis.com/coffee/hot&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;recipe&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&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;recipe&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="nx"&gt;makeCoffee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipe&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="nx"&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;The coffee was made with this &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;barista&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;recipe&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;fetchCappiccunoRecipe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;makeCoffee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;barista&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the &lt;code&gt;barista&lt;/code&gt; function is called, the recipe will be fetched which will be passed in as a parameter in the &lt;code&gt;makeCoffee&lt;/code&gt; function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways ✨
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;async/await&lt;/code&gt; will make your code easier to work with, especially since it makes it more readable. It is also a more concise way of writing asynchronous functions. Alternatively, it could have been written like this,&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="nx"&gt;fetchCappiccunoRecipe&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.sampleapis.com/coffee/hot&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;function&lt;/span&gt; &lt;span class="nx"&gt;makeCoffee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipe&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="nx"&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;The coffee was made with this &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;recipe&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="nx"&gt;barista&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;fetchCappiccunoRecipe&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&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="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;recipe&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;makeCoffee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipe&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="nx"&gt;barista&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 that there is more nesting and callback functions. Overall, a more confusing experience. &lt;/p&gt;

&lt;h2&gt;
  
  
  Next Steps ✨
&lt;/h2&gt;

&lt;p&gt;There may be opportunities in your code base to refactor functions to use &lt;code&gt;async/await&lt;/code&gt;. I recommend this article on migrating promises into &lt;code&gt;async/await&lt;/code&gt; functions: &lt;a href="https://afteracademy.com/blog/migrating-from-promise-chains-to-async-await/"&gt;https://afteracademy.com/blog/migrating-from-promise-chains-to-async-await/&lt;/a&gt; if you need additional guidance! Thanks for reading and happy coding :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>async</category>
      <category>await</category>
      <category>promises</category>
    </item>
    <item>
      <title>Working With Cache</title>
      <dc:creator>Valentina Peric</dc:creator>
      <pubDate>Tue, 18 May 2021 01:25:52 +0000</pubDate>
      <link>https://forem.com/valentinaperic/working-with-cache-317m</link>
      <guid>https://forem.com/valentinaperic/working-with-cache-317m</guid>
      <description>&lt;p&gt;Whether you are new to programming or you have been at it for a couple of years, you will run into cache. Maybe you have deployed updated code to a project that you have been working on and noticed that &lt;em&gt;the changes are not there.&lt;/em&gt; You refresh the page and still nothing! You check deployment and everything looks good. Why are the changes not showing up? The answer probably is that the cache has not cleared. &lt;br&gt;
 &lt;br&gt;
So, what is cache? How do you work with it? &lt;br&gt;
 &lt;br&gt;
Cache is something that the browser will access quickly so it does not have to make a request to the server to see the contents of the page you are trying to access. &lt;br&gt;
 &lt;br&gt;
Picture this: you want to bake cookies. Instead of going to the store to retrieve flour, sugar, chocolate chips, etc. you go right to your kitchen and grab the ingredients you need. Instead of making the request to the server (i.e. going to the store to retrieve items), you accessed cache (i.e kitchen) to get your items in a much faster time. &lt;br&gt;
 &lt;br&gt;
Cache enhances user experience so you do not have to sit and wait for a page to load. In other words, you do not have to go the store every single time you want to bake cookies. &lt;br&gt;
 &lt;br&gt;
Back to the beginning of the article: Always. Remember. To. Clear. Cache! 😄&lt;/p&gt;

</description>
      <category>cache</category>
      <category>webdev</category>
      <category>musing</category>
    </item>
    <item>
      <title>Writing If Statements with the Ternary Operator</title>
      <dc:creator>Valentina Peric</dc:creator>
      <pubDate>Mon, 24 Aug 2020 16:47:10 +0000</pubDate>
      <link>https://forem.com/valentinaperic/writing-if-statements-with-the-ternary-operator-37j</link>
      <guid>https://forem.com/valentinaperic/writing-if-statements-with-the-ternary-operator-37j</guid>
      <description>&lt;p&gt;When I first saw if statements using ternary operators, I was confused. &lt;em&gt;"Why is there a question mark?"&lt;/em&gt; and &lt;em&gt;"Why is there a colon?"&lt;/em&gt; were probably some of the thoughts I had. &lt;/p&gt;

&lt;p&gt;After some research and practice, I was finally getting the grasp on how to take my if statements to the next level using ternary operators.&lt;/p&gt;

&lt;h1&gt;
  
  
  Prerequisites 📝
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;A working knowledge of JavaScript (i.e. if statements and truthy/falsy expressions)&lt;/li&gt;
&lt;li&gt;A code editor (I recommend &lt;a href="https://visualstudio.microsoft.com/"&gt;Visual Studio Code&lt;/a&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Let's Get Started ✨
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;First, let's breakdown ternary operators using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator"&gt;MDN&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy. This operator is frequently used as a shortcut for the if statement.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It looks like this,&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;condition&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;truthy&lt;/span&gt; &lt;span class="nx"&gt;expression&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;falsy&lt;/span&gt; &lt;span class="nx"&gt;expression&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Second, let's take a look at an if statement that could be refactored using the ternary operator&lt;/strong&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;let&lt;/span&gt; &lt;span class="nx"&gt;userIdValid&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="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nx"&gt;userIdValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the user ID is valid&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;userIdValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the user ID is not valid&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;This if statement is a great opportunity to refactor using ternary operators. Let's break it down step by step.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the condition &lt;code&gt;(userId.value.length === 10)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Find the truthy value &lt;code&gt;userIdValid = "the user ID is valid";&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Find the falsy value &lt;code&gt;userIdValid = "the user ID is not valid";&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Put it all together using "?" and ":"
&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;userIdValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the user ID is valid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;userIdValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the user ID is not valid&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;&lt;em&gt;Notice that you only need the ";" at the very end of the statement.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;🎉As a bonus🎉, you can refactor this even further by assigning the statement to the &lt;code&gt;userIdValid&lt;/code&gt; variable like this,&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;let&lt;/span&gt; &lt;span class="nx"&gt;userIdValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the user ID is valid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the user ID is not valid&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;h1&gt;
  
  
  Key takeaways ✨
&lt;/h1&gt;

&lt;p&gt;Refactoring this took 8 lines of code and simplified it down to 1 line. This is great! Adding a code comment right above the statement can help explain what is going on. This is helpful for future you and other fellow developers!&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;//checking the validity of the userId by checking its length&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userIdValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the user ID is valid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the user ID is not valid&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;And there you have it! You just wrote an if statement using ternary operators. &lt;/p&gt;

&lt;h1&gt;
  
  
  Next Steps ✨
&lt;/h1&gt;

&lt;p&gt;Take a look at some of the if statements you have already written in past or current projects. Do any of them present an opportunity to be refactored using ternary if statements? If yes, I encourage you to give it a try!&lt;/p&gt;

&lt;p&gt;Thanks for reading! Was this article helpful for you? Any ideas that can be shared? Post a comment below!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;P.S.&lt;/strong&gt; This is my first technical blog post! I found a lot of encouragement from reading &lt;a href="https://www.developersguidetocontent.com/"&gt;The Developer's Guide to Content Creation&lt;/a&gt; by &lt;a href="https://dev.to/radiomorillo"&gt;Stephanie Morillo&lt;/a&gt;. I highly recommend it!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>refactorit</category>
    </item>
  </channel>
</rss>
