<?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: Taulant Sulko</title>
    <description>The latest articles on DEV Community by Taulant Sulko (@taulantsulko).</description>
    <link>https://dev.to/taulantsulko</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%2F315742%2F5077d6e5-6ffc-4f6e-ba3f-00cc9c465ba1.jpg</url>
      <title>DEV Community: Taulant Sulko</title>
      <link>https://dev.to/taulantsulko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/taulantsulko"/>
    <language>en</language>
    <item>
      <title>The Useless Machine and what can it teach us about software and computation</title>
      <dc:creator>Taulant Sulko</dc:creator>
      <pubDate>Thu, 09 Jul 2020 17:30:38 +0000</pubDate>
      <link>https://dev.to/taulantsulko/the-useless-machine-and-what-can-it-teach-us-about-software-and-computation-2al6</link>
      <guid>https://dev.to/taulantsulko/the-useless-machine-and-what-can-it-teach-us-about-software-and-computation-2al6</guid>
      <description>&lt;p&gt;You may be familiar with The Useless Machine, made famous by American cognitive scientist Marvin Minsky. For those who aren't, it is a useless object with no real purpose. &lt;/p&gt;

&lt;p&gt;There are many examples, but the most well-known was made by Minsky himself. His version is a box with the sole function of switching itself &lt;code&gt;OFF&lt;/code&gt; once the user has turned it &lt;code&gt;ON&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdvjthgwnlek6rm3gzb9y.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdvjthgwnlek6rm3gzb9y.gif" alt="The Useless Machine Animation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's very silly, and can be looked at as nothing but a fun and addictive toy. However, I think this very basic automaton offers an interesting lens to look at computing through. &lt;/p&gt;

&lt;h2&gt;
  
  
  What are the implications of machine that can switch itself &lt;code&gt;OFF&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;I often find, when writing code, that I am essentially doing is creating metaphorical switches, then telling the computer whether they should be turned &lt;code&gt;ON&lt;/code&gt; or &lt;code&gt;OFF&lt;/code&gt;. Or in more broad terms whenever something should happen or shouldn't happen.&lt;/p&gt;

&lt;p&gt;In the case of the Useless Machine, we can predict that the machine will reverse our decision every time. It is going beyond just switching between two states. The Useless Machine can put itself into a new state.&lt;/p&gt;

&lt;p&gt;Let's do a practical breakdown of each element and how it can be represented as code.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The switch&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A physical switch has two states: &lt;code&gt;ON&lt;/code&gt; and &lt;code&gt;OFF&lt;/code&gt;. When it's &lt;code&gt;ON&lt;/code&gt; the current flows through it; when it's turned &lt;code&gt;OFF&lt;/code&gt; it blocks the current by cutting the flow.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgb7p8qy6fl6vd71dqcny.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgb7p8qy6fl6vd71dqcny.jpg" alt="The Switch"&gt;&lt;/a&gt;&lt;br&gt;
In programming the switch can be represented as &lt;code&gt;boolean&lt;/code&gt; values &lt;code&gt;TRUE&lt;/code&gt; and &lt;code&gt;FALSE&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How can we represent the switch in a programming language? For simplicity let's track if the switch is &lt;code&gt;ON&lt;/code&gt;. We can do this by declaring a variable called &lt;code&gt;isSwitchOn&lt;/code&gt; and setting it to &lt;code&gt;false&lt;/code&gt;. This represents that the switch is &lt;code&gt;OFF&lt;/code&gt;. As we know The Useless Machine is turned &lt;code&gt;OFF&lt;/code&gt; before the user turns it &lt;code&gt;ON&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;isSwitchOn&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  The role of the switch
&lt;/h4&gt;

&lt;p&gt;The switch has a very interesting role in The Useless Machine. If we were to break this down as &lt;a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="noopener noreferrer"&gt;MVC&lt;/a&gt; (Model View Controller) diagram. The switch here is a Model, View and Controller all in one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpfb8cynv8snt0pcdz2hf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpfb8cynv8snt0pcdz2hf.png" alt="MVC"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Switch Role&lt;/th&gt;
&lt;th&gt;Computational Role&lt;/th&gt;
&lt;th&gt;Description of Role&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Model&lt;/td&gt;
&lt;td&gt;Memory&lt;/td&gt;
&lt;td&gt;The switch is a database that can hold either of two values &lt;code&gt;ON&lt;/code&gt; or &lt;code&gt;OFF&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;View&lt;/td&gt;
&lt;td&gt;Display&lt;/td&gt;
&lt;td&gt;You can read the database by seeing the status of the switch if it is &lt;code&gt;ON&lt;/code&gt; or &lt;code&gt;OFF&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Controller&lt;/td&gt;
&lt;td&gt;Logic&lt;/td&gt;
&lt;td&gt;The switch can change the value of the database by the act of switching from &lt;code&gt;ON&lt;/code&gt; to &lt;code&gt;OFF&lt;/code&gt; or from &lt;code&gt;OFF&lt;/code&gt; to &lt;code&gt;ON&lt;/code&gt;. If something is not &lt;code&gt;ON&lt;/code&gt; then it is &lt;code&gt;OFF&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The user&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The user interacts with the box by turning the switch &lt;code&gt;ON&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwxdo2unlh2lw35tap6w0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwxdo2unlh2lw35tap6w0.jpg" alt="The User"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The user turning the switch &lt;code&gt;ON&lt;/code&gt; can be represented as a function that changes the value of &lt;code&gt;isSwitchOn&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;turnSwitchOn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;isSwitchOn&lt;/span&gt; &lt;span class="o"&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;turnSwitchOn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  The role of the user
&lt;/h4&gt;

&lt;p&gt;Usually people build machines that create efficiencies like saving time by doing something faster or better. Why would anyone use The Useless Machine? That's simple. It's a fun and addictive toy.&lt;/p&gt;

&lt;p&gt;How do users get addicted to technology? Let's analyze The Useless Machine through The Hook model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7stl0gic6tfi2rru4ggz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7stl0gic6tfi2rru4ggz.png" alt="The Hook"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Trigger&lt;/td&gt;
&lt;td&gt;User sees that the switch is turned &lt;code&gt;OFF&lt;/code&gt;. Wants to turn it &lt;code&gt;ON&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Action&lt;/td&gt;
&lt;td&gt;User turns the switch &lt;code&gt;ON&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Variable Rewards&lt;/td&gt;
&lt;td&gt;The user is entertained by the fact that the machine can turn the switch back to the &lt;code&gt;OFF&lt;/code&gt; position.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Investment&lt;/td&gt;
&lt;td&gt;The user gets invested into thinking about automatons. Ponders about life and computers and finite state machines.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;External Trigger&lt;/td&gt;
&lt;td&gt;User sees that the switch is turned &lt;code&gt;OFF&lt;/code&gt;. Wants to turn it &lt;code&gt;ON&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The creepy arm inside the box&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There is a tireless hand inside The Useless Machine that is tasked with turning the switch &lt;code&gt;OFF&lt;/code&gt; whenever it finds that it is turned &lt;code&gt;ON&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7dvis287rf5zjd9iwge7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7dvis287rf5zjd9iwge7.jpg" alt="The Arm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This arm in programming can be represented with a recursive function. When you call a function in itself it will run forever. The &lt;code&gt;turnSwitchOff()&lt;/code&gt; function will run forever in a loop to check if the &lt;code&gt;isSwitchOn&lt;/code&gt; is ever &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;turnSwitchOff&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="nx"&gt;isSwitchOn&lt;/span&gt; &lt;span class="o"&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="nx"&gt;isSwitchOn&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="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;turnSwitchOff&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;turnSwitchOff&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc74bsjjotfh646v2gkoi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc74bsjjotfh646v2gkoi.gif" alt="The Useless Machine Animation White Background"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The role of the creepy arm
&lt;/h4&gt;

&lt;p&gt;We have to notate here that computers and machines have no nuance. They just do what they are told and they do it very fast. This arm doesn't know that is being annoying. It just knows that it has to turn the switch &lt;code&gt;OFF&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0ld8absnu1wfs97perpl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0ld8absnu1wfs97perpl.png" alt="S1 to S2"&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;S1&lt;/code&gt; = &lt;code&gt;OFF&lt;/code&gt;, &lt;code&gt;S2&lt;/code&gt; = &lt;code&gt;ON&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the scheme of Automata Theory, The Useless Machine would be considered between a Finite-state machine and a Pushdown automaton. &lt;br&gt;
A Finite State machine can be switched from &lt;code&gt;S1&lt;/code&gt; "State 1" (&lt;code&gt;OFF&lt;/code&gt; in our example) to &lt;code&gt;S2&lt;/code&gt; "State 2" (&lt;code&gt;ON&lt;/code&gt; in our example). What is interesting about The Useless Machine is that it can switch itself back from &lt;code&gt;S2&lt;/code&gt; "State 2" to &lt;code&gt;S1&lt;/code&gt; "State 1" on it's own. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffqj7lmr536iytccm6a1q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffqj7lmr536iytccm6a1q.jpg" alt="Automata Theory"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Automata Theory&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Combinational logic&lt;/td&gt;
&lt;td&gt;The ability to hold boolean values like &lt;code&gt;TRUE&lt;/code&gt; and &lt;code&gt;FALSE&lt;/code&gt;. In our case is &lt;code&gt;ON&lt;/code&gt; and &lt;code&gt;OFF&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Finite-state machine&lt;/td&gt;
&lt;td&gt;A machine that can be switched from 2 or more states. A door could fit this category. With a key you can switch the state from open to close and vice versa. In our case we can turn the switch from &lt;code&gt;ON&lt;/code&gt;/&lt;code&gt;S2&lt;/code&gt; to &lt;code&gt;OFF&lt;/code&gt;/&lt;code&gt;S1&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pushdown Automaton&lt;/td&gt;
&lt;td&gt;A machine that can create a top down chain reaction of states. A mechanical calculator fits this category it is complex yet still limited in the calculations it can make. The Useless Machine in theory creates a chain but it does not move further as it doesn't have other states to change but itself but going from &lt;code&gt;ON&lt;/code&gt;/&lt;code&gt;S1&lt;/code&gt; state to &lt;code&gt;OFF&lt;/code&gt;/&lt;code&gt;S2&lt;/code&gt; state.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Turing Machine&lt;/td&gt;
&lt;td&gt;A machine that can make complex calculations and changes states. A modern computer is a Turning Machine. The Useless Machine is not even close to a Turing machine but thanks for sticking around.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;I wanted to show that even such a simple machine can teach us so much about computation. For me as a self-taught software developer it is very important to have strong visual metaphors to ground your knowledge.&lt;/p&gt;

&lt;p&gt;I hope you found this article interesting to read. Please feel free to comment or reach out if you &lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Understanding Events in Javascript</title>
      <dc:creator>Taulant Sulko</dc:creator>
      <pubDate>Fri, 07 Feb 2020 19:01:24 +0000</pubDate>
      <link>https://dev.to/taulantsulko/understanding-events-in-javascript-4k28</link>
      <guid>https://dev.to/taulantsulko/understanding-events-in-javascript-4k28</guid>
      <description>&lt;p&gt;Been doing a Javascript refresher lately and I am finally getting a good understanding of what the &lt;code&gt;addEventListener()&lt;/code&gt; method does. Most human interactions in Javascript are tracked by these Events. Understanding the Event Listener is key to designing interactions with JS.&lt;/p&gt;

&lt;h1&gt;
  
  
  What are we listening to?
&lt;/h1&gt;

&lt;p&gt;Let's say you want to listen to events on the button below.&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="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"button"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Click Me&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  How are we selecting the element?
&lt;/h1&gt;

&lt;p&gt;First you need to select the button in our javascript code. In this example below we are doing it with the &lt;code&gt;getELementById()&lt;/code&gt; selector.&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;var&lt;/span&gt; &lt;span class="nx"&gt;button&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="nx"&gt;getELementById&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  How are we listening to the element?
&lt;/h1&gt;

&lt;p&gt;Now that we have the button contained in a variable we can use the addEventListener method to start listening to those events. We want to know when it is was clicked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This won't return anything yet. It's missing 2 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="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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 the part that always seemed a bit unclear to me. What is &lt;code&gt;"click"&lt;/code&gt;? What is function(e)? How do they relate to each other? &lt;/p&gt;

&lt;p&gt;Let's break it down.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called the Event Type. This is probably the simplest one to understand. In this scenario we are waiting for a click to happen on the button element.&lt;/p&gt;

&lt;p&gt;Other event types: onlclick, onmouseenter, onmouseleave, onmouseout, onmouseover etc.&lt;/p&gt;

&lt;h1&gt;
  
  
  What happens when you "click"?
&lt;/h1&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="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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;e&lt;/code&gt; here just returns the event object and also takes the event object as an argument.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;e&lt;/code&gt; (event object) contains information about the "click" that just happened.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;e&lt;/code&gt; holds a lot of information that can help us understand what happened.&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="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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientX&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;clientY&lt;/span&gt;&lt;span class="p"&gt;);}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above will log the &lt;code&gt;X&lt;/code&gt; and &lt;code&gt;Y&lt;/code&gt; position of the mouse inside the browser when the click happened.&lt;/p&gt;

&lt;p&gt;Here is a way to test this right away in your browser console. Copy &amp;amp; Paste the code below and start clicking on the browser screen.&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;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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Event objects in the console.log
&lt;/h1&gt;

&lt;p&gt;Here is a screenshot of the above line in the browser console. &lt;/p&gt;

&lt;p&gt;Notice how a unique event object is created for each time a &lt;code&gt;"click"&lt;/code&gt; is registered.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i4kINbl_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/media/EQMcTaoWoAA2Bba%3Fformat%3Dpng%26name%3Dsmall" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i4kINbl_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/media/EQMcTaoWoAA2Bba%3Fformat%3Dpng%26name%3Dsmall" alt="" width="591" height="263"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Properties of a MouseEvent
&lt;/h1&gt;

&lt;p&gt;As you can see there is a lot of hidden information in just one click. The &lt;code&gt;MouseEvent&lt;/code&gt; object makes this information accessible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;isTrusted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="nx"&gt;screenX&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;390&lt;/span&gt;
&lt;span class="nx"&gt;screenY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;567&lt;/span&gt;
&lt;span class="nx"&gt;clientX&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;368&lt;/span&gt;
&lt;span class="nx"&gt;clientY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;433&lt;/span&gt;
&lt;span class="nx"&gt;ctrlKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="nx"&gt;shiftKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="nx"&gt;altKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="nx"&gt;metaKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="nx"&gt;relatedTarget&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="nx"&gt;pageX&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;368&lt;/span&gt;
&lt;span class="nx"&gt;pageY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;433&lt;/span&gt;
&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;368&lt;/span&gt;
&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;433&lt;/span&gt;
&lt;span class="nx"&gt;offsetX&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;361&lt;/span&gt;
&lt;span class="nx"&gt;offsetY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;308&lt;/span&gt;
&lt;span class="nx"&gt;movementX&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="nx"&gt;movementY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="nx"&gt;fromElement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="nx"&gt;toElement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;
&lt;span class="nx"&gt;layerX&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;368&lt;/span&gt;
&lt;span class="nx"&gt;layerY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;433&lt;/span&gt;
&lt;span class="nx"&gt;view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Window&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Window&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;opener&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Window&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;frames&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Window&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;…&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nl"&gt;detail&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nx"&gt;sourceCapabilities&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;InputDeviceCapabilities&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;firesTouchEvents&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="nl"&gt;which&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nx"&gt;type&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="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;
&lt;span class="nx"&gt;currentTarget&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="nx"&gt;eventPhase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="nx"&gt;bubbles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="nx"&gt;cancelable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="nx"&gt;defaultPrevented&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="nx"&gt;composed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="nx"&gt;timeStamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;8990.880000055768&lt;/span&gt;
&lt;span class="nx"&gt;srcElement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;
&lt;span class="nx"&gt;returnValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="nx"&gt;cancelBubble&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope you enjoyed the read. ✌️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
