<?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: Vinicio Vladimir Sanchez Trejo</title>
    <description>The latest articles on DEV Community by Vinicio Vladimir Sanchez Trejo (@vinicio).</description>
    <link>https://dev.to/vinicio</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%2F148952%2F87c11058-f116-4808-810d-506efa277cd5.png</url>
      <title>DEV Community: Vinicio Vladimir Sanchez Trejo</title>
      <link>https://dev.to/vinicio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vinicio"/>
    <language>en</language>
    <item>
      <title>The Secret of a Successful Code Challenge Interview: Having a plan</title>
      <dc:creator>Vinicio Vladimir Sanchez Trejo</dc:creator>
      <pubDate>Mon, 14 Jun 2021 15:57:38 +0000</pubDate>
      <link>https://dev.to/vinicio/the-secret-of-a-successful-code-challenge-interview-having-a-plan-2ek0</link>
      <guid>https://dev.to/vinicio/the-secret-of-a-successful-code-challenge-interview-having-a-plan-2ek0</guid>
      <description>&lt;p&gt;You know; I used to think I could show up to an interview and ace a code challenge without any extra preparation; besides my computer science studies and my daily coding sessions.&lt;/p&gt;

&lt;p&gt;I was...greatly mistaken. After very disappointing interviews, it was obvious something was wrong. I knew I could code, so what was the problem? Well, I discovered several problems, but one of the biggest ones was that I didn't have a process! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J_lqySk8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/596bmton1johmw1kt3xc.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J_lqySk8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/596bmton1johmw1kt3xc.jpeg" alt="Me during my first whiteboard interview"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Over several years I developed an interview process that allowed me to: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Feel more confident during code challenges (and...avoid panicking as much haha).&lt;/li&gt;
&lt;li&gt;Increase the chances of arriving at a working solution.&lt;/li&gt;
&lt;li&gt;Showcase my skills even if I can't produce working code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Today, I want to share my process with you :)&lt;/p&gt;

&lt;h2&gt;
  
  
  The Big Picture
&lt;/h2&gt;

&lt;p&gt;You are going to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Understand the problem&lt;/li&gt;
&lt;li&gt;Experiment by solving several examples&lt;/li&gt;
&lt;li&gt;Identify an algorithm&lt;/li&gt;
&lt;li&gt;Optimize your algorithm&lt;/li&gt;
&lt;li&gt;Write code (Notice how this is step 5 out of 7)&lt;/li&gt;
&lt;li&gt;Discuss possible improvements&lt;/li&gt;
&lt;li&gt;Be proud! We did it!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's add more detail on how this system works; but before that, two important points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is a flexible process; play with it and make it your own. As a general guideline; the more confused I am in an interview, the more I rely on the process.&lt;/li&gt;
&lt;li&gt;You can download a nice cheatsheet of the process by clicking &lt;a href="https://www.cadejo.dev/free-updates/?source=interview-process-diagram"&gt;here!&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With that said. Let's do this!&lt;/p&gt;

&lt;h2&gt;
  
  
  1 - Understanding the Problem
&lt;/h2&gt;

&lt;p&gt;This is the most important step in the entire process. It was born out of my biggest interview mistakes. Many people start writing code as soon as the interviewer finishes describing the problem. &lt;/p&gt;

&lt;p&gt;I think that's one of the worst mistakes you could make in a code challenge. Here's why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you misunderstood the problem, you are going to waste precious time whilst getting back on track.&lt;/li&gt;
&lt;li&gt;You are trying to solve the problem as you code, and, what's worse, you didn't give your brain enough time to come up with a solution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What do I do then?
&lt;/h3&gt;

&lt;p&gt;Luckily for us, there is an effective way to avoid this pitfall: &lt;strong&gt;carefully examine the problem and identify some key details:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Goals&lt;/li&gt;
&lt;li&gt;Inputs &amp;amp; Outputs&lt;/li&gt;
&lt;li&gt;Scope&lt;/li&gt;
&lt;li&gt;Assumptions&lt;/li&gt;
&lt;li&gt;Constraints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ask a healthy amount of questions if anything seems unclear. These details will allow you to create a solid algorithm without annoying bugs blindsiding you as you code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Re-stating the problem
&lt;/h3&gt;

&lt;p&gt;Before moving to the next step, it's useful to re-state the challenge to the interviewer.  Think about this as a final chance for you or the interviewer to catch any misunderstandings.&lt;/p&gt;

&lt;h2&gt;
  
  
  2 - Experimenting with Examples
&lt;/h2&gt;

&lt;p&gt;Is it time to think about &lt;code&gt;for&lt;/code&gt; loops? Nope. It's time to visually solve 1-3 regular examples and 1-3 edge cases. &lt;/p&gt;

&lt;h3&gt;
  
  
  ...Why? I need to get to the code dude!
&lt;/h3&gt;

&lt;p&gt;Here is the thing:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Your brain is already an amazing problem-solving supercomputer. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You may be doing yourself a disservice by immediately thinking about loops and variables. You could even end up coding a faulty implementation.&lt;/p&gt;

&lt;p&gt;By spending some time visually solving examples, you are going to give your brain enough time to recognize patterns that can be used to solve the problem. These patterns are harder to see if you are already thinking about how to name your variables or other implementation details.&lt;/p&gt;

&lt;p&gt;Also, if you have absolutely no idea about how to solve a problem, you are doing something much more productive than just staring at the problem and thinking really, &lt;em&gt;really&lt;/em&gt; hard. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--57f3hlqp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke71r5yuiss7kcjzb7to.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--57f3hlqp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke71r5yuiss7kcjzb7to.jpg" alt="Thinking really really hard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how I'm asking you to equally divide your time between regular cases and edge cases. Regular cases are used to identify patterns and edge cases are used to make sure we have a solid algorithm. I often see interviewees neglect one or the other. They are equally valuable!&lt;/p&gt;

&lt;h2&gt;
  
  
  3 - Identifying an Algorithm
&lt;/h2&gt;

&lt;p&gt;Take a look at the previous step. Think about the process your brain followed to solve the examples. What are the common steps? Can you think of a way to express those steps in a way that can be 'mechanized' by the computer? &lt;/p&gt;

&lt;p&gt;Notice how I didn't call this step &lt;em&gt;'Identifying an Optimal Algorithm'&lt;/em&gt;. For now, I'm asking you to focus on finding something that works, and later, you'll focus on optimizations.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I keep track of my algorithm?
&lt;/h3&gt;

&lt;p&gt;I recommend that you document your thought process using English as much as you can. However, if you identify the most complicated (or confusing) part of the problem, you could use pseudocode to give yourself a little more clarity.&lt;/p&gt;

&lt;p&gt;Having said that, I do recommend that you stick to English since you are going to optimize your algorithm in the next step.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens if I can't identify an algorithm?
&lt;/h3&gt;

&lt;p&gt;I can (and will) write several more articles about what to do when you are stuck, but my go-to moves are to try to divide the problem into smaller problems or to reduce the problem into a simpler version.&lt;/p&gt;

&lt;p&gt;Don't worry. I'll come back soon with more material about what to do when you are stuck.&lt;/p&gt;

&lt;h2&gt;
  
  
  4 - Deciding on an Algorithm to Code
&lt;/h2&gt;

&lt;p&gt;Now that you have a solid algorithm, it's time to think about optimizations. I recommend you start by noting your current time and space complexity (also known as Big O). &lt;/p&gt;

&lt;p&gt;Once you have that, brainstorm data structures, algorithms, and functions that could improve performance. Have a conversation with the interviewer about whether they want you to optimize for time or space.&lt;/p&gt;

&lt;p&gt;Notice how I didn't say &lt;em&gt;'Finding an O(X) solution'&lt;/em&gt; or &lt;em&gt;'Finding the most optimal algorithm'&lt;/em&gt;.  Finding the best algorithm is the goal, but don't panic if you can't find it.  &lt;/p&gt;

&lt;p&gt;Spend a healthy amount of time here, select an approach, and then move on.  A working solution is more valuable than an incomplete challenge.&lt;/p&gt;

&lt;h3&gt;
  
  
  How much time should I spend here before I move to step 5?
&lt;/h3&gt;

&lt;p&gt;That's a great question! I like to give myself 10-20 minutes to write code and discuss improvements, so I recommend you spend 10-20 minutes here. (This assumes a 1-hour interview with only one problem)&lt;/p&gt;

&lt;p&gt;I'll write an article with a breakdown of recommended times by interview step, but I hope this is enough information for now.&lt;/p&gt;

&lt;h3&gt;
  
  
  It's finally time to (maybe) use pseudocode.
&lt;/h3&gt;

&lt;p&gt;As I said before, I don't recommend you always use pseudocode in code challenges, but I recommend pseudocoding areas that are particularly confusing or complicated.&lt;/p&gt;

&lt;p&gt;Why? Because pseudocode allows you to think about your algorithm from a high-level perspective; without dealing with the lower-level detail required by code. Keep pseudocode to a minimum though, and use it only to answer specific questions about your approach. &lt;/p&gt;

&lt;p&gt;Now it's time to...&lt;/p&gt;

&lt;h2&gt;
  
  
  5 - Writing Code
&lt;/h2&gt;

&lt;p&gt;FINALLY. Finally!? What great wisdom can I share with you about the "most important step" in the interview process? You'll have to go back to step 1 since that's the most important step. Here, we'll just write some code.&lt;/p&gt;

&lt;p&gt;Writing code should be the most boring part of the interview. All of the uncertainty of the challenge should have been clarified in the previous steps. If you have a lot of questions at this point, we have failed at successfully navigating the code challenge.&lt;/p&gt;

&lt;p&gt;I do recommend two things for this step though:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consistently double-check your examples to make sure that your code is not missing a vital step or edge case.&lt;/li&gt;
&lt;li&gt;Don't forget to state your code's performance. Often the interviewer will remind you about it, but you can't count on that.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6 -Discussing Improvements
&lt;/h2&gt;

&lt;p&gt;Now that you are "done" and the interview is about to end, mention possible improvements to your algorithm: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What would you change about your algorithm if you had more time to work?&lt;/li&gt;
&lt;li&gt;How would you build a unit-test suite for your algorithm?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7 - YOU DID IT!
&lt;/h2&gt;

&lt;p&gt;You did it! High Five! If everything worked out you should have had a successful code challenge interview. Maybe even a job offer in a couple of days!&lt;/p&gt;

&lt;p&gt;Of course, it's easier said than done. In the next several months, I'll be going deeper into each step of this process. In the meantime, head over to cadejo.dev and grab your &lt;a href="https://www.cadejo.dev/free-updates/?source=interview-process-diagram"&gt;interview process cheatsheet!&lt;/a&gt; Also, reach out on &lt;a href="https://twitter.com/vladimirsan"&gt;social media&lt;/a&gt; to let me know how you like it! &lt;/p&gt;

&lt;p&gt;Before I go though, I want to thank all the students and teachers that helped me build this process over several years of my career. I'm standing on the shoulders of giants and  I couldn't have done it without you all!&lt;/p&gt;

</description>
      <category>career</category>
      <category>interview</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>JavaScript: Var and Let</title>
      <dc:creator>Vinicio Vladimir Sanchez Trejo</dc:creator>
      <pubDate>Sat, 13 Mar 2021 22:53:27 +0000</pubDate>
      <link>https://dev.to/vinicio/javascript-var-and-let-2d4f</link>
      <guid>https://dev.to/vinicio/javascript-var-and-let-2d4f</guid>
      <description>&lt;p&gt;Today I want to take you on an adventure where we'll discover the key differences between &lt;code&gt;var&lt;/code&gt; variables and &lt;code&gt;let&lt;/code&gt; variables.  Many introductions to coding (in Javascript) usually start with &lt;code&gt;var&lt;/code&gt; variables, but I believe you should move to &lt;code&gt;let&lt;/code&gt; variables as soon as you can. You'll have fewer bugs, and a better programming experience. Let's do this!&lt;/p&gt;

&lt;p&gt;First, I want to introduce the concept of &lt;strong&gt;Scope&lt;/strong&gt;. A definition I love to use goes like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Scope is the area of code where you can access a symbol&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A symbol, in this context, can be a variable or a function. We say that a symbol is 'within scope' when you can safely use it without any errors. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// We can log variable here because is within scope&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Things become a little bit more interesting when you are dealing with functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will not work&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;thisIsAFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will work&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will not work&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Notice how the logs outside of the function will not work (i.e. the name &lt;code&gt;variable&lt;/code&gt; is not within scope), but the log inside the function will work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is that?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; variables use what we know as &lt;strong&gt;Function-based scope&lt;/strong&gt;. If you declare a &lt;code&gt;var&lt;/code&gt; variable inside a function, the variable will be within scope everywhere inside the function.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting"&gt;Hoisting&lt;/a&gt; is going to slightly complicate where you can access your variable. In general, It's safer to use your &lt;code&gt;var&lt;/code&gt; variable only after you declare it. We'll talk about hoisting in an upcoming article, so get excited!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, let's add an if statement inside our function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will not work&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;thisIsAFunction&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="kc"&gt;true&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="c1"&gt;// This is a simple if statement to avoid confusion&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This works&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This works&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will not work&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;var&lt;/code&gt; variable is only within scope inside the function where it was declared. Notice how even though &lt;code&gt;variable&lt;/code&gt; was declared inside an if statement, you can still use it outside the statement. That's function-based scope at play!&lt;/p&gt;

&lt;p&gt;Now let's go full power and change our &lt;code&gt;var&lt;/code&gt; variable into a &lt;code&gt;let&lt;/code&gt; variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will not work&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;thisIsAFunction&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="kc"&gt;true&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="c1"&gt;// This is a simple if statement to avoid confusion&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This works&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will not work&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will not work&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Notice how as soon as we change &lt;code&gt;var&lt;/code&gt; to &lt;code&gt;let&lt;/code&gt;, one more log stops working.  &lt;/p&gt;

&lt;h2&gt;
  
  
  What's the difference between the log in line 6 and the log in line 8?
&lt;/h2&gt;

&lt;p&gt;The difference is that they are in different &lt;strong&gt;blocks&lt;/strong&gt;. If you are thinking 'Well, what's a block?, I've got you covered my friend. &lt;/p&gt;

&lt;p&gt;If you want to get super technical, a block is a "lexical structure of source code which is grouped together", but I like to introduce the concept as: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Block -  whatever is contained inside curly brackets: functions, loops, if statements, etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Objects are a funny exception to the definition I just gave about curly brackets, but that's the only exception I know of.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's think about the blocks we have in our current function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;thisIsAFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Start of block A&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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="c1"&gt;// Start of block B&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// End of block B&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// End of block A&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;variable&lt;/code&gt; was defined inside block B, it can only be used inside block B (here comes the important point) and inside every block contained within B.&lt;/p&gt;

&lt;h2&gt;
  
  
  Technically Speaking, variable is inside block A Right?. Why is the console.log breaking?
&lt;/h2&gt;

&lt;p&gt;Great point. Block B is inside Block A, so technically &lt;code&gt;variable&lt;/code&gt; was declared inside block A.&lt;/p&gt;

&lt;p&gt;However, the scope resolution rule &lt;code&gt;let&lt;/code&gt; uses is going to look for the closest enclosing block (that would be block b) and allow you to use the variable everywhere inside that block and every other block within it. &lt;/p&gt;

&lt;p&gt;Blocks containing that 'closest enclosing block' won't have access to &lt;code&gt;variable&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So what would happen if we move variable before the if statement?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;thisIsAFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Start of block A&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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="c1"&gt;// Start of block B&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// End of block B&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// End of block A&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;variable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;It would be accessible everywhere inside block A as that would be the closest enclosing block. Since block B is inside block A, it'll also be accessible inside block B. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aEXQVu9C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ck53zymw49o4pu5xgo1y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aEXQVu9C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ck53zymw49o4pu5xgo1y.jpg" alt="blocks, blocks everywhere meme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  This seems like adding a bunch of rules on top of var. What's the point?
&lt;/h2&gt;

&lt;p&gt;Great question! Blocks tend to be smaller than functions. Basing our scoping rules on smaller scopes will mean that a variable name is 'available' on a smaller area of code. &lt;/p&gt;

&lt;p&gt;A smaller area of code means it is less likely to change that variable by mistake. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RdpXkRcx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ek0t705nc0ke3crdlr3s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RdpXkRcx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ek0t705nc0ke3crdlr3s.jpg" alt="we don't make mistakes, we make happyc bugs meme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  I get it! Anything 'else?
&lt;/h2&gt;

&lt;p&gt;One last thing. &lt;code&gt;let&lt;/code&gt; also has built-in protections to avoid re-declaring the same variable by mistake.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;kali&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;is cute&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// LOTS OF CODE IN BETWEEN&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;kali&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This will break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As soon as you try to re-declare the name &lt;code&gt;kali&lt;/code&gt;, you'll get an error along the lines of  &lt;code&gt;redeclaration of identifier kali&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;On the other hand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;kali&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;is cute&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// LOTS OF CODE IN BETWEEN&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;kali&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This works just fine&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;var&lt;/code&gt; variables will let you re-declare the same name over and over without any complaint. You could end up overstepping on someone else's (or even your own) variables without even realizing it. That's another big reason to use &lt;code&gt;let&lt;/code&gt; variables as much as you can.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Scope is the area of the code where you can access a name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; variables use function-based scope. They can be used inside the function where they are defined.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; variables use block-based scope. They can be used inside the block where they are defined.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; variables will not let you re-declare the same name.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Thanks!
&lt;/h2&gt;

&lt;p&gt;Thank you very much for reading my article! I have a youtube video covering these topics if you would like a more interactive demo: &lt;a href="https://youtu.be/kIJ2Jh7Qwso"&gt;https://youtu.be/kIJ2Jh7Qwso&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like my content and would like me to help you in your coding journey, you should join my newsletter: &lt;a href="https://www.cadejo.dev/free-updates/"&gt;https://www.cadejo.dev/free-updates/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>es6</category>
      <category>javascript</category>
      <category>newbie</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
