<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Write to learn!</title>
    <description>This blog is to show my skills and write about what I've been learning in my career
</description>
    <link>http://nathpaiva.com.br/</link>
    <atom:link href="http://nathpaiva.com.br/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Thu, 14 Sep 2023 01:59:20 +0000</pubDate>
    <lastBuildDate>Thu, 14 Sep 2023 01:59:20 +0000</lastBuildDate>
    <generator>Jekyll v3.9.3</generator>
    
      <item>
        <title>Answering an interview question - 01</title>
        <description>&lt;h1 id=&quot;post-summary&quot;&gt;Post summary&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#answering-an-interview-question---01&quot;&gt;Answering an interview question - 01&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#the-challenge&quot;&gt;The challenge&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#audio-api&quot;&gt;Audio API&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#loop&quot;&gt;Loop&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#what-the-promise-does-for-us&quot;&gt;Promise&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#thats-all&quot;&gt;That’s all&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#link-references&quot;&gt;Link references&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;answering-an-interview-question---01&quot;&gt;Answering an interview question - 01&lt;/h1&gt;

&lt;p&gt;Hey there, in this post I want to share learns about &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&quot; target=&quot;_blank&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement/Audio&quot; target=&quot;_blank&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Audio API&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A few years ago, I did a technical interview where the interviewer asked me to play a song from an array. Well, I miserably failed, and it wasn’t because I didn’t know about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt; or the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Audio API&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I failed because when I’m being tested by someone, my brain can’t process the challenge as I wish 🫠. This is an issue I have in my life in many situations. So, regarding code, this blog is a safe place to develop my confidence and share what I love to do. Maybe in the future, I will be more prepared for those “silly” questions &lt;em&gt;(I hate an interview process. Who doesn’t?)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Anyway, let’s start talking about code. Because this is what we love to do, at least I do love hahahahaha. 😂&lt;/p&gt;

&lt;h2 id=&quot;the-challenge&quot;&gt;The challenge&lt;/h2&gt;

&lt;p&gt;So, basically the challenge is to create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;function&lt;/code&gt; that receives an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;array&lt;/code&gt; of song paths (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt;) and plays each song sequentially. The next song must be played only after the previous one has finished. To resolve this challenge, we must know &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Audio API&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loops&lt;/code&gt; and asynchronous functions (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt;).&lt;/p&gt;

&lt;h3 id=&quot;audio-api&quot;&gt;Audio API&lt;/h3&gt;

&lt;p&gt;First of all, we have to know that the latest browsers don’t let us play media automatically after the page renders. &lt;a href=&quot;https://developer.chrome.com/blog/autoplay/&quot; target=&quot;_blank&quot;&gt;We got this exception&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href=&quot;https://developer.chrome.com/blog/autoplay/&quot; target=&quot;_blank&quot;&gt;(index):245 Uncaught (in promise) DOMException: play() failed because the user didn’t interact with the document first.&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The user must do some interaction with the page before the script starts the song. So, to make this work first we need to make this interaction happen.&lt;/p&gt;

&lt;p&gt;In our HTML file, we can just create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;button&lt;/code&gt;, and then create the action to start.&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;button&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Play&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;On the JS side, we can add a listener for the button element.&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// first get the button from the DOM&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;buttonElement&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;querySelector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// with this element we can add the addEventListener for the `click` type.&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;buttonElement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;this message will show after click&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Inside the button action, we can trigger the function to load each file and then play the song. Therefore, we can start the function creation which will receive the array and play each song. For this stage we want only to log the file name.&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;autoPlaylist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;song&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;🚀 song:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;song&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;buttonElement&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;querySelector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;buttonElement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;/samples/sample-01.mp3&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;/samples/sample-02.mp3&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;/samples/sample-03.mp3&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;autoPlaylist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;loop&quot;&gt;Loop&lt;/h3&gt;

&lt;p&gt;As we can see on the code above, the loop chosen to log each value is the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration&quot; target=&quot;_blank&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt;&lt;/a&gt;, we could also use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while&lt;/code&gt; or any functional array method such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter,&lt;/code&gt; but not &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forEach&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;First, we didn’t choose any functional array method because after playing the song we won’t need any new array.&lt;/p&gt;

&lt;p&gt;So, why are we not using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forEach&lt;/code&gt;? Basically, in the next step we have to play our audio, and we can’t play all at the same time. We have to run it asynchronously. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forEach&lt;/code&gt; method has a different behaviour for async, and instead of waiting to finish the async code and run the next item, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forEach&lt;/code&gt; resolves all at once.&lt;/p&gt;

&lt;p&gt;If we just use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Audio API&lt;/code&gt; and call the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;play&lt;/code&gt; method without changing our function to run as async, all songs will play at the same time. Let’s take a look at the following code:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;autoPlaylist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;song&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// start a new instance of Audio with the song path as param&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;track&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Audio&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;song&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// play the track&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this code, the result we will have is all songs playing together. Because the loop doesn’t know whether the song has finished or not, nor the content which is running inside it. And well, we don’t want it, right? So now it’s time to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt;, and let our loop know if should go to the next song or not. The question now is:&lt;/p&gt;

&lt;h3 id=&quot;what-the-promise-does-for-us&quot;&gt;What the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt; does for us?&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt;&lt;/a&gt; is an object that gives us methods which we can call whether an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; action finishes successfully or not. We have to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt; if we have to execute something that takes a while to complete and after completion we have to run the next action. With the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.resolve()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.reject()&lt;/code&gt; methods our code will understand exactly when the execution has finished. So, let’s take a look at how it works with our songs.&lt;/p&gt;

&lt;p&gt;Previously our code was playing all songs at the same time, so we need to control it. The first thing to do is declare our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoPlaylist&lt;/code&gt; function as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt;. With this declaration, we are able to wait for an async function to finish to call the next action.&lt;/p&gt;

&lt;p&gt;Now we will use &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function&quot; target=&quot;_blank&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt;&lt;/a&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new Promise()&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// declare it is an async function&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;autoPlaylist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;song&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;audioList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/**
     * create a new Promise instance and wait for the resolution
     * the Promise constructor gives an executor function.
     * And it receives two functions as params the `resolve` and `reject`.
     * Which we will define that the audio finished.
     *
     * Now inside the executor function we can start the audio
     * */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;_reject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;cm&quot;&gt;/**
       * start the new instance of Audio:
       * As we are using before this instance give some methods in order to work
       * properly with the audio
       * */&lt;/span&gt;
      &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;track&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Audio&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;song&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// with the instance we can play the song&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// and we can add a listener check when its ends&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ended&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;cm&quot;&gt;/**
         * when the song has finished we can use the `resolve` function
         * from the Promise executor to finish the asynchronous code
         * and move on to the next action
         * */&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;thats-all&quot;&gt;That’s all&lt;/h2&gt;

&lt;p&gt;Phew… Ya, it’s a lot. But now we have our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoPlaylist&lt;/code&gt; playing each song sequentially and not all at the same time. Of course, we can break down the code above to have better organization, but after all the main concept is that.&lt;/p&gt;

&lt;p&gt;To conclude, in this article we saw &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Audio API&lt;/code&gt;. Sometimes working with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt; in run time while we have to wait for something else to continue could not be easy. Nevertheless, now we can understand a bit more how it works. Following, I’m adding the list of documentations I used to create it, and also the demo with the example created here.&lt;/p&gt;

&lt;h2 id=&quot;link-references&quot;&gt;Link references&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API&quot; target=&quot;_blank&quot;&gt;Audio API&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector&quot; target=&quot;_blank&quot;&gt;querySelector&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration&quot; target=&quot;_blank&quot;&gt;loop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map&quot; target=&quot;_blank&quot;&gt;map&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce&quot; target=&quot;_blank&quot;&gt;reduce&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter&quot; target=&quot;_blank&quot;&gt;filer&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach&quot; target=&quot;_blank&quot;&gt;forEach&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&quot; target=&quot;_blank&quot;&gt;Promise&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function&quot; target=&quot;_blank&quot;&gt;async await&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gist.github.com/joeytwiddle/37d2085425c049629b80956d3c618971&quot; target=&quot;_blank&quot;&gt;Do not use forEach with async-await&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://codesandbox.io/s/autoplaylist-x36jc3?file=/src/index.mjs&quot; target=&quot;_blank&quot;&gt;Demo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks, see you 👋.&lt;/p&gt;
</description>
        <pubDate>Thu, 07 Sep 2023 18:49:00 +0000</pubDate>
        <link>http://nathpaiva.com.br/javascript/2023/09/07/answering-an-interview-question-01.html</link>
        <guid isPermaLink="true">http://nathpaiva.com.br/javascript/2023/09/07/answering-an-interview-question-01.html</guid>
        
        <category>javascript</category>
        
        <category>interview</category>
        
        
        <category>javascript</category>
        
      </item>
    
      <item>
        <title>I'm back</title>
        <description>&lt;h1 id=&quot;hello-im-back&quot;&gt;Hello, I’m back!&lt;/h1&gt;

&lt;p&gt;This is my first post after a long time, and I’m really excited about it. I’m back to the blog because I want to develop my writing skills and also be prepared to do interviews in the future.&lt;/p&gt;

&lt;p&gt;Last year I’ve moved to Canada, and I was working as as Senior Software Engineer in a new product in a start up, unfortunately the product didn’t work out, and I was laid off. So I’ve decided to take some time off to study and improve my skills and be prepared for my next role.&lt;/p&gt;

&lt;p&gt;Sometimes interview scares me a lot. Even though I’ve been working with software development for a long time, on interviews I freeze and I can’t show my abilities as I should do. So I’ve decided to start writing about what I’m studying and what I’m learning, and also share some tips about how to prepare for it.&lt;/p&gt;

&lt;p&gt;So the next posts I will be answering some questions that I’ve been asked in interviews, and also some tips about how to prepare for it.&lt;/p&gt;

&lt;p&gt;I hope you enjoy it, and if you have any questions or suggestions, please let me know.&lt;/p&gt;

&lt;p&gt;Take care, and see you soon! 👋&lt;/p&gt;
</description>
        <pubDate>Fri, 04 Aug 2023 18:20:00 +0000</pubDate>
        <link>http://nathpaiva.com.br/general/2023/08/04/im-back.html</link>
        <guid isPermaLink="true">http://nathpaiva.com.br/general/2023/08/04/im-back.html</guid>
        
        <category>general</category>
        
        
        <category>general</category>
        
      </item>
    
      <item>
        <title>operadores</title>
        <description>&lt;h1 id=&quot;operadores-em-javascript&quot;&gt;Operadores em Javascript&lt;/h1&gt;

&lt;p&gt;Estou de volta para escrever mais sobre o JS! Quando tava estudando o curso do Daciuk cai no conteúdo sobre operadores, cara o impressionante é que nunca parei para pensar em operadores, eu simplesmente só saía usando eles sem entender as diferenças, os seus nomes e o entendimento de para que realmente serve!&lt;/p&gt;

&lt;p&gt;Gente isso é tão ruim, não façam isso! Mas voltando… vou falar dos operadores em Javascript e bom, temos alguns tipos de operadores, vou listar todos e tentar explicar um pouco sobre cada um.&lt;/p&gt;

&lt;p&gt;Os operadores são:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Operador de atribuição&lt;/li&gt;
  &lt;li&gt;Operador de comparação&lt;/li&gt;
  &lt;li&gt;Operador aritmético&lt;/li&gt;
  &lt;li&gt;Operadores lógicos&lt;/li&gt;
  &lt;li&gt;Operadores de string&lt;/li&gt;
  &lt;li&gt;Operadores condicionais&lt;/li&gt;
  &lt;li&gt;Operador vírgula&lt;/li&gt;
  &lt;li&gt;Operador unário&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;operador-de-atribuição&quot;&gt;Operador de atribuição&lt;/h2&gt;

&lt;p&gt;Esse é o mais fácil e eu nunca tinha parado para pensar que ele era um operador, esse operador é o &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Com esse operador você consegue dizer que o valor que está à direita terá o mesmo valor que está sendo setado.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// valor de x é 1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;operador-de-comparação&quot;&gt;Operador de comparação&lt;/h2&gt;

&lt;p&gt;Esse operador como o nome diz faz a comparação dos valores para gente. Quando usamos ele a resposta que é retornada como verdadeiro ou falso o famoso boolean true ou false.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Mas ainda podemos comparar se o primeiro valor é maior ou menor.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Podemos usar dois operadores também, para saber se ele é maior ou igual ou menor ou igual.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Esse operador no dia a dia ajuda muito a gente além de fazer essas comparações que eu citei ele ajuda a ver se além de ser o que precisamos se ele é do mesmo tipo ou não.&lt;/p&gt;

&lt;p&gt;Como assim? No javascript temos vário tipos de
Então se quisermos saber se dois elementos são iguais podemos fazer assim:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;😱 como assim? Bom basicamente no javascript o primeiro valor é um tipo número e o segundo valor é um tipo string, por mais que os dois sejam 1 o tipo deles é diferente, então quando fazemos essa comparação estamos vendo também se eles têm o mesmo tipo!&lt;/p&gt;

&lt;h2 id=&quot;operador-aritmético&quot;&gt;Operador aritmético&lt;/h2&gt;

&lt;p&gt;Esse tipo de operador tornam dois ou mais valores um único resultado, sendo ele &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt; ou &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;number&lt;/code&gt;, sendo possível até mesmo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt; e &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;number&lt;/code&gt;.
Os principais operadores aritméticos são &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; e &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;. Esse operadores funcionam como na matemática, adição, subtração, multiplicação e divisão.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// retorna 4&lt;/span&gt;
&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;oi pessoal&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// retorna oi pessoal4&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// retorna 9&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// retorna 2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Quando usamos o operador de adição para string chamamos isso de concatenação no javascript, nada mais é do que juntar valores.&lt;/p&gt;

&lt;p&gt;Também temos o operador &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&lt;/code&gt;, que é o módulo, esse operador retorna o resto de uma divisão.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// retorna 2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;operador-lógico&quot;&gt;Operador lógico&lt;/h2&gt;

&lt;p&gt;Esse tipo de operador é usado com valores Boolean que retornam um valor Boolean verdadeiro. O operador lógico compara dois ou mais valores e vê se eles são verdadeiros. Temos como operador lógico o &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; e o &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;||&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;O &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; vê se as duas expressões comparadas são verdadeiras, já o &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;||&lt;/code&gt; vê se uma das expressões são verdadeiras.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Nesse caso estamos vendo se a primeira expressão temos um número e se ele é do valor 1, o que é verdadeiro e no segundo vemos se a expressão temos um numero e se ele é do valor 2 o que também é verdadeiro. Sendo assim essa comparação lógica &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; é true.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Nesse caso estamos vendo se a primeira expressão temos um número e se ele é do valor 2, o que é falso e no segundo vemos se a expressão temos um numero e se ele é do valor 3 o que é verdadeiro. Sendo assim essa comparação lógica &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;||&lt;/code&gt; é true, pois uma das expressões comparadas é verdadeira.&lt;/p&gt;

&lt;p&gt;Ainda temos como expressão lógica o &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt; que negativa a nossa pergunta.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ou&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Essa duas negações nos retorna true pois a comparação não é verdadeira mais transforma para true&lt;/p&gt;

&lt;h2 id=&quot;operador-de-string&quot;&gt;Operador de string&lt;/h2&gt;

&lt;p&gt;Como já mencionamos no operador aritmético, podemos “somar” número com string, ou até mesmo string com string e como falei lá isso se chama concatenar.&lt;/p&gt;

&lt;p&gt;Mas também podemos concatenar de uma outra forma sem ser como já foi feito.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;minhaString&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;alfa&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;minhaString&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;beto&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// É avaliada como &quot;alfabeto&quot; e atribui este valor a minhastring.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;operador-condicional&quot;&gt;Operador condicional&lt;/h2&gt;

&lt;p&gt;O operador condicional utiliza três operandos. Ele faz primeiro uma verificação para depois mostrar um resultado ou o outro.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;idade&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Adulto&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Menor&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Nesse caso, se idade for maior ou igual a 18, ele fala que o status é adulto, se não ele é menor.&lt;/p&gt;

&lt;h2 id=&quot;operador-vírgula&quot;&gt;Operador vírgula&lt;/h2&gt;

&lt;p&gt;Esse operador eu nunca tinha pensando que seria um operador, rsrs, ele serve para poder setar varias variáveis declarando uma única vexa palavra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt;. Isso é muito utilizado dentro de &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ola&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Ou ainda no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt;…&lt;/p&gt;

&lt;p&gt;for (var i = 0, j = 9; i &amp;lt;= 9; i++, j–) {
  console.log(“a[” + i + “][” + j + “]= “ + a[i][j]);
}&lt;/p&gt;

&lt;h2 id=&quot;operador-unário&quot;&gt;Operador unário&lt;/h2&gt;

&lt;p&gt;O operador unário é uma operação com um operador.&lt;/p&gt;

&lt;p&gt;Temos alguns operadores do tipo unário:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delete&lt;/code&gt;
Ele apaga um objeto, uma propriedade de um objeto ou um elemento no índice especificado de um array.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pessoa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;na&quot;&gt;nome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;‘&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Nath&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;’&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;na&quot;&gt;idade&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;delete&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pessoa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;idade&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pessoa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;
	&lt;span class=&quot;na&quot;&gt;nome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;‘&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Nath&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;’&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;na&quot;&gt;idade&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;na&quot;&gt;nome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;‘&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Tag&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;’&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;na&quot;&gt;idade&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;28&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}];&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;delete&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pessoa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;typeof&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Ele retorna uma string indicando o tipo do operando.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;“&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Nath&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;”&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;idade&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// retorna “string”&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;idade&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;// retorna “number”&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Ele especifica que uma expressão deve ser avaliada sem retorno de valor.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;javascript:void(document.form.submit())&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Enviar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/a&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;fim&quot;&gt;Fim…&lt;/h2&gt;

&lt;p&gt;Esses são os operadores que existem, além deles ainda temos o relacional e operador bit a bit, que ainda preciso entender de verdade como funciona. Muitas das coisas que li e aprendi estudando sobre operador é que usava muito todos eles e alguns não sabia que era um tipo de operador. Foi bem bacana ler e aprender sobre esse assunto.&lt;/p&gt;

&lt;p&gt;Vou voltar a escrever sobre operadores assim que tiver uma compreensão maior sobre os que faltaram… rsrs&lt;/p&gt;

&lt;h3 id=&quot;referencia&quot;&gt;Referencia:&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Guide/Expressions_and_Operators&quot; target=&quot;blank&quot;&gt;Operadores&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 04 Dec 2017 22:02:40 +0000</pubDate>
        <link>http://nathpaiva.com.br/javascript/2017/12/04/operadores.html</link>
        <guid isPermaLink="true">http://nathpaiva.com.br/javascript/2017/12/04/operadores.html</guid>
        
        <category>javascript</category>
        
        
        <category>javascript</category>
        
      </item>
    
      <item>
        <title>var, const e let</title>
        <description>&lt;p&gt;Primeiro post vou falar de variável e mostrar uma pequena (bem pequena mesmo) diferença entre &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; e &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;let&lt;/code&gt;. Depois vou fazer outro post para abordar bem a diferença entre eles.&lt;/p&gt;

&lt;p&gt;Como tudo no javascript, variável pode ser explicado com a matemática…&lt;/p&gt;

&lt;p&gt;Na matemática quando temos que resolver um problema atribuímos o dado do problema a uma nomenclatura, para ajudar na resolução (ao menos eu fazia isso rsrs).&lt;/p&gt;

&lt;p&gt;Ex. Maria tem 10 balas e quer dividir com suas 2 amigas, quantas balas cada amiga vai receber?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;quantidade&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;de&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;balas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;quantidade&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;de&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;amigas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;quantidade&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;de&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;balas&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;por&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;amiga&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;passamos&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;um&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pouco&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;adiaente&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;já&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fazendo&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cálculo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Como que ficaria isso se fosse no JS?&lt;/p&gt;

&lt;p&gt;É muito simples, bastaria a gente colocar a chave que denomina que é uma variável, a palavra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt;, no lugar de &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quantidade de&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;balas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;amigas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;quantidadeDeBalasPorAmigas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;balas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;amigas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 5 balas por amiga&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Sim, podemos calcular as variáveis 🙃.&lt;/p&gt;

&lt;p&gt;Sendo assim nós colocamos a palavra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; para lembrar que ela é um elemento do nosso código que vamos utilizar em breve para fazer alguma ação, como no exemplo. Guardamos a quantidade de balas, depois a quantidade de amigas para depois calcular e guardar em uma próxima variável o valor total esperado.&lt;/p&gt;

&lt;p&gt;Beleza, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; é tranquilo já sabemos usar maaas o ES6 está ai e queremos saber, qual a diferença dela com &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; e &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;let&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Nossa querida &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; tem alguns problemas que vou abordar em posts mais pra frente e alguns dos problemas conseguimos resolver usando as novas atribuições. Por que?&lt;/p&gt;

&lt;p&gt;Começando com o &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt;, basicamente se nós quisermos acessar a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var balas&lt;/code&gt; e trocar o valor dela a qualquer momento, no JS nós podemos fazer isso. Nem sempre isso é bom pode acarretar alguns problemas, podemos esquecer que já usamos uma &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; no script e podemos reescrever e bom, o &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; ele veio para nos ajudar com isso.&lt;/p&gt;

&lt;p&gt;Quando usamos &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; nós podemos escrever ele somente uma vez e assim garantimos que o valor dele sempre será o mesmo.&lt;/p&gt;

&lt;p&gt;Já o &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;let&lt;/code&gt; ele pode ser reescrito, porém ele garante que no mesmo script só existe um único valor declarado.&lt;/p&gt;

&lt;p&gt;Sim pessoal, ainda temos muitas diferenças com o &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt; e &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;let&lt;/code&gt; além de saber mais sobre como é o comportamento de &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; dentro do script. Acredito que para começar está bacana! 😬 Comentem! 😜😘&lt;/p&gt;
</description>
        <pubDate>Mon, 13 Nov 2017 21:39:40 +0000</pubDate>
        <link>http://nathpaiva.com.br/javascript/2017/11/13/var-const-let.html</link>
        <guid isPermaLink="true">http://nathpaiva.com.br/javascript/2017/11/13/var-const-let.html</guid>
        
        <category>javascript</category>
        
        
        <category>javascript</category>
        
      </item>
    
      <item>
        <title>Por que criei o blog?</title>
        <description>&lt;h1 id=&quot;javascript-é-️&quot;&gt;JavaScript é ❤️&lt;/h1&gt;

&lt;p&gt;O primeiro post tem que ser sobre -o porque eu criei o blog… IOhIHOI 🐽-. Nem sei porque eu tenho que falar sobre isso, na verdade acho que só eu vou ler, mas ok… Vai que alguém aparece por aqui, não é mesmo?&lt;/p&gt;

&lt;p&gt;Minha ideia é conseguir absorver o máximo que eu posso das coisas que estudo e acredito muito que publicando algo num blog pessoal possa me ajudar.&lt;/p&gt;

&lt;p&gt;Hoje o que estou estudando e o que quero me aperfeiçar é JavaScript, simplesmente porque eu o amo.&lt;/p&gt;

&lt;p&gt;Não posso dizer que eu manjo MUITO, porém eu tenho noção que sei bastante sobre ele, o que me chateia muitas vezes é o fato de não saber explicar algo. Acredito que conseguindo escrever um pouco sobre o que esteja estudando possa me ajudar nisso.&lt;/p&gt;

&lt;p&gt;Tenho certeza que escrevendo num blog vou aprender a escrever melhor -to precisando também-.&lt;/p&gt;

&lt;p&gt;É isso pessoal, minha ideia é começar dos primórdios e ir avançando.&lt;/p&gt;

&lt;p&gt;Caso tenham dúvidas sobre como pegar a base do javascript, estou lendo bastante e fazendo o curso de &lt;a href=&quot;https://www.udemy.com/curso-javascript-ninja&quot; title=&quot;Curso de Javascript Ninja do Daciuk&quot; target=&quot;_blank&quot;&gt;Javascript Ninja do Daciuk&lt;/a&gt;, que é um curso maravilhoso! ❤️&lt;/p&gt;

&lt;p&gt;O que tiverem de dúvidas podem comentar que trocaremos uma ideia!&lt;/p&gt;

&lt;p&gt;Ps. Thanks &lt;a href=&quot;http://themes.jekyllrc.org/centrarium/&quot; title=&quot;Link para o template utilizado no blog&quot; target=&quot;_blank&quot;&gt;Centrarium&lt;/a&gt; for your template!&lt;/p&gt;

&lt;p&gt;Ps2. Caso tenha algum erro de escrita ou coisa parecida, acho legal abrir um PRzinha, por que não? rsrs&lt;/p&gt;
</description>
        <pubDate>Tue, 07 Nov 2017 08:43:59 +0000</pubDate>
        <link>http://nathpaiva.com.br/javascript/2017/11/07/porque-do-blog.html</link>
        <guid isPermaLink="true">http://nathpaiva.com.br/javascript/2017/11/07/porque-do-blog.html</guid>
        
        <category>javascript</category>
        
        
        <category>javascript</category>
        
      </item>
    
  </channel>
</rss>
