<?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: Amer Sikira</title>
    <description>The latest articles on DEV Community by Amer Sikira (@amersikira).</description>
    <link>https://dev.to/amersikira</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%2F78398%2F9d066d28-0e66-48f2-9275-d31ceb46d92c.jpg</url>
      <title>DEV Community: Amer Sikira</title>
      <link>https://dev.to/amersikira</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amersikira"/>
    <language>en</language>
    <item>
      <title>How we doubled conversions with one simple button</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Wed, 23 Apr 2025 16:01:38 +0000</pubDate>
      <link>https://dev.to/amersikira/how-we-doubled-conversions-with-one-simple-button-3aec</link>
      <guid>https://dev.to/amersikira/how-we-doubled-conversions-with-one-simple-button-3aec</guid>
      <description>&lt;p&gt;There are times in marketing and UX when flashy ideas and “growth hacks” take center stage. But sometimes, the most powerful wins come from doing something incredibly simple: removing friction.&lt;/p&gt;

&lt;p&gt;This is one of those times.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup: A tool store with a conversion problem
&lt;/h2&gt;

&lt;p&gt;Our client runs a WooCommerce store that sells electric tools—think drills, grinders, welders, and other professional-grade equipment. But there was a hurdle: their target audience wasn’t always comfortable with online shopping.&lt;/p&gt;

&lt;p&gt;Conversion rate was a bit below average, not a lot.&lt;/p&gt;

&lt;p&gt;A big portion of their customers weren’t tech-savvy. Some didn’t trust entering their credit card details online. Others would get stuck in the checkout process or simply abandon the cart out of confusion.&lt;/p&gt;

&lt;p&gt;The usual "add to cart → checkout → enter all your details → pay online" flow wasn’t working for them. Even though the products were solid and pricing was competitive, something about the experience kept turning users away right before the finish line.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Idea: Strip it down to the basics
&lt;/h2&gt;

&lt;p&gt;Instead of optimizing for the digital-native shopper, we decided to meet the actual customer where they were.&lt;/p&gt;

&lt;p&gt;We added a &lt;strong&gt;“One Click Buy”&lt;/strong&gt; button next to the regular “Add to Cart.” Here’s how it worked:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users only had to enter their phone number—the name field was optional.&lt;/li&gt;
&lt;li&gt;The selected product and user info were sent through WooCommerce like any other order.&lt;/li&gt;
&lt;li&gt;The client’s sales team would call the customer directly, confirm the order, and handle the rest manually.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s it. No login. No address input. No card info. Just a phone number and a real person on the other end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it worked
&lt;/h2&gt;

&lt;p&gt;This change wasn’t just technical, it was psychological.&lt;/p&gt;

&lt;p&gt;We weren’t trying to force a one-size-fits-all funnel. We were solving a real-world problem for a specific audience. That’s why it worked.&lt;/p&gt;

&lt;p&gt;Here’s what happened next:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first few days were quiet. People weren’t sure what to make of this new option.&lt;/li&gt;
&lt;li&gt;But then orders started rolling in. Couple of orders here and there.&lt;/li&gt;
&lt;li&gt;And then they kept coming.&lt;/li&gt;
&lt;li&gt;Within a week, conversions from the new button became the prevalent source of orders compared to the standard checkout flow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep in mind, we weren’t even running an A/B test. We just monitored the orders coming from the new button vs. the regular one. When it became clear that the new flow was outperforming the old, we didn’t hesitate to keep it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The tech behind it
&lt;/h2&gt;

&lt;p&gt;Technically, this wasn’t a major engineering project. We used an existing WordPress plugin that allowed us to set up quick-buy forms. A few small tweaks later, and it worked exactly as needed.&lt;/p&gt;

&lt;p&gt;WooCommerce handled the back end. The form submitted product info + phone number, and the client’s team handled the confirmation call—something they were already doing anyway.&lt;/p&gt;

&lt;p&gt;So rather than reinventing the wheel, we made it easier to get rolling.&lt;/p&gt;

&lt;p&gt;The human element&lt;br&gt;
One key reason this worked: our client already had a process for calling every customer. That might sound like a burden in eCommerce, but for this business—and this audience—it was the right move.&lt;/p&gt;

&lt;p&gt;A call gave the customer peace of mind. It gave the sales team a chance to confirm or even upsell. It built trust, which is often the missing ingredient in online checkouts.&lt;/p&gt;

&lt;p&gt;We didn’t have to change the business model, we just had to reduce the friction between intent and action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons you can apply
&lt;/h2&gt;

&lt;p&gt;This isn’t just a win for one client. It’s a reminder of something we all forget too often in digital:&lt;/p&gt;

&lt;p&gt;Your user doesn’t care about your funnel. They care about how easy it is to get what they want.&lt;/p&gt;

&lt;p&gt;If your audience isn't 100% comfortable with technology, don’t build for the ones who are. Build for the ones who are buying.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thought
&lt;/h2&gt;

&lt;p&gt;Doubling conversions didn’t require a redesign, a rebrand, or a replatforming. It took one button, one phone number, and a little empathy.&lt;/p&gt;

&lt;p&gt;It doesn’t have to be beautiful. It has to work.&lt;/p&gt;

&lt;p&gt;If you want to connect with me you can find me on &lt;a href="https://www.linkedin.com/in/amer-sikira/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>marketing</category>
      <category>webshop</category>
      <category>wordpress</category>
      <category>woocommerce</category>
    </item>
    <item>
      <title>Rest and Spread Operators Demystified: A Beginner’s Guide to Enhancing Your Programming Skills</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Thu, 20 Apr 2023 01:05:51 +0000</pubDate>
      <link>https://dev.to/amersikira/rest-and-spread-operators-demystified-a-beginners-guide-to-enhancing-your-programming-skills-402o</link>
      <guid>https://dev.to/amersikira/rest-and-spread-operators-demystified-a-beginners-guide-to-enhancing-your-programming-skills-402o</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://webinuse.com/rest-and-spread-operators-demystified-beginners-guide/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;br&gt;
In 2015 ES6 (ECMA Script 6) was introduced. It brought a lot of new and improved features. Two features, among others, that we got with ES6 are rest and spread operators.&lt;/p&gt;

&lt;p&gt;If you’ve been learning JavaScript or have tried some general programming, you’ve probably come across these handy little tools. In this article, we’ll explore what rest and spread operators are, the differences between them, and how you can use them to level up your coding skills. So, let’s jump right in!&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;What are Rest and Spread Operators?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before we get into the details, let’s clarify what we’re talking about when we mention rest and spread operators. Both operators came with the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" rel="noopener noreferrer"&gt;ES6 (ECMAScript 2015) JavaScript&lt;/a&gt; standard.&lt;/p&gt;

&lt;p&gt;As you can see from the name, ECMAScript 2015 (ES6 version) was introduced in 2015 and it was a major upgrade from the previous version of JavaScript.&lt;/p&gt;

&lt;p&gt;Rest and spread operators are designed to make your life easier when working with arrays and objects. Now, let’s finally see how and why you should learn them.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Rest Operator&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters" rel="noopener noreferrer"&gt;rest operator&lt;/a&gt; is denoted by three consecutive dots (…). Its main purpose is to collect the remaining elements of an array or object into a new array or object.&lt;/p&gt;

&lt;p&gt;It’s especially useful when you’re dealing with functions that can accept a varying number of arguments.&lt;/p&gt;

&lt;p&gt;For example, let’s say you have a function that calculates the sum of all its arguments:&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;sum&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;numbers&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;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;total&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num&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;sum&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;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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the rest operator collects all the arguments passed to the &lt;code&gt;sum&lt;/code&gt; function into an array called &lt;code&gt;numbers&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This makes it incredibly easy to handle functions with a variable number of arguments.&lt;/p&gt;

&lt;p&gt;Rest operator helps us to, literally, have as many arguments to our function as we want. Read more about &lt;a href="https://webinuse.com/unlimited-arguments-in-a-javascript-function-2-ways/" rel="noopener noreferrer"&gt;an unlimited number of arguments in a JavaScript function&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;Like the rest operator, the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax" rel="noopener noreferrer"&gt;spread operator&lt;/a&gt; is also represented by three consecutive dots (…). However, its purpose is different. The spread operator is used to expand the elements of an array or object, making it simpler to manipulate and merge data structures.&lt;/p&gt;

&lt;p&gt;For instance, if you want to merge two arrays, you can use the spread operator 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;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&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;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array2&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;5&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mergedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;array2&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;mergedArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: [1, 2, 3, 4, 5, 6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the spread operator helps you combine arrays with ease, making your code cleaner and more concise.&lt;/p&gt;

&lt;p&gt;This syntax is pretty popular with React developers. It is the most commonly used while manipulating React state.&lt;/p&gt;

&lt;p&gt;It is also well utilized by the type of developers who propagate that &lt;a href="https://css-tricks.com/understanding-immutability-in-javascript/" rel="noopener noreferrer"&gt;data should be immutable&lt;/a&gt;. Meaning that once we set a variable it should hold its data and data’s structure forever, without ever changing it.&lt;/p&gt;

&lt;p&gt;This approach to development usually means more typing. But with a spread operator, it’s much easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Difference Between Rest and Spread Operators&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;At this point, you might be thinking, “Wait, both operators look the same. How do I know when to use which one?” That’s a great question! The key difference between rest and spread operators lies in the way how you use them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The rest operator collects elements into a new array or object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The spread operator expands elements from an existing array or object.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In other words, you can think of the rest operator as a “gatherer” and the spread operator as an “expander”.&lt;/p&gt;

&lt;p&gt;Imagine you’re working on a jigsaw puzzle. The rest operator is like the process of gathering all the scattered puzzle pieces from the table and placing them into a neat pile.&lt;/p&gt;

&lt;p&gt;On the other hand, the spread operator is like taking the pieces from the pile and spreading them out, making it easier to see each piece and find where it fits in the bigger picture.&lt;/p&gt;

&lt;p&gt;Both operators have their unique purposes, and understanding their roles will help even complete beginners grasp their usage in the world of programming.&lt;/p&gt;

&lt;p&gt;Now that we’ve covered the basics let’s dive deeper into each operator and see how they can help you level up your coding game.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Rest Operator in Detail&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;As we mentioned earlier, the rest operator is incredibly useful when dealing with functions that accept a varying number of arguments. Let’s take a closer look at how it works and how you can use it in your code.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Collecting Remaining Elements in an Array&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;One of the most common use cases for the rest operator is to collect the remaining elements in an array. Let’s say you have an array of numbers, and you want to separate the first element from the rest of the elements:&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;numbers&lt;/span&gt; &lt;span class="o"&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;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;4&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&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;first&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 1&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;rest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: [2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the rest operator collects all the remaining elements in the &lt;code&gt;numbers&lt;/code&gt; array (excluding the first element) into a new array called &lt;code&gt;rest&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This technique is particularly useful when working with &lt;a href="https://webinuse.com/array-destructuring-everything-we-need-to-know/" rel="noopener noreferrer"&gt;array destructuring&lt;/a&gt;. It helps us extract values in an easy and convenient way. Before the rest operator, you would probably use &lt;a href="https://webinuse.com/array-loops-in-javascript/" rel="noopener noreferrer"&gt;array loops&lt;/a&gt;, or some trick with a &lt;a href="https://webinuse.com/javascript-array-length-the-definitive-guide/" rel="noopener noreferrer"&gt;&lt;code&gt;.length&lt;/code&gt; property&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Simplifying Function Arguments&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Another powerful use case for the rest operator is simplifying function arguments. As we saw earlier, the rest operator can easily handle a variable number of function arguments, making it an invaluable tool for writing flexible, reusable code.&lt;/p&gt;

&lt;p&gt;Consider the following example, where we create a function that accepts multiple strings and returns a new string with all the input strings concatenated together:&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;concatenateStrings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;separator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;strings&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;strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;separator&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;concatenateStrings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &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;Hello&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;world!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: "Hello world!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the rest operator collects all the arguments passed to the &lt;code&gt;concatenateStrings&lt;/code&gt; function (excluding the first argument, &lt;code&gt;separator&lt;/code&gt;) into an array called &lt;code&gt;strings&lt;/code&gt;. This allows the function to accept any number of input strings, making it incredibly flexible and reusable.&lt;/p&gt;

&lt;p&gt;The example above is a great example of how we can prevent some unwanted errors.&lt;/p&gt;

&lt;p&gt;Instead of using the rest syntax, imagine we used &lt;code&gt;concatenateStrings(separator, stringOne, stringTwo)&lt;/code&gt;. This would limit us to only two strings. And if you omit any of those, it would throw an error. If you pass more than expected, nothing would happen.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Handling Variable Number of Function Arguments&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Similar to the previous example, we can pass only the rest operator as a function parameter.&lt;/p&gt;

&lt;p&gt;Imagine you need to create a function that calculates the average of any number of input numbers. The rest operator makes this a breeze:&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;average&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;numbers&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;total&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;numbers&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="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;average&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;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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the rest operator collects all the arguments passed to the &lt;code&gt;average&lt;/code&gt; function into an array called &lt;code&gt;numbers&lt;/code&gt;. This allows the function to calculate the average of any number of input numbers with ease.&lt;/p&gt;

&lt;p&gt;This function also is not dependent on any number of arguments. It can accept as many as you need and still return the correct result.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Common Pitfalls and Best Practices&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As you begin incorporating the rest operator into your code, keep these potential pitfalls and best practices in mind:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Remember that the rest operator can only be used once in a destructuring assignment or function parameter list, and it must appear at the end.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When using the rest operator in a function, avoid using the &lt;code&gt;arguments&lt;/code&gt; object, as it can lead to less readable and less efficient code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always test your code thoroughly, particularly when using the rest operator with other ES6 features such as destructuring and default parameters.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Spread Operator in Detail&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we’ve covered the rest operator, let’s take a closer look at the spread operator and explore its various use cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Expanding Elements in an Array or Object&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As we mentioned earlier, the spread operator is used to expand the elements of an array or object, making it easier to manipulate and merge data structures. For example, you can use the spread operator to clone an array or object, preserving the original data while creating a new, separate copy:&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;originalArray&lt;/span&gt; &lt;span class="o"&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;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clonedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;originalArray&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;clonedArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: [1, 2, 3]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the spread operator expands the elements of the &lt;code&gt;originalArray&lt;/code&gt; and creates a new array called &lt;code&gt;clonedArray&lt;/code&gt; with the same elements. This technique is particularly useful when you need to work with a copy of an array or object without modifying the original data.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Simplifying the Manipulation of Arrays and Objects&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The spread operator can significantly simplify the manipulation of arrays and objects in your code. For example, you can use the spread operator to add elements to an 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;originalArray&lt;/span&gt; &lt;span class="o"&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;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&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="nx"&gt;originalArray&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="mi"&gt;5&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;newArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: [0, 1, 2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the spread operator expands the elements of the &lt;code&gt;originalArray&lt;/code&gt; and creates a new array called &lt;code&gt;newArray&lt;/code&gt; with additional elements at the beginning and end.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Real-Life Examples and Code Snippets&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s look at a few more examples of how the spread operator can be used in real-life programming scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Combining Arrays Using Spread Operator&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Suppose you need to combine multiple arrays into a single array. With the spread operator, you can achieve this quickly and easily:&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;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apple&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="s1"&gt;banana&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="s1"&gt;orange&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;vegetables&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;carrot&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="s1"&gt;broccoli&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="s1"&gt;pepper&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;combinedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;vegetables&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;combinedArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: ['apple', 'banana', 'orange', 'carrot', 'broccoli', 'pepper']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Cloning Objects Using Spread Operator&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine you need to create a copy of an object with some additional properties. The spread operator makes this a breeze:&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;originalObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&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;updatedObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;originalObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New York&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;occupation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Software Developer&lt;/span&gt;&lt;span class="dl"&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;updatedObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: {name: 'John', age: 30, city: 'New York', occupation: 'Software Developer'}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the spread operator expands the properties of the &lt;code&gt;originalObject&lt;/code&gt; and creates a new object called &lt;code&gt;updatedObject&lt;/code&gt; with additional properties.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Using Spread Operator with Strings&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The spread operator can also be used with strings, expanding them into arrays of individual characters:&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;string&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;characters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;string&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;characters&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: ['h', 'e', 'l', 'l', 'o']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the spread operator expands the characters of the &lt;code&gt;string&lt;/code&gt; and creates a new array called &lt;code&gt;characters&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Common Pitfalls and Best Practices&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;As you begin incorporating the spread operator into your code, keep these potential pitfalls and best practices in mind:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Be aware that the spread operator creates a shallow copy of an array or object. This means that if your original data structure contains nested arrays or objects, the spread operator will only create a new reference to the nested data rather than a deep copy. In such cases, you may need to use other methods, such as &lt;code&gt;JSON.parse(JSON.stringify())&lt;/code&gt;, to create a deep copy of your data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When using the spread operator with objects, make sure to consider the order of properties. If you have duplicate properties in the original and new objects, the latter property will overwrite the former.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remember that the spread operator can be used in various places, such as function calls, array literals, and object literals. Make sure to familiarize yourself with these different use cases to maximize their potential in your code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Congratulations! You’ve made it through our comprehensive guide to rest and spread operators. With your newfound understanding of these powerful tools, you’re well-equipped to write cleaner, more efficient, and more versatile code.&lt;/p&gt;

&lt;p&gt;As a beginner in web development, it’s essential to get comfortable with rest and spread operators, as they are widely used in modern JavaScript and can significantly enhance your programming skills.&lt;/p&gt;

&lt;p&gt;So, don’t hesitate to practice using them in your projects and explore their various applications.&lt;/p&gt;

&lt;p&gt;Remember, the rest operator is your go-to “gatherer” when you need to collect elements into a new array or object, while the spread operator is your trusty “expander” when you need to simplify the manipulation of arrays and objects.&lt;/p&gt;

&lt;p&gt;Keep honing your coding skills, and before you know it, you’ll be a master of rest and spread operators in no time. Good luck, and happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>I tried Google Cloud products and here is what every beginner should know</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Wed, 30 Nov 2022 19:30:53 +0000</pubDate>
      <link>https://dev.to/amersikira/i-tried-google-cloud-products-and-here-is-what-every-beginner-should-know-29op</link>
      <guid>https://dev.to/amersikira/i-tried-google-cloud-products-and-here-is-what-every-beginner-should-know-29op</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published on &lt;a href="https://webinuse.com/i-tried-google-cloud-products-and-here-is-what-every-beginner-should-know/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I’ve been a web developer for quite some time. More than a decade (I am getting old, I know 😅). So far, my projects were usually predefined in terms of the stack. I worked a lot for the government and you know those guys. They, usually, use mature stuff. The stuff that has been long on the market.&lt;/p&gt;

&lt;p&gt;For example, I’ve built CMS for Federal Journalist Association, but they explicitly wanted it with PHP, MySQL, HTML, CSS, and Vanilla JS. I’ve tried to tell them that there are more exciting things, but they were stubborn.&lt;/p&gt;

&lt;p&gt;Up until this point, my experience with Google Cloud (Firebase) was minimal. I used Firestore for some small stuff, and I used auth for a few projects, but nothing serious.&lt;/p&gt;

&lt;p&gt;Lately, my company got a project for banking software. They’ll use it for better track of pre-loan documents (this is obviously not the full story, but there is this little thing called NDA 😁). We’ve talked about the stack and we decided that it will be React, NodeJS, along with GC products.&lt;/p&gt;

&lt;p&gt;Since I had experience with MERN, I was not worried. I decided to dive into GCP. A little disclaimer: I have zero experience with other cloud providers like AWS and Azure. So some of my views may be biased a bit. Also, since I developed this using NodeJS and ReactJS I will stick to these two technologies, and I’ll I’m going to say is from this POV.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting out
&lt;/h2&gt;

&lt;p&gt;Starting with GCP is super easy. All you have to do is head over to &lt;a href="https://firebase.google.com/" rel="noopener noreferrer"&gt;Firebase Console&lt;/a&gt; and create a project. After you do that you have a few options to choose from.&lt;/p&gt;

&lt;p&gt;This is where I encountered my first problem. I had no idea that you can use Firebase SDK and Firebase Admin SDK. The first one is for front-end tech, like ReactJS. The latter one is for back-end tech, like NodeJS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Front-end
&lt;/h2&gt;

&lt;p&gt;If you decide to use Firebase just on front-end which is awesome for building serverless apps all you have to do is go to your project’s root and run&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install firebase&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After that, you open your &lt;code&gt;.env&lt;/code&gt; file and save all the required keys. &lt;strong&gt;Don’t forget&lt;/strong&gt; to add &lt;code&gt;REACT_APP_&lt;/code&gt; before your variable names.&lt;/p&gt;

&lt;p&gt;Here is an example of the keys:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    REACT_APP_API_KEY="AIzaSEsfdoiweRfsadljffaiower234ER",
    REACT_APP_AUTH_DOMAIN="your-app.firebaseapp.com",
    REACT_APP_PROJECT_ID="your-project-id",
    REACT_APP_STORAGE_BUCKET="your-app.appspot.com",
    REACT_APP_MESSAGING_SENDER_ID="1234567",
    REACT_APP_APP_ID="1:23432:web:234423ab234243cd21323"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll get all those keys as soon as you create your project. But don’t worry you can find them later under Project Settings.&lt;/p&gt;

&lt;p&gt;I would also recommend installing &lt;a href="https://www.npmjs.com/package/react-firebase-hooks" rel="noopener noreferrer"&gt;React Firebase Hooks from NPM&lt;/a&gt;. A library that makes it 100x easier to use Firebase with React.&lt;/p&gt;

&lt;h2&gt;
  
  
  Back-end
&lt;/h2&gt;

&lt;p&gt;Using Firebase on the back end is almost the same. The only difference is that you install Firebase with:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install firebase-admin&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;It does have some slightly different API and you should really take care of that, otherwise, you would end up like me solving bugs for hours only to realize that you used the wrong API.&lt;/p&gt;

&lt;p&gt;One piece of advice is when searching Google for solutions always use %QUERY% firebase admin. This way, you will mostly end up with the right solutions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other things
&lt;/h2&gt;

&lt;p&gt;Obviously, the easiest way to start with either of those is to open Firebase YouTube Channel and take a look at some intro videos. After that, you should just &lt;a href="https://firebase.google.com/docs/build" rel="noopener noreferrer"&gt;read the docs&lt;/a&gt;. Most of the problems that you’ll face can be solved by simply reading documentation. At least in the beginning. For everything else there is StackOverflow 🤣.&lt;/p&gt;

&lt;p&gt;Now, let’s take a look at some of the benefits of GCP and why should you use it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication
&lt;/h3&gt;

&lt;p&gt;One of the main reasons I wanted to use GCP is authentication. It’s really simple and easy to use. You set up the type of auth you want to use, in the console, and then with just a few simple lines of code, you have authentication.&lt;/p&gt;

&lt;p&gt;As I’ve mentioned earlier you get a lot of help from React Firebase Hooks. There are also some great tutorials online with ready-to-use code snippets.&lt;/p&gt;

&lt;p&gt;You can log in, log out, register, authenticate, and delete users from the front end. You can also set up additional authentication, if needed, via the back end. It’s really easy because Google uses JWT and it has all APIs for your to use, out of the box.&lt;/p&gt;

&lt;h3&gt;
  
  
  Database
&lt;/h3&gt;

&lt;p&gt;There are two types of databases that you can use (there are more but these two are more than enough for most projects).&lt;/p&gt;

&lt;p&gt;Realtime database and Firestore database. To learn more about the Realtime database you can visit this &lt;a href="https://firebase.google.com/docs/database" rel="noopener noreferrer"&gt;link&lt;/a&gt;. If you also want to learn more about Firestore, you can do so &lt;a href="https://firebase.google.com/docs/firestore" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Both of them are very easy to use. If you ever used MongoDB with Mongoose, that’s almost it. Even easier in my humble opinion.&lt;/p&gt;

&lt;p&gt;You can use DB either with ReactJS, to create serverless applications, or with NodeJS for more robust and customized solutions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Storage
&lt;/h3&gt;

&lt;p&gt;Another great thing about GCP is storage. Upload is simply 🤯. All you have to do is provide a path to your file and that’s it. Everything else is done by Google. How cool is that?&lt;/p&gt;

&lt;p&gt;You can upload, delete, modify, basically do anything with those files just providing your Storage path.&lt;/p&gt;

&lt;p&gt;I really like that part.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hosting
&lt;/h3&gt;

&lt;p&gt;Now, there are two different types of hosting with Google. I am saying this primarily for those who are coming from old-school languages like PHP.&lt;/p&gt;

&lt;p&gt;Before we used to have some hosting, usually with cPanel or Plesk and we would put all our code there.&lt;/p&gt;

&lt;p&gt;Now you use hosting to host your front-end stuff. It’s quite easy to use.&lt;/p&gt;

&lt;p&gt;All you have to do is install Firebase CLI. After that, you log in to your desired account and initialize hosting with firebase init hosting then just a few little steps and that’s it.&lt;/p&gt;

&lt;p&gt;If you want to run your code locally, instead of &lt;code&gt;npm start&lt;/code&gt; (for React) you can use &lt;code&gt;firebase serve --only hosting&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;IMHO, this is better because you immediately can fix some bugs, before pushing to your server. Once you are ready to push just type in your console firebase deploy –only hosting and in a couple of seconds you should see your code up and running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functions
&lt;/h3&gt;

&lt;p&gt;Functions are also hosting, but for your back-end. It works in the same way as your front end hosting except for one small detail.&lt;/p&gt;

&lt;p&gt;The first thing is to install Firebase CLI, if you haven’t already, then you just type &lt;code&gt;npm install -g firebase-tools&lt;/code&gt; in the folder where your project is.&lt;/p&gt;

&lt;p&gt;In order for your code to work on Cloud Functions you have to wrap your app in a functions wrapper. I had a bit of a problem with this, but &lt;a href="https://www.youtube.com/watch?v=DYfP-UIKxH0" rel="noopener noreferrer"&gt;this video&lt;/a&gt; really saved my day.&lt;/p&gt;

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

&lt;p&gt;There are many other things you can do with Firebase and GCP, but IMHO this is all one beginner needs to know to build a fully functional app.&lt;/p&gt;

&lt;p&gt;After this, you can dive into different databases, computing engines, etc.&lt;/p&gt;

&lt;p&gt;I am really hooked up to GCP and I think I will hardly ever decide to change it. I know that a lot of people are saying that AWS is better, and cheaper, but I don’t care. I like GCP and it fully suits my coding style.&lt;/p&gt;

&lt;p&gt;If you have any questions you can write them in the comments or you can reach me on &lt;a href="https://twitter.com/SikiraAmer" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Top 3 ways to easily detect when a user leaves a page using JavaScript</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Wed, 23 Mar 2022 19:12:45 +0000</pubDate>
      <link>https://dev.to/amersikira/top-3-ways-to-easily-detect-when-a-user-leaves-a-page-using-javascript-2ce4</link>
      <guid>https://dev.to/amersikira/top-3-ways-to-easily-detect-when-a-user-leaves-a-page-using-javascript-2ce4</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://webinuse.com/detect-when-a-user-leaves-the-page-using-javascript/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are times when we need to detect when a user leaves a web page or clicks on a link. Sometimes we use this data to change something on the website, sometimes to send analytics and sometimes we just need to know this because of something else.&lt;/p&gt;

&lt;p&gt;In this article, we are going to cover the top 3 easiest ways how we can detect when a user leaves the page. Unfortunately, each and every one of these ways has some flaws, but also it has its application to some situations.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Detect exit before page start unloading
&lt;/h3&gt;

&lt;p&gt;As per &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;: The &lt;code&gt;beforeunload&lt;/code&gt; event is fired when the window, the document, and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.&lt;/p&gt;

&lt;p&gt;This means that when we click on a link, or we want to close the tab, or browser, or anything that would remove our current link from the browser, one moment before the browser starts doing that action will fire &lt;code&gt;beforeunload&lt;/code&gt; event.&lt;/p&gt;

&lt;p&gt;Let’s say we clicked some link. One moment before the browser starts opening this new link, it will fire the event.&lt;/p&gt;

&lt;p&gt;This event can be used to send some data to the back end, or to change something in our localStorage, or whatever we need to do.&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;beforeUnloadListener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;//Send something to back end&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="s1"&gt;uri&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;beforeunload&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;beforeUnloadListener&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to keep in mind though that this event is not reliably fired. There are some issues with it. It does not always detect when a user leaves a web page, especially on mobile devices. For more info on issues and other data, we can visit &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Detect when a user leaves a web page using pagehide event
&lt;/h3&gt;

&lt;p&gt;Similar to &lt;code&gt;beforeunload&lt;/code&gt; event, &lt;code&gt;pagehide&lt;/code&gt; is fired when a user is leaving the current page, e.g. clicking the back button. But, according to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;, same as &lt;code&gt;beforeunload&lt;/code&gt; the &lt;code&gt;pagehide&lt;/code&gt; event is not always reliably fired.&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;pageHideListener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;//Send something to back end&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="s1"&gt;uri&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pagehide&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pageHideListener&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. visiblitychange event
&lt;/h3&gt;

&lt;p&gt;The most reliable way to detect when a user leaves a web page is to use &lt;code&gt;visiblitychange&lt;/code&gt; event. This event will fire to even the slightest changes like changing tabs, minimizing the browser, switching from browser to another app on mobile, etc. More info about this event can be found on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilitychange_event" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is an excellent way to send some analytics to our back end and is shown in the example below.&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onvisibilitychange&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;visibilityState&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hidden&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="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendBeacon&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/log&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;analyticsData&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;&lt;strong&gt;NOTE:&lt;/strong&gt; This event is excellent for sending analytics, and similar, data to the back end, but if we want to send information if a user still has opened a certain page we should use either the first two events or we should ping user every few seconds.&lt;/p&gt;

&lt;p&gt;If you have any questions or anything you can find me on my &lt;a href="https://twitter.com/SikiraAmer" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, or you can read some of my other articles like &lt;a href="https://webinuse.com/everything-we-need-to-know-about-css-border/" rel="noopener noreferrer"&gt;Everything we need to know about CSS Borders&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Everything we need to know about CSS Borders</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Thu, 16 Dec 2021 10:12:10 +0000</pubDate>
      <link>https://dev.to/amersikira/everything-we-need-to-know-about-css-borders-186b</link>
      <guid>https://dev.to/amersikira/everything-we-need-to-know-about-css-borders-186b</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://webinuse.com/everything-we-need-to-know-about-css-border/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The CSS border properties allow us to style element's border. We can set the width, style, radius, and color of element's border. We can style borders in three different ways.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Specifying CSS border properties individually&lt;/li&gt;
&lt;li&gt;Specifying every border individually&lt;/li&gt;
&lt;li&gt;Using shorthand property&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Specifying CSS border properties individually
&lt;/h3&gt;

&lt;p&gt;There are four properties that we can use to style borders.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;border-style&lt;/code&gt; property that specifies the border type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;border-color&lt;/code&gt; is used to specify the color of the border(s)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;border-width&lt;/code&gt; specifies how wide/thick our border is&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;border-radius&lt;/code&gt; property is used to specify the amount of radius of the border&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Border style
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;border-style&lt;/code&gt; property is used to specify the type of the border we are going to use. There are several predefined values that we can use.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;none&lt;/code&gt; - without any type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;solid&lt;/code&gt; - solid border. Probably the most popular choice&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dashed&lt;/code&gt; - dashed border&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dotted&lt;/code&gt; - dotted border&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;double&lt;/code&gt; - double border&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;groove&lt;/code&gt; - 3D grooved border, works with &lt;code&gt;border-color&lt;/code&gt; value&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ridge&lt;/code&gt; - 3D ridged border, works with &lt;code&gt;border-color&lt;/code&gt; value&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;inset&lt;/code&gt; - 3D inset border, works with &lt;code&gt;border-color&lt;/code&gt; value&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;outset&lt;/code&gt; - 3D outset border, works with &lt;code&gt;border-color&lt;/code&gt; value&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;hidden&lt;/code&gt; - hides border&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amersikira/embed/mdBWPvW?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Border color
&lt;/h4&gt;

&lt;p&gt;CSS border &lt;code&gt;border-color&lt;/code&gt; property is used to specify the color of the border. It accepts any type of color value like HEX, HSL, RGB, RGBA, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; &lt;code&gt;border-color&lt;/code&gt; property can be used, only, with other CSS border properties, not alone. If used alone, it does not work because it has no border to change color.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amersikira/embed/PoJpzbR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Border width
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;border-width&lt;/code&gt; property can be used with custom size expressed in pixels, em, rem, vw, vh, almost anything except &lt;code&gt;%&lt;/code&gt;, or we can use three of the predefined values: &lt;code&gt;thin&lt;/code&gt;, &lt;code&gt;medium&lt;/code&gt;, &lt;code&gt;thick&lt;/code&gt;. If no width is specified, the default width of the border is &lt;code&gt;medium&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; &lt;code&gt;border-width&lt;/code&gt; property must be used with &lt;code&gt;border-type&lt;/code&gt; property, otherwise it does not work.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amersikira/embed/oNGZLEw?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Border radius
&lt;/h4&gt;

&lt;p&gt;We all love those rounded corners, right? In order to make one we have to use &lt;code&gt;border-radius&lt;/code&gt; property. This CSS border property allows us to specify the amount of "roundness" we want. We can use any &lt;a href="https://webinuse.com/css-units-guide-everything-you-need-to-know/" rel="noopener noreferrer"&gt;CSS unit&lt;/a&gt; to specify the value for &lt;code&gt;border-radius&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amersikira/embed/zYEZZWp?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Specifying every border individually
&lt;/h3&gt;

&lt;p&gt;We can specify every border individually, by refering directly to that exact border. There are four options: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;border-top&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;border-right&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;border-bottom&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;border-left&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When using any of those options, we have to group values in single value, using space as delimiter. Order of the values is: &lt;code&gt;border-width&lt;/code&gt;, &lt;code&gt;border-type&lt;/code&gt;, &lt;code&gt;border-color&lt;/code&gt;. We can specify every value except the one for radius. That is because, &lt;code&gt;border-radius&lt;/code&gt; has its own way of specifying such things. Let's see an example.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amersikira/embed/yLzMMQX?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Specifying border radius for every border individually
&lt;/h4&gt;

&lt;p&gt;To specify border radius for any border individually we have to use some/all of the following CSS border properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;border-top-left-radius&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;border-top-right-radius&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;border-bottom-right-radius&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;border-bottom-left-radius&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, we can specify border radius for any of those corners, using any of CSS Units.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amersikira/embed/rNGyyEd?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Using shorthand property
&lt;/h3&gt;

&lt;p&gt;We can leverage CSS' ability to declare multiple values, using single property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;    &lt;span class="nt"&gt;border-width&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;1&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;2&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;3&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;4&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;border-color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;red&lt;/span&gt; &lt;span class="nt"&gt;blue&lt;/span&gt; &lt;span class="nt"&gt;green&lt;/span&gt; &lt;span class="nt"&gt;yellow&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;border-style&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;solid&lt;/span&gt; &lt;span class="nt"&gt;dashed&lt;/span&gt; &lt;span class="nt"&gt;dotted&lt;/span&gt; &lt;span class="nt"&gt;solid&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives us power to apply different styles to all/some borders in single line. The only rule that we have to follow is that values are ordered in following way: top, right, bottom, left. Meaning starting from top clock-wise.&lt;/p&gt;

&lt;p&gt;If all four borders are the same, there is even shorter syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;    &lt;span class="nt"&gt;border&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;1&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="nt"&gt;solid&lt;/span&gt; &lt;span class="nt"&gt;red&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same as we have mentioned above, there is strict order of the values. 1. is always &lt;code&gt;border-width&lt;/code&gt;, 2. is always &lt;code&gt;border-style&lt;/code&gt;, and 3. is always &lt;code&gt;border-color&lt;/code&gt;. Order of the items must be respected.&lt;/p&gt;

&lt;p&gt;If you have any questions or anything you can find me on my &lt;a href="https://twitter.com/AmerSikira" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, or you can read some of my other articles like &lt;a href="https://webinuse.com/javascript-string-length/" rel="noopener noreferrer"&gt;JavaScript String replace method&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>css</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript Object Keys</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Fri, 26 Nov 2021 08:53:53 +0000</pubDate>
      <link>https://dev.to/amersikira/javascript-object-keys-h1</link>
      <guid>https://dev.to/amersikira/javascript-object-keys-h1</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://webinuse.com/javascript-object-keys/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We have already mentioned JavaScript Object Keys when we spoke about &lt;a href="https://webinuse.com/object-manipulation-in-javascript/" rel="noopener noreferrer"&gt;Object manipulation in JavaScript&lt;/a&gt;. In this article, we are going to cover &lt;code&gt;Object.keys()&lt;/code&gt; in-depth.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the Object?
&lt;/h3&gt;

&lt;p&gt;According to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;, the Object class represents one of JavaScript’s data types. It is used to store various keyed collections and more complex entities. Objects can be created using the Object() constructor or the object initializer / literal syntax.&lt;/p&gt;

&lt;p&gt;The Object is one of the most complex &lt;a href="https://webinuse.com/guide-to-javascript-data-types/" rel="noopener noreferrer"&gt;JavaScript data types&lt;/a&gt;. It can hold any other data type. It can hold functions, also. Here is an example of some objects.&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;//The first way of creating Object in JavaScript&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//The second way of creating Object in &lt;/span&gt;
&lt;span class="c1"&gt;//JavaScript using Object constructor.&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Obj&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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="nx"&gt;username&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;id&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;name&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;username&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;user&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;Obj&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&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="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//The third way of creating object in JavaScript &lt;/span&gt;
&lt;span class="c1"&gt;//is by using ES6 object literalls&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;id&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;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&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;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;id&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="nx"&gt;username&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What are JavaScript Object Keys?
&lt;/h3&gt;

&lt;p&gt;Every JavaScript Object consists of keys and values. The key is the “name” by which we can call a value. Let’s make an 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Doe&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;In our example, keys are id, name, username, and values are 1, “John” and “Doe”. But there is a difference between the Object key, which is “the name” and &lt;code&gt;Object.keys()&lt;/code&gt; method.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is JavaScript Object.keys() method?
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Object.keys()&lt;/code&gt; method returns an array of a given object’s own enumerable property names, iterated in the same order that a normal loop would, as per &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This means that when we use &lt;code&gt;Object.keys()&lt;/code&gt; it will return an array of strings. Those strings will be the keys of the 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;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//(3) ['id', 'name', 'username']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One small thing though is that if the keys are numbers than &lt;code&gt;Object.keys()&lt;/code&gt; will return those number in ascending order, regardless of their order as keys in 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;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&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="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="mi"&gt;102&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 hundred and two&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Twenty five&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sixty four&lt;/span&gt;&lt;span class="dl"&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//(4) ['1', '25', '64', '102']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there is a mix of letters and numbers then JavaScript Object Keys will firstly return numbers in ascending order, then letters in the order they appeared.&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&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;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Five&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="s1"&gt;c&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;Letter c&lt;/span&gt;&lt;span class="dl"&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="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&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;Letter a&lt;/span&gt;&lt;span class="dl"&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//(4) ['1', '5', 'c', 'a']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have any questions or anything you can find me on my &lt;a href="https://twitter.com/AmerSikira" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, or you can read some of my other articles like &lt;a href="https://webinuse.com/javascript-array-length-the-definitive-guide/" rel="noopener noreferrer"&gt;JavaScript Array length – the definitive guide&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript Array length – the definitive guide</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Wed, 24 Nov 2021 10:11:08 +0000</pubDate>
      <link>https://dev.to/amersikira/javascript-array-length-the-definitive-guide-1366</link>
      <guid>https://dev.to/amersikira/javascript-array-length-the-definitive-guide-1366</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://webinuse.com/javascript-array-length-the-definitive-guide/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;JavaScript Array length property is something we use, almost, every time we work with an array. But, often, we forget how powerful this property can really be.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is JavaScript Array length property
&lt;/h3&gt;

&lt;p&gt;The length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array, as per &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What does this mean? This means that length property “belongs” to Array &lt;a href="https://webinuse.com/guide-to-javascript-data-types/" rel="noopener noreferrer"&gt;data type&lt;/a&gt; and that it, either, returns the number of elements or sets the number of elements. Since the Array index is zero-based, the integer that is returned by length must be greater than the highest index by 1.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dense and sparse arrays
&lt;/h3&gt;

&lt;p&gt;There are two types of Arrays in JavaScript: dense and sparse. JavaScript Array length property gives different results for these two types. That is why we said that length must be greater than the highest index by 1. This means that sometimes &lt;code&gt;.length&lt;/code&gt; property doesn’t actually return the exact number of elements, but rather the highest index plus 1. In order to explain this, we need to take a look at the difference between dense and sparse arrays.&lt;/p&gt;

&lt;h4&gt;
  
  
  Dense arrays
&lt;/h4&gt;

&lt;p&gt;Dense arrays are the type of arrays that we usually work with.&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;arr&lt;/span&gt; &lt;span class="o"&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;Audi&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;BMW&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;Mercedes&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;This is a typical example of a dense array, the type of array we are familiar with and work with.&lt;/p&gt;

&lt;p&gt;JavaScript Array length property will always return the exact number of elements when it comes to dense arrays.&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;arr&lt;/span&gt; &lt;span class="o"&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;Audi&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;BMW&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;Mercedes&lt;/span&gt;&lt;span class="dl"&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;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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="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;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Result:&lt;/span&gt;
&lt;span class="c1"&gt;// 3&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;highestIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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;1&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;highestIndex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Result:&lt;/span&gt;
&lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, &lt;code&gt;.length&lt;/code&gt; returned &lt;code&gt;3&lt;/code&gt; which is the exact number of items in the array &lt;code&gt;arr&lt;/code&gt;. The highest index is &lt;code&gt;2&lt;/code&gt;. Let’s try to add elements to an array and empty an array, and then check an index and the length.&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;arr&lt;/span&gt; &lt;span class="o"&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;Audi&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;BMW&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;Mercedes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="cm"&gt;/**
 * We know that arr.length === 3
 * Let's add an element to array
 * */&lt;/span&gt;
&lt;span class="nx"&gt;arr&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ferrari&lt;/span&gt;&lt;span class="dl"&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;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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="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;arr&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="c1"&gt;// Result:&lt;/span&gt;
&lt;span class="c1"&gt;// 4&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * In previous example highest index
 * was 2, let's check it now, after
 * we have added another element
 * */&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;arr&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;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Result:&lt;/span&gt;
&lt;span class="c1"&gt;//3&lt;/span&gt;

&lt;span class="c1"&gt;//Let's empty our array&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&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;arr&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="c1"&gt;// Result:&lt;/span&gt;
&lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we have emptied an array, it is pointless to check for index, because there is nothing inside our array.&lt;/p&gt;

&lt;h4&gt;
  
  
  Sparse arrays
&lt;/h4&gt;

&lt;p&gt;What is a “Sparse array?” It is, basically, an array with holes. This means that sparse array doesn’t, necessarily, start at 0. Also, this means that sparse arrays don’t have sequential elements, there can be cases when there is not element after element, but rather a hole. Let’s take a look at several examples.&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;arr&lt;/span&gt; &lt;span class="o"&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;Audi&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;BMW&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;Mercedes&lt;/span&gt;&lt;span class="dl"&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;arr2&lt;/span&gt; &lt;span class="o"&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;JavaScript&lt;/span&gt;&lt;span class="dl"&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;arr3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="nx"&gt;arr3&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Python&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;In the first variable &lt;code&gt;arr&lt;/code&gt;, we have 3 elements and “hole”/”empty space” on index 2. The second variable has 4 empty indexes and on the fifth we have &lt;code&gt;"JavaScript"&lt;/code&gt;. &lt;code&gt;arr3&lt;/code&gt; variable is created as an empty array. And then we assigned &lt;code&gt;"Python"&lt;/code&gt; on index 50. This means that there are 50 empty places before &lt;code&gt;"Python"&lt;/code&gt;, or 49 indexes. These were all examples of sparse arrays. Now, let’s use the JavaScript Array length property on all of those, to see what will happen.&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;arr&lt;/span&gt; &lt;span class="o"&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;Audi&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;BMW&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;Mercedes&lt;/span&gt;&lt;span class="dl"&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;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//(4) ['Audi', 'BMW', empty, 'Mercedes']&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="o"&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;JavaScript&lt;/span&gt;&lt;span class="dl"&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;arr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//(5) [empty × 4, 'JavaScript']&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="nx"&gt;arr3&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Python&lt;/span&gt;&lt;span class="dl"&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;arr3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//(51) [empty × 50, 'Python']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see in the example above &lt;code&gt;console.log()&lt;/code&gt; returned more than length. Even though we expected an integer, we got an integer plus array. This is because the browser is aware that this is a sparse array and it gave us the heads up. Also, wherever we had “hole” browser returned &lt;code&gt;empty&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Manipulate array with JavaScript Array length property
&lt;/h3&gt;

&lt;p&gt;JavaScript Array length property allows us to change the array’s length, by extending it, shortening it, or emptying it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Extend array
&lt;/h4&gt;

&lt;p&gt;When we use the JavaScript Array length property to extend an array what do we get? A Sparse Array. Here is an 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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&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;Audi&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;BMW&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;Mercedes&lt;/span&gt;&lt;span class="dl"&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;arr&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="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//3&lt;/span&gt;

&lt;span class="nx"&gt;arr&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;5&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;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//(5) ['Audi', 'BMW', 'Mercedes', empty × 2]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above we used our standard array and then we used &lt;code&gt;.length&lt;/code&gt; which returned 3. After that, we used JavaScript Array length’s ability to mutate array and we set the array’s length to 5. When we console logged &lt;code&gt;arr&lt;/code&gt; again we saw that, now, &lt;code&gt;arr&lt;/code&gt; has a length of 5 and that two empty indexes are added at the end of it. So, basically, we got a sparse array.&lt;/p&gt;

&lt;h4&gt;
  
  
  Shorten an array
&lt;/h4&gt;

&lt;p&gt;As we said, the JavaScript Array length property gives us the ability to shorten an array. When we use &lt;code&gt;.length&lt;/code&gt; property, it just takes the exact number of elements that we provided from the beginning of the array. The rest of the elements are discarded and the array is mutated.&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;arr&lt;/span&gt; &lt;span class="o"&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;Audi&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;BMW&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;Mercedes&lt;/span&gt;&lt;span class="dl"&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;arr&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="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//3&lt;/span&gt;

&lt;span class="nx"&gt;arr&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;1&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;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//['Audi']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have shortened our array to 1 element, and only the first element is kept. The rest of them are deleted.&lt;/p&gt;

&lt;h4&gt;
  
  
  Empty an array
&lt;/h4&gt;

&lt;p&gt;We can empty an array using JavaScript Array length. Once we pass &lt;code&gt;0&lt;/code&gt; as our array’s length, our array will become empty.&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;arr&lt;/span&gt; &lt;span class="o"&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;Audi&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;BMW&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;Mercedes&lt;/span&gt;&lt;span class="dl"&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;arr&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="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//3&lt;/span&gt;

&lt;span class="nx"&gt;arr&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;0&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;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//Result:&lt;/span&gt;
&lt;span class="c1"&gt;//[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see from the code snippet when we set our length to &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;console.log()&lt;/code&gt; returned an empty array.&lt;/p&gt;

&lt;p&gt;If you have any questions or anything you can find me on my &lt;a href="https://twitter.com/AmerSikira" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, or you can read some of my other articles like &lt;a href="https://webinuse.com/how-to-create-custom-stackable-toast/" rel="noopener noreferrer"&gt;How to create custom stackable toasts&lt;/a&gt;?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to create custom stackable toasts</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Tue, 23 Nov 2021 09:20:46 +0000</pubDate>
      <link>https://dev.to/amersikira/how-to-create-custom-stackable-toasts-b18</link>
      <guid>https://dev.to/amersikira/how-to-create-custom-stackable-toasts-b18</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was initially published on &lt;a href="https://webinuse.com/how-to-create-custom-stackable-toast/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Whenever we build some app there is a chance we are going to need stackable toasts. Toasts are basically notifications that inform users of some action that they can’t control. E.g. network connection lost, data saved, there is an error. Sometimes we need to show multiple notifications at once, that is where stackable toasts are coming in handy.&lt;/p&gt;

&lt;p&gt;The first thing we are going to do is create a basic HTML template.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"X-UA-Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"IE=edge"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Document&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use VSCode and Emmet. When we type in &lt;code&gt;html:5&lt;/code&gt;in an empty .html file, and then we press TAB, Emmet will create the same boilerplate as above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add some JavaScript
&lt;/h3&gt;

&lt;p&gt;Now we need to add some JavaScript to our app.&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;createToast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;heading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No heading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No message&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="c1"&gt;//Create empty variable for toasts container&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;//If container doesn't already exist create one&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#toast-holder&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;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&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;toast-holder&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;container&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;// If container exists asign it to a variable&lt;/span&gt;
    &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#toast-holder&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="c1"&gt;//Create our toast HTML and pass the variables heading and message&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;toast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&amp;lt;div class="single-toast fade-in"&amp;gt;
                  &amp;lt;div class="toast-header"&amp;gt;
                    &amp;lt;span class="toast-heading"&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;heading&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/span&amp;gt;
                    &amp;lt;a href="#" class="close-toast"&amp;gt;X&amp;lt;/a&amp;gt;
                  &amp;lt;/div&amp;gt;
                  &amp;lt;div class="toast-content"&amp;gt;
                    &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
                  &amp;lt;/div&amp;gt;
               &amp;lt;/div&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Once our toast is created add it to the container&lt;/span&gt;
  &lt;span class="c1"&gt;// along with other toasts&lt;/span&gt;
  &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;toast&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="nf"&gt;createToast&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;createToast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is heading&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;This is the message&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;Let’s analyze our code snippet. We created a function &lt;code&gt;createToast()&lt;/code&gt;. This function accepts two parameters: &lt;code&gt;heading&lt;/code&gt; and &lt;code&gt;message&lt;/code&gt;. These two will be passed to our toast as toast heading and toast content. If we omit those two parameters then for &lt;code&gt;heading&lt;/code&gt; the value will be &lt;code&gt;'No heading'&lt;/code&gt; and for the &lt;code&gt;message&lt;/code&gt; value will be &lt;code&gt;'No message'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After that, we have created an empty &lt;code&gt;container&lt;/code&gt; variable. To this variable, we will assign &lt;code&gt;#toast-container&lt;/code&gt;. Why did we do this? Why not creating &lt;code&gt;div&lt;/code&gt; in HTML? Because we are creating stackable toast dynamically and we want to have full control over the page. We don’t want this container to mess with our layout. Also, this means less CSS code and less HTML code. It is not much, but baby steps.&lt;/p&gt;

&lt;p&gt;Then, we created HTML for our toast, and we passed our variables from the beginning &lt;code&gt;heading&lt;/code&gt; and &lt;code&gt;message&lt;/code&gt;. After this HTML is created, we add a toast to our container. As we can see we called function two times. The first time we passed no parameters, and the second time we passed both parameters. This is what we got.&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%2F1tfme5w5poscapaddqxc.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%2F1tfme5w5poscapaddqxc.png" alt="Stackable toasts on the left side with no CSS styling" width="651" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Screenshot of the result&lt;/small&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s beautify our stackable toasts
&lt;/h3&gt;

&lt;p&gt;Since we have no CSS, everything is just “standing” there waiting for us. So let’s add some CSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;border-box&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;#toast-holder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;fixed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.single-toast&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#5b5b5b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;.5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;.3s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;max-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.toast-header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;space-between&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#ccc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.close-toast&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;inherit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.toast-content&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.fade-in&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fadeIn&lt;/span&gt; &lt;span class="n"&gt;linear&lt;/span&gt; &lt;span class="m"&gt;.5s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.fade-out&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fadeOut&lt;/span&gt; &lt;span class="n"&gt;linear&lt;/span&gt; &lt;span class="m"&gt;.5s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="n"&gt;fadeIn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;max-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;max-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&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;@keyframes&lt;/span&gt; &lt;span class="n"&gt;fadeOut&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;max-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;max-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&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 first thing we did is we made sure that nothing had margin or padding and that &lt;code&gt;box-sizing&lt;/code&gt; is set to &lt;code&gt;border-box&lt;/code&gt;. This part of the code makes sure that we do not have weird overflows. After that, we set &lt;code&gt;position:fixed&lt;/code&gt; on our container so it doesn’t mess with the page layout. Also, we made sure that it is on the right side of the screen because that is normally where a user would expect it. Apart from this, we set &lt;code&gt;display: flex; flex-direction: column;&lt;/code&gt;, because we need toasts to be stacked on top of each other, not side by side.&lt;/p&gt;

&lt;p&gt;Then, we added some styling for our stackable toasts. The only thing that, probably, needs explaining is &lt;code&gt;max-height&lt;/code&gt;. In CSS we can’t animate &lt;code&gt;height&lt;/code&gt;, but we can &lt;code&gt;max-height&lt;/code&gt;. That is the reason why we used this property. Also, we created two animations &lt;code&gt;fadeIn&lt;/code&gt; and &lt;code&gt;fadeOut&lt;/code&gt;. So we can animate the entrance and exit, later. This is what we got now.&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%2Fgcarnxgpdcpdqgd63fc2.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%2Fgcarnxgpdcpdqgd63fc2.png" alt="Stackable toasts well designed and positioned on the right side" width="674" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Screenshot of the result&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;As we can see, the layout has changed and also the styling of our toasts is changed. Now, we need to create a function for removing our stackable toasts. We don’t want them there forever.&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;removeToast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//First we need to prevent default&lt;/span&gt;
  &lt;span class="c1"&gt;// to evade any unexpected behaviour&lt;/span&gt;
  &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;//After that we add a class to our toast (.single-toast)&lt;/span&gt;
  &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fade-out&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;//After CSS animation is finished, remove the element&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;500&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 created &lt;code&gt;removeToast()&lt;/code&gt; function and we passed an event parameter to it. Since we are clicking on the &lt;code&gt;a&lt;/code&gt; tag, we want to prevent any unwanted behavior, hence &lt;code&gt;e.preventDefault()&lt;/code&gt;. After that, we assigned a new class &lt;code&gt;.fade-out&lt;/code&gt; to our &lt;code&gt;.single-toast&lt;/code&gt; so we have nice exit animation. After that animation is finished, we remove toast entirely from the DOM.&lt;/p&gt;

&lt;p&gt;Now, we need to pass this function to our &lt;code&gt;createToast&lt;/code&gt; function and create &lt;code&gt;eventListener&lt;/code&gt;. Let’s see our revised &lt;code&gt;createToast()&lt;/code&gt; function.&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;createToast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;heading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No heading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No message&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="c1"&gt;//Create empty variable for toasts container&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;//If container doesn't already exist create one&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#toast-holder&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;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&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;toast-holder&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;container&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;// If container exists asign it to a variable&lt;/span&gt;
    &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#toast-holder&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="c1"&gt;//Create our toast HTML and pass the variables heading and message&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;toast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&amp;lt;div class="single-toast fade-in"&amp;gt;
                  &amp;lt;div class="toast-header"&amp;gt;
                    &amp;lt;span class="toast-heading"&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;heading&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/span&amp;gt;
                    &amp;lt;a href="#" class="close-toast"&amp;gt;X&amp;lt;/a&amp;gt;
                  &amp;lt;/div&amp;gt;
                  &amp;lt;div class="toast-content"&amp;gt;
                    &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
                  &amp;lt;/div&amp;gt;
               &amp;lt;/div&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Once our toast is created add it to the container&lt;/span&gt;
  &lt;span class="c1"&gt;// along with other toasts&lt;/span&gt;
  &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;toast&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


  &lt;span class="cm"&gt;/**
   * THIS PART WE HAVE ADDED
   * */&lt;/span&gt;

    &lt;span class="c1"&gt;//Save all those close buttons in one variable&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;toastsClose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.close-toast&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;//Loop thorugh that variable&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;toastsClose&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="nx"&gt;i&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="c1"&gt;//Add event listener&lt;/span&gt;
    &lt;span class="nx"&gt;toastsClose&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;removeToast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kc"&gt;false&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="nf"&gt;removeToast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//First we need to prevent default&lt;/span&gt;
  &lt;span class="c1"&gt;// to evade any unexpected behaviour&lt;/span&gt;
  &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;//After that we add a class to our toast (.single-toast)&lt;/span&gt;
  &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fade-out&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;//After CSS animation is finished, remove the element&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="nf"&gt;createToast&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;createToast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is heading&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;This is the message&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 only thing that is left is to remove container if it is empty.&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;isEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selector&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&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;0&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;What we did here is we checked if &lt;code&gt;innerHTML&lt;/code&gt; is empty. But before that, we made sure that we trimmed empty spaces. This way we prevented empty spaces to be “counted” as text by JavaScript. This function needs to be added to our &lt;code&gt;removeToast()&lt;/code&gt; function. Why there? Because we want to make sure that this is checked after the stackable toast is removed. Let’s check our improved &lt;code&gt;removeToast()&lt;/code&gt; function.&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;removeToast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//First we need to prevent default&lt;/span&gt;
  &lt;span class="c1"&gt;// to evade any unexpected behaviour&lt;/span&gt;
  &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;//After that we add a class to our toast (.single-toast)&lt;/span&gt;
  &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fade-out&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;//After CSS animation is finished, remove the element&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * WE HAVE ADDED THIS PART
     * */&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#toast-holder&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;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;isEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#toast-holder&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#toast-holder&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#toast-holder&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="mi"&gt;500&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;isEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selector&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&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;0&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 can checkout live example of our stackable toast on CodePen.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amersikira/embed/zYdQxKg?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;If you have any questions or anything you can find me on my &lt;a href="https://twitter.com/AmerSikira" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, or you can read some of my other articles like &lt;a href="https://webinuse.com/how-to-easily-add-wordpress-menu-to-custom-theme/" rel="noopener noreferrer"&gt;How to easily add a WordPress menu to a custom theme&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>css</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Embed assets in a WordPress theme</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Mon, 22 Nov 2021 15:17:04 +0000</pubDate>
      <link>https://dev.to/amersikira/embed-assets-in-a-wordpress-theme-4024</link>
      <guid>https://dev.to/amersikira/embed-assets-in-a-wordpress-theme-4024</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="https://webinuse.com/embed-assets-in-a-wordpress-theme/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the last article, we talked about &lt;a href="https://webinuse.com/how-to-convert-any-html-template-into-wordpress-theme/" rel="noopener noreferrer"&gt;How to simply convert any HTML template into a WordPress theme in under 10 minutes&lt;/a&gt;. Today we are going to continue our journey on creating a WordPress theme out of any HTML template. We are going to see how to embed assets in a WordPress theme.&lt;/p&gt;

&lt;p&gt;In today’s article, we are going to continue our journey, but we are going to use a real template. This template is courtesy of &lt;a href="https://twitter.com/natension" rel="noopener noreferrer"&gt;Nathan&lt;/a&gt; and this template can be found for FREE on &lt;a href="https://aptex.gumroad.com/l/portfolio-template-01?campaign_name=webinuse" rel="noopener noreferrer"&gt;aptex.gumroad.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In order to be able to embed assets in a WordPress theme first thing we need to do is create a file called functions.php.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is functions.php
&lt;/h3&gt;

&lt;p&gt;Functions.php is the file where we extend our theme’s functions. It is one of the default WordPress files, and it acts like our theme’s plugin. We leverage WordPress hooks and functions to achieve new or add existing functionality using PHP.&lt;/p&gt;

&lt;p&gt;If we want to add widget area, woocommerce support, menu support, and a lot of other things, we do that by editing functions.php.&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://www.wpbeginner.com/glossary/functions-php/" rel="noopener noreferrer"&gt;WPBeginner&lt;/a&gt; the functions.php file automatically loads when you install and activate a theme on your WordPress site.&lt;/p&gt;

&lt;p&gt;For more information about functions.php, we can &lt;a href="https://developer.wordpress.org/themes/basics/theme-functions/" rel="noopener noreferrer"&gt;visit official Developer Resources on WordPress.org&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to create functions.php
&lt;/h3&gt;

&lt;p&gt;We need to create functions.php at the root of our theme. In our last article, in the end, we explained &lt;a href="https://webinuse.com/how-to-convert-any-html-template-into-wordpress-theme/" rel="noopener noreferrer"&gt;how and where themes are uploaded&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After we create our functions.php we are going to paste some code inside. For now, we are going to assume that the only thing we want to include is style.css file.&lt;/p&gt;

&lt;p&gt;WordPress has a special hook for embedding assets and it is called &lt;code&gt;wp_enqueue_style()&lt;/code&gt;. This hook, or function, is used when we want to enqueue style in our WordPress theme. Style is anything that is being used for styling our website, like CSS or fonts. This is the only, proper, way to embed assets in a WordPress theme.&lt;/p&gt;

&lt;p&gt;Let’s take a look at &lt;code&gt;wp_enqueue_style()&lt;/code&gt; function parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;wp_enqueue_style&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$handle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nv"&gt;$deps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;bool&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="nv"&gt;$ver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$media&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'all'&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Enqueue style parameters
&lt;/h3&gt;

&lt;p&gt;According to official &lt;a href="https://developer.wordpress.org/reference/functions/wp_enqueue_style/" rel="noopener noreferrer"&gt;WordPress docs&lt;/a&gt; as the first parameter we need to pass the &lt;code&gt;$handle&lt;/code&gt;, that is the name of stylesheet, and it should be unique. The second parameter is &lt;code&gt;$src&lt;/code&gt; and it holds the source of the style that we want to embed.&lt;/p&gt;

&lt;p&gt;This source can be anything. From third-party URLs like &lt;a href="https://cdn.example.com/path-to-style" rel="noopener noreferrer"&gt;https://cdn.example.com/path-to-style&lt;/a&gt;, or path to a local file. Path to local file is relative to WordPress root directory. Also, when using external stylesheets, &lt;code&gt;wp_enqueue_style()&lt;/code&gt; doesn’t need protocol like http or https. It is enough to start our link with a double forward slash, &lt;code&gt;//&lt;/code&gt;. There is also another option when we want to use path relative to our theme, but we will look at that later.&lt;/p&gt;

&lt;p&gt;Why &lt;code&gt;$handle&lt;/code&gt; needs to be unique? Because of the third parameter, &lt;code&gt;$deps&lt;/code&gt;. This parameter holds an array of handles that this stylesheet depends on.&lt;/p&gt;

&lt;p&gt;The fourth parameter is &lt;code&gt;$ver&lt;/code&gt; that holds the current version of the stylesheet. This is used for multiple purposes like caching or making sure that our site serves the right stylesheet. &lt;strong&gt;NOTE:&lt;/strong&gt; This should be updated every time we update our stylesheet.&lt;/p&gt;

&lt;p&gt;The last parameter is &lt;code&gt;$media&lt;/code&gt;. We pass media query to this parameter if it is applicable. E.g. &lt;code&gt;(max-width: 1024px)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to enqueue style.css
&lt;/h3&gt;

&lt;p&gt;The first thing we need to do is embed style.css stylesheet in our theme.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * theme functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package theme
 */&lt;/span&gt;


&lt;span class="cd"&gt;/**
* Enqueue scripts and styles.
*/&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;theme_scripts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;wp_enqueue_style&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'theme-style'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;get_stylesheet_uri&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="k"&gt;array&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="nf"&gt;add_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'wp_enqueue_scripts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'theme_scripts'&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Let’s analyze our code. After a few comments, we created a function &lt;code&gt;theme_scripts&lt;/code&gt;. Inside that function, we embedded our style.css file. Instead of using a path, as everyone would expect, there is a &lt;a href="https://developer.wordpress.org/reference/functions/get_stylesheet_uri/" rel="noopener noreferrer"&gt;built-in WordPress function for getting the default stylesheet&lt;/a&gt;. This function, &lt;code&gt;get_stylesheet_uri()&lt;/code&gt;, retrieves stylesheet URI for the current theme.&lt;/p&gt;

&lt;p&gt;After our function &lt;code&gt;theme_scripts&lt;/code&gt; we used built-in WordPress function, hook, &lt;a href="https://developer.wordpress.org/reference/functions/add_action/" rel="noopener noreferrer"&gt;&lt;code&gt;add_action&lt;/code&gt; that will register our function&lt;/a&gt;. We will not get into writing more about add_action, but we will say that if we want to enqueue script, or enqueue style, we need to use this exact same code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;add_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'wp_enqueue_scripts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'function_name'&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We only need to change &lt;code&gt;'function_name'&lt;/code&gt; parameter to match our function’s name.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to embed other assets to our WordPress theme
&lt;/h3&gt;

&lt;p&gt;Let’s say we want to add Google Fonts, we only need to add another &lt;code&gt;wp_enqueue_script&lt;/code&gt; to our &lt;code&gt;theme_scripts&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * theme functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package theme
 */&lt;/span&gt;


&lt;span class="cd"&gt;/**
* Enqueue scripts and styles.
*/&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;theme_scripts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;wp_enqueue_style&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'theme-style'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;get_stylesheet_uri&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="k"&gt;array&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;//We have added this line of code&lt;/span&gt;
    &lt;span class="nf"&gt;wp_enqueue_style&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'google-fonts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'//fonts.googleapis.com/css2?family=Inter:wght@400;600;700;900&amp;amp;family=Roboto+Slab:wght@900&amp;amp;display=swap'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;array&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="nf"&gt;add_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'wp_enqueue_scripts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'theme_scripts'&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we did? We picked a &lt;a href="https://fonts.google.com/" rel="noopener noreferrer"&gt;Google Fonts&lt;/a&gt;, we copied the link, and pasted it as &lt;code&gt;$src&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to enqueue script
&lt;/h3&gt;

&lt;p&gt;Instead of using &lt;code&gt;wp_enqueue_style&lt;/code&gt; function, we are going to use &lt;code&gt;wp_enqueue_script&lt;/code&gt; function. It is the same as &lt;code&gt;wp_enqueue_style&lt;/code&gt; only the last parameter is different.&lt;/p&gt;

&lt;p&gt;Instead of &lt;code&gt;$media&lt;/code&gt; parameter we have &lt;code&gt;$in_footer&lt;/code&gt; parameter. It is &lt;code&gt;false&lt;/code&gt; by default. This parameter decides whether our script will be embedded in header or footer. If it is &lt;code&gt;false&lt;/code&gt; then our script will be embedded in header, otherwise in footer.&lt;/p&gt;

&lt;p&gt;Let’s enqueue script to our theme. Since we are using &lt;a href="https://aptex.gumroad.com/l/portfolio-template-01" rel="noopener noreferrer"&gt;Portfolio HTML template&lt;/a&gt; we will enqueue app.js file from the theme root that comes with the template.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * theme functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package theme
 */&lt;/span&gt;


&lt;span class="cd"&gt;/**
* Enqueue scripts and styles.
*/&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;theme_scripts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;wp_enqueue_style&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'theme-style'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;get_stylesheet_uri&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="k"&gt;array&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="nf"&gt;wp_enqueue_style&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'google-fonts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'//fonts.googleapis.com/css2?family=Inter:wght@400;600;700;900&amp;amp;family=Roboto+Slab:wght@900&amp;amp;display=swap'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;array&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;//We have added this line of code&lt;/span&gt;
    &lt;span class="nf"&gt;wp_enqueue_script&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'default-js'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;get_template_directory_uri&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'/app.js'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;array&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="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;add_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'wp_enqueue_scripts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'theme_scripts'&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;As shown in the example above, we just added &lt;code&gt;wp_enqueue_script&lt;/code&gt; to &lt;code&gt;theme_scripts&lt;/code&gt;. But there is one thing we haven’t seen before. It is &lt;a href="https://developer.wordpress.org/reference/functions/get_template_directory_uri/" rel="noopener noreferrer"&gt;&lt;code&gt;get_template_directory_uri()&lt;/code&gt; function&lt;/a&gt;. This function basically retrieves the URI of the currently active theme.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to learn more about what is WordPress you can check this awesome course &lt;a href="https://gumroad.com/a/882791539/kSrqD" rel="noopener noreferrer"&gt;Building websites with WordPress&lt;/a&gt; by &lt;a href="https://twitter.com/natmiletic" rel="noopener noreferrer"&gt;Nat Miletic&lt;/a&gt;. This course is excellent for those who want to start with WordPress. Nat is teaching us what is WordPress, what can we use it for, how can we use it, in a really nice and simple way.&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;Affiliate&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you have any questions or anything you can find me on my &lt;a href="https://twitter.com/AmerSikira" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, or you can read some of my other articles like &lt;a href="https://webinuse.com/how-to-bind-events-to-dynamically-created-elements/" rel="noopener noreferrer"&gt;How to bind events to dynamically created elements in JavaScript&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>theme</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to bind events to dynamically created elements in JavaScript</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Fri, 19 Nov 2021 16:44:19 +0000</pubDate>
      <link>https://dev.to/amersikira/how-to-bind-events-to-dynamically-created-elements-in-javascript-3pk5</link>
      <guid>https://dev.to/amersikira/how-to-bind-events-to-dynamically-created-elements-in-javascript-3pk5</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published at &lt;a href="https://webinuse.com/how-to-bind-events-to-dynamically-created-elements/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When working with JavaScript and DOM, we often find ourselves in need of creating some elements dynamically. Especially when working with some tables and fetching data from the server. Sometimes we have to bind events to dynamically created elements. And that is exactly what we are going to do now.&lt;/p&gt;

&lt;p&gt;When jQuery was in use (it is still today, but not as much as before) we would do by simply attaching the event to &lt;code&gt;body&lt;/code&gt;, and then attaching it to the element. Basically, jQuery would find that element for us in the DOM tree and run the desired function. Let’s see an 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="nf"&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;body&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&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;dynamic-element&lt;/span&gt;&lt;span class="dl"&gt;"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, jQuery would attach events to dynamically created elements, or any elements for that matter, that we have passed as a parameter.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bind events to dynamically created elements through bubbling
&lt;/h3&gt;

&lt;p&gt;The first way we are going to try is to attach events to dynamically created elements using a method of bubbling. What does this mean? This means that we are going to target the parent instead of the actual element. And, then, we are going to check if a user clicked on the element or somewhere else.&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;div&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;container&lt;/span&gt;&lt;span class="dl"&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;button&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click-btn&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;The&lt;/span&gt; &lt;span class="nx"&gt;First&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="sr"&gt;/div&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;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="cm"&gt;/**
     * First, we select parent element
     * */&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#container&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * After that we add event listener to that element
     * */&lt;/span&gt;
    &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&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="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="cm"&gt;/**
         * Then, we check if we clicked on an element that has
         * .click-btn class
         * */&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click-btn&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="cm"&gt;/**
             * If we have clicked on such element we
             * run some function
             * */&lt;/span&gt;
          &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`You have clicked &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&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="cm"&gt;/**
     * Now let's create our dynamic element
     * Another button
     * */&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;btn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="cm"&gt;/**
     * In order for event bubbling to work,
     * we have to add the same class as we used
     * in our eventListener
     * */&lt;/span&gt;
    &lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;className&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;//Then we add some text inside that button&lt;/span&gt;
    &lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&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 dynamically created button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;//And we append it&lt;/span&gt;
    &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&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;As we can see in the example above, we have added &lt;code&gt;eventListener&lt;/code&gt; to the parent, but it will run only if a user clicks on an element with a class &lt;code&gt;click-btn&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After we’ve done that, we dynamically created another button with the same class. And by the logic of things, &lt;code&gt;eventListener&lt;/code&gt; will run the same function on that button, as on the one we created in HTML.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to dynamically attach events to dynamically created elements
&lt;/h3&gt;

&lt;p&gt;The second way to bind events to dynamically created elements is by creating a function and running that function on the elements we’ve created. Sounds confusing? Let’s create some examples.&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;div&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;container&lt;/span&gt;&lt;span class="dl"&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;button&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click-btn&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;The&lt;/span&gt; &lt;span class="nx"&gt;First&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="sr"&gt;/div&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;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="cm"&gt;/**
 * First let's create a function
 * */&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;dynamicFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`You have clicked on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&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="cm"&gt;/**
 * Now let's create our dynamic element
 * */&lt;/span&gt;

&lt;span class="c1"&gt;//First we select our container&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#container&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//Then, we create a button&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;btn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//Then, we add it the same as their respective siblings&lt;/span&gt;
&lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;className&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//Now, we add it some text&lt;/span&gt;
&lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&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 dynamically created button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//Lastly, append it to the container&lt;/span&gt;
&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Since we are going to attach eventListener to a class
 * we need a loop
 * */&lt;/span&gt;

 &lt;span class="c1"&gt;//Let's store our elements to variable&lt;/span&gt;

 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.click-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="c1"&gt;//Then, we loop through those elements&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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;elements&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="nx"&gt;i&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="c1"&gt;//We add eventListener to each element&lt;/span&gt;
     &lt;span class="nx"&gt;elements&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dynamicFunction&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;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&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;What we did in this example is instead of setting the event to parent, we set it to the exact elements.&lt;/p&gt;

&lt;p&gt;But there is a catch. If we created our button, let’s say after we did &lt;code&gt;.fetch()&lt;/code&gt; we would need to run a loop again. Why? Because new elements were not part of the DOM when we run the loop the first time. So we can consider that as a drawback.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which way is better?
&lt;/h3&gt;

&lt;p&gt;They are equal, depending on our preferences. If we want to write a function once then the first way is better. If we do not mind writing the same code several times, then it does not matter.&lt;/p&gt;

&lt;p&gt;If you have any questions or anything you can find me on my &lt;a href="https://twitter.com/AmerSikira" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, or you can read some of my other articles like &lt;a href="https://webinuse.com/how-to-convert-any-html-template-into-wordpress-theme/" rel="noopener noreferrer"&gt;How to simply convert any HTML template into a WordPress theme in under 10 minutes&lt;/a&gt;?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to simply convert any HTML template into a WordPress theme in under 10 minutes</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Wed, 17 Nov 2021 10:25:24 +0000</pubDate>
      <link>https://dev.to/amersikira/how-to-simply-convert-any-html-template-into-a-wordpress-theme-in-under-10-minutes-13p1</link>
      <guid>https://dev.to/amersikira/how-to-simply-convert-any-html-template-into-a-wordpress-theme-in-under-10-minutes-13p1</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published at &lt;a href="https://webinuse.com/how-to-convert-any-html-template-into-wordpress-theme/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;At the first, this heading might sound intimidating and clickbait. It is not, well maybe a clickbait a little bit, but bear with us. &lt;a href="http://What%20is%20WordPress?%20A%20super%20simple%20guide%20for%20beginners" rel="noopener noreferrer"&gt;WordPress&lt;/a&gt; theme market is huge today and there are companies that earned millions on one theme only, like &lt;a href="https://avada.theme-fusion.com/" rel="noopener noreferrer"&gt;Avada theme&lt;/a&gt;. To make such a theme we need a theme of professionals and probably several months of work.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to learn more about what is WordPress you can check this awesome course &lt;a href="https://gumroad.com/a/882791539/kSrqD" rel="noopener noreferrer"&gt;Building websites with WordPress&lt;/a&gt; by &lt;a href="https://twitter.com/natmiletic" rel="noopener noreferrer"&gt;Nat Miletic&lt;/a&gt;. This course is excellent for those who want to start with WordPress. Nat is teaching us what is WordPress, what can we use it for, how can we use it, in a really nice and simple way.&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;Affiliate Link&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Most of the themes that are selling today come with a tone of additional stuff like builders, widgets, extensions, multiple layouts, etc.&lt;/p&gt;

&lt;p&gt;What are we gonna talk about is how to convert our HTML template to a WordPress theme. And in future articles, we will continue to build upon that.&lt;/p&gt;

&lt;p&gt;There are out-of-the-box solutions that we can use to create a bare-bones WP theme, that only has a basic skeleton. Some of the places where we can create such themes are &lt;a href="http://underscores.me/" rel="noopener noreferrer"&gt;Underscores&lt;/a&gt;, &lt;a href="https://understrap.com/" rel="noopener noreferrer"&gt;UnderStrap&lt;/a&gt;, and &lt;a href="https://themble.com/bones/" rel="noopener noreferrer"&gt;Bones&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Structure of a WordPress theme
&lt;/h3&gt;

&lt;p&gt;Every WP theme has the same basic structure. We only need style.css and index.php, header.php, and footer.php to have a WordPress theme. Usually (read always) there are more files to a WP theme. Some may say that we need even fewer files, but let’s be nice and separate everything as it should be.&lt;/p&gt;

&lt;p&gt;Our theme will have the following structure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  header.php&lt;/li&gt;
&lt;li&gt;  footer.php&lt;/li&gt;
&lt;li&gt;  index.php&lt;/li&gt;
&lt;li&gt;  style.css&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Later, in other articles, we will add some more files, but for now, we will only make the homepage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; This is only a simple theme, with almost everything hardcoded, but we will improve our theme during the course of time.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Let’s create header.php
&lt;/h3&gt;

&lt;p&gt;The first thing we will do is create header.php. Then, we paste anything from the existing HTML template, that comes before the body tag and we include the body tag, into header.php.&lt;/p&gt;

&lt;p&gt;If we have something, like a hero image, or jumbotron that repeats on all pages, we can insert that also. In our case, we will use navigation, but later we will change that navigation to the WordPress menu. Also, we will put the main tag, because it will be the element that will hold all other elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * The header for our theme
 *
 * This is the template that displays all of the 
 * &amp;lt;head&amp;gt; section and opening body tag
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package theme
 */&lt;/span&gt;

&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt; &lt;span class="nf"&gt;language_attributes&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt; &lt;span class="nf"&gt;bloginfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'charset'&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"profile"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://gmpg.org/xfn/11"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt; &lt;span class="nf"&gt;wp_head&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;nav&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/#about"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;About&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/#portfolio"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Portfolio&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/#contact"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;main&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"main"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Next is footer.php
&lt;/h3&gt;

&lt;p&gt;After we created header.php we can create footer.php for our WordPress theme. Footer.php will hold everything that we have in the footer, any additional part that will be repeated across the whole website, at the bottom. Plus, we need to add a closing body and html tag. But the first thing is we need to do, at least with this example that we have created here, we need to close the main tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * The template for displaying the footer
 *
 * Contains the closing of the #content div and all content after.
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package theme
 */&lt;/span&gt;

&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/main&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;footer&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"footer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Here goes some content for the footer&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/footer&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;


&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. The main part – index.php
&lt;/h3&gt;

&lt;p&gt;After we have created header.php and footer.php, we can continue with index.php. If there are no other files like single.php, page.php, post.php, something-template.php, index.php will be used as the template for everything. But we will deal with this in our future articles. For now, let’s just create index.php.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package theme
 */&lt;/span&gt;

&lt;span class="nf"&gt;get_header&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;section&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"hero"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"hero-img"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/section&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;section&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"about"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;This is about section&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Here comes some text about us&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;We can eve use some call to action&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;section&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"portfolio"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"portfolio-img"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"portfolio-img"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"portfolio-img"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"portfolio-img"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"portfolio-img"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"portfolio-img"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;section&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"contact-us"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;textarea&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/textarea&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Send&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;

&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
    &lt;span class="nf"&gt;get_footer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s analyze a code a little bit. The first thing we see is &lt;code&gt;get_header()&lt;/code&gt; function. It is actually a built-in WordPress function that runs our header.php that we have previously created. So instead of using &lt;code&gt;include&lt;/code&gt;, or &lt;code&gt;require&lt;/code&gt;, from PHP, we use &lt;code&gt;get_header&lt;/code&gt;. Why? Because &lt;code&gt;get_header&lt;/code&gt; does a lot more job than just including a file.&lt;/p&gt;

&lt;p&gt;After that, we can add any HTML we want. We just added some super basic stuff.&lt;/p&gt;

&lt;p&gt;When we are done with HTML we use &lt;code&gt;get_footer()&lt;/code&gt; function that includes our footer and runs some more stuff in between.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. style.css in a WordPress theme
&lt;/h3&gt;

&lt;p&gt;Normally CSS files are used for styling our content. When we are using WP, style.css means more than simple styling. For a WordPress theme, style.css is crucial because it holds the theme’s name, which is required in order for the theme to be recognized and activated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/*!
Theme Name: theme
Theme URI: http://theme.com/
Author: Webinuse
Author URI: https://webinuse.com
*/&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;border-box&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;As shown in the example above in the first few lines there is a comment with several lines that holds some information. This information is used by WordPress in order to recognize themes and get some information about them. The only mandatory line is Theme Name, everything else is optional. There are more data that we can put in style.css, but we will tackle that some other time.&lt;/p&gt;

&lt;p&gt;After the comment, we can put any styles we want. Same as with any other CSS file. If we convert some HTML template, that already exists, style.css is the perfect place to paste our CSS code.&lt;/p&gt;

&lt;p&gt;This is basically it! We have created a working WordPress theme. In the following articles, we will create more advanced features. Also, we will add more page templates to our WordPress theme and some other cool things.&lt;/p&gt;

&lt;p&gt;Now when we want to upload it, we put all of our files in a single folder and name this folder using our theme name slug, e.g. my-super-awesome-theme. After that, we zip that folder and upload it through the Themes installer in WordPress, and we can activate our theme.&lt;/p&gt;

&lt;p&gt;If you have any questions or anything you can find me on my &lt;a href="https://twitter.com/AmerSikira" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, or you can read some of my other articles like &lt;a href="https://webinuse.com/how-to-warn-users-when-the-connection-is-lost/" rel="noopener noreferrer"&gt;How to warn users when the connection is lost?&lt;/a&gt;&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to warn users when the connection is lost?</title>
      <dc:creator>Amer Sikira</dc:creator>
      <pubDate>Tue, 16 Nov 2021 09:18:07 +0000</pubDate>
      <link>https://dev.to/amersikira/how-to-warn-users-when-the-connection-is-lost-5579</link>
      <guid>https://dev.to/amersikira/how-to-warn-users-when-the-connection-is-lost-5579</guid>
      <description>&lt;p&gt;&lt;em&gt;This blog post was originally published at &lt;a href="https://webinuse.com/how-to-warn-users-when-the-connection-is-lost/" rel="noopener noreferrer"&gt;webinuse.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are times when we need to track users’ internet connection. This is especially useful if we are building CRM, CMS, or other content-related software. Maybe the user is trying to save something and the connection is lost, instead of losing what he/she already did, we can warn our user that connection is lost and save data locally, e.g. &lt;a href="https://webinuse.com/how-to-use-localstorage-api/" rel="noopener noreferrer"&gt;localStorage&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Besides warning, we can send them a message that we will save everything locally, so users know that we care about their time, experience, and data.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to register lost connection
&lt;/h3&gt;

&lt;p&gt;Thankfully JavaScript has the solution for that. It is called &lt;code&gt;.onoffline&lt;/code&gt; event. Once JS registers that the internet connection is lost, it will fire &lt;code&gt;.onoffline&lt;/code&gt; event. This event supports every major browser, according to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/online_event" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Same as any other event, there are two ways to add an event listener. Let’s take a look at the example below.&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="cm"&gt;/**
 * We can trigger onoffline event by using addEventListener
 * */&lt;/span&gt;

 &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;offline&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;event&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;//here we run our code, what to do&lt;/span&gt;
     &lt;span class="c1"&gt;//when connection is lost&lt;/span&gt;
 &lt;span class="p"&gt;})&lt;/span&gt;


 &lt;span class="cm"&gt;/**
  * We can trigger onoffline event by using onoffline
  * */&lt;/span&gt;

  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onoffline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&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;//here we run our code, what to do&lt;/span&gt;
     &lt;span class="c1"&gt;//when connection is lost&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to register when connection is restored
&lt;/h3&gt;

&lt;p&gt;Once we registered a lost connection, we would probably want to register when the connection is restored.&lt;/p&gt;

&lt;p&gt;Similar to &lt;code&gt;.onoffline&lt;/code&gt; event, there is &lt;code&gt;.ononline&lt;/code&gt; event. It is triggered when JS notices that we are back online again. Let’s check an example. This event supports every major browser, according to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/online_event" 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="cm"&gt;/**
 * We can trigger ononline event by using addEventListener
 * */&lt;/span&gt;

 &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;online&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;event&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;//here we run our code, what to do&lt;/span&gt;
     &lt;span class="c1"&gt;//when connection is restored&lt;/span&gt;
 &lt;span class="p"&gt;})&lt;/span&gt;


 &lt;span class="cm"&gt;/**
  * We can trigger onoffline event by using ononline
  * */&lt;/span&gt;

  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ononline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&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;//here we run our code, what to do&lt;/span&gt;
     &lt;span class="c1"&gt;//when connection is restored&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  What to do?
&lt;/h3&gt;

&lt;p&gt;When a user is offline we can send a warning and save data to the localStorage.&lt;/p&gt;

&lt;p&gt;Why is this good?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  User is warned and knows that if something is not working properly it is not app’s fault, but rather someone else’s&lt;/li&gt;
&lt;li&gt;  Better user experience. Shows that we were thinking about them even when internet is not working&lt;/li&gt;
&lt;li&gt;  We can save user additional pain of losing data that they already entered&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, let’s dive in in the example of how will it look like if we try to implement this in the real world.&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="cm"&gt;/**
 * First we can deal with 
 * offline state
 * */&lt;/span&gt;

 &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;offlineState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      * The first thing we need to do is
      * get data that user could posibly
      * enter or change, e.g. form elements
      * */&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#input&lt;/span&gt;&lt;span class="dl"&gt;"&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="cm"&gt;/**
      * Our example is simple, but usually here
      * we would collect all data and 
      * put it in array or object.
      * */&lt;/span&gt;

      &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Your connection is lost! Data is saved locally!&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="c1"&gt;//Now, let's add event listener&lt;/span&gt;

 &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;offline&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;offlineState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


 &lt;span class="cm"&gt;/**
  * Next thing we will do is create
  * function for when application is back 
  * online
  * */&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;onlineState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="cm"&gt;/**
       * The first thing we will do is
       * get data from localStorage
       * and return it to form
       * */&lt;/span&gt;

       &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

       &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#input&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

       &lt;span class="c1"&gt;//Now we notify user that everything is &lt;/span&gt;
       &lt;span class="c1"&gt;//back online again&lt;/span&gt;

       &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;We are back online! Keep the good work!&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="c1"&gt;//Next thing is creating event listener&lt;/span&gt;

  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;online&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onlineState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This example just shows what can we do. It is not a definitive guide.&lt;/p&gt;

&lt;p&gt;If you are interested in me writing complete function for &lt;code&gt;.onoffline&lt;/code&gt; and &lt;code&gt;.ononline&lt;/code&gt; events, you can find me on &lt;a href="https://twitter.com/AmerSikira" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have any questions or anything you can find me on my &lt;a href="https://twitter.com/AmerSikira" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, or you can read some of my other articles like &lt;a href="https://webinuse.com/what-is-wordpress/" rel="noopener noreferrer"&gt;What is WordPress? A super simple guide for beginners&lt;/a&gt;.&lt;/p&gt;

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