<?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: Eduardo Henao</title>
    <description>The latest articles on DEV Community by Eduardo Henao (@behuti).</description>
    <link>https://dev.to/behuti</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%2F1062071%2F2672543d-0881-4109-82f3-be1cdd72dfde.jpg</url>
      <title>DEV Community: Eduardo Henao</title>
      <link>https://dev.to/behuti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/behuti"/>
    <language>en</language>
    <item>
      <title>var vs let vs const</title>
      <dc:creator>Eduardo Henao</dc:creator>
      <pubDate>Thu, 11 Apr 2024 14:26:18 +0000</pubDate>
      <link>https://dev.to/behuti/var-vs-let-vs-const-51p3</link>
      <guid>https://dev.to/behuti/var-vs-let-vs-const-51p3</guid>
      <description>&lt;p&gt;This is one of the most common questions you'll face in an interview, and it is very important to know that there are 3 ways to declare variables (and 1 more) in JavaScript.&lt;/p&gt;

&lt;p&gt;It's common to start by the bad practice, but in this occasion, I'll start by the good, so in that way you already know what should be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  You should be always use &lt;strong&gt;let&lt;/strong&gt; and &lt;strong&gt;const&lt;/strong&gt;.
&lt;/h2&gt;

&lt;p&gt;This is going to make sense in a couple of minutes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's start with &lt;strong&gt;let&lt;/strong&gt;.
&lt;/h3&gt;

&lt;p&gt;This word allows you to declare a variable, which its value can change (reassignment). And cannot be re-declared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myName = "Eduardo"; // Variable declaration 🌱
myName = "Octavio"; // Variable Reassignment

let myName = "Sarah"; // ❌ Re-declaration: This produces an error 

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Now... what happens with &lt;strong&gt;const&lt;/strong&gt;?
&lt;/h3&gt;

&lt;p&gt;It's the just same behavior than &lt;strong&gt;let&lt;/strong&gt;, but you cannot reassign the value of a constant, as its name says it should be constant.&lt;/p&gt;

&lt;p&gt;let's see how it works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myIdNumber = 2930493843; // constant declaration 🌱

myIdNumber = 12345678974; // ❌ Reassignment of a const: This produces an error

const myIdNumber = 45678215687; // ❌ Re-declaration: This produces an error 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! Now you know what's the difference between let and const, but... What happens with &lt;strong&gt;var&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;Well, this is something related with 2 main things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Scope&lt;/li&gt;
&lt;li&gt;The re-declaration&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;var&lt;/strong&gt; was the very first option that JavaScript had to create variables, but it has some caveats. This is a pretty opiniated topic and you'll find that some people defend this, and that's okay. But you need to understand the risk of having those "weird" behaviors to be prepared to deal with them.&lt;/p&gt;

&lt;h2&gt;
  
  
  var has a &lt;em&gt;global scope&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;The first thing you need to know about &lt;strong&gt;var&lt;/strong&gt; is that its scope is global, how is that?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function someFancyCalcFunction() {
  var x = 293847191237413;
  console.log(x); // 293847191237413
}

console.log(x); // 293847191237413 
/* 
You have access to the variable value 
out of the function where it was initially declared.
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's analyze the previous snippet, I have created a function and I have declared a variable inside of it, but here is the "non-good" part and it is that I'm being able to console it out of the function scope. And the non-good doesn't end there, I can modify the value outside of the function which can lead id pretty lexical complexity and hard to read and you can lose pretty easily the idea of why that variable was created and where I should stop using it.&lt;/p&gt;

&lt;h2&gt;
  
  
  var can be &lt;em&gt;redeclared&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;This is one of the most dangerous features of &lt;strong&gt;var&lt;/strong&gt; usage. And that's because you can redeclare a variable with the same name. Imagine that you create a variable at the very beginning of your code, and you use that variable, but you forget about the name, and the value of that variable. and then 5000 lines after (this is a pretty bad practice, but I just want to create a scenario) you create another variable with the same name. Whit var that's possible and that can lead in several problems, specifically if you are working in a dev team.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myIdNumber = 2930493843; // constant declaration 🌱

myIdNumber = 12345678974; // ✔ Reassignment is allowed

var myIdNumber = 45678215687; // ✔ Re-declaration is allowed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>essentials</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What the f#$k is strict equality.</title>
      <dc:creator>Eduardo Henao</dc:creator>
      <pubDate>Wed, 10 Apr 2024 14:06:24 +0000</pubDate>
      <link>https://dev.to/behuti/what-the-fk-is-strict-equality-58eo</link>
      <guid>https://dev.to/behuti/what-the-fk-is-strict-equality-58eo</guid>
      <description>&lt;p&gt;Sometimes I see some memes regarding Javascript equality and yeah, some of them are pretty funny, here is my favorite.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9726sw3ikldgmlmawwir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9726sw3ikldgmlmawwir.png" alt="Image description" width="500" height="760"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I see that there is a common pattern, this "weird" behavior always happens with the double equal sign operator &lt;code&gt;==&lt;/code&gt; which we can call &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#loose_equality_using"&gt;loose equality&lt;/a&gt;. This happens because it perfoms the &lt;a href="https://tc39.es/ecma262/multipage/abstract-operations.html#sec-islooselyequal"&gt;IsLooselyEqual&lt;/a&gt; algorithm, and the Javascript engine will perform a typer cohertion to try to match both sides in type and value.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can avoid these "weird" behavior?
&lt;/h2&gt;

&lt;p&gt;Well when you use the triple equal operator &lt;code&gt;===&lt;/code&gt; javascript doesn't convert at all the type of your values, and then performs a comparison and checks that both values and types, and if both are for example the same number and the same type, the result of the comparison will be 'true'.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>booleans</category>
      <category>comparison</category>
    </item>
  </channel>
</rss>
