<?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>Forem: Ron Northcutt</title>
    <description>The latest articles on Forem by Ron Northcutt (@rlnorthcutt).</description>
    <link>https://forem.com/rlnorthcutt</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%2F1165280%2F3938e619-df7d-42ff-89c4-6133c1b70991.jpeg</url>
      <title>Forem: Ron Northcutt</title>
      <link>https://forem.com/rlnorthcutt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rlnorthcutt"/>
    <language>en</language>
    <item>
      <title>Variables in Javascript: A Comprehensive Guide to Var, Let, and Const</title>
      <dc:creator>Ron Northcutt</dc:creator>
      <pubDate>Mon, 22 Jan 2024 08:00:00 +0000</pubDate>
      <link>https://forem.com/appsmith/variables-in-javascript-a-comprehensive-guide-to-var-let-and-const-4n86</link>
      <guid>https://forem.com/appsmith/variables-in-javascript-a-comprehensive-guide-to-var-let-and-const-4n86</guid>
      <description>&lt;p&gt;In software, a variable is a symbolic name that represents a &lt;em&gt;value&lt;/em&gt; or a &lt;em&gt;reference to a value&lt;/em&gt;. It's a way to store information that can be reused and manipulated throughout your program. You can think of a variable like a label or even a sign pointer - it represents the data (which can potentially change) but is not the actual data itself. &lt;/p&gt;

&lt;p&gt;I am often reminded of that great scene in the Bruce Lee movie, "Enter the Dragon", where he tells his student that "it is like a finger pointing the way to the moon - don't concentrate on the finger, or you will miss all of that heavenly glory." The variable is important, but what it represents is actually what you care about.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/4O9o4CKTGzQ?start=74"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Ok, maybe that's a little bit of a stretch, but you get the idea. Variables are essential building blocks in JavaScript programming, allowing developers to store and manipulate data. There are 3 main ways to declare a variable, and your choice can have significant implications on your code's behavior and maintainability. Many developers just choose one and use it for everything, but that's just lazy and can sometimes cause problems.&lt;/p&gt;

&lt;p&gt;Let's explore these three methods of variable declaration in JavaScript: &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt;, diving into when and why to use each, along with their respective advantages and drawbacks.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;var&lt;/code&gt;: Function-Scoped Variables
&lt;/h2&gt;

&lt;p&gt;In JavaScript, &lt;code&gt;var&lt;/code&gt; is a keyword used to declare a variable. It has some specific behaviors that set it apart from other variable declaration keywords like &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's what you need to know about &lt;code&gt;var&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Function Scope&lt;/strong&gt;: Variables declared with &lt;code&gt;var&lt;/code&gt; are function-scoped, meaning they are accessible within the function where they were declared (or globally if declared outside any function).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Hoisting&lt;/strong&gt;: Unlike &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;, variables declared with &lt;code&gt;var&lt;/code&gt; are "hoisted" to the top of their containing function or global scope. This means that they are technically available from the beginning of that scope, but their value will be &lt;code&gt;undefined&lt;/code&gt; until the code where they are assigned is executed.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Reassignment&lt;/strong&gt;: You can reassign new values to a variable declared with &lt;code&gt;var&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Redeclaration&lt;/strong&gt;: In non-strict mode, you can redeclare a variable using &lt;code&gt;var&lt;/code&gt; in the same scope without getting an error, which is not allowed with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;No Block Scope&lt;/strong&gt;: &lt;code&gt;var&lt;/code&gt; does not respect block scope (such as inside an &lt;code&gt;if&lt;/code&gt; statement or a loop), which can sometimes lead to unexpected behavior. If you declare a variable with &lt;code&gt;var&lt;/code&gt; inside a block, it's actually available to the entire surrounding function or global scope.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Because of these peculiarities, and with the introduction of &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; in ES6 (ECMAScript 2015), the use of &lt;code&gt;var&lt;/code&gt; has become less common in modern JavaScript, and it's often recommended to use &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; instead for clearer scoping rules and better maintainability.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to Use
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  You need a variable with function-level scope.&lt;/li&gt;
&lt;li&gt;  You are working with older code that doesn't support ES6.&lt;/li&gt;
&lt;li&gt;  Typically, you should avoid &lt;code&gt;var&lt;/code&gt;, but it can be handy in some cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Function Scope:&lt;/strong&gt; Variables are accessible within the entire function where they're declared.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Hoisting:&lt;/strong&gt; Variables are moved to the top of their scope and initialized with &lt;code&gt;undefined&lt;/code&gt;, allowing them to be referenced before declaration.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Lack of Block Scope:&lt;/strong&gt; Variables can be accessed outside the block they were declared in, leading to potential bugs.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Hoisting Quirks:&lt;/strong&gt; Can cause confusion as variables are available before their declaration.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&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;varExample&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;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined, because of hoisting&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 10&lt;/span&gt;

      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Same variable, even though it's in a different block&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;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 20, because var does not have block scope&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. &lt;code&gt;let&lt;/code&gt;: Block-Scoped Variables
&lt;/h2&gt;

&lt;p&gt;So, if we had &lt;code&gt;var&lt;/code&gt;, why did we need something else? The reason why the &lt;code&gt;let&lt;/code&gt; keyword was introduced to javascript was because &lt;em&gt;function&lt;/em&gt; scope is confusing and this led to a number of bugs and errors. &lt;code&gt;let&lt;/code&gt; was was introduced in ES6 (ECMAScript 2015) as an alternative to var, with some key differences in behavior:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Block Scope&lt;/strong&gt;: Unlike var, variables declared with let are block-scoped, meaning they are only accessible within the block in which they were declared (e.g., inside an if statement or a loop).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;No Hoisting&lt;/strong&gt;: Although let declarations are hoisted, the variables are not initialized until the code execution reaches the declaration. Attempting to access the variable before its declaration will result in a ReferenceError.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Reassignment&lt;/strong&gt;: Like var, you can reassign new values to a variable declared with let.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;No Redeclaration&lt;/strong&gt;: In strict mode, you cannot redeclare a variable using let in the same scope.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  When to Use
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  You need a variable with block-level scope.&lt;/li&gt;
&lt;li&gt;  You expect to reassign the variable within its scope.&lt;/li&gt;
&lt;li&gt;  Probably should be your default method for creating variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Block Scope:&lt;/strong&gt; Variables are only accessible within the block where they're declared, reducing potential errors.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;No Hoisting Issues:&lt;/strong&gt; Variables are not initialized until the code execution reaches the declaration.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;More Limited Scope:&lt;/strong&gt; Might require more careful planning of where the variable is declared.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&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;letExample&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;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError, because it doesn't exist yet (no hoisting)&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Different variable because it's in a different block&lt;/span&gt;
            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I see how it works&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="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;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 10, because we are in the block for the original variable&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;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError, because y is block-scoped&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. &lt;code&gt;const&lt;/code&gt;: Block-Scoped Immutable References
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;const&lt;/code&gt; keyword, which, like &lt;code&gt;let&lt;/code&gt;, was introduced in ES6 (ECMAScript 2015). It has similarities to &lt;code&gt;let&lt;/code&gt;, but with some unique characteristics:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Block Scope&lt;/strong&gt;: Just like &lt;code&gt;let&lt;/code&gt;, variables declared with &lt;code&gt;const&lt;/code&gt; are block-scoped.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;No Hoisting&lt;/strong&gt;: Similar to &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt; declarations are hoisted, but accessing them before their declaration in the code results in a ReferenceError.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;No Reassignment&lt;/strong&gt;: Unlike &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt;, once a variable is assigned with &lt;code&gt;const&lt;/code&gt;, it cannot be reassigned. Attempting to do so will result in a TypeError.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;No Redeclaration&lt;/strong&gt;: You cannot redeclare a variable using &lt;code&gt;const&lt;/code&gt; in the same scope.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Must Be Initialized&lt;/strong&gt;: A &lt;code&gt;const&lt;/code&gt; declaration must be initialized with a value at the time it's declared.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  When to Use
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  You want to declare a variable that should not be reassigned.&lt;/li&gt;
&lt;li&gt;  You need block-level scoping.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Immutable Reference:&lt;/strong&gt; Prevents reassignment, making the code more predictable.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Block Scope:&lt;/strong&gt; Like &lt;code&gt;let&lt;/code&gt;, variables are only accessible within the block where they're declared.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Not Fully Immutable:&lt;/strong&gt; Only the reference is constant, not the object itself. If the variable is an object, its properties can still be altered.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&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;constExample&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="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;PI&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError, because it doesn't exist yet (no hoisting)&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14159&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: Assignment to constant variable&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;PI&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError, because PI is block-scoped&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&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="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This is fine because object properties can be changed&lt;/span&gt;
        &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// TypeError, reassignment not allowed&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In modern JavaScript development, understanding the appropriate use of &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt; is crucial. While you can safely plan to use &lt;code&gt;let&lt;/code&gt; most of the time, it is important to understand how block scope works so you know when it makes sense.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Use &lt;code&gt;var&lt;/code&gt; for function-level scope, mainly when dealing with &lt;strong&gt;older code&lt;/strong&gt; or specific scoping needs.&lt;/li&gt;
&lt;li&gt;  Utilize &lt;code&gt;let&lt;/code&gt; when block-level scope is required, and you &lt;strong&gt;expect the value to change within the block&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  Opt for &lt;code&gt;const&lt;/code&gt; when you want to ensure that the reference to a &lt;strong&gt;value stays constant within a block scope&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By carefully choosing the right declaration method, developers can write code that is not only more readable but also more resilient and less prone to errors and unexpected behavior. It is usually better to hit scope-related errors earlier (more strict) than later (less strict).&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>variables</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>A Hands-on Guide to Git Rebase &amp; Resolving Conflicts</title>
      <dc:creator>Ron Northcutt</dc:creator>
      <pubDate>Fri, 08 Dec 2023 07:00:00 +0000</pubDate>
      <link>https://forem.com/appsmith/a-hands-on-guide-to-git-rebase-resolving-conflicts-59d5</link>
      <guid>https://forem.com/appsmith/a-hands-on-guide-to-git-rebase-resolving-conflicts-59d5</guid>
      <description>&lt;h2&gt;
  
  
  Goal
&lt;/h2&gt;

&lt;p&gt;By the end of this tutorial, you will have a practical understanding of how to use git rebase to integrate changes from one branch to another and how to maintain a clean commit history.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Want a more information about this image? Checkout the "&lt;a href="https://community.appsmith.com/content/guide/understanding-git-rebase" rel="noopener noreferrer"&gt;Understanding Git Rebase&lt;/a&gt;" guide.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic understanding of Git&lt;/li&gt;
&lt;li&gt;Git installed on your system&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Git rebase&lt;/code&gt; is a powerful command that allows you to integrate changes from one branch into another. Instead of merging, which takes all the changes in one branch and merges them into another with a new &lt;code&gt;merge&lt;/code&gt; commit, &lt;code&gt;rebase&lt;/code&gt; takes a different approach. It replays your changes on top of the branch you're rebasing onto. This results in a cleaner, more linear commit history.&lt;/p&gt;

&lt;p&gt;Of course, it's not all sunshine and rainbows - problems do pop up. So, lets take a look at how to use &lt;code&gt;git rebase&lt;/code&gt; and also how to deal with conflicts when they arise.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxmkfccc2uwpxa9cm3qzr.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%2Fxmkfccc2uwpxa9cm3qzr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Set up a Test Repository
&lt;/h3&gt;

&lt;p&gt;Lets go ahead and create a test repository that we can use to play around with &lt;code&gt;git rebase&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;# Initialize a new repository
git init rebase-demo
cd rebase-demo

# Create a main branch for primary development
git checkout -b main

# Create a file and commit it to the main branch
echo "Initial content" &amp;gt; file.txt
git add file.txt
git commit -m "Initial commit on main"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Create a New Feature Branch
&lt;/h3&gt;

&lt;p&gt;Now, let's create a new feature branch that we will &lt;code&gt;rebase&lt;/code&gt; into the main branch later. We will create a simple file and commit that change to the branch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create a feature branch and add a file to it.
git checkout -b feature-branch
echo "Some feature content" &amp;gt;&amp;gt; file.txt
git commit -am "Add feature content"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Make More Changes to the Main Branch
&lt;/h3&gt;

&lt;p&gt;Let's go back to the main branch and make some more changes. These changes are what our &lt;code&gt;rebase&lt;/code&gt; will be added to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Go back to the main branch and make changes
git checkout main
echo "Another line in main" &amp;gt;&amp;gt; file.txt
git commit -am "Update in main"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you have two branches: &lt;code&gt;main&lt;/code&gt; and &lt;code&gt;feature-branch&lt;/code&gt;, both with unique commits.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F14hwmfasg34f01xf5t63.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%2F14hwmfasg34f01xf5t63.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  4. Rebase the Feature Branch
&lt;/h3&gt;

&lt;p&gt;Now, the magic happens. We switch to the feature branch, and &lt;code&gt;rebase&lt;/code&gt; it &lt;em&gt;into&lt;/em&gt; main.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout feature-branch
git rebase master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait, is that it? Is it really that simple?&lt;/p&gt;

&lt;p&gt;It can be, for sure! Assuming all went well, the commit history will show the &lt;code&gt;feature-branch&lt;/code&gt; commits will be on the main branch. Those experienced or very observant developers may have noticed a problem above... we will have a conflict. However, not all is lost. Let's take a look at how you can adapt to this situation and fix any minor problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Resolve Conflicts
&lt;/h3&gt;

&lt;p&gt;During the rebase, you'll encounter a conflict because line 2 of &lt;code&gt;file.txt&lt;/code&gt; is different in each branch. That's ok - no need to panic. All merge conflicts can be fixed with the same basic steps. Here's how to resolve it: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Identifying the Conflict&lt;/strong&gt;: Git will indicate that there's a conflict. You'll see a message similar to:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
error: could not apply a669cc6... Add feature content
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This message tells you that there's a conflict in &lt;code&gt;file.txt&lt;/code&gt; that needs resolution.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Evaluating the status&lt;/strong&gt;: One of the great things about Git is that you can always ask for a status update to see what is going on and what you may need to work on. Just make a note of the problems to fix and handle them one at a time.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interactive rebase in progress; onto 117fde3
Last command done (1 command done):
    pick a669cc6 Add feature content
No commands remaining.
You are currently rebasing branch 'feature-branch' on '117fde3'.
    (fix conflicts and then run "git rebase --continue")
    (use "git rebase --skip" to skip this patch)
    (use "git rebase --abort" to check out the original branch)
Unmerged paths:
    (use "git restore --staged &amp;lt;file&amp;gt;..." to unstage)
    (use "git add &amp;lt;file&amp;gt;..." to mark resolution)
    both modified:   file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Viewing the Conflict&lt;/strong&gt;: Depending on how many changes you are dealing with, you may know exactly what the problem is. Alternatively, you can also use &lt;code&gt;git diff&lt;/code&gt; to see the changes. Open &lt;code&gt;file.txt&lt;/code&gt; in your favorite editor. You'll see the conflicting changes demarcated like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Initial content
&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; HEAD
Another line in master
=======
Some feature content
 &amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Add feature content
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The content between &lt;code&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; HEAD&lt;/code&gt; and &lt;code&gt;=======&lt;/code&gt; is from the &lt;code&gt;main&lt;/code&gt; branch, and the content between &lt;code&gt;=======&lt;/code&gt; and &lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Add feature content&lt;/code&gt; is from the &lt;code&gt;feature-branch&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Resolving the Conflict&lt;/strong&gt;: Edit &lt;code&gt;file.txt&lt;/code&gt; to keep the content you want. For instance, if you want to keep the content from both branches but want the feature content to appear before the master content, you'd modify &lt;code&gt;file.txt&lt;/code&gt; to look like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Initial content
Some feature content
Another line in master
Save and close the file.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Marking the Conflict as Resolved&lt;/strong&gt;: After resolving the conflict, mark it as resolved with Git:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Continuing the Rebase&lt;/strong&gt;: Once the conflict is resolved and marked as such, continue the &lt;code&gt;rebase&lt;/code&gt; process:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git rebase --continue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there are more conflicts, Git will pause again and let you resolve them. If there are no more conflicts, the &lt;code&gt;rebase&lt;/code&gt; will be completed.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Check the Commit History:
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;git log&lt;/code&gt; to see a clean, linear history of commits, showing that the feature branch commits now come after the master branch commits. Once the &lt;code&gt;rebase&lt;/code&gt; is successful, it will be a fast-forward merge, maintaining a linear history.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffso2js9ctdkajc747wx8.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%2Ffso2js9ctdkajc747wx8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;Git rebase&lt;/code&gt; is a powerful tool for maintaining a clean commit history and integrating changes from one branch into another. While it can be a bit more complex than merging, especially when conflicts arise, the resulting linear history can be worth the extra effort, especially in large projects.&lt;/p&gt;

&lt;p&gt;Remember, practice makes perfect. Feel free to experiment with git in safe environments (like our demo repo) before applying it to larger projects. Happy rebasing!&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/docs/git-rebase" rel="noopener noreferrer"&gt;Git Rebase Docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://community.appsmith.com/content/guide/understanding-git-rebase" rel="noopener noreferrer"&gt;Understanding Git Rebase&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>rebase</category>
      <category>tutorial</category>
      <category>developer</category>
    </item>
    <item>
      <title>How to Correctly Execute a Loop Synchronously with Javascript</title>
      <dc:creator>Ron Northcutt</dc:creator>
      <pubDate>Wed, 11 Oct 2023 08:37:59 +0000</pubDate>
      <link>https://forem.com/appsmith/how-to-correctly-execute-a-loop-synchronously-with-javascript-4gkm</link>
      <guid>https://forem.com/appsmith/how-to-correctly-execute-a-loop-synchronously-with-javascript-4gkm</guid>
      <description>&lt;p&gt;Understanding synchronous and asynchronous operations is fundamental when working with JavaScript, especially when dealing with loops. In this article, we will explore how to run loops synchronously, even when they contain asynchronous tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Synchronous for Loop
&lt;/h2&gt;

&lt;p&gt;To execute a loop synchronously with JavaScript, you can use the for loop. The for loop allows you to iterate over a sequence of values and execute a block of code for each value. By default, the for loop is synchronous, meaning that each iteration will wait for the previous iteration to complete before starting.&lt;/p&gt;

&lt;p&gt;Here's an example of a for loop that counts from 1 to 10 synchronously:&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;1&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;amp;&lt;/span&gt;&lt;span class="nx"&gt;lt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="o"&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;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;p&gt;In this example, the loop starts with i equal to 1 and increments i by 1 each time through the loop until i is equal to 10. The console.log() statement inside the loop will be executed for each value of i, printing the value to the console.&lt;/p&gt;

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

&lt;p&gt;However, real-world scenarios might require you to perform asynchronous tasks within your loop, like fetching data from a server. Here's where understanding the synchronous nature of loops becomes essential.&lt;/p&gt;

&lt;p&gt;For instance, if you used JavaScript's native fetch method or another API call within a loop, it would not work synchronously by default. Each request would be initiated almost simultaneously, and the responses would likely return out of order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making Loops Synchronous with Asynchronous Tasks
&lt;/h2&gt;

&lt;p&gt;If you need to execute an asynchronous task inside the loop, such as making an API call or fetching data, you can use the async/await syntax to wait for the task to complete before moving on to the next iteration of the loop. Here's an 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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchData&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="c1"&gt;// Simulating an API call&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&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;setTimeout&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;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Data for &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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;1000&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myFunction&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;1&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetchData&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;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;data&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the fetchData() function makes an asynchronous API call and returns the result. Inside the myFunction() loop, we use the await keyword to wait for the API call to complete before moving on to the next loop iteration. This ensures that the loop is executed synchronously, with each iteration waiting for the previous iteration to complete before starting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understand your needs
&lt;/h2&gt;

&lt;p&gt;While using async/await within loops guarantees order, it might not be the most efficient approach for all use cases, especially if the asynchronous operations don't depend on the result of the previous one. In such cases, running them concurrently might be more time-efficient. You might also want to use events or even Promise.all() to handle all the results when they are complete.&lt;/p&gt;

&lt;p&gt;Using loops synchronously in JavaScript is straightforward. However, when introducing asynchronous operations within loops, it's essential to ensure they maintain the desired flow. The async/await syntax offers a neat way to achieve this, but it's vital to consider the efficiency and nature of the tasks when applying this method.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Evolution of Git: A Dive Into Tech History</title>
      <dc:creator>Ron Northcutt</dc:creator>
      <pubDate>Tue, 19 Sep 2023 16:49:19 +0000</pubDate>
      <link>https://forem.com/appsmith/the-evolution-of-git-a-dive-into-tech-history-2j52</link>
      <guid>https://forem.com/appsmith/the-evolution-of-git-a-dive-into-tech-history-2j52</guid>
      <description>&lt;p&gt;In the earliest days of my career, I never used version control. At that time, Git didn't exist and most version control systems were clunky tools used mostly by corporate engineering teams. I was doing a lot of freelance consulting, so most of my projects were just me working alone. SFTP, occasional backups, and lots of &lt;code&gt;*.bak&lt;/code&gt; files worked reasonably well. Still, I lost work or wished I could return to an earlier version after multiple refactors... especially since I was still learning.&lt;/p&gt;

&lt;p&gt;When I joined my first team as a developer, I was introduced to "Subversion". This was a whole new world - the ability to see all changes, go back in time, and to safely make changes was great. It changed the way I thought about code. The problem is that it was slooooow. I was working on a large project, and updates took quite a while.&lt;/p&gt;

&lt;p&gt;Then, I joined a company as a Sr. Developer, and they were using this new thing called "Git". I wasn't familiar with it, and actually, my first commit to the main branch broke the app in production &lt;em&gt;(cue heart attack)&lt;/em&gt;! I had no idea about merge conflicts or how to fix them. However, once I got the basics down, I became a huge fan. It was FAST. So much faster than Subversion, and really much easier to use. With just a few commands, I was fairly proficient, and for more complex things I could easily find help online.&lt;/p&gt;

&lt;p&gt;Today, it is almost impossible to learn about software development without learning about Git. For many developers, the command &lt;code&gt;git commit&lt;/code&gt; is a daily ritual. It's the heartbeat of countless projects yet, how many of us know the story behind this powerful tool? We've all heard bits and pieces, but let's take a deeper look at Git and how it came into being.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Git?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;"I'm a very lazy person who likes to get credit for things other people actually do. In the Linux project, I started it, and I'm still considered the central point, but I'm not a good programmer. I'm a good manager. My only job is to say 'no' to people, and sometimes I accept patches and put them into my tree. I wanted a tool where I can do that. That was my only design criteria, and it turns out when you do that kind of tool, you can use it for a lot of other things too."&lt;/p&gt;

&lt;p&gt;-- Linus Torvalds&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To start with, what the heck is Git? At its core, Git is a distributed version control system (VCS). It lets multiple users track changes in source code during software development, maintaining a history of code changes, and ensuring traceability. It is like a &lt;strong&gt;shared digital diary&lt;/strong&gt; for computer code, allowing many people to write in it while keeping a record of all changes made over time.&lt;/p&gt;

&lt;p&gt;But unlike many version control systems that came before it, Git operates on a distributed model, meaning every developer's working copy of the code is also a repository with complete history and version tracking abilities, independent of network access. This means that developers can work locally on their machine, and then sync those changes later. This was a big change in how VCS affects the daily workflow of a developer.&lt;/p&gt;

&lt;p&gt;One of Git's key advantages is its efficiency in handling changes. Instead of sending an entire copy of the codebase each time, Git only transmits the differences (or changes) from the last known version. This approach significantly reduces the amount of data transmitted and results in faster sync times, providing a performance advantage, especially for large projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Genesis: Why Was Git Created?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;"So I’d like to stress that while it really came together in just about ten days or so (at which point I did my first *kernel* commit using git), it wasn’t like it was some kind of mad dash of coding. The actual amount of that early code is actually fairly small, it all depended on getting the basic ideas right. And that I had been mulling over for a while before the whole project started. I’d seen the problems others had. I’d seen what I wanted to avoid doing."&lt;/p&gt;

&lt;p&gt;-- Linus Torvalds&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Flashback to 2005. The Linux kernel, an enormous open-source project, used a proprietary VCS called BitKeeper. However, due to a conflict between the community and the company behind BitKeeper, the free-of-charge status was revoked. Necessity is the mother of invention, and this incident paved the way for the creation of a new system. Linus Torvalds, the creator of the Linux kernel, took the reins. He aimed to create a tool that was:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Distributed:&lt;/strong&gt; Unlike other systems where a central repository was required.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Compatible:&lt;/strong&gt; The new tool would incorporate as many features and workflows as BitKeeper.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Secure:&lt;/strong&gt; Ensure the integrity of source code and protect against corruption, accidental or malicious.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, after a few days of work, Torvalds had a working prototype of Git, and less than a month later, Git was managing the Linux kernel source code. On April 7, 2005, Linus Torvalds made the &lt;a href="https://github.com/git/git/commit/e83c5163316f89bfbde7d9ab23ca2e25604af290"&gt;first-ever commit to Git&lt;/a&gt;. It wasn’t a grand feature; it was a simple README file. However, the major version (v1.0) took its time and was released on December 21, 2005. But, what's interesting is that version 2.0 was released almost a &lt;em&gt;decade&lt;/em&gt; later on June 1, 2014.&lt;/p&gt;

&lt;p&gt;Git got the important things right in the beginning, and that set the stage for a valuable project.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Showdown: Git vs. CVS
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;'Well, it was obviously designed for our workflow, so that is part of it. I’ve already mentioned the whole “distributed” part many times, but it bears repeating. But it was also designed to be efficient enough for a biggish project like Linux, and it was designed to do things that people considered “hard” before git – because those are the things *I* do every day.'&lt;/p&gt;

&lt;p&gt;-- Linus Torvalds&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Concurrent Versions System (CVS) was one of the pioneering tools in the version control domain. So how does Git stand out when compared to such a veteran system?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Distributed vs. Centralized:&lt;/strong&gt; CVS follows a centralized model, meaning the version history is stored in a central server. If that server crashes without any backups, you lose everything. Git’s distributed approach ensures that every developer has a local copy of the entire history, making it decentralized and significantly safer.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Performance:&lt;/strong&gt; Git’s local operations, thanks to its distributed nature, means that many tasks are faster in Git than CVS. There's no need to communicate with a central server for every tiny operation.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Branch Management:&lt;/strong&gt; Branching in Git is a walk in the park. It's an integral part of the workflow. In CVS, branching is a cumbersome process, often avoided due to its complexity.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Atomic Operations:&lt;/strong&gt; Git operations, like commits, happen atomically. Either they succeed with all changes or fail without any. This isn't always the case with CVS.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Data Integrity:&lt;/strong&gt; Git uses a SHA-1 hash to manage data, ensuring the repository’s integrity. If something goes awry, it's immediately noticeable.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Notable Performance Improvements in Git
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;"Just to pick an example: the concept of “merging” was generally considered to be something really quite painful and hard in most SCM’s. You’d plan your merges, because they were big deals. That’s not acceptable to me, since I commonly do tens of merges a day when in the merge window, and even then, the biggest overhead shouldn’t be the merge itself, it should be testing the result. The “git” part of the merge is just a couple of seconds, it should take me much longer just to write the merge explanation message."&lt;/p&gt;

&lt;p&gt;-- Linus Torvalds&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Git's inception was driven by the need for performance, but that didn’t stop the community from further refining it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Packed Refs:&lt;/strong&gt; Instead of keeping every single object (commit, tree, blob) as individual files, Git packs them.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Delta Compression:&lt;/strong&gt; When packing, Git identifies the differences between versions and &lt;em&gt;stores just the changes&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Garbage Collection:&lt;/strong&gt; Over time, some objects become obsolete. Git has a garbage collector that removes these unnecessary objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These optimizations, combined with many under-the-hood improvements, ensure that Git remains lightning-fast, even for mammoth repositories. Soon after its creation, developers around the world started contributing. Junio Hamano is one such name that stands out. Within just a few months of Git's inception, he took over its maintenance, ensuring it didn’t remain just a side project but evolved into a robust system.&lt;/p&gt;

&lt;p&gt;Under Hamano's stewardship and with contributions from developers worldwide, Git was not just about committing or branching. It introduced:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Staging Area&lt;/strong&gt;: This intermediary area allows developers to format and review commits before finalizing them.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Remote Repositories&lt;/strong&gt;: With platforms like GitHub and GitLab, remote repositories became a staple, facilitating collaboration among developers globally.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Hooks&lt;/strong&gt;: Custom scripts triggered by important actions, enhancing Git's capabilities and automation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's hard to imagine Git without some of these features today, but think about how innovative and unique these were when they were created.&lt;/p&gt;

&lt;h3&gt;
  
  
  "Git": An Odd Name with a Humorous Origin
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;“I'm an egotistical bastard, so I name all my projects after myself. First Linux, now Git."&lt;/p&gt;

&lt;p&gt;-- Linus Torvalds&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In an email, when asked about the name, he once quipped, “The name 'git' was given by Linus Torvalds when he wrote the very first version. He described the tool as ‘the stupid content tracker’ and the name as (depending on your way): random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of 'get' may or may not be relevant. Stupid. Contemptible and despicable. Simple. Take your pick from the dictionary of slang.”&lt;/p&gt;

&lt;p&gt;"Git" is British slang for a silly or contemptible person. Silly? Irreverent? Absolutely! But it’s another testament to Torvald’s personality and the informal, community-driven spirit of open-source software.&lt;/p&gt;

&lt;h3&gt;
  
  
  Git's Journey Continues
&lt;/h3&gt;

&lt;p&gt;From its rapid inception to becoming the backbone of software versioning, Git’s journey is a testament to open-source power. Its success lies not just in its utility but in a global community that continues to nurture and refine it. Git has transitioned from a quick solution to a kernel project hiccup into an essential tool for individual developers and tech giants alike. Its distributed nature, performance optimizations, and robust branching capabilities make it superior to many version control predecessors.&lt;/p&gt;

&lt;p&gt;So, as you &lt;code&gt;git push&lt;/code&gt; your next big feature or troubleshoot with a &lt;code&gt;git blame&lt;/code&gt;, take a moment to appreciate this tool's history, and perhaps, share its story. Because, in a way, every developer using Git today is part of its ongoing saga.&lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@yancymin?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Yancy Min&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/842ofHC6MaI?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>linux</category>
      <category>opensource</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
