<?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: Garrin Costa, Jr.</title>
    <description>The latest articles on Forem by Garrin Costa, Jr. (@gmcjr).</description>
    <link>https://forem.com/gmcjr</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%2F3892762%2Fee42a687-4cd8-4265-ac77-af583fb876b8.jpeg</url>
      <title>Forem: Garrin Costa, Jr.</title>
      <link>https://forem.com/gmcjr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gmcjr"/>
    <language>en</language>
    <item>
      <title>What’s Actually Happening When You Use Git</title>
      <dc:creator>Garrin Costa, Jr.</dc:creator>
      <pubDate>Mon, 25 May 2026 13:48:05 +0000</pubDate>
      <link>https://forem.com/gmcjr/whats-actually-happening-when-you-use-git-464e</link>
      <guid>https://forem.com/gmcjr/whats-actually-happening-when-you-use-git-464e</guid>
      <description>&lt;p&gt;I’d been using Git comfortably for months before getting uncomfortable with how little I actually understood.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git add&lt;/code&gt;. &lt;code&gt;git commit&lt;/code&gt;. &lt;code&gt;git push&lt;/code&gt; were memory at this point. But ask me what Git was actually doing under the hood? I couldn’t tell you. I had some (mostly wrong) assumptions but that was about it.&lt;/p&gt;

&lt;p&gt;Then I discovered the &lt;code&gt;.git/&lt;/code&gt; directory…&lt;/p&gt;




&lt;h2&gt;
  
  
  Git Is Not What You Think It Is
&lt;/h2&gt;

&lt;p&gt;Many people think of Git as a “track changes” tool. That’s the way I thought of it. Like the version history in a Google Doc. That’s not wrong, but it’s not the whole picture.&lt;/p&gt;

&lt;p&gt;Git’s creator, Linus Torvalds, described it more precisely:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“Git is fundamentally a content-addressable filesystem with a VCS user interface written on top of it.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, what is a content-addressable filesystem?&lt;/p&gt;

&lt;p&gt;Instead of storing files by name or location, Git stores them by their &lt;strong&gt;content&lt;/strong&gt;. Every piece of data gets run through a hashing algorithm (SHA-1) that produces a unique 40-character fingerprint. That fingerprint is the file’s identity in Git’s database. The same content will always return the same hash, always.&lt;/p&gt;

&lt;p&gt;Understanding this immediately explained much of what felt confusing about Git.&lt;/p&gt;




&lt;h2&gt;
  
  
  The &lt;code&gt;.git/&lt;/code&gt; Directory Is Your Entire Repository
&lt;/h2&gt;

&lt;p&gt;Open any Git project and run this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; .git/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HEAD    config    description    hooks    info    objects    refs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.git/&lt;/code&gt; is a hidden directory created when a Git repository is initialized.&lt;br&gt;
This directory is your entire repository. Every commit, every version of every file, the full history — all of it lives inside &lt;code&gt;.git/&lt;/code&gt;. Your working directory (the files you actually edit) is just a projection of what’s in there.&lt;/p&gt;

&lt;p&gt;You can copy &lt;code&gt;.git/&lt;/code&gt; somewhere else, and you’ll have everything. On the other hand, if you delete &lt;code&gt;.git/&lt;/code&gt; you lose your entire history. Not just your latest changes. Everything.&lt;br&gt;
It’s hidden for a good reason, but it is hugely helpful to be aware of it.&lt;/p&gt;

&lt;p&gt;Two very important directories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;objects/&lt;/code&gt;&lt;/strong&gt; — Git’s content database. Every blob, tree, and commit lives here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;refs/&lt;/code&gt;&lt;/strong&gt; — Pointers. Branch names, tags, remotes. All of them are just pointers to commits in &lt;code&gt;objects/&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Three Objects. One Chain.
&lt;/h2&gt;

&lt;p&gt;Git stores everything as one of three object types. Understanding these three types makes everything else click.&lt;/p&gt;
&lt;h3&gt;
  
  
  Blob — The File
&lt;/h3&gt;

&lt;p&gt;A blob (Binary Large OBject) is an immutable object with a header. It contains the raw file content and nothing more.&lt;br&gt;
It does not store the filename or its location. It has no reference to itself or any other files.&lt;br&gt;
If one character in a file changes, Git creates a brand new blob with a completely different SHA. The old one stays untouched.&lt;/p&gt;

&lt;p&gt;Run this on any blob SHA to see for yourself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git cat-file &lt;span class="nt"&gt;-p&lt;/span&gt; &amp;lt;blob SHA&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll see the raw file content. No filename. No path. Just the content.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tree — The Snapshot
&lt;/h3&gt;

&lt;p&gt;A tree object represents a directory. It’s a list of entries — each one containing a file mode, object type, SHA, and filename.&lt;/p&gt;

&lt;p&gt;This is where filenames live. The tree connects a name to a blob. Run &lt;code&gt;git cat-file -p&lt;/code&gt; on a tree SHA and you’ll see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100644 blob b7aec520dec0a7516c18eb4c68b64ae1eb9b5a5e    README.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mode, type, SHA, filename. The tree is the snapshot of your project at a given moment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Commit — The Pointer
&lt;/h3&gt;

&lt;p&gt;A commit object is simpler than most people expect. Run &lt;code&gt;git cat-file -p&lt;/code&gt; on a commit SHA:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="n"&gt;tree&lt;/span&gt; &lt;span class="m"&gt;7102&lt;/span&gt;&lt;span class="n"&gt;e6ffc1a508e552d53f28bbeb0a976124d7e6&lt;/span&gt;
&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="n"&gt;d48a0fbe5c378c69ad9522883d1d2f8dbe5ebd69&lt;/span&gt;
&lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="n"&gt;Gmcjr&lt;/span&gt; &amp;lt;&lt;span class="n"&gt;gmcostajr&lt;/span&gt;@&lt;span class="n"&gt;gmail&lt;/span&gt;.&lt;span class="n"&gt;com&lt;/span&gt;&amp;gt; &lt;span class="m"&gt;1779654747&lt;/span&gt; -&lt;span class="m"&gt;0500&lt;/span&gt;
&lt;span class="n"&gt;committer&lt;/span&gt; &lt;span class="n"&gt;Gmcjr&lt;/span&gt; &amp;lt;&lt;span class="n"&gt;gmcostajr&lt;/span&gt;@&lt;span class="n"&gt;gmail&lt;/span&gt;.&lt;span class="n"&gt;com&lt;/span&gt;&amp;gt; &lt;span class="m"&gt;1779654747&lt;/span&gt; -&lt;span class="m"&gt;0500&lt;/span&gt;

&lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="n"&gt;commit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it. A commit is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A pointer to a tree (the snapshot)&lt;/li&gt;
&lt;li&gt;A pointer to its parent commit (the history)&lt;/li&gt;
&lt;li&gt;Metadata that includes the author (who made changes/wrote the code) and committer (who made the commit), as well as when the commit was made, and the commit message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The chain is always:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;commit → tree → blob(s)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;There’s actually a fourth object type — the annotated tag. I won’t go into detail here, but the annotated tag wraps a commit with its own metadata and SHA, and is often used for marking official releases.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The History Is a Graph, Not a Line
&lt;/h2&gt;

&lt;p&gt;The chain — commit → tree → blob — implies something linear. It isn’t.&lt;/p&gt;

&lt;p&gt;Every commit points to its parent. Most commits have one. Merge commits have two. Over time, those parent-child relationships form a &lt;strong&gt;DAG: a Directed Acyclic Graph&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Directed means the edges go one way — a commit points to its parent, never the reverse. Acyclic means no loops — you can’t follow the chain and end up back where you started. Graph means the structure branches and rejoins freely.&lt;/p&gt;

&lt;p&gt;This is why &lt;code&gt;git log&lt;/code&gt; can look like a tree but isn’t one. Trees don’t rejoin. A DAG can. Every time you merge two branches, the merge commit has two parent pointers — two incoming edges — and the graph folds back on itself.&lt;/p&gt;

&lt;p&gt;Understanding that Git’s history is a DAG explains a lot: why &lt;code&gt;git log --graph&lt;/code&gt; draws forks and merges the way it does, why reachability matters (an object is “live” if you can reach it by following edges from a named ref), and why Git’s garbage collector targets objects with no incoming path from any branch or tag.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Stuff That Trips People Up
&lt;/h2&gt;

&lt;h3&gt;
  
  
  A Branch Is a Text File
&lt;/h3&gt;

&lt;p&gt;Run this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; .git/refs/heads/main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll get a single SHA. That’s your &lt;code&gt;main&lt;/code&gt; branch. A 41-character text file. That’s the entire concept of a branch in Git — a file containing the SHA of the commit it currently points to.&lt;/p&gt;

&lt;p&gt;Creating a branch creates a file. Switching branches changes which file HEAD points to. Deleting a branch deletes the file.&lt;/p&gt;

&lt;p&gt;Which brings us to something important.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deleting a Branch Doesn’t Delete Your Commits
&lt;/h3&gt;

&lt;p&gt;When you delete a branch, you delete the pointer. You do not delete the objects in &lt;code&gt;.git/objects/&lt;/code&gt;. The commits, trees, and blobs that branch pointed to are still in the database. They’re just unreachable by name.&lt;/p&gt;

&lt;p&gt;You can prove it. Delete a branch, then run &lt;code&gt;git cat-file -p&lt;/code&gt; on a commit SHA that was on it. It’s still there.&lt;/p&gt;

&lt;p&gt;Git’s garbage collector (&lt;code&gt;git gc&lt;/code&gt;) will eventually clean up truly unreachable objects — but not for at least two weeks by default. Which means if you accidentally delete a branch, &lt;code&gt;git reflog&lt;/code&gt; can save you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Staging Already Writes to the Database
&lt;/h3&gt;

&lt;p&gt;Most people think &lt;code&gt;git add&lt;/code&gt; just “queues” a file for the next commit. It does more than that.&lt;/p&gt;

&lt;p&gt;When you run &lt;code&gt;git add&lt;/code&gt;, Git immediately:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Hashes your file content&lt;/li&gt;
&lt;li&gt;Creates a blob object&lt;/li&gt;
&lt;li&gt;Writes it to &lt;code&gt;.git/objects/&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Updates the index (staging area) to track it&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The blob is already stored before you ever run &lt;code&gt;git commit&lt;/code&gt;. The commit just formalizes it — creating the tree and commit objects that make the blob part of your permanent history.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;git reset&lt;/code&gt; Moves a Pointer. It Doesn’t Delete Data.
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;git reset --hard &amp;lt;SHA&amp;gt;&lt;/code&gt; feels destructive. It’s not — at least not immediately.&lt;/p&gt;

&lt;p&gt;What it actually does: moves the branch pointer back to the specified commit. The commits you “reset away” are still in &lt;code&gt;.git/objects/&lt;/code&gt;. They’re just unreachable from your current branch.&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;git reflog&lt;/code&gt; after a reset and you’ll see every place HEAD has pointed. Your “lost” commits are listed there, recoverable by SHA.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git revert&lt;/code&gt; works differently. Instead of moving a pointer, it creates a brand new commit that applies the inverse of a previous commit’s changes. History is preserved. This is why &lt;code&gt;git revert&lt;/code&gt; is safe on shared branches and &lt;code&gt;git reset&lt;/code&gt; is not — reset rewrites history, revert adds to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Merge Commits Have Two Parents
&lt;/h3&gt;

&lt;p&gt;When you merge two branches and there’s a conflict, Git compares three blobs: the common ancestor, your version, and their version. This is called a three-way merge.&lt;/p&gt;

&lt;p&gt;If the same lines changed on both sides, Git writes conflict markers to your working directory and waits for you to resolve them. Once you do, &lt;code&gt;git add&lt;/code&gt; creates a new blob. Then &lt;code&gt;git commit&lt;/code&gt; creates a &lt;strong&gt;merge commit&lt;/strong&gt; — the only commit type with two parent pointers, one for each branch tip.&lt;/p&gt;

&lt;p&gt;That two-parent structure is exactly what the DAG looks like in the object database.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Mental Model Matters
&lt;/h2&gt;

&lt;p&gt;You don’t need to know this stuff to use Git day-to-day. But once you do, a few things happen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Detached HEAD stops being scary. It just means HEAD is pointing directly at a commit SHA instead of a branch name.&lt;/li&gt;
&lt;li&gt;Merge conflicts make sense. Two commits changed the same lines. Git can’t pick — it’s asking you to resolve a pointer conflict.&lt;/li&gt;
&lt;li&gt;“Lost” commits stop feeling lost. The objects are almost always still there. &lt;code&gt;git reflog&lt;/code&gt; is your safety net.&lt;/li&gt;
&lt;li&gt;You stop being afraid of &lt;code&gt;git reset&lt;/code&gt;, &lt;code&gt;git rebase&lt;/code&gt;, and other commands that feel dangerous.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git log --graph&lt;/code&gt; stops being decorative. It’s a literal rendering of the DAG — every fork is a branch tip, every convergence is a merge commit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Git isn’t magic. It’s a content-addressable filesystem with a great interface. A few text files, a database of hashed objects, and a set of rules for how they point to each other.&lt;/p&gt;

&lt;p&gt;Open &lt;code&gt;.git/&lt;/code&gt; and look around. It’s less mysterious than you think.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Further reading:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain" rel="noopener noreferrer"&gt;Pro Git, Chapter 10 — Git Internals&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://jwiegley.github.io/git-from-the-bottom-up/" rel="noopener noreferrer"&gt;Git from the Bottom Up — John Wiegley&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>softwaredevelopment</category>
      <category>versioncontrol</category>
      <category>gitinternals</category>
    </item>
    <item>
      <title>What is jQuery Really? A Look Under the Hood</title>
      <dc:creator>Garrin Costa, Jr.</dc:creator>
      <pubDate>Sat, 25 Apr 2026 18:25:40 +0000</pubDate>
      <link>https://forem.com/gmcjr/what-is-jquery-really-a-look-under-the-hood-2h0l</link>
      <guid>https://forem.com/gmcjr/what-is-jquery-really-a-look-under-the-hood-2h0l</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is jQuery Really?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;jQuery can drastically simplify working with HTML, CSS, and JavaScript. Taking a little time to understand how a tool works — the mechanism that drives it — is often the difference between having access to a tool and having command over it.&lt;/p&gt;

&lt;p&gt;To better understand what jQuery is doing under the hood, it is helpful to understand a little bit about the DOM. The DOM (Document Object Model) is a programming interface, built by browsers, which represents the structure of a web document in memory. It is created when a webpage is loaded, representing the document with a tree-like structure. The DOM can be thought of like a bridge or a hub, allowing a webpage's content, styling, and interactivity to be accessed and manipulated dynamically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F23md6mouwa91jit98h7e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F23md6mouwa91jit98h7e.jpg" alt="A look under the hood — engine components exposed, illustrating the mechanism beneath the surface." width="800" height="534"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;A look under the hood — engine components exposed, illustrating the mechanism beneath the surface.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The DOM Problem jQuery Solves&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The DOM exposes a set of built-in browser APIs for accessing and manipulating elements. These APIs offer a world of capabilities and give us precise control, but they are verbose and low-level by design, requiring a lot of explicit instruction for even simple operations.&lt;/p&gt;

&lt;p&gt;jQuery wraps those verbose native APIs in shorter, more readable methods. We write the jQuery shorthand — jQuery handles the verbose part under the hood.&lt;/p&gt;

&lt;p&gt;The example below demonstrates a simple DOM interaction. We're going to grab a button on a webpage and change its color when clicked — the same operation written first in vanilla JavaScript, then in jQuery:&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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;button&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;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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&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="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you might notice, the code needed to perform relatively simple modifications can get cumbersome. This is just ONE modification.&lt;/p&gt;

&lt;p&gt;Now here's the same operation done with jQuery:&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="nf"&gt;$&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;$&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="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;background-color&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;jQuery collapses our JavaScript into just a few lines of clean, readable code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;$ is Just a Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As we pull back the curtain on jQuery, let's first look at the &lt;code&gt;$&lt;/code&gt; symbol.&lt;/p&gt;

&lt;p&gt;A common misconception many beginners have about this is that &lt;code&gt;$&lt;/code&gt; is special syntax — something built into JavaScript itself. Perhaps assuming JavaScript just knows what &lt;code&gt;$&lt;/code&gt; means.&lt;/p&gt;

&lt;p&gt;However, that is not the case. We know that &lt;code&gt;$&lt;/code&gt; is a valid character to use when naming variables in JavaScript, but that's all. On its own, &lt;code&gt;$&lt;/code&gt; means nothing to JavaScript unless defined — which is exactly what happens when jQuery is imported. Both &lt;code&gt;jQuery&lt;/code&gt; and its alias &lt;code&gt;$&lt;/code&gt; are defined as the jQuery function. By convention, most developers use &lt;code&gt;$&lt;/code&gt;, but either works — what matters is consistency. &lt;/p&gt;

&lt;p&gt;We pass element selectors into the function as arguments. It returns an array-like container (the jQuery object) that contains the selected DOM nodes. This gives us access to jQuery's methods, which we can execute on the selected elements.&lt;/p&gt;

&lt;p&gt;To get access to the jQuery library and use it in a project, you can download the library and then in your project's HTML file, create a script tag with a &lt;code&gt;src&lt;/code&gt; attribute that points to your copy of jQuery:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Local download --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"jquery.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another option is to load jQuery via CDN:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- CDN --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://code.jquery.com/jquery-4.0.0.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What You Actually Get Back&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unlike vanilla JavaScript which returns raw DOM elements directly, jQuery returns an array-like container holding the nodes. With jQuery, we manipulate elements through this container, not directly.&lt;/p&gt;

&lt;p&gt;You can see what this looks like for yourself. Open your DevTools console on any jQuery page and run &lt;code&gt;$('p')&lt;/code&gt;. What you get back should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T {0: p, 1: p, 2: p, 3: p, 4: p, 5: p, 6: p, 7: p, 8: p, 9: p.copyright, 10: p, length: 11, prevObject: T}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What you're seeing is the jQuery object. It contains indexed elements and a &lt;code&gt;length&lt;/code&gt; — the number of matched elements — along with some internal jQuery properties (&lt;code&gt;prevObject&lt;/code&gt;) that are beyond the scope of this article. If we expand it, we can see a vast array of methods and properties now accessible to us. This behavior of returning the jQuery object enables us to write the same code whether our selector matches one element or many — jQuery handles the iteration. &lt;/p&gt;

&lt;p&gt;Vanilla JavaScript requires a manual 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;var&lt;/span&gt; &lt;span class="nx"&gt;paragraphs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&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="nx"&gt;paragraphs&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;paragraphs&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;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whereas jQuery silently iterates:&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="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;color&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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;Much simpler in form, easy to follow, and easy to chain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Chaining Works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we call a jQuery function, what we get back is the jQuery object, rather than the raw node that we're operating on. This is by design. What's special about this behavior is that the jQuery object — that container that wraps our raw data — comes equipped with many of jQuery's built-in methods and properties. It gives us something to operate on. This is what makes chaining jQuery methods so simple and effective.&lt;/p&gt;

&lt;p&gt;Why would we want to chain methods? What makes this so useful? Take a look at this 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="nf"&gt;$&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;background-color&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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;slideUp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slideDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we've performed three different operations on our &lt;code&gt;#button&lt;/code&gt; element without having to re-select it three times. It's simple, logical, and sleek.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event Handling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Event handlers wait for a specific event and invoke a callback function when that event occurs. jQuery makes this operation concise and readable. With jQuery, &lt;code&gt;.on()&lt;/code&gt; is the universal event handler. Though not the only way to implement event handling, it is preferred for flexibility and consistency. It looks 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="nf"&gt;$&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;on&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// do something&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;jQuery also provides shorthand methods for specific event types — here are a few common ones.&lt;/p&gt;

&lt;p&gt;Let's say we have some card elements on our page and we want to blur the background when we hover on a card. This requires a couple of functions. In jQuery, we can do this like so:&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="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#card&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;hover&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;$&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="nf"&gt;addClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;expanded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;body&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blurred&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;$&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="nf"&gt;removeClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;expanded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;body&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;removeClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blurred&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="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;$&lt;/code&gt; is a function, into which we pass the &lt;code&gt;#card&lt;/code&gt; element selector. Since jQuery returns its container object, we can then chain &lt;code&gt;.hover()&lt;/code&gt; to the object and pass the functions that will perform our desired action. Those functions can also call the jQuery object to do so.&lt;/p&gt;

&lt;p&gt;Here's an example of showing/hiding options based on checkbox state:&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="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#checkbox&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;change&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#options-list&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toggle&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;We start with calling &lt;code&gt;$&lt;/code&gt; and pass into it the &lt;code&gt;#checkbox&lt;/code&gt; selector. We implement the &lt;code&gt;.change()&lt;/code&gt; jQuery method on the returned object, letting us pass a callback function which passes another &lt;code&gt;$&lt;/code&gt; call with our next element selector as its argument, returning the jQuery object onto which we can apply the &lt;code&gt;.toggle()&lt;/code&gt; method. Clean and concise.&lt;/p&gt;

&lt;p&gt;And here's one more example showing how we might use jQuery to implement an event listener that starts music playback on spacebar press:&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="nf"&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="nf"&gt;keydown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &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;music&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;play&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;This again shows how simple and readable our code can be when implementing operations that often prove tedious and difficult to follow otherwise.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope this has helped in showing some of jQuery's capabilities and usefulness, as well as given some insight into what's going on under the hood. A deeper understanding of how jQuery interacts with our code makes bugs easier to avoid — and when they do occur, easier to debug. It also deepens our understanding of JavaScript as a whole.&lt;/p&gt;

&lt;p&gt;Another benefit to understanding a library like this is that it can serve as a template for understanding ANY library. While syntax, structure, and individual details will vary, the broader concepts often overlap. This can make expanding our range of knowledge and broadening our toolsets a much less daunting and more rewarding experience.&lt;/p&gt;

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