<?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: Robiul H.</title>
    <description>The latest articles on DEV Community by Robiul H. (@robiulhr).</description>
    <link>https://dev.to/robiulhr</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%2F979860%2F2c294196-6819-4343-a56a-a05809481fb4.jpeg</url>
      <title>DEV Community: Robiul H.</title>
      <link>https://dev.to/robiulhr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/robiulhr"/>
    <language>en</language>
    <item>
      <title>JavaScript Loop Best Practices for Optimal Performance</title>
      <dc:creator>Robiul H.</dc:creator>
      <pubDate>Thu, 03 Aug 2023 16:45:35 +0000</pubDate>
      <link>https://dev.to/robiulhr/javascript-loop-best-practices-for-optimal-performance-2pem</link>
      <guid>https://dev.to/robiulhr/javascript-loop-best-practices-for-optimal-performance-2pem</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;JavaScript Loop&lt;/strong&gt; is a fundamental part of any programming language, and JavaScript is no exception. &lt;a href="https://www.w3schools.com/js/js_loop_for.asp"&gt;JavaScript loop&lt;/a&gt; is a powerful tool that allows developers to iterate over collections of data, perform operations on each item, and make decisions based on certain conditions.&lt;/p&gt;

&lt;p&gt;However, loops can quickly become a source of problems if not implemented properly. Poorly written loops can lead to performance issues, bugs, and code that is difficult to maintain.&lt;/p&gt;

&lt;p&gt;Whether you're a beginner or an experienced developer, writing efficient and effective loops can be a challenging task.&lt;/p&gt;

&lt;p&gt;In this comprehensive guide, we'll explore the best practices for writing &lt;strong&gt;JavaScript loop&lt;/strong&gt; that will help to take your code from noob to pro. We'll start with the basics, including choosing the right loop type and optimizing loop performance.&lt;/p&gt;

&lt;p&gt;Then we'll dive into more advanced topics like using functional programming techniques and being careful with the asynchronous loops to avoid weird things in your code.&lt;/p&gt;

&lt;p&gt;Whether you're working with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"&gt;arrays&lt;/a&gt;, &lt;a href="https://www.freecodecamp.org/news/objects-in-javascript/"&gt;objects&lt;/a&gt;, or other data structures, our tips and tricks will help you write clean, concise, and bug-free loops.&lt;/p&gt;

&lt;p&gt;So if you're ready to master the art of &lt;strong&gt;JavaScript loop&lt;/strong&gt; , buckle up and let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the Right Type of Loop
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I2iC8kUT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684046753665/d7006b0c-f094-43c5-b886-ebfd4ca63072.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I2iC8kUT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684046753665/d7006b0c-f094-43c5-b886-ebfd4ca63072.png" alt="Choosing the Right Type of Loop" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you are writing a loop in JavaScript, the First thing you need to do is choose the right type of loop. JavaScript offers several types of loops, including &lt;a href="https://www.programiz.com/javascript/for-loop"&gt;for loop&lt;/a&gt;, &lt;a href="https://www.javascripttutorial.net/javascript-while-loop/"&gt;while loop&lt;/a&gt;, and &lt;a href="https://www.geeksforgeeks.org/javascript-do-while-loop/"&gt;do-while loop&lt;/a&gt;, each with its own strengths and weaknesses. Depending on the specific requirements of your code, one type of loop may be more efficient or more readable than another.&lt;/p&gt;

&lt;p&gt;Therefore, it's essential to consider the situation and choose the appropriate loop type to achieve the desired results.&lt;/p&gt;

&lt;p&gt;let's enlighten them one by one:&lt;/p&gt;

&lt;p&gt;Starting with the most popular one, The &lt;code&gt;For loop&lt;/code&gt;. It's the most common type of loop in JavaScript and is often used when you know how many times you want to execute a block of code.&lt;/p&gt;

&lt;p&gt;For example, imagine you have an array of numbers and you want to calculate their sum:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Output: 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the &lt;code&gt;for loop&lt;/code&gt; is used to iterate over each element in the numbers array and add its value to the &lt;code&gt;sum&lt;/code&gt; variable. Next &lt;code&gt;While loop&lt;/code&gt;, it is useful when you want to repeatedly execute a block of code until a certain condition is met.&lt;/p&gt;

&lt;p&gt;For example, imagine you want to generate random numbers between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; until you get a number greater than &lt;code&gt;0.5&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;randomNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;randomNumber&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;randomNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;randomNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Output: a number greater than 0.5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the while loop repeatedly generates a new random number until it gets one that is greater than &lt;code&gt;0.5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next &lt;code&gt;Do-while loop&lt;/code&gt;, It's similar to &lt;code&gt;while loop&lt;/code&gt;, but the difference is that a &lt;code&gt;do-while loop&lt;/code&gt; will always execute its block of code at least once, regardless of whether the condition is initially &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, imagine you want to ask the user to enter a number between &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;10&lt;/code&gt;, and keep asking until they enter a valid number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; 
&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Enter a number between 1 and 10:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&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;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Output: a number between 1 and 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the &lt;code&gt;do-while loop&lt;/code&gt; repeatedly prompts the user to enter a number until they enter a number between &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;10&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, as you can see, choosing the right type of loop depends on the specific task you are trying to accomplish. If you know how many times you want to execute a block of code, a &lt;code&gt;for loop&lt;/code&gt; may be the best choice. If you want to repeatedly execute a block of code until a certain condition is met, &lt;code&gt;while loop&lt;/code&gt; or &lt;code&gt;do-while loop&lt;/code&gt; may be more appropriate.&lt;/p&gt;

&lt;p&gt;By selecting the right type of loop, you can make your code more efficient and easier to read.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use the Right Loop Control Statement
&lt;/h2&gt;

&lt;p&gt;After choosing the appropriate loop for your desired task, the Next thing you need to know is how to use the right loop control statement. There are two main loop control statements available in JavaScript: &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These Loop control statements allow you to control the flow of a loop. &lt;code&gt;break&lt;/code&gt; is used to exit a loop completely when a certain condition is met, On the other hand, &lt;code&gt;continue&lt;/code&gt; is used to skip the current iteration of the loop and move on to the next one.&lt;/p&gt;

&lt;p&gt;For example, let's say you have an array of numbers and you want to find the first &lt;code&gt;even&lt;/code&gt; number in the array using a for loop. You can use &lt;code&gt;break&lt;/code&gt; to exit the loop once you find the first &lt;code&gt;even&lt;/code&gt; number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;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="s2"&gt;`The first even number is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
        &lt;span class="k"&gt;break&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the loop will exit as soon as it finds the first &lt;code&gt;even&lt;/code&gt; number (which is &lt;code&gt;2&lt;/code&gt;). Without the &lt;code&gt;break&lt;/code&gt; statement, the loop would continue iterating through the rest of the array.&lt;/p&gt;

&lt;p&gt;On the other hand, let's say you want to print out all the &lt;code&gt;odd&lt;/code&gt; numbers in the array, skipping over any &lt;code&gt;even&lt;/code&gt; numbers. You can use &lt;code&gt;continue&lt;/code&gt; to skip the &lt;code&gt;even&lt;/code&gt; numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="k"&gt;continue&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="s2"&gt;`Odd number: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the loop will skip over the numbers and only print out the &lt;code&gt;odd&lt;/code&gt; numbers (which are &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt;, &lt;code&gt;5&lt;/code&gt;, and &lt;code&gt;7&lt;/code&gt;). Without the continue statement, the loop would print out all the numbers in the array.&lt;/p&gt;

&lt;p&gt;Now we know how to choose the right loop for our task also when to use &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;let's move to the next thing we need to think of when writing a loop and this is optimizing the Loop performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimizing The Loop Performance
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QBvEcrNY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684046784028/80df2221-3b42-4b0f-a501-e936397acf56.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QBvEcrNY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684046784028/80df2221-3b42-4b0f-a501-e936397acf56.gif" alt="Optimizing The Loop Performance" width="498" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In normal situations, it may not create a big change in our code performance but when we will work with a large number of data Optimizing the loop performance will become one of the most important things. Because Loop performance can have a significant impact on the overall performance of your JavaScript code.&lt;/p&gt;

&lt;p&gt;Therefore, it's essential to profile and optimize the performance of your loops.&lt;/p&gt;

&lt;p&gt;Here are some tips for optimizing loop performance:&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoiding unnecessary calculations
&lt;/h3&gt;

&lt;p&gt;One way to optimize loop performance is by avoiding unnecessary calculations.&lt;/p&gt;

&lt;p&gt;To Understand the point let's have a look at the example below:&lt;/p&gt;

&lt;p&gt;suppose you have a loop that iterates over an &lt;code&gt;array&lt;/code&gt;. In that case, when you use a for loop to iterate over an array, the &lt;code&gt;length&lt;/code&gt; of the array is checked in each iteration to see if the loop should continue.&lt;/p&gt;

&lt;p&gt;For example, consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In each iteration of the loop, &lt;code&gt;arr.length&lt;/code&gt; is checked to see if &lt;code&gt;i&lt;/code&gt; is less than the &lt;code&gt;length&lt;/code&gt; of the array.&lt;/p&gt;

&lt;p&gt;However, &lt;code&gt;arr.length&lt;/code&gt; is a property of the array, and accessing it in each iteration can be expensive if the array is very large.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: In JavaScript, an array is a special type of object that can store a collection of values of any data type. Every array has a &lt;code&gt;length&lt;/code&gt; property, which represents the number of elements in the array. This &lt;code&gt;length&lt;/code&gt; property is automatically updated as elements are added to or removed from the array. Since an array is an object, it can have properties and methods just like any other object. However, the &lt;code&gt;length&lt;/code&gt; property is unique to arrays and allows for easy access to the number of elements in the array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To improve performance, you can cache the &lt;code&gt;length&lt;/code&gt; of the array outside the loop, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="c1"&gt;// caching the length outside the loop &lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing this, you avoid accessing the &lt;code&gt;length&lt;/code&gt; property of the array in each iteration, which can improve the performance of your code. This technique can be particularly useful when working with large arrays.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: It's worth noting that modern JavaScript engines like V8 are optimized to handle loops efficiently, and in some cases, caching the length of the array may not provide a significant performance boost. However, it's still a good practice to follow, especially when working with large arrays or in performance-critical code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Use efficient loops
&lt;/h3&gt;

&lt;p&gt;Another way to optimize loop performance is by using efficient loops.&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;for loop&lt;/code&gt; is generally more efficient than the &lt;code&gt;forEach&lt;/code&gt; loop when iterating over arrays.&lt;/p&gt;

&lt;p&gt;Let's see the code snippet below to understand this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// Using for loop for iterating over the array &lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above example, we are using a &lt;code&gt;for loop&lt;/code&gt; to iterate over the &lt;code&gt;arr&lt;/code&gt; array. But the question is we could do the same using the &lt;code&gt;forEach loop&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;why this is the better approach. right?&lt;/p&gt;

&lt;p&gt;This topic is a bit complex and deserves a separate blog post to explain briefly. But one of the main reasons is When we use the &lt;code&gt;forEach&lt;/code&gt; loop, it creates a new function scope for every element in the array. This means that for each element, a new function is created, and when the iteration is done, those functions are destroyed.&lt;/p&gt;

&lt;p&gt;This process is not an issue if the array is small. However, for larger arrays, it can slow down the loop's execution.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;code&gt;for loop&lt;/code&gt; creates a single function scope for the entire loop, which means that it only has to create and destroy a single function scope.&lt;/p&gt;

&lt;p&gt;Also, &lt;code&gt;for loop&lt;/code&gt; provides a way to get control over the loop iteration using the &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; which &lt;code&gt;forEach loop&lt;/code&gt; simply doesn't provide.&lt;/p&gt;

&lt;p&gt;That's why the &lt;code&gt;for loop&lt;/code&gt; is more optimized for looping over arrays.&lt;/p&gt;

&lt;h3&gt;
  
  
  Minimize DOM manipulation
&lt;/h3&gt;

&lt;p&gt;In loops, it's important to minimize DOM manipulation as much as possible. Because, When working with the Document Object Model (DOM) in JavaScript, manipulating the DOM can be a time-consuming process.&lt;/p&gt;

&lt;p&gt;Each time you make a change to the DOM, the browser has to reflow and repaint the page, which can slow down your application, especially when working with large numbers of elements.&lt;/p&gt;

&lt;p&gt;For Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Inefficient DOM manipulation &lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myDiv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="c1"&gt;// More efficient DOM manipulation let output = '' &lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myDiv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the given example, the first loop is inefficient because it manipulates the DOM on every iteration. The &lt;code&gt;innerHTML&lt;/code&gt; property of the &lt;code&gt;myDiv&lt;/code&gt; element is updated with a new paragraph element on every iteration, resulting in a total of &lt;code&gt;1000&lt;/code&gt; manipulations of the DOM.&lt;/p&gt;

&lt;p&gt;On the other hand, The second loop is more efficient because it minimizes DOM manipulation. Instead of updating the DOM on every iteration, the loop concatenates a string of HTML to a variable called output.&lt;/p&gt;

&lt;p&gt;Once the loop is complete, the output string is used to update the &lt;code&gt;innerHTML&lt;/code&gt; property of the &lt;code&gt;myDiv&lt;/code&gt; element just once, resulting in a single manipulation of the DOM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid Common Mistakes
&lt;/h2&gt;

&lt;p&gt;There are a few common mistakes that developers normally do and you should be aware of and &lt;strong&gt;avoid common mistakes&lt;/strong&gt; when working with loops in JavaScript:&lt;/p&gt;

&lt;h3&gt;
  
  
  Modifying Loop Variables Inside the Loop
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Modifying loop variables inside the loop&lt;/strong&gt; can lead to unexpected results or errors. It's generally not a good practice to modify loop variables inside the loop. Instead, you should use a separate variable to store any values that you want to modify.&lt;/p&gt;

&lt;p&gt;For example, consider the following loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="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;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// modifying i inside the loop &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this loop, i is being modified inside the loop, which can lead to unexpected results. Instead, you should use a separate variable to store any values that you want to modify, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="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;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;modifiedValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// using a separate variable to modify i &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Not Using Curly Braces
&lt;/h3&gt;

&lt;p&gt;It's possible to write a loop without curly braces, but it's generally not recommended. Without curly braces, it can be difficult to read and understand your code, and it can also lead to unexpected results or errors.&lt;/p&gt;

&lt;p&gt;For example, consider the following loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="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;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This loop does not have curly braces, which can make it difficult to read and understand the code. It's better to always use curly braces, even for single-line statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="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;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Be careful of Infinite Loop
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;infinite loop&lt;/strong&gt; is a loop that never stops. This can happen if you don't set up the correct exit condition or if the exit condition is never met. Infinite loops can cause your program to crash or hang, and they can be difficult to debug.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;consider a loop that always increments the value of a variable but never checks if it has exceeded a certain value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This code will result in an infinite loop because the count variable is never incremented beyond &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's why you should always be careful about infinite loops. To avoid infinite loops, make sure you set up the correct exit condition and test it thoroughly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Off-by-One Errors
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Off-by-one&lt;/code&gt; &lt;strong&gt;errors&lt;/strong&gt; occur when you use the wrong comparison operator or when you don't take into account the initial value of a loop variable. These errors can lead to incorrect results in your program or even crashes in some cases.&lt;/p&gt;

&lt;p&gt;For example, let's say you have an array with &lt;code&gt;10&lt;/code&gt; elements, and you want to loop through it and perform some operation on each element.&lt;/p&gt;

&lt;p&gt;Here's how you might write the loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; 
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This code will loop through all &lt;code&gt;10&lt;/code&gt; elements of the array and print their values to the console. However, what if you accidentally use the wrong comparison operator and write &lt;code&gt;i &amp;lt;= arr.length&lt;/code&gt; instead of &lt;code&gt;i &amp;lt; arr.length&lt;/code&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; 
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This code will result in an &lt;code&gt;off-by-one&lt;/code&gt; error, because it will try to access an element that doesn't exist when &lt;code&gt;i&lt;/code&gt; is equal to &lt;code&gt;arr.length&lt;/code&gt;. The loop will run a total of &lt;code&gt;11&lt;/code&gt; times instead of &lt;code&gt;10&lt;/code&gt;, and the final iteration will throw an error because &lt;code&gt;arr[10]&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To avoid &lt;code&gt;off-by-one&lt;/code&gt; errors, always double-check your loop conditions to make sure you're using the correct comparison operator and taking into account the initial value of your loop variable.&lt;/p&gt;

&lt;p&gt;By avoiding these &lt;strong&gt;common mistakes&lt;/strong&gt; , you can ensure that your loops are running as intended and reduce the chances of errors or crashes in your program. It's always a good idea to double-check your loops for these mistakes before running your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Recursion if needed
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dEDtsXJG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684046622484/e951d0b4-b840-4395-a43a-67b2085166f0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dEDtsXJG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684046622484/e951d0b4-b840-4395-a43a-67b2085166f0.png" alt="Use Recursion if needed" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While loops are powerful, sometimes &lt;a href="https://medium.com/sessionstack-blog/how-javascript-works-recursion-in-javascript-what-it-is-and-how-it-is-used-eef3d734f20d"&gt;recursion&lt;/a&gt; can be an even more elegant solution.&lt;/p&gt;

&lt;p&gt;It is particularly useful for traversing hierarchical data structures, where it's not possible to know how many levels of nesting there are in advance. It can also lead to simpler and more elegant code than using nested loops, especially in cases where the depth of the structure is not known in advance.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: It's important to use recursion carefully and avoid infinite recursion, which can lead to a stack overflow error.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Let's say you have a nested array of numbers, and you want to find the &lt;code&gt;sum&lt;/code&gt; of all the numbers in the array, including the nested ones. One way to do this is by using recursion. Here's an example of how you could use recursion to find the &lt;code&gt;sum&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nestedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; 
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sumArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
     &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="c1"&gt;// If the element is an array, call the function recursively &lt;/span&gt;
            &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;sumArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="c1"&gt;// If the element is a number, add it to the sum &lt;/span&gt;
            &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt; 
      &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sumArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nestedArray&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we used &lt;code&gt;Recursion&lt;/code&gt; because the input data, &lt;code&gt;nestedArray&lt;/code&gt;, is a &lt;code&gt;hierarchical&lt;/code&gt; data structure and the function need to traverse all the elements of the structure, regardless of the number of levels of nesting.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Recursion&lt;/code&gt; allows the function to call itself repeatedly until it reaches the deepest level of nesting and returns a value that can be used to calculate the final &lt;code&gt;sum&lt;/code&gt;. The function checks whether each element in the array is an array itself, and if it is, the function calls itself with that element as the input.&lt;/p&gt;

&lt;p&gt;This process repeats until all the nested arrays are traversed and the function reaches the deepest level of nesting, which is a number.&lt;/p&gt;

&lt;p&gt;At that point, the function returns the number to the previous level of recursion, which can then use that value to calculate the &lt;code&gt;sum&lt;/code&gt; of all the elements in the current level of nesting.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;JavaScript loop&lt;/strong&gt; is a fundamental part of any developer's toolbox, but they can be tricky to master. By following these best practices, you can write more efficient and effective loops that will help take your code to the next level.&lt;/p&gt;

&lt;p&gt;Choose the right type of loop, use the right loop control statement, optimize loop performance, avoid common mistakes, and consider using recursion when appropriate. By keeping these tips in mind, you'll be well on your way to becoming a &lt;strong&gt;JavaScript loop&lt;/strong&gt; expert.&lt;/p&gt;

&lt;p&gt;So take the time to evaluate and optimize your loops, and your code will be better for it.&lt;/p&gt;




&lt;p&gt;If you enjoyed reading the post and found it helpful, you can support me by buying me a cup of coffee as a token of appreciation. Your support means a lot to me and encourages me to keep creating valuable content for you. Thank you!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/robiulhr"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6Oibfu3K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Visit my personal Blog where I write more content like this: &lt;a href="//robiul.dev"&gt;Roblog&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Other posts of mine:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/javascript-settimeout-method-all-you-need-to-know"&gt;Javascript setTimeout method - All you need to know&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/foreach-method-in-javascript-a-comprehensive-guide"&gt;forEach method in JavaScript - A Comprehensive Guide&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/javascript-hasownproperty-method"&gt;Javascript hasOwnProperty: A Powerful Property Checking tool&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/round-to-2-decimal-places-in-javascript"&gt;How to Round a Number to 2 Decimal Places in JavaScript&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/how-to-check-if-a-string-is-empty-in-javascript"&gt;How to Check if a String is Empty in JavaScript&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Simplify Your Code with Arrow Functions in JavaScript</title>
      <dc:creator>Robiul H.</dc:creator>
      <pubDate>Mon, 29 May 2023 12:06:15 +0000</pubDate>
      <link>https://dev.to/robiulhr/simplify-your-code-with-arrow-functions-in-javascript-2ahm</link>
      <guid>https://dev.to/robiulhr/simplify-your-code-with-arrow-functions-in-javascript-2ahm</guid>
      <description>&lt;p&gt;Arrow functions were introduced in ECMAScript 6 (ES6) as a concise syntax alternative to traditional function expressions. &lt;/p&gt;

&lt;p&gt;They have a more compact syntax and automatically bind the &lt;code&gt;this&lt;/code&gt; value lexically, resulting in a more intuitive and predictable behavior compared to regular functions.&lt;/p&gt;

&lt;p&gt;Here are a few benefits of using arrow functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Compact Syntax&lt;/code&gt;: Arrow functions allow you to write functions with a more concise syntax, reducing the amount of code you need to write and read. They are especially useful for short, one-line functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Lexical this Binding&lt;/code&gt;: Unlike regular functions, arrow functions don't have their own this value. Instead, they inherit the this value from the enclosing scope. This behavior eliminates the need for &lt;code&gt;bind&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt;, or &lt;code&gt;apply&lt;/code&gt; when dealing with the context of this.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Implicit Return&lt;/code&gt;: Arrow functions with a single expression automatically return the result of that expression. This simplifies the code by eliminating the need for explicit &lt;code&gt;return&lt;/code&gt; statements.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's explore some specific scenarios where arrow functions can help you simplify your code:&lt;/p&gt;

&lt;h2&gt;
  
  
  Shorter Function Definitions
&lt;/h2&gt;

&lt;p&gt;Arrow functions are perfect for concise function definitions, especially when the function body is a single statement or expression. Consider the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Regular function expression&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Arrow function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the arrow function &lt;code&gt;add&lt;/code&gt; provides a more concise and readable way to define the function. The &lt;code&gt;return&lt;/code&gt; statement is implicitly handled, reducing the code clutter and improving code readability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterating over Arrays
&lt;/h2&gt;

&lt;p&gt;Arrow functions are commonly used when working with arrays and performing operations like mapping, filtering, or reducing. They allow you to write more succinct code by eliminating the need for explicit function declarations and return statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Using regular functions&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;squaredNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&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="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Using arrow function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;squaredNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The arrow function in this example simplifies the mapping operation by removing the &lt;code&gt;function&lt;/code&gt; keyword and &lt;code&gt;return&lt;/code&gt; statement. This results in cleaner and more readable code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Callback Functions
&lt;/h2&gt;

&lt;p&gt;Arrow functions are ideal for callback functions, as they provide a concise way to define inline functions. Whether you're working with event handlers, asynchronous operations, or functional programming constructs, arrow functions can make your code more elegant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Regular function expression&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Button clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Arrow function&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Button clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the arrow function condenses the event listener callback function into a single line, improving code readability and reducing visual noise.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Asynchronous Operations
&lt;/h2&gt;

&lt;p&gt;When working with asynchronous operations, such as fetching data from an API or making AJAX calls, arrow functions can simplify the syntax and improve code readability. The concise syntax of arrow functions makes them well-suited for handling asynchronous callbacks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Regular function expression&lt;/span&gt;
&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Arrow function&lt;/span&gt;
&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, arrow functions eliminate the need for verbose function expressions and return statements, making the code more streamlined and easier to follow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Binding Context to Event Handlers
&lt;/h2&gt;

&lt;p&gt;When working with event handlers, arrow functions can help simplify code by automatically binding the context of this. With traditional functions, you often need to use bind or assign this to a variable to preserve the correct context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Regular function expression&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// refers to the button element&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Arrow function&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// refers to the lexical scope where the arrow function is defined&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the arrow function preserves the context of this, eliminating the need for additional workarounds and improving code clarity.&lt;/p&gt;

&lt;p&gt;By leveraging arrow functions in these scenarios and more, you can simplify your code, make it more readable, and increase your productivity as a JavaScript developer. &lt;/p&gt;

&lt;p&gt;Remember to use them judiciously and consider the specific context and requirements of your code when deciding to use arrow functions.&lt;/p&gt;

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

&lt;p&gt;Arrow functions in JavaScript offer a powerful way to simplify your code and enhance readability. Their compact syntax, implicit return, and lexical this binding make them a valuable tool in your coding arsenal. &lt;/p&gt;

&lt;p&gt;By leveraging arrow functions, you can write cleaner, more concise code that is easier to understand and maintain.&lt;/p&gt;




&lt;p&gt;If you enjoyed reading the post and found it helpful, you can support me by buying me a cup of coffee as a token of appreciation. Your support means a lot to me and encourages me to keep creating valuable content for you. &lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/robiulhr" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Visit my personal Blog where I write more content like this: &lt;a href="https://robiul.dev/" rel="noopener noreferrer"&gt;Roblog&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Other posts of mine:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/javascript-variables-beginner-thinking" rel="noopener noreferrer"&gt;Javascript variables (Beginner thinking)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/javascript-settimeout-all-you-need-to-know" rel="noopener noreferrer"&gt;Javascript setTimeout - All you need to know&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/foreach-javascript-a-comprehensive-guide" rel="noopener noreferrer"&gt;forEach JavaScript - A Comprehensive Guide&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/javascript-hasownproperty-method" rel="noopener noreferrer"&gt;Javascript hasOwnProperty: A Powerful Property Checking tool&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/round-to-2-decimal-places-in-javascript" rel="noopener noreferrer"&gt;How to Round a Number to 2 Decimal Places in JavaScript&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Is javascript compiled or interpreted language?</title>
      <dc:creator>Robiul H.</dc:creator>
      <pubDate>Thu, 11 May 2023 08:58:17 +0000</pubDate>
      <link>https://dev.to/robiulhr/is-javascript-compiled-or-interpreted-language-l20</link>
      <guid>https://dev.to/robiulhr/is-javascript-compiled-or-interpreted-language-l20</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;You have probably read that JavaScript is an interpreted language, while others argue that it's not an interpreted language and that it's actually a compiled language.&lt;/p&gt;

&lt;p&gt;This can be confusing,&lt;/p&gt;

&lt;p&gt;I was often confused because I received conflicting answers whenever I read books or articles about JavaScript being an interpreted or compiled language. But the truth is that it depends on how the language is implemented.&lt;/p&gt;

&lt;p&gt;Let me explain.&lt;/p&gt;

&lt;p&gt;To clarify this point, let's first examine what are compiler and interpreter also the difference between them&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiler
&lt;/h2&gt;

&lt;p&gt;A compiler is a software tool that translates human-readable source code into machine-executable code. The process of compilation involves several stages, including &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;lexical analysis&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;syntax analysis (parsing)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;semantic analysis&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;code generation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;optimization&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During lexical analysis, the compiler breaks down the source code into a series of tokens, which are individual units of meaning. For example, in the statement &lt;code&gt;"x = 5 + 3;"&lt;/code&gt;, the tokens would be &lt;code&gt;"x"&lt;/code&gt;, &lt;code&gt;"="&lt;/code&gt;, &lt;code&gt;"5"&lt;/code&gt;, &lt;code&gt;"+"&lt;/code&gt;, and &lt;code&gt;"3"&lt;/code&gt;. The lexer also assigns a type to each token, such as "identifier", "operator", or "literal".&lt;/p&gt;

&lt;p&gt;Then, during syntax analysis (parsing), the compiler checks the order and structure of the tokens to ensure that they form a valid program. This involves analyzing the grammar of the programming language and constructing a syntax tree that represents the structure of the program.&lt;/p&gt;

&lt;p&gt;After the syntax analysis is complete, the compiler performs semantic analysis to check for logical errors in the code. This involves verifying that the program follows the rules of the language, such as type checking, scoping rules, and function calls.&lt;/p&gt;

&lt;p&gt;Once the compiler has verified the correctness of the program, it generates machine code or bytecode that can be executed by the computer. This involves translating the syntax tree and semantic information into low-level instructions that can be executed by the computer's hardware.&lt;/p&gt;

&lt;p&gt;Finally, the compiler performs optimization to improve the efficiency of the generated code. This process involves analyzing the code and making changes to improve the memory usage.&lt;/p&gt;

&lt;p&gt;Overall, the compiler is an essential tool for translating human-readable source code into machine-executable code. The compilation process involves several stages, each of which is critical for producing correct and efficient code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interpreter
&lt;/h2&gt;

&lt;p&gt;An interpreter is a program that reads and executes code written in a high-level programming language directly, without requiring an intermediate compilation step. Unlike a compiler, which translates the entire source code into machine code before execution, an interpreter reads and executes the source code line by line, translating and executing each statement as it is encountered.&lt;/p&gt;

&lt;p&gt;When an interpreter runs a program, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;it first performs lexical analysis and parsing of the source code to generate an abstract syntax tree (AST) representing the program's structure. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The interpreter then walks through the AST, executing each node in turn. During execution, the interpreter may generate and manipulate additional data structures to represent the state of the program, such as a symbol table for tracking variable values.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One advantage of using an interpreter is that it allows for more dynamic and interactive development, as programmers can test and modify their code on the fly without having to recompile the entire program. However, interpreting code can be slower than executing compiled code, since the interpreter has to perform additional work to translate and execute each statement.&lt;/p&gt;

&lt;p&gt;Overall, an interpreter provides a convenient way to execute code in a high-level language, but may not be as performant as compiled code in certain situations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiler vs. Interpreter in a simple way
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxrn6wo59tew6rnb4tags.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxrn6wo59tew6rnb4tags.png" alt="Compiler vs. Interpreter in a simple way"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is a lot of technical information about compilers and interpreters, so let's make it simple:&lt;/p&gt;

&lt;p&gt;And like everything else in life, let's try to understand it with a cup of tea.&lt;/p&gt;

&lt;p&gt;Let's say you want to make a cup of black tea, you go to Google and search for the ingredients list, this is what you came up with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Black tea leaves&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Water&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sugar (optional)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Milk (optional)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are 2 ways to make the tea, the Compiler or the Interpreter way.&lt;/p&gt;

&lt;p&gt;The compiler will first, before doing any steeping, organize all the ingredients in front of him, the specific amounts of every single ingredient, only then, will he steep all the ready components of the tea.&lt;/p&gt;

&lt;p&gt;The interpreter will take his cup and will start by reading the ingredients, line by line. he will boil the water, add the tea leaves, steep them for a few minutes, add sugar or milk if desired, and then strain the tea into the cup.&lt;/p&gt;

&lt;p&gt;The build (preparation) time of the compiler will be longer than the interpreter's. However, the run (steeping) time will be much shorter.&lt;/p&gt;

&lt;p&gt;Now that you know the difference let’s talk about JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is JavaScript Compiled or an Interpreted Language?
&lt;/h2&gt;

&lt;p&gt;When I first started learning JavaScript, I, like many others, was taught that it was an interpreted language, and I believed it for a long time. However, after conducting further research, I have come to realize that this is not entirely accurate.&lt;/p&gt;

&lt;p&gt;While many people still consider JavaScript to be an interpreted language, there is evidence to suggest that it is actually a compiled language. One example of this can be seen in the way that the JavaScript engine handles bugs.&lt;/p&gt;

&lt;p&gt;When you encounter a syntax error in your JavaScript code, the engine will typically alert you to the problem before it even begins executing the code. This suggests that the code is compiled and validated before it is executed, which is a characteristic of compiled languages.&lt;/p&gt;

&lt;p&gt;You can try this for yourself by opening the following HTML code in your browser and checking the console:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!);


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

&lt;/div&gt;

&lt;p&gt;The last line of code is missing a closing quotation mark, which should trigger a syntax error. However, if JavaScript were truly an interpreted language, the console would have already printed the first nine lines before throwing an error. &lt;/p&gt;

&lt;p&gt;Instead, the program crashes immediately,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;Uncaught&lt;/span&gt; &lt;span class="nx"&gt;SyntaxError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Invalid&lt;/span&gt; &lt;span class="nx"&gt;or&lt;/span&gt; &lt;span class="nx"&gt;unexpected&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Thats indicating that it was compiled and validated before execution.&lt;/p&gt;

&lt;p&gt;While there is still debate over whether JavaScript is truly a compiled language or not, it's clear that it's not entirely accurate to call it an interpreted language.&lt;/p&gt;

&lt;p&gt;Another way to prove that JavaScript is not an entirely interpreted language is through the concept of &lt;code&gt;hoisting&lt;/code&gt;. For example, consider the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In traditional programming languages, a function must be defined before it can be called. However, in JavaScript, the function can be called before it is defined. This behavior is known as &lt;code&gt;hoisting&lt;/code&gt;, and it's a fundamental aspect of how JavaScript works.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;

Note: We will explore more about &lt;span class="sb"&gt;`hoisting`&lt;/span&gt; in other blog for now this explanation is enough.&lt;span class="sb"&gt;


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

&lt;/div&gt;

&lt;p&gt;How does the JavaScript engine know about the &lt;code&gt;max&lt;/code&gt; function before it reaches the declaration? The only reasonable answer to this question is that the code must first be compiled before execution.&lt;/p&gt;

&lt;p&gt;So, JavaScript is a compiled language, right?&lt;/p&gt;

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

&lt;p&gt;Well, it's complicated. In the past, every programming language was fairly easy to categorize as either compiled or interpreted. However, with the modern approach of running source code, a sort of "in-between" area has been created.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does JavaScript actually get translated?
&lt;/h2&gt;

&lt;p&gt;JavaScript has come a long way since its inception as a simple scripting language. In the early days, JavaScript engines were only interpreters, which executed code line by line. However, as JavaScript became more popular and its use cases expanded, performance issues emerged. Interpreting code on-the-fly resulted in a loss of performance, especially for complex applications. This led to the development of new engines that use a just-in-time (JIT) compiler.&lt;/p&gt;

&lt;p&gt;A JIT compiler is different from traditional compilers, such as those used for C++. Traditional compilers have plenty of time to optimize the code during compilation, but JIT compilers must compile code just before it's executed. As soon as the JavaScript code is about to run, it gets compiled into executable bytecode.&lt;/p&gt;

&lt;p&gt;Despite the differences in how JavaScript is compiled compared to other compiled languages, the process still follows some of the same rules as traditional compilers. The JavaScript code is parsed before execution, which makes it look like a parsed language. However, the code is converted to binary form before execution.&lt;/p&gt;

&lt;p&gt;To better understand this process, let's take a closer look at how code execution works behind the scenes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First, the code is transpiled using tools like Babel or Webpack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The transpiled code is given to the engine, which parses it to an Abstract Syntax Tree (AST).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The AST is then converted to bytecode that is understood by the machine. This is an Intermediate Representation (IR), which is further optimized by the JIT compiler.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After optimization, the JS Virtual Machine (VM) executes the code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;Some&lt;/span&gt; &lt;span class="nx"&gt;people&lt;/span&gt; &lt;span class="nx"&gt;argue&lt;/span&gt; &lt;span class="nx"&gt;that&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;JS&lt;/span&gt; &lt;span class="nx"&gt;VM&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;interpreting&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;bytecode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;but&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;entirely&lt;/span&gt; &lt;span class="nx"&gt;accurate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;If&lt;/span&gt; &lt;span class="nx"&gt;we&lt;/span&gt; &lt;span class="nx"&gt;were&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;that&lt;/span&gt; &lt;span class="nx"&gt;definition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;we&lt;/span&gt; &lt;span class="nx"&gt;would&lt;/span&gt; &lt;span class="nx"&gt;also&lt;/span&gt; &lt;span class="nx"&gt;have&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;say&lt;/span&gt; &lt;span class="nx"&gt;that&lt;/span&gt; &lt;span class="nx"&gt;Java&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;which&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;another&lt;/span&gt; &lt;span class="nx"&gt;JVM&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;driven&lt;/span&gt; &lt;span class="nx"&gt;language&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;also&lt;/span&gt; &lt;span class="nx"&gt;interpreted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; 


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

&lt;/div&gt;

&lt;p&gt;Thus, we can conclude that JavaScript is executed in three phases: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Parsing &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compiling &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Executing&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript code is initially interpreted before any execution begins. For example, consider the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;The output shows the interpreter behavior in the above code example, where first &lt;code&gt;Hello&lt;/code&gt; is printed to the console, and then an error is reported. This output strongly supports the fact that JavaScript is an interpreted language.&lt;/p&gt;

&lt;p&gt;While it may seem like JavaScript code is being executed line by line, this is only true during the parsing phase. In reality, the entire code is compiled at once to convert it into machine-readable code before execution. Therefore, JavaScript is a just-in-time compiled language that uses an interpreter in its first phase.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;

Note: JavaScript can operate in an interpreted manner in older browsers. However, every modern browser currently supports "JIT", so JavaScript code is always compiled. Whether JavaScript is compiled or interpreted depends on the environment in which it is run. If it runs in older browsers, it's interpreted. If it runs in modern browsers, it's compiled.&lt;span class="sb"&gt;


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Explaining JIT in JavaScript
&lt;/h2&gt;

&lt;p&gt;Now that we have a basic understanding of how JavaScript works and what JIT compilers are, let's delve deeper into how JIT compilers work and why they're important. &lt;/p&gt;

&lt;p&gt;But don't worry, we won't get too technical with jargon. Instead, we'll explore JIT compilers in simple terms to understand how they work, why they're important, and the reason they were introduced in the first place.&lt;/p&gt;

&lt;p&gt;JIT (just-in-time) compilation is a technique used by modern JavaScript engines to improve the performance of JavaScript code. Unlike traditional compilers that optimize code during compilation, JIT compilers take a middle-ground approach. They initially interpret the code and then selectively compile parts of it that are used repeatedly. This approach balances fast start-up time with improved performance.&lt;/p&gt;

&lt;p&gt;When a JavaScript engine encounters a function, it can either interpret the code on-the-fly or compile the code before execution. Interpreting the code can be faster to start execution but slower in terms of overall performance, as the code is translated line-by-line each time the function is executed. Compiling the code before execution can be slower to start, but it can result in faster execution due to machine code optimizations. &lt;/p&gt;

&lt;p&gt;In this approach, the code is compiled into machine code, which is then executed by the processor.&lt;/p&gt;

&lt;p&gt;JIT compilers take a middle-ground approach, where they initially interpret the code and then selectively compile parts of it that are used repeatedly. As a result, JIT compilers offer a balance of fast start-up time and improved performance.&lt;/p&gt;

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

&lt;p&gt;To understand how JIT compilers work, let's consider an example:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&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="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In this code, we have a simple &lt;code&gt;add&lt;/code&gt; function that adds two numbers and a loop that calls this function &lt;code&gt;100&lt;/code&gt; million times with different inputs.&lt;/p&gt;

&lt;p&gt;When the JavaScript engine first encounters this code, it will interpret it on-the-fly and execute it. However, as the loop runs repeatedly, the engine detects that the &lt;code&gt;add&lt;/code&gt; function is being called repeatedly and decides to compile it to machine code to improve performance.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;JIT&lt;/code&gt; compiler analyzes the code and optimizes it for the specific inputs being used in the loop. &lt;/p&gt;

&lt;p&gt;For example, it may decide to inline the &lt;code&gt;add&lt;/code&gt; function, which means replacing the function call with the actual addition code, eliminating the overhead of calling a function. Additionally, it may also perform other optimizations such as constant folding or dead-code elimination, which remove unnecessary computations.&lt;/p&gt;

&lt;p&gt;Overall, JIT compilers help improve the performance of JavaScript code by selectively compiling parts of it that are used repeatedly, while still allowing for fast start-up times.&lt;/p&gt;

&lt;p&gt;Now that we have a better understanding of how the JIT compiler works, let's delve into the issues we previously discussed and their underlying causes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the Pre-execution syntax error alerts
&lt;/h2&gt;

&lt;p&gt;Let's explore the reason behind syntax error alerts in JavaScript using the same example we discussed earlier.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World from javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!);


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

&lt;/div&gt;

&lt;p&gt;In the case of the code example provided, the JavaScript engine compiles the code before execution, and the syntax error is detected during the compilation phase. This is because the engine uses the &lt;code&gt;JIT&lt;/code&gt; compiler that optimizes code on the fly as it's executed. When the engine encounters an error during the compilation phase, it immediately throws an error without executing any code.&lt;/p&gt;

&lt;p&gt;Therefore, the fact that JavaScript detects syntax errors during the compilation phase suggests that it's more like a compiled language than an interpreted one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the story of hoisting?
&lt;/h2&gt;

&lt;p&gt;Let's now understand the hoisting in the context of &lt;code&gt;JIT&lt;/code&gt; engine using the code we used before:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;So, how does the JavaScript engine know about the &lt;code&gt;max&lt;/code&gt; function before it reaches the declaration? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F02lm7eulkzfpwccb9yxf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F02lm7eulkzfpwccb9yxf.gif" alt="How does hoisting works in javascript?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The answer lies in the Just-In-Time (&lt;code&gt;JIT&lt;/code&gt;) compilation process that occurs behind the scenes when JavaScript code is executed in modern browsers.&lt;/p&gt;

&lt;p&gt;During the optimization step, the &lt;code&gt;JIT&lt;/code&gt; compiler can analyze the code to determine which functions are likely to be called frequently and which variables are likely to be accessed repeatedly. By identifying these patterns, the compiler can optimize the code for faster execution, which can result in significant performance gains. &lt;/p&gt;

&lt;p&gt;In the case of hoisting, the compiler can recognize the pattern of a function being declared at the top of its scope and optimize the code accordingly.&lt;/p&gt;

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

&lt;p&gt;In summary, while JavaScript is commonly thought of as an interpreted language, it is actually a Just-In-Time compiled language. Modern JavaScript engines use a &lt;code&gt;JIT&lt;/code&gt; compiler to optimize the code for execution. This compiler allows JavaScript to be executed much faster and in an efficient way than traditional interpreted languages. While the compiling of Javascript works in a different way, if compared to other compiled languages, it still follows some rules that reflect the process of compiling.&lt;/p&gt;

&lt;p&gt;JavaScript code is parsed before execution, which makes it look like a parsed language, but the code is actually converted to binary(machine code that is directly executed by the computer's hardware) form before execution. This conversion process involves several steps, including transpiling the code, parsing it to an Abstract Syntax Tree (&lt;code&gt;AST&lt;/code&gt;), converting it to bytecode, and optimizing it with a &lt;code&gt;JIT&lt;/code&gt; compiler.&lt;/p&gt;

&lt;p&gt;So, to answer the question &lt;code&gt;"Is javascript compiled or interpreted language?"&lt;/code&gt; the answer is that it is a bit of both. It is interpreted in older browsers, but in modern browsers, it is compiled with the help of a &lt;code&gt;JIT&lt;/code&gt; compiler.&lt;/p&gt;




&lt;p&gt;If you enjoyed reading the post and found it helpful, you can support me by buying me a cup of coffee as a token of appreciation. Your support means a lot to me and encourages me to keep creating valuable content for you. Thank you!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/robiulhr" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Visit my personal Blog where I write more content like this: &lt;a href="https://robiul.dev/" rel="noopener noreferrer"&gt;Roblog&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Resource:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.linkedin.com/pulse/javascript-compiled-language-st%C3%A9phane-moreau/" rel="noopener noreferrer"&gt;https://www.linkedin.com/pulse/javascript-compiled-language-st%C3%A9phane-moreau/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://stackdiary.com/tutorials/is-javascript-a-compiled-or-interpreted-language/" rel="noopener noreferrer"&gt;https://stackdiary.com/tutorials/is-javascript-a-compiled-or-interpreted-language/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://almogad.medium.com/javascript-is-it-compiled-or-interpreted-9779278468fc" rel="noopener noreferrer"&gt;https://almogad.medium.com/javascript-is-it-compiled-or-interpreted-9779278468fc&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/is-javascript-interpreted-or-compiled/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/is-javascript-interpreted-or-compiled/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Other posts of mine:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/javascript-variables-beginner-thinking" rel="noopener noreferrer"&gt;Javascript variables (Beginner thinking)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/javascript-loop-best-practices-for-optimal-performance" rel="noopener noreferrer"&gt;JavaScript Loop Best Practices for Optimal Performance&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://robiul.dev/javascript-settimeout-all-you-need-to-know" rel="noopener noreferrer"&gt;Javascript setTimeout - All you need to know&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Javascript variables (Beginner thinking)</title>
      <dc:creator>Robiul H.</dc:creator>
      <pubDate>Fri, 05 May 2023 04:11:06 +0000</pubDate>
      <link>https://dev.to/robiulhr/inside-the-javascript-variable-part-1-beginner-thinking-3hnm</link>
      <guid>https://dev.to/robiulhr/inside-the-javascript-variable-part-1-beginner-thinking-3hnm</guid>
      <description>&lt;p&gt;Programming is all about manipulating and displaying data, which can be any kind of information used in computer programs, such as social media usernames, age, and profile photos. To work with this data and create interesting things, programmers need a way to store and keep track of it. This is where the concept of variables comes in.&lt;/p&gt;

&lt;p&gt;Variable is an essential concept in almost every programming language, and there is much to know and understand about it. It is important for us to have a clear and deep understanding of these concepts related to the Variable.&lt;/p&gt;

&lt;p&gt;In this post, we will explore almost all of the concepts related to variables in programming, but from a beginner's perspective. We will keep things simple to understand, and use examples to explore everything.&lt;/p&gt;

&lt;p&gt;Even if you are new to programming and don't have much knowledge about JavaScript, you will be able to understand all the concepts we cover.&lt;/p&gt;

&lt;p&gt;So, let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  variables in javascript
&lt;/h2&gt;

&lt;p&gt;In JavaScript, a variable is a named reference to a memory location that can hold different types of data, such as numbers, strings, booleans, objects, or functions. To access or modify the data stored in a variable, we use its name.&lt;/p&gt;

&lt;p&gt;It's a little confusing, right?&lt;/p&gt;

&lt;p&gt;let's have a look at the computer memory.&lt;/p&gt;

&lt;p&gt;If you have a basic understanding of computer memory then you must know that Computer memory has millions of cells on it and every cell has its own address.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7Ektp6K0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1669532444900/LYMQxA0ah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Ektp6K0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1669532444900/LYMQxA0ah.png" alt="Computer memory - Variable Beginner Thinking in Javascript | Part - 1 (robiul.dev).png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our program, if we want to store value to access it later throughout the program, we have to keep it somewhere in the computer memory and we will need a way to call that value whenever we need.&lt;br&gt;&lt;br&gt;
we could have done that using the memory address after storing the data in the memory.&lt;br&gt;&lt;br&gt;
The problem is we can't use this address directly in our program. because you know the computer works in the binary system and in the binary system this memory address looks too weird and confusing. also, it's almost impossible to memorize.&lt;/p&gt;

&lt;p&gt;Here variable comes in to solve this problem.&lt;/p&gt;

&lt;p&gt;Variable gives us the simplest solution to handle it. We can simply create a variable and assign it to the value we want to store. now we don't need to memorize the weird and confusing memory address we can do the same using a simple and human-readable name.&lt;/p&gt;

&lt;p&gt;When we are creating a variable and assigning a value to it. behind the since the Compilers and interpreters store the data inside the memory and replace the symbolic names of variables with the real data location/memory address.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4GXBP3_g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1669532908523/Cs5PSyu8H.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4GXBP3_g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1669532908523/Cs5PSyu8H.png" alt="Assigning value in computer memory cell - Variable Beginner Thinking in Javascript | Part - 1 (robiul.dev).png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That means our data is stored in memory and the variable is pointing to this memory location.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In real scenarios, memory allocation is not as simple as I discussed here. also, Primitive type and Reference type values are treated differently when assigning them to variables in javascript. but for this post, I have avoided the advanced things and kept it simple so you can get an overall idea of how things work behind the scenes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you are still confused with it, simply think of variables as named containers that hold information and can be referred to the data simply by naming the container.&lt;/p&gt;

&lt;p&gt;That means,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Creating a variable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Giving a standard name to the variable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assigning a value to it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of these steps as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Taking a container&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Labeling it&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Putting something in this container.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OYXJijox--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1669539923138/EFg7IaK78.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OYXJijox--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1669539923138/EFg7IaK78.png" alt="variable as container - Variable Beginner Thinking in Javascript | Part - 1 (robiul.dev).png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; A Javascript variable is a container, not a value, this means that variables arent themselves values; they are a named container for values.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Declaring Variables
&lt;/h2&gt;

&lt;p&gt;Before you use a variable in a JavaScript program, you must create it, we call this declaring a variable.&lt;/p&gt;

&lt;p&gt;we can Declare a JavaScript Variable using &lt;code&gt;var&lt;/code&gt; or &lt;code&gt;let&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name; 
let age;

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

&lt;/div&gt;



&lt;p&gt;Here we are creating two variables, one using var and one using let.&lt;/p&gt;

&lt;p&gt;var and let are the keywords that tell JavaScript youre declaring a variable.&lt;/p&gt;

&lt;p&gt;name and age are the names of those variables.&lt;/p&gt;

&lt;p&gt;In Javascript, Semicolons(;) are totally optional (unless you want to have multiple statements in a single line, of course). (similar to a full stop(.) in the English language.)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Though Semicolons(;) are optional in javascript, there are many languages where Semicolon(;) is a big factor. In those languages you must end the line using a semicolon otherwise will occur an error to your program.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can also declare multiple variables with the same &lt;code&gt;var&lt;/code&gt; or &lt;code&gt;let&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name, age;
let birthYear, address;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;There is another keyword to declare variables in javascript which is const. This is used to declare the constant variables in javascript.&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Pi = 3.1416

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


&lt;p&gt;we will talk about this in any other blog.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Naming Variables
&lt;/h2&gt;

&lt;p&gt;Not only in JavaScript but also, in all other languages variables must be identified with unique names following some rules and regulations. These unique names are called identifiers. These Identifiers or names can be short (like &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt;) or more descriptive (&lt;code&gt;age&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;userName&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Rules for variable Name
&lt;/h3&gt;

&lt;p&gt;There are some general rules to follow when we are choosing a name (unique identifiers) for the variable.&lt;/p&gt;

&lt;p&gt;Here are the rules below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Names can contain letters, Number digits(0-9), underscores &lt;code&gt;_&lt;/code&gt;, and dollar signs &lt;code&gt;$&lt;/code&gt; but Spaces and special symbols/punctuation characters (like @, !, #, etc.) are not allowed to be used in the variable's name.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: In Javascript '_' and '$' are just like letters. They don't have any special meaning.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Variable names must be started with either a letter, an underscore &lt;code&gt;_&lt;/code&gt;, or the dollar sign &lt;code&gt;$&lt;/code&gt;. It is not allowed to start variable names using numbers (0-9).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JavaScript Names/identifiers are case sensitive (Variables named &lt;code&gt;apple&lt;/code&gt; and &lt;code&gt;APPLE&lt;/code&gt; are two different variables.).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are some reserved words, which cannot be used as variable names in your program because they are used by the language itself.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;For example: let, var, for, and function are reserved.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The code below gives an Uncaught SyntaxError:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let var = 5; // Uncaught SyntaxError: Unexpected token 'var'
let function = 5; // Uncaught SyntaxError: Unexpected token 'function'

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In JavaScript, When the variable name contains multiple words, it is generally written in camelCase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is: words go one after another, every word except the first one starting with a capital letter.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;For example, firstName, lastName, birthYear, etc.&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is possible to use almost any language in a variable name, but not recommended. We should use English in variable names even if were writing a small script. so that if people from other countries need to read it they can read it. Because we will write code for global developers not only for our region.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are some valid and invalid variable names below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Example of valid variables names
let camelCased // first word is lowercase any additional word starts with an uppercase letter
let here2num // number in the middle, but not starting with numbers
let JAVASCRIPT_IN_UPPERCASE // uppercase letters with underscores
let _Javascript_ //start and end with an underscore, with letters in the middle
let $_$ // dollar signs and underscores
let $_foo3 // mix the dollar sign, underscores, letters and numbers
let _12foo //start with underscores and contain numbers and letters
let _ // contains only underscore
let $ // contain only a dollar sign

// Example of invalid variables names

let random% // Don't use the percentage symbol
let 11years // Don't start with number(s)
let some-let // Don't use dashes
let one variable // Don't use spaces
let function // Don't use reserved keywords

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Naming variable Good practices
&lt;/h3&gt;

&lt;p&gt;Giving a good name to the variable is one of the most important and complex skills in programming. Proper descriptive variable names can create a good impression for a programmer in the viewer's eyes.&lt;/p&gt;

&lt;p&gt;In a real project, most of the time a programmer spends modifying and extending an existing code base rather than writing something completely new. When we return to the code after doing something else for a while or maybe for a long period of time, its much easier to get back into a flow for those codes that is well-labeled. Or, in other words, when the variables have been declared with good names.&lt;/p&gt;

&lt;p&gt;So, Please spend time thinking about the right name when you are declaring a variable. Doing so will make you benefited in the future.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Though variable names can be created in any way you want, it's a good practice to use human-readable, obvious meanings and maximally descriptive (the value the variable is referencing). If we name a variable as user then we should name related variables as currentUser or newUser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stay away from short names like a, b, and c. These kinds of short names are easy to type. But these are only useful in small programs. When you will work on a big project, will forget the context of these kinds of names.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Though There is no limit to the length of the variable name and it can be almost unlimited in length, You should avoid creating extremely long variable names. Shorter variable names help the JavaScript engine to execute the program faster.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Since JavaScript is case-sensitive, apple and Apple are two different identifiers. But we should not use both of them in our program. Because this will occur confusion in the long term.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The best way to create a variable is to use multiple words in camelCase. (e.g., &lt;code&gt;myVarName&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Undefined value of a variable
&lt;/h2&gt;

&lt;p&gt;When we declare a variable without assigning a value to it will have the value &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's the default behavior of javascript. Though this container should be empty, it is not. It still contains a value that is &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: &lt;code&gt;undefined&lt;/code&gt; is one type of data in javascript. we will see about &lt;code&gt;undefined&lt;/code&gt; and other data types in any other blog in detail.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you want to see their value, you can do that by simply doing &lt;code&gt;console.log()&lt;/code&gt; in your web browser's console the output will be &lt;code&gt;undefined&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name, age, birthYear;
console.log(name); // undefined
console.log(age); // undefined
console.log(birthYear); // undefined

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Undeclared/Unassigned variable
&lt;/h2&gt;

&lt;p&gt;Suppose you want to use a variable in a statement, but you haven't declared this variable yet. In this case, your code will throw a &lt;code&gt;ReferenceError&lt;/code&gt; showing that the variable is not defined. In short, if we want to access an undeclared variable, the code will throw a runtime error.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(xyz); // ReferenceError: xyz is not defined

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

&lt;/div&gt;



&lt;p&gt;Try running the above line in the browser console.&lt;/p&gt;

&lt;p&gt;In the real program, You should never try accessing a variable without declaring it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Don't get confused with the undefined and Unassigned/Undeclared variables they are very different things. An undefined variable is a variable that has been declared in the program but has not been initialized with a value. In contrast, an undeclared variable is a variable that has not been declared yet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Assigning value to the variable
&lt;/h2&gt;

&lt;p&gt;After the declaration has been completed, we can use the equal(&lt;code&gt;=&lt;/code&gt;) sign to assign a value to the variable:&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let age;
age = 22;

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

&lt;/div&gt;



&lt;p&gt;where the &lt;code&gt;age&lt;/code&gt; is the name of the variable and it is assigned a value of &lt;code&gt;22&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: In JavaScript, the equal sign (&lt;code&gt;=&lt;/code&gt;) is an "assignment" operator, not an "equal to" operator like in algebra. Though the following does not make sense in algebra:&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = x + 10;

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

&lt;/blockquote&gt;

&lt;p&gt;But In JavaScript, it makes perfect sense:&lt;/p&gt;

&lt;p&gt;First It calculates the value of &lt;code&gt;x + 10&lt;/code&gt; and assigns the result to variable &lt;code&gt;x&lt;/code&gt;. In simple words, The value of &lt;code&gt;x&lt;/code&gt; is incremented by &lt;code&gt;10&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: in JavaScript, the "equal to" operator is written like &lt;code&gt;==&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also, the variable declaration and initialization can be combined. that means variable initialization can be done in the declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var greetings = "Hello";
let name = "Robiul"

// or,
var greetings = "Hello", name = "Robiul";

//The same declaration can even span across multiple lines using comma(,):

var greetings = "Hello", 
      name = "Robiul";

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: we can assign a variable value from user input&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Gets user input
var name = prompt("What is your name?");
var age = prompt("What is your favorite age? ");
console.log(name)  
console.log(age)

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

&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Changing/Updating/Re-assigning the Value of a Variable.
&lt;/h2&gt;

&lt;p&gt;The meaning of the word variable is anything that can vary. That means once the initialization is finished we can change or update the value of the variable later on in the program if required.&lt;/p&gt;

&lt;p&gt;It is similar to re-initializing the variable. We can update/change the value by just typing the name of the variable followed by an equals sign(&lt;code&gt;=&lt;/code&gt;) and then followed by the new value we want it to store.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var greetings = "Hello";
let myHobby = "Drawing";
console.log(greetings); // Hello
console.log(myHobby); // Drawing

// changing the value 
greetings = "Hi";
myHobby = "Programming"
console.log(greetings); // Hi
console.log(myHobby); // Programming

// also, we can change the value as many times as we want:

let message;
message = "Hello";
console.log(message); // Hello
message = "World"; 
console.log(message); // World

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;javascript variable doesn't contain the history of the variable, which means when we change the value of a variable it removes the old data and stores the new one.&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: There are a few functional programming languages, like Scala or Erlang that don't allow changing variable values.&lt;br&gt;&lt;br&gt;
In such languages, once the value is assigned in a variable, its there forever. If we want to reassign the variable or want to change the value of the variable, the language forces us to create a new variable (declare a new variable). We cant reuse the old one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Accessing JavaScript variables
&lt;/h2&gt;

&lt;p&gt;As a beginner, you might be thinking about what is the procedure to access/use the value that is stored in a specific variable. It's simpler than declaring and assigning the variable. You just need to write the name of the variable that contains the value you want to access and you are done. This things also called referencing a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Declare and initialize the variable
var myNumVariable = 85
let myTextVariable = 'This is text.'

// Access the values in myNumVariable and myTextVariable
myNumVariable
// 85
myTextVariable
// 'This is text.'

// Re-assign myNumVariable and myTextVariable
myNumVariable = 50
myTextVariable = 'This is a updated Text'

// Access the values in myNumVariable and myTextVariable again
myNumVariable
// 50
myTextVariable
// 'This is a updated Text'

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Basic usage of variables
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Once you declare a variable and initialize it, you can reference this variable by name anywhere elsewhere in your code.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 10;
x + 2;
console.log(x) // 12

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can use an already declared variable when declaring a new variable.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 100;
var y = x + 102;
console.log(y) // 202

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;you can do all kinds of arithmetic operations with JavaScript variables, using operators like &lt;code&gt;=&lt;/code&gt;, &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, and more. :
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 5 + 2 + 3;
console.log(x) // 10

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can also add a string to another string, but strings will be concatenated:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = "John" + " " + "Doe";
console.log(x) // John Doe

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: During arithmetic operations If you put a number in quotes, the rest of the numbers will be treated as strings, and all of them will be concatenated. Now try this:&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let y = "5" + 2 + 3;
console.log(y) // 523

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


&lt;p&gt;This is called coercion in javascript&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why should we use variable
&lt;/h2&gt;

&lt;p&gt;So far we have discussed what is variable, how to create it, and how it works in our program. But haven't discussed why this is so important and why should we use the variable.&lt;/p&gt;

&lt;p&gt;Let's have a look at this too. we will discuss some point on how variable helps programmers to write optimized and efficient code :&lt;/p&gt;

&lt;p&gt;To Understand these points let's see a program of a pretty simple and state-forward game called guess my number. This game logic is very simple, we will use a number in our code and the user will have to guess the number if the user guesses the correct number our program will show a successful message and if the user is wrong the program will show a failed message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(userInput==20){
  console.log("Hurrah, You guess the correct number.");
}else if(userInput&amp;lt;20){
  console.log("Sorry, Your guessed number is small. Please try a bigger one.")
}else if(useInput&amp;gt;20){
  console.log("Sorry, Your guessed number is Big. Please try a bigger one.")
}

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

&lt;/div&gt;



&lt;p&gt;Here in this program, we have used the number &lt;code&gt;20&lt;/code&gt; to make our program logic. This program will perform fine but this program is not well coded.&lt;/p&gt;

&lt;p&gt;let's see what's wrong with this program and why this is not well coded and how variable makes our life easy and help us to make our code more efficient and optimized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first problem with this program is we have to memorize the number &lt;code&gt;20&lt;/code&gt; or have to check the number again and again whenever we will use it in our program. Maybe it doesn't seem a big problem for a small program and simple value like this but imagines a program that has a thousand lines of code and maybe hundreds of value like this number or maybe some huge numbers like &lt;code&gt;204025021&lt;/code&gt;, &lt;code&gt;12242250221&lt;/code&gt; and we can't memorize all of them.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myNum = 20;
if(userInput==myNum){
  console.log("Hurrah, You guess the correct number.");
}else if(userInput&amp;lt;myNum){
  console.log("Sorry, Your guessed number is small. Please try a bigger one.")
}else if(useInput&amp;gt;myNum){
  console.log("Sorry, Your guessed number is Big. Please try a bigger one.")
}

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now here comes the second problem with this code. that is when we will try to change the number we have to change the number from every place where we have used the number. and imagine when we will work on a big project we have to use a variable in several places. if we would need to change the value from all the places how painful and time-consuming it would be.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myNum = 20;
console.log(myNumber) // 20
if(userInput==myNum){
console.log(myNumber) // 20
  console.log("Hurrah, You guess the correct number.");
}else if(userInput&amp;lt;myNum){
  console.log(myNumber) // 20
  console.log("Sorry, Your guessed number is small. Please try a bigger one.")
}else if(useInput&amp;gt;myNum){
  console.log(myNumber) // 20
  console.log("Sorry, Your guessed number is Big. Please try a bigger one.")
}
// change the value
myNum = 30;
console.log(myNumber) // 30
if(userInput==myNum){
  console.log(myNumber) // 30
  console.log("Hurrah, You guess the correct number.");
}else if(userInput&amp;lt;myNum){
  console.log(myNumber) // 30
  console.log("Sorry, Your guessed number is small. Please try a bigger one.")
}else if(useInput&amp;gt;myNum){
  console.log(myNumber) // 30
  console.log("Sorry, Your guessed number is Big. Please try a bigger one.")
}

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

&lt;/div&gt;



&lt;p&gt;how simple it is! right?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Another program with this program is we are using the number in a statical way. That means our program is not dynamic now. If we need any changed value we have to edit the source code which is very bad. To write a modern program we should have a feature to change the value dynamically.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  let myNum = prompt("enter a number?");
if(userInput==myNum){
  console.log("Hurrah, You guess the correct number.");
}else if(userInput&amp;lt;myNum){
  console.log("Sorry, Your guessed number is small. Please try a bigger one.")
}else if(useInput&amp;gt;myNum){
  console.log("Sorry, Your guessed number is Big. Please try a bigger one.")
}

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

&lt;/div&gt;



&lt;p&gt;Now our code is dynamic we can change the value whenever we want without changing or editing the source code.&lt;/p&gt;

&lt;p&gt;Thus, variable helps us to write optimized, developer-friendly, and easy-to-understand code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Though variable helps us in many ways. but too many variables can harm the code performance very badly and it's can increase the server cost. so, we should be careful when we are declaring a variable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A variable is a computer memory location paired with an associated symbolic name, which contains some information or data referred to as a value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can simply think of variables as named containers that hold information and can be referred to the data simply by naming the container.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can Declare a JavaScript Variable using &lt;code&gt;var&lt;/code&gt; or &lt;code&gt;let&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All JavaScript variables must be identified with unique names. These unique names are called identifiers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are some general rules to follow when we are constructing a name (unique identifiers) for the variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A variable declared without a value will have the value undefined.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A variable that has not been declared yet is called an undeclared.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the declaration, we can use the equal(&lt;code&gt;=&lt;/code&gt;) sign to assign value to the variable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can combine variable declaration with variable initialization. that means initialization can be done in the declaration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once a variable has been initialized with a value, you can change (or update) that value anytime and anywhere within its scope by giving it a different value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can access a value of a variable simply by using the name of the variable. This is also called referencing a variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can reference a variable by name elsewhere in your code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can use a variable when declaring other variables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can do arithmetic with JavaScript variables, using operators like &lt;code&gt;=&lt;/code&gt; and &lt;code&gt;+&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can also add strings, but strings will be concatenated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;variable helps us to write optimized and efficient code. Though it is helpful in many ways, too many variables can be harmful. so, we should be careful of it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you enjoyed reading the post and found it helpful, you can support me by buying me a cup of coffee as a token of appreciation. Your support means a lot to me and encourages me to keep creating valuable content for you. Thank you!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/robiulhr"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6Oibfu3K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Visit my personal Blog where I write more content like this: &lt;a href="//robiul.dev"&gt;Roblog&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.w3schools.com/js/js%5C_variables.asp"&gt;https://www.w3schools.com/js/js\_variables.asp&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://javascript.info/variables"&gt;https://javascript.info/variables&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.javascripttutorial.net/javascript-variables"&gt;https://www.javascripttutorial.net/javascript-variables&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/javascript-variables-beginners-guide"&gt;https://www.freecodecamp.org/news/javascript-variables-beginners-guide&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.scaler.com/topics/javascript/javascript-variables/"&gt;https://www.scaler.com/topics/javascript/javascript-variables/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://blog.alexdevero.com/javascript-variables-introduction/"&gt;https://blog.alexdevero.com/javascript-variables-introduction/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/understanding-let-const-and-var-keywords"&gt;https://www.freecodecamp.org/news/understanding-let-const-and-var-keywords&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://medium.com/@avishaa27/what-are-variables-in-javascript-3aa9a3324a1e"&gt;https://medium.com/@avishaa27/what-are-variables-in-javascript-3aa9a3324a1e&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.javascript.com/learn/variables"&gt;https://www.javascript.com/learn/variables&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.toolsqa.com/javascript/javascript-variables"&gt;https://www.toolsqa.com/javascript/javascript-variables&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://data-flair.training/blogs/javascript-variable-tutorial/"&gt;https://data-flair.training/blogs/javascript-variable-tutorial/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://medium.com/@shreyanshshah242/variables-in-javascript-13099376b279"&gt;https://medium.com/@shreyanshshah242/variables-in-javascript-13099376b279&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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