<?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: Rowland</title>
    <description>The latest articles on DEV Community by Rowland (@rowleks).</description>
    <link>https://dev.to/rowleks</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%2F1208385%2F4e3e48d3-eec1-40f4-8843-c31cd2c8d3d0.jpg</url>
      <title>DEV Community: Rowland</title>
      <link>https://dev.to/rowleks</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rowleks"/>
    <language>en</language>
    <item>
      <title>Data Serialization: A Concise Guide to JSON, YAML, TOML, and More</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Thu, 26 Feb 2026 18:25:13 +0000</pubDate>
      <link>https://dev.to/rowleks/data-serialization-a-concise-guide-to-json-yaml-toml-and-more-23co</link>
      <guid>https://dev.to/rowleks/data-serialization-a-concise-guide-to-json-yaml-toml-and-more-23co</guid>
      <description>&lt;p&gt;As a developer, you’ve most likely encountered serialization, perhaps without realizing it. It happens whenever you read config files, use APIs, or save application state.&lt;/p&gt;

&lt;p&gt;So what is it exactly? Serialization is the process of converting data from its in-memory representation into a format that can be stored or transmitted. The reverse, reading that stored format back into usable data, is called deserialization.&lt;/p&gt;

&lt;p&gt;A common example of this process is copying text to your clipboard from a rich text editor. The editor has all sorts of in-memory data about that text, font size, bold state, color, line height etc. When you paste it into a plain text app like Notepad, it gets stripped down to just the characters. That stripping process is a crude form of serialization, converting a rich in-memory object into a simpler transmittable format.&lt;/p&gt;

&lt;p&gt;The format you choose matters. It affects how readable your code and config files are, how easy it is to debug issues, how well your tools and libraries support it, and sometimes even performance at scale. In this post, I review popular serialization formats such as JSON, YAML, and TOML, along with other noteworthy options, evaluating the advantages and disadvantages of each.&lt;/p&gt;

&lt;h4&gt;
  
  
  JSON
&lt;/h4&gt;

&lt;p&gt;JSON (JavaScript Object Notation) is probably the most widely used serialization format in software today. It started as a subset of JavaScript but has since become the standard for data exchange across virtually every programming language and platform.&lt;/p&gt;

&lt;p&gt;This is what it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="err"&gt;“name”:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;“John”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="err"&gt;“age”:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="err"&gt;“email”:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;“john@example.com”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="err"&gt;“roles”:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;“admin”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;“editor”&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="err"&gt;“address”:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="err"&gt;“city”:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;“Lagos”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="err"&gt;“country”:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;“Nigeria”&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="err"&gt;“active”:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JSON's popularity stems from its intentional simplicity, supporting six data types: strings, numbers, booleans, null, arrays, and objects.&lt;/p&gt;

&lt;p&gt;JSON is the standard format utilized for REST APIs, browser storage such as &lt;code&gt;localStorage&lt;/code&gt;, and configuration files within the JavaScript ecosystem, including &lt;code&gt;package.json&lt;/code&gt;. When two services require data exchange over HTTP, JSON is typically the preferred option.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every major programming language has a JSON parser, either built in or easily available&lt;/li&gt;
&lt;li&gt;Easy to read and understand&lt;/li&gt;
&lt;li&gt;Lightweight with minimal overhead&lt;/li&gt;
&lt;li&gt;Excellent tooling support, including formatters, validators, and editor plugins&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No support for comments, which is a real limitation when using it for configuration files&lt;/li&gt;
&lt;li&gt;Strict syntax means a single trailing comma or missing quote can break parsing&lt;/li&gt;
&lt;li&gt;Limited data types with no native support for dates or binary data&lt;/li&gt;
&lt;li&gt;Deeply nested structures can get hard to read quickly&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Worth knowing:&lt;/strong&gt; Two unofficial extensions of JSON exist to address the lack of comments. &lt;strong&gt;JSONC&lt;/strong&gt; (JSON with Comments) adds support for single-line &lt;code&gt;//&lt;/code&gt; and multi-line &lt;code&gt;/* */&lt;/code&gt; comments. It's what VS Code uses for its &lt;code&gt;settings.json&lt;/code&gt; and what TypeScript uses for &lt;code&gt;tsconfig.json&lt;/code&gt;, so most developers encounter it without realizing it has a distinct name. &lt;strong&gt;JSON5&lt;/strong&gt; goes further, also allowing trailing commas and unquoted keys. Both require parsers that explicitly support them since standard JSON parsers will reject the extra syntax.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  YAML
&lt;/h4&gt;

&lt;p&gt;YAML (YAML Ain’t Markup Language) was built with human readability as the main priority. It uses indentation instead of brackets and braces, which makes it feel closer to plain text than most other formats.&lt;/p&gt;

&lt;p&gt;What it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;John&lt;/span&gt;
&lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;
&lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;john@example.com&lt;/span&gt;
&lt;span class="na"&gt;roles&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;admin&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;editor&lt;/span&gt;
&lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Lagos&lt;/span&gt;
  &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Nigeria&lt;/span&gt;
&lt;span class="na"&gt;active&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

&lt;span class="c1"&gt;# You can also include a comment&lt;/span&gt;
&lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;localhost&lt;/span&gt;
  &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5432&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;myapp_db&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Almost the same data as the JSON example above, but noticeably less visual noise.&lt;/p&gt;

&lt;p&gt;YAML is the standard in the DevOps world. Docker Compose files, Kubernetes manifests, GitHub Actions workflows, Ansible playbooks, and most CI/CD configuration files are written in YAML. It also shows up in frameworks like Ruby on Rails and static site generators like Hugo and Jekyll.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Very readable, especially for people who may not have extensive technical knowledge&lt;/li&gt;
&lt;li&gt;Supports comments, which makes it much more practical for configuration files&lt;/li&gt;
&lt;li&gt;Handles a wider range of data types natively, including dates and multi-line strings&lt;/li&gt;
&lt;li&gt;Less syntactic clutter compared to JSON&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Indentation-sensitive, so a misplaced space can cause silent bugs or confusing parse errors&lt;/li&gt;
&lt;li&gt;The full spec has a surprising number of edge cases. One well-known example is the “Norway problem” where the string &lt;code&gt;NO&lt;/code&gt; gets parsed as &lt;code&gt;false&lt;/code&gt; in some parsers&lt;/li&gt;
&lt;li&gt;Generally slower to parse than JSON&lt;/li&gt;
&lt;li&gt;Some YAML parsers support object deserialization features that can introduce security vulnerabilities if you’re not careful&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Worth knowing:&lt;/strong&gt; YAML is actually a superset of JSON. Any valid JSON document is also valid YAML. This means YAML parsers can read JSON files, though you’d rarely want to mix the two intentionally.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  TOML
&lt;/h4&gt;

&lt;p&gt;TOML (Tom’s Obvious, Minimal Language) was created by Tom Preston-Werner as a configuration format that is easy to read and has predictable, unambiguous behavior. It maps cleanly to a hash table, which makes it straightforward to parse and work with programmatically.&lt;/p&gt;

&lt;p&gt;How it looks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="c"&gt;# Application configuration (Also supports comments)&lt;/span&gt;

&lt;span class="nn"&gt;[app]&lt;/span&gt;
&lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"MyApp"&lt;/span&gt;
&lt;span class="py"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1.0.0"&lt;/span&gt;
&lt;span class="py"&gt;debug&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

&lt;span class="nn"&gt;[database]&lt;/span&gt;
&lt;span class="py"&gt;host&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"localhost"&lt;/span&gt;
&lt;span class="py"&gt;port&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;
&lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"myapp_db"&lt;/span&gt;

&lt;span class="nn"&gt;[server]&lt;/span&gt;
&lt;span class="py"&gt;host&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.0.0.0"&lt;/span&gt;
&lt;span class="py"&gt;port&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;
&lt;span class="py"&gt;allowed_origins&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"https://example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"https://app.example.com"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nn"&gt;[[users]]&lt;/span&gt;
&lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;
&lt;span class="py"&gt;role&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"admin"&lt;/span&gt;

&lt;span class="nn"&gt;[[users]]&lt;/span&gt;
&lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Bob"&lt;/span&gt;
&lt;span class="py"&gt;role&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"editor"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;[[double bracket]]&lt;/code&gt; syntax is how TOML handles arrays of tables which looks a little unusual at first but becomes intuitive quickly.&lt;/p&gt;

&lt;p&gt;TOML has become the standard for Rust projects through &lt;code&gt;Cargo.toml&lt;/code&gt; and Python packaging through &lt;code&gt;pyproject.toml&lt;/code&gt;. It's also picking up adoption in Go and other ecosystems. It works best for application configuration where you want clarity and correctness without a lot of ambiguity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Very few surprises. The spec is strict enough that parsing behavior is consistent across different implementations&lt;/li&gt;
&lt;li&gt;Supports comments&lt;/li&gt;
&lt;li&gt;Types are explicit. Dates, integers, floats, and strings are all clearly typed with no implicit coercion&lt;/li&gt;
&lt;li&gt;The section-based structure feels natural for grouping related settings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not as universally supported as JSON or YAML across all languages and frameworks&lt;/li&gt;
&lt;li&gt;Gets awkward with deeply nested structures&lt;/li&gt;
&lt;li&gt;Not really designed for general-purpose data exchange. It’s a configuration format and works best when treated as one&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Other Formats Worth Knowing
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;XML&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This was the dominant format used for REST APIs before JSON took over. It’s verbose and harder to read, but it’s still widely used in enterprise systems, SOAP APIs, and document formats like SVG and Microsoft Office files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;user&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;name&amp;gt;&lt;/span&gt;John&lt;span class="nt"&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;age&amp;gt;&lt;/span&gt;30&lt;span class="nt"&gt;&amp;lt;/age&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;active&amp;gt;&lt;/span&gt;true&lt;span class="nt"&gt;&amp;lt;/active&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/user&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;CSV (Comma-Separated Values)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;CSV’s simplicity and ubiquity have made it the most common format for tabular data, spreadsheets, and data pipelines, despite its limitations which includes: lack of type information, nesting, and a consistent standard for special characters. Its widespread adoption stems from universal accessibility and broad tool support.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id,name,email,role,active
1,Alice,alice@example.com,admin,true
2,John,john@example.com,editor,true
3,Carol,carol@example.com,viewer,false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Protocol Buffers (Protobuf)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Built by Google, is a binary serialization format focused on performance and efficiency. It’s not human-readable, but it produces significantly smaller payloads and parses faster than any text-based format. It’s commonly used in gRPC services and high-throughput microservice architectures. You define your data structure in a &lt;code&gt;.proto&lt;/code&gt; schema file first, then Protobuf generates the serialization code for your language.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;syntax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"proto3"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int32&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;repeated&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;roles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;4&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;Running &lt;code&gt;protoc --go_out=. user.proto&lt;/code&gt; against that file generates a &lt;code&gt;user.pb.go&lt;/code&gt; file containing the &lt;code&gt;User&lt;/code&gt; struct and all the binary encoding logic. You never write or touch that file. What you do write is your own application code that imports and uses the generated struct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// This is inside the auto-generated user.pb.go - you don't write this&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;   &lt;span class="kt"&gt;string&lt;/span&gt;   &lt;span class="s"&gt;`protobuf:"bytes,1,opt,name=name"`&lt;/span&gt;
    &lt;span class="n"&gt;Age&lt;/span&gt;    &lt;span class="kt"&gt;int32&lt;/span&gt;    &lt;span class="s"&gt;`protobuf:"varint,2,opt,name=age"`&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;   &lt;span class="s"&gt;`protobuf:"bytes,3,opt,name=email"`&lt;/span&gt;
    &lt;span class="n"&gt;Roles&lt;/span&gt;  &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`protobuf:"bytes,4,rep,name=roles"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those struct tags carry the field number information at runtime so &lt;code&gt;proto.Marshal&lt;/code&gt; knows how to encode each field into binary. In your own code, you import the generated package and use it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// This is your application code - you write this&lt;/span&gt;
&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;pb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"john@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Roles&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"admin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"editor"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Serialize to binary&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;proto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;// Deserialize back&lt;/span&gt;
&lt;span class="n"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;pb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;proto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unmarshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// John&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is important to understanding that the field numbers in the &lt;code&gt;.proto&lt;/code&gt; file (the &lt;code&gt;= 1&lt;/code&gt;, &lt;code&gt;= 2&lt;/code&gt; next to each field) are what Protobuf actually writes to the binary output, not the field names. Each field is encoded as a tag followed by its value. The tag is a single byte that packs two things together: the field number and a wire type.&lt;/p&gt;

&lt;p&gt;The wire type tells the decoder how to read the bytes that follow. For example, wire type &lt;code&gt;0&lt;/code&gt; means read a variable-length integer, wire type &lt;code&gt;2&lt;/code&gt; means read the next byte as a length then read that many bytes (used for strings and arrays), and wire type &lt;code&gt;1&lt;/code&gt; means always read 8 bytes (used for doubles). So from a single byte the decoder knows both which field it's reading and how many bytes to consume for the value.&lt;/p&gt;

&lt;p&gt;This is why Protobuf payloads are so compact. Instead of writing &lt;code&gt;"name": "John"&lt;/code&gt; as a string key-value pair like JSON, it writes a tag byte followed directly by the value with almost no structural overhead. For the &lt;code&gt;name&lt;/code&gt; field, the actual bytes on the wire look 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;0x0a 0x04 0x4a 0x6f 0x68 0x6e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;0x0a&lt;/code&gt; is the tag byte encoding field number &lt;code&gt;1&lt;/code&gt; and wire type &lt;code&gt;2&lt;/code&gt; (length-delimited). &lt;code&gt;0x04&lt;/code&gt; is the string length (4 bytes for "John"). The remaining four bytes are the UTF-8 characters J, o, h, n. That's the entire field, six bytes total. The equivalent in JSON, &lt;code&gt;"name": "John"&lt;/code&gt;, is 14 characters. It also means field numbers are permanent. If you change one after data has already been serialized, you'll break deserialization of existing data because the tags no longer match.&lt;/p&gt;

&lt;p&gt;This is also why the &lt;code&gt;.proto&lt;/code&gt; file needs to be shared between services. The binary output is not self-describing, so without the schema, the bytes are meaningless. JSON carries its field names in the payload itself, which is why any parser can read it without prior knowledge of the structure. In practice, teams using Protobuf keep their &lt;code&gt;.proto&lt;/code&gt; files in a shared repository that all services reference so everyone stays in sync.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pardon me for rambling so much about Protobuf. It is not the most common format you’ll encounter day to day, but I got carried away learning more about it during research and felt compelled to share.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;MessagePack&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;It takes the same structure as JSON but serializes it as binary. The result is a much smaller payload with faster parsing. It shows up in real-time applications and anywhere that bandwidth or latency is a serious concern. The API feels familiar to anyone used to JSON.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;decode&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@msgpack/msgpack&lt;/span&gt;&lt;span class="dl"&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;active&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="c1"&gt;// Serialize&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;encoded&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encode&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="c1"&gt;// Deserialize&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;encoded&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;decoded&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: "John", age: 30, active: true }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the &lt;code&gt;name&lt;/code&gt; field with value &lt;code&gt;"John"&lt;/code&gt;, the MessagePack binary output looks 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;0xa4 0x4a 0x6f 0x68 0x6e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare that to Protobuf’s encoding of the same value which was six bytes: &lt;code&gt;0x0a 0x04 0x4a 0x6f 0x68 0x6e&lt;/code&gt;. Protobuf needed two bytes of overhead (the tag byte and the length byte) before the value. MessagePack only needed one because it packs the type and length into a single byte for short strings.&lt;/p&gt;

&lt;p&gt;The more important difference between the two is that MessagePack is self-describing. The encoded bytes carry enough information to deserialize the data without any external schema. Anyone with a MessagePack decoder can read the bytes and get back the original structure. Protobuf cannot do this. Without the &lt;code&gt;.proto&lt;/code&gt; file, the binary output is meaningless. This makes MessagePack more flexible for ad hoc data exchange, while Protobuf is better suited for structured service-to-service communication where both sides share a schema and need strict type contracts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Many Tools Accept Multiple Formats
&lt;/h3&gt;

&lt;p&gt;Some developers may be surprised that popular tools like Prettier use multiple serialization formats, inferring the configuration format from the file extension. Valid configuration methods include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.prettierrc             (JSON or YAML, no extension)
.prettierrc.json
.prettierrc.yaml
.prettierrc.yml
.prettierrc.toml
prettier.config.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same is true for ESLint (&lt;code&gt;.eslintrc.json&lt;/code&gt;, &lt;code&gt;.eslintrc.yaml&lt;/code&gt;, &lt;code&gt;.eslintrc.js&lt;/code&gt;), Babel, Stylelint, and many other tools in the JavaScript ecosystem. The data inside is structurally the same regardless of which format you use. It's just a matter of preference.&lt;/p&gt;

&lt;p&gt;This is worth keeping in mind when setting up a project. If your team is more comfortable with YAML than JSON, or you want to be able to leave comments in your config, you often have that option without having to change tools.&lt;/p&gt;

&lt;h3&gt;
  
  
  Converting Between Formats
&lt;/h3&gt;

&lt;p&gt;Sometimes, you might take over a project with one format and need to switch it, or you may have to convert a configuration file to align with the requirements of a specific tool. Fortunately, there are tools available that can handle this for you, saving you from the hassle of manual rewriting.&lt;/p&gt;

&lt;p&gt;For online converters, sites like &lt;a href="http://transform.tools" rel="noopener noreferrer"&gt;transform.tools&lt;/a&gt; are very handy. You can paste JSON and get YAML, TOML, CSV, and several other formats back in seconds. It covers a wide range of conversions and is probably the quickest option for one-off tasks.&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%2F9t90tunvppdzunowk6p1.png" 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%2F9t90tunvppdzunowk6p1.png" alt="Transforming JSON to YAML in [transform.tools](http://transform.tools)" width="800" height="450"&gt;&lt;/a&gt;Transforming JSON to YAML in &lt;a href="http://transform.tools" rel="noopener noreferrer"&gt;transform.tools&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you prefer working in the terminal or writing scripts, most programming languages offer libraries that simplify data format conversion. For instance, in Python, converting a YAML file to JSON takes just a few lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;yaml&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;config.yaml&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;yaml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;safe_load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;config.json&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&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;Using the &lt;code&gt;yaml&lt;/code&gt; library, you can safely load the contents of a YAML file, and with the &lt;code&gt;json&lt;/code&gt; library, you can write the data back out as a JSON file with proper formatting. This approach can also be reversed or applied to any two formats supported by parser libraries available in your chosen language.&lt;/p&gt;

&lt;p&gt;Remember that conversions are not always lossless. YAML and TOML support comments, whereas JSON does not. Consequently, converting a well-commented YAML or TOML file to JSON will result in the loss of those comments. Additionally, TOML includes explicit date and time types that lack a direct equivalent in JSON, which means such values might be converted into plain strings depending on the tool used. Always carefully review the output before finalizing or committing to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Choose the Right Format
&lt;/h3&gt;

&lt;p&gt;There’s no one-size-fits-all answer, but some practical defaults can guide your decision:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For APIs or data exchange over HTTP, JSON is the best starting point. It’s universally supported and works seamlessly with any programming language.&lt;/li&gt;
&lt;li&gt;For tabular data or files that non-developers need to access and edit, CSV remains the most practical and accessible choice.&lt;/li&gt;
&lt;li&gt;For CI/CD pipelines, container orchestration, or infrastructure tooling, YAML is the industry standard. Familiarize yourself with its quirks and always use a linter to avoid errors.&lt;/li&gt;
&lt;li&gt;For application configuration, especially in languages like Rust or Python, consider TOML. It’s clean, explicit, and avoids many of YAML’s pitfalls.&lt;/li&gt;
&lt;li&gt;For high-volume, low-latency communication between internal services, explore Protocol Buffers or MessagePack. While you lose human-readability, you gain significant performance advantages.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Serialization formats are something most developers pick up implicitly over time. You use JSON because the tutorial used JSON. You use YAML because Kubernetes uses YAML. That’s fine as a starting point, but understanding why each format exists and what it’s optimized for makes you more deliberate about these choices.&lt;/p&gt;

&lt;p&gt;JSON is universal and simple, making it the default for data exchange. YAML prioritizes readability and is well-suited for configuration-heavy tools. TOML offers clarity and consistency for application config. Binary formats step in when performance becomes a real constraint.&lt;/p&gt;

&lt;p&gt;None of them are perfect for every situation. Knowing the tradeoffs means you can pick the right tool rather than defaulting to whatever was used in the last project.&lt;/p&gt;

&lt;p&gt;This is also by no means an exhaustive list. There are formats I didn’t cover here, like Avro, BSON, INI, HCL, and others that have their own niches and communities around them. If you work with one regularly or think it deserved a mention, feel free to drop it in the comments. I’d love to hear which formats others are using and what problems they’re solving with them.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>go</category>
      <category>python</category>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Wed, 25 Feb 2026 01:04:37 +0000</pubDate>
      <link>https://dev.to/rowleks/-4c58</link>
      <guid>https://dev.to/rowleks/-4c58</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/fastapplyai" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__org__pic"&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%2Forganization%2Fprofile_image%2F10148%2F2d5868a3-6267-48c7-91f9-5ebeca0a1449.png" alt="FastApply" width="128" height="128"&gt;
      &lt;div class="ltag__link__user__pic"&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%2Fuser%2Fprofile_image%2F1092985%2Fad4e5ca3-0204-4d68-97f2-2d03235c7211.jpg" alt="" width="406" height="406"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/fastapplyai/how-to-land-a-tech-job-in-the-usa-in-90-days-your-2026-action-plan-4mkh" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;How to Land a Tech Job in the USA in 90 Days: Your 2026 Action Plan&lt;/h2&gt;
      &lt;h3&gt;Oluchi John for FastApply ・ Feb 16&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#howtolandatechjobinusa&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#landatechjobin90days&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#90daystechjobplan&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#2026techjobplan&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>howtolandatechjobinusa</category>
      <category>landatechjobin90days</category>
      <category>90daystechjobplan</category>
      <category>2026techjobplan</category>
    </item>
    <item>
      <title>Understanding Website Cookies And Why We “Accept” Them</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Fri, 24 Oct 2025 10:41:08 +0000</pubDate>
      <link>https://dev.to/rowleks/understanding-website-cookies-and-why-we-accept-them-44hh</link>
      <guid>https://dev.to/rowleks/understanding-website-cookies-and-why-we-accept-them-44hh</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Before you read this article, “Do you accept my cookies”? Chances are, you’ve clicked “accept” on numerous cookie pop-ups over the past few weeks without fully understanding why. Cookies have become so ubiquitous now that you can hardly visit any site for the first time without getting those familiar pop ups seeking for your approval.&lt;/p&gt;

&lt;p&gt;But why do websites ask for permission to use cookies in the first place? What would happen if they just used them without asking? And if you’re building a website yourself, what do you need to know? Let’s dive into the world of cookies, privacy laws, and what it all means for both users and website owners.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are web cookies?
&lt;/h3&gt;

&lt;p&gt;Let’s go over what cookies actually are: Web cookies are small text files that websites store on your device to remember information about you and your browsing activity. They are used to personalize your online experience, such as remembering login information or items in a shopping cart, and to make websites more user-friendly.&lt;/p&gt;

&lt;p&gt;The image below shows how cookies actually look like when viewed with your browser’s DevTools&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%2F2ik80iu9ajkz6cr12t8i.png" 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%2F2ik80iu9ajkz6cr12t8i.png" alt="How cookies look like when stored" width="800" height="424"&gt;&lt;/a&gt;Source: &lt;a href="https://developer.chrome.com/docs/devtools/application/cookies?hl=it" rel="noopener noreferrer"&gt;Chrome for Developers&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How cookies are stored on your device?
&lt;/h3&gt;

&lt;p&gt;When you visit some websites, they sends cookies to your browser, which stores them on your device. Then on subsequent visits, your browser returns the cookie to the server, allowing it to recognize you and personalize your experience.&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%2Fzkmtm8tfg5d40hr2rhea.png" 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%2Fzkmtm8tfg5d40hr2rhea.png" alt="How cookies are stored in your browser" width="800" height="416"&gt;&lt;/a&gt;Source: &lt;a href="https://www.oreilly.com/library/view/web-penetration-testing/9781788623377/9d598c49-3cb4-4ccc-a959-dddc42f19daf.xhtml" rel="noopener noreferrer"&gt;O’Reilly Media&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If that’s it and cookies are so harmless, why the constant consent requests?&lt;/p&gt;

&lt;h3&gt;
  
  
  The Legal Framework Behind Cookie Consent
&lt;/h3&gt;

&lt;p&gt;The cookie consent requirement isn’t just a courtesy. It’s mandated by law, particularly by the EU’s General Data Protection Regulation (GDPR) and similar privacy regulations around the world. These laws require websites to obtain your explicit consent before storing certain types of cookies on your device.&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%2Fvqqog99792mqbelte5sq.png" 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%2Fvqqog99792mqbelte5sq.png" alt="Cookies Consent form" width="800" height="500"&gt;&lt;/a&gt;Source: &lt;a href="https://futuretheory.co/understanding-internet-cookies/" rel="noopener noreferrer"&gt;Futuretheory&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s important to note that not all cookies require consent. Strictly necessary cookies, such as those that keep you logged into a website or remember items in your shopping cart, are generally exempt. The consent requirement primarily applies to tracking and advertising cookies, analytics cookies that monitor your behavior, and third-party cookies from external services.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Consequences of Non-Compliance
&lt;/h3&gt;

&lt;p&gt;So what happens if a website simply ignores these requirements and uses cookies without permission? The consequences can be severe. Under GDPR, companies can face fines of up to €20 million or 4% of their global annual revenue, whichever is higher. Beyond regulatory fines, companies also risk lawsuits from privacy regulators and class action lawsuits from users.&lt;/p&gt;

&lt;p&gt;Tech giants like Google, Amazon, and Meta have collectively been fined hundreds of millions of euros for cookie violations. The reality is that before these privacy laws existed, websites did exactly what many of us might prefer: they simply used cookies without asking. The annoying banners we see today are the direct result of regulations attempting to give users control over their personal data.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Tracking Cookies Actually Work
&lt;/h3&gt;

&lt;p&gt;To understand why these laws exist, it helps to see concrete examples of what these cookies actually do.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tracking and Advertising Cookies
&lt;/h4&gt;

&lt;p&gt;Consider the Facebook Pixel. When you visit an online store, Facebook places a cookie on your device that tracks which products you view. Later, when you’re scrolling through Instagram, you suddenly see ads for those exact products. That cookie has followed you from the store to Facebook’s platforms, creating a connection between your browsing behavior and the ads you see.&lt;/p&gt;

&lt;p&gt;Google Ads operates similarly through retargeting. Browse vacation rentals in Hawaii without booking, and you’ll likely see Hawaii rental ads following you across random blogs, news sites, and YouTube for the next week. One cookie tracking you across all these different websites.&lt;/p&gt;

&lt;h4&gt;
  
  
  Analytics Cookies
&lt;/h4&gt;

&lt;p&gt;Analytics tools like Google Analytics track far more than just page views. They monitor how long you spend reading articles, which links you click, what device you’re using, and whether you’re a returning visitor. Over time, they build a detailed profile of your behavior that website owners use to optimize their content strategy.&lt;/p&gt;

&lt;p&gt;Heatmap tools like Hotjar take this even further, recording where your mouse moves, what you click, and how far you scroll down a page. Some can even record your actual browsing session like a video replay.&lt;/p&gt;

&lt;h4&gt;
  
  
  Third-Party Cookies
&lt;/h4&gt;

&lt;p&gt;Perhaps most surprisingly, even elements you don’t interact with can track you. That innocent “Share on X” button isn’t just a button. It allows X to know you visited that page, even if you never click it. Embedded YouTube videos let Google track every page where you watched a video, building a comprehensive profile of your interests across the entire web. Even “Sign in with Google” or “Sign in with Facebook” buttons can track which sites you visit, whether or not you use them to log in.&lt;/p&gt;

&lt;p&gt;The fundamental issue is that these cookies allow companies to follow you across the internet and build detailed profiles about your interests, shopping habits, and online behavior, often without you having any awareness it’s happening.&lt;/p&gt;

&lt;p&gt;The image below illustrates the difference between first party and third party cookies&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%2F0g4rnu7ni6avxwuj02w9.png" 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%2F0g4rnu7ni6avxwuj02w9.png" alt="First Party cookies v Third party cookies" width="800" height="463"&gt;&lt;/a&gt;Source: &lt;a href="https://www.kwanzoo.com/first-vs-third-party-cookies-explained" rel="noopener noreferrer"&gt;Kwanzoo&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What Website Owners Need to Know
&lt;/h3&gt;

&lt;p&gt;If you’re building a website, understanding your responsibilities around cookies is crucial. The moment you include elements like Google Sign-In buttons, embedded YouTube videos, or similar third-party services, you become responsible for the cookies they place on your visitors’ devices. Even though you didn’t create those cookies, they’re on your website, and you need to get consent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation Options
&lt;/h3&gt;

&lt;p&gt;You have several approaches to handle this properly. The most common is implementing a cookie consent banner using tools like CookieBot, OneTrust, or similar services. These present users with clear options before any tracking cookies are loaded.&lt;/p&gt;

&lt;p&gt;An increasingly popular alternative is using click-to-load placeholders. Instead of auto-loading a YouTube video (which immediately places cookies), you show a placeholder image. The video and its associated cookies only load when the user actively clicks to watch it. This approach is less intrusive and provides implicit consent through user action.&lt;/p&gt;

&lt;p&gt;You can also explore privacy-focused alternatives, such as YouTube’s privacy-enhanced mode (using the youtube-nocookie.com domain), self-hosting videos instead of using YouTube, or implementing privacy-focused authentication methods instead of relying on Google or Facebook.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Reality of Compliance
&lt;/h3&gt;

&lt;p&gt;Here’s the uncomfortable truth: many smaller websites don’t implement cookie consent properly and technically violate GDPR. While enforcement typically focuses on larger companies with deeper pockets, the legal requirement applies to everyone operating in the EU or serving EU visitors. If you’re building a website for public use, especially if you expect international traffic, implementing a proper cookie consent solution is the safest and most ethical approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other Important Cookie Considerations
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Cookie Lifespan
&lt;/h3&gt;

&lt;p&gt;Cookies aren’t all created equal in terms of how long they stick around. Session cookies disappear the moment you close your browser, which is why you might need to log back into a website after closing and reopening your browser. Persistent cookies, however, can remain on your device for months or even years. This is why you might see ads for something you searched for weeks ago — those tracking cookies are still there, quietly doing their job.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Changing Landscape of Third-Party Cookies
&lt;/h3&gt;

&lt;p&gt;The distinction between first-party and third-party cookies matters more than ever. First-party cookies are set by the website you’re actually visiting and are generally less controversial. Third-party cookies, set by external domains like ad networks, are the primary tracking mechanism that has raised privacy concerns.&lt;/p&gt;

&lt;p&gt;Major browsers are responding to these concerns. Safari and Firefox already block third-party cookies by default, and Google Chrome is in the process of phasing them out entirely. This represents a seismic shift in how online tracking works.&lt;/p&gt;

&lt;h3&gt;
  
  
  Taking Control of Your Cookies
&lt;/h3&gt;

&lt;p&gt;As a user, you’re not powerless. Every major browser allows you to view exactly what cookies are stored on your device, see what data they contain, and delete them individually or all at once. You can find these options in your browser’s privacy settings, and it’s surprisingly revealing to see just how many tracking cookies accumulate during normal browsing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cookie Walls and Consent
&lt;/h3&gt;

&lt;p&gt;You may have encountered websites that present what’s known as a “cookie wall”: accept cookies or you can’t use the site. In the EU, this practice exists in a legal gray area. Many regulators consider it non-compliant because true consent must be freely given, not coerced by blocking access to content.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Future of Tracking
&lt;/h3&gt;

&lt;p&gt;It’s worth noting that cookies themselves aren’t inherently problematic. They enable genuinely useful features like staying logged in, remembering your preferences, and keeping items in your shopping cart. The controversy specifically surrounds tracking cookies that follow you around the internet without providing clear benefits to you.&lt;/p&gt;

&lt;p&gt;However, as cookies face increasing restrictions, tracking isn’t simply disappearing. Companies are developing “cookieless tracking” methods, including browser fingerprinting and server-side tracking techniques. The tools may evolve, but the fundamental tension between user privacy and data-driven business models continues.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Cookie consent banners may be annoying, but they represent an important shift in how we think about online privacy. They’re a visible reminder that our browsing behavior is valuable data, and that we have a right to control who collects it and how it’s used.&lt;/p&gt;

&lt;p&gt;For internet users, understanding cookies helps you make informed decisions about your privacy. You can choose which cookies to accept, regularly clear tracking cookies, or use privacy-focused browsers and extensions to limit tracking.&lt;/p&gt;

&lt;p&gt;For website owners and developers, proper cookie consent isn’t just about legal compliance. It’s about respecting your visitors and being transparent about how their data is collected and used. Yes, implementing cookie consent adds complexity to your website, but it’s the right thing to do in an era where privacy concerns are more pressing than ever.&lt;/p&gt;

&lt;p&gt;The conversation around cookies and privacy is far from over. As technology evolves and regulations adapt, we’ll continue to see changes in how websites track users and how users protect their privacy. But the fundamental principle remains: people have a right to know what data is being collected about them and to have meaningful control over that collection.&lt;/p&gt;

&lt;p&gt;The next time you see a cookie banner, you’ll know exactly why it’s there and what’s at stake in that simple “Accept” or “Reject” decision.&lt;/p&gt;

&lt;p&gt;Thank you for “accepting my cookies” and reading until the end.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>learning</category>
      <category>cookies</category>
    </item>
    <item>
      <title>How to Upgrade to The New Ubuntu 25.10 “Questing Quokka”</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Fri, 24 Oct 2025 10:29:13 +0000</pubDate>
      <link>https://dev.to/rowleks/how-to-upgrade-to-the-new-ubuntu-2510-questing-quokka-38ik</link>
      <guid>https://dev.to/rowleks/how-to-upgrade-to-the-new-ubuntu-2510-questing-quokka-38ik</guid>
      <description>&lt;p&gt;Ubuntu 25.10, with the code-name &lt;strong&gt;Questing Quokka&lt;/strong&gt;, is the latest stable release as of October 9, 2025, bringing many notable enhancements across the desktop experience, system security, and under-the-hood improvements. For users currently on Ubuntu 24.04 LTS or earlier versions, understanding how to enjoy these features while planning an eventual step up to Ubuntu 26.04 LTS is important.&lt;/p&gt;

&lt;h2&gt;
  
  
  What’s New in Ubuntu 25.10?
&lt;/h2&gt;

&lt;p&gt;Ubuntu 25.10 includes GNOME 49, providing a more polished, user-friendly desktop experience with new lock screen media controls, smoother animations, and better support for fractional scaling on high-resolution displays. A major change for GNOME users is the removal of the traditional Xorg/X11 desktop session; GNOME now runs exclusively on Wayland, improving graphics performance and security while maintaining legacy support for X11 apps via XWayland.&lt;/p&gt;

&lt;p&gt;Security is a primary focus in 25.10, which introduces a Rust-based reimplementation of the &lt;code&gt;sudo&lt;/code&gt; command (&lt;code&gt;sudo-rs&lt;/code&gt;) and core command-line utilities (&lt;code&gt;rust-coreutils&lt;/code&gt;). This enhances memory safety and reduces vulnerabilities in essential system tools. The installer now supports TPM-backed full disk encryption on compatible devices, making encryption stronger and easier to recover via new built-in recovery key management options.&lt;/p&gt;

&lt;p&gt;Ubuntu 25.10 also ships with Linux kernel 6.17 and Mesa 25.2 graphics drivers, offering improved hardware support including new Intel Lunar Lake chipsets and better support for AMD ray tracing and NVIDIA performance on graphics. Other user-facing additions include two new default apps: the GPU-accelerated Ptyxis terminal with advanced features like multiple profiles and container workflows, and Loupe, a modern, gesture-friendly image viewer.&lt;/p&gt;

&lt;p&gt;Beyond new features, Ubuntu 25.10 refines many existing components: the Software Updater is less intrusive, desktop icons now have more keyboard shortcuts, and the Raspberry Pi variant gains a robust A/B boot system for safer updates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Planning Upgrades to 25.10 and Beyond
&lt;/h2&gt;

&lt;p&gt;If you are running Ubuntu 24.04 LTS or lower, a direct jump to 25.10 is not officially supported yet. Instead, the recommended upgrade path is incremental:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Upgrade from 24.04 LTS to 24.10&lt;/li&gt;
&lt;li&gt;Then from 24.10 to 25.04&lt;/li&gt;
&lt;li&gt;Then from 25.04 to 25.10&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Following this approach minimizes the risk of broken dependencies and system instability.&lt;/p&gt;

&lt;p&gt;Looking ahead, Ubuntu 26.04 LTS is scheduled for April 2026. When planning to upgrade to 26.04 LTS, upgrading sequentially through the 25.x releases now (especially 25.10) reduces the number of steps needed later and ensures your system is ready for the next LTS milestone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Upgrading via the Terminal
&lt;/h2&gt;

&lt;p&gt;If you are currently on an LTS release, you may need to adjust your system’s release upgrade settings in &lt;code&gt;/etc/update-manager/release-upgrades&lt;/code&gt; by setting &lt;code&gt;Prompt=normal&lt;/code&gt; to allow upgrades to non-LTS versions.&lt;/p&gt;

&lt;p&gt;To do that, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/update-manager/release-upgrades
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And set &lt;code&gt;Prompt=normal&lt;/code&gt;&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%2Fr97rlhehidty0vogta3a.png" 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%2Fr97rlhehidty0vogta3a.png" alt="Linux Terminal" width="800" height="584"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, follow these general steps for each intermediate version:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Update and upgrade your current system:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update &amp;amp;&amp;amp; sudo apt upgrade -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Launch the release upgrade process
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo do-release-upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Reboot when prompted and confirm you have the new version before proceeding to the next upgrade by repeating the first two steps above.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Fresh Install Considerations
&lt;/h2&gt;

&lt;p&gt;Sometimes, a fresh installation of Ubuntu 25.10 or future 26.04 LTS can be beneficial for a clean start. Keep in mind that a clean install erases existing apps and settings by default.&lt;/p&gt;

&lt;p&gt;To keep some of your apps, config and settings:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Export your installed .deb package list before reinstalling:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo dpkg --get-selections &amp;gt; package.list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Back up your home directory and hidden config files to preserve personal settings.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -cvpzf backup-home.tar.gz /home/&amp;lt;your_username&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Perform the clean install, during installation, choose the option to overwrite the existing system.&lt;/li&gt;
&lt;li&gt;Reinstall your packages:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo dpkg - set-selections &amp;lt; package.list
sudo apt-get dselect-upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Reinstall your user files and configs
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -xvpzf backup-home.tar.gz -C /home/your_username
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach ensures you maintain your personalized environment while benefiting from the clean install’s stability.&lt;/p&gt;

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

&lt;p&gt;Ubuntu 26.04 LTS offers significant advancements that make upgrading or reinstalling worthwhile. While the upgrade process is best done in stages through supported releases, a fresh install combined with careful backups can provide a clean and efficient transition. Whichever method you choose, Ubuntu’s vibrant community and robust tooling ensure a smooth experience moving forward.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>Understanding Web and Internet Fundamentals</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Fri, 10 Oct 2025 12:42:48 +0000</pubDate>
      <link>https://dev.to/rowleks/understanding-web-and-internet-fundamentals-3bf1</link>
      <guid>https://dev.to/rowleks/understanding-web-and-internet-fundamentals-3bf1</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Behind every website you visit, video you stream, or message you send, there’s a sophisticated chain of communication happening that you never see. Understanding how &lt;em&gt;servers&lt;/em&gt;, &lt;em&gt;DNS&lt;/em&gt;, &lt;em&gt;protocols&lt;/em&gt;, &lt;em&gt;ports&lt;/em&gt;, and other internet concepts and technologies work together isn’t just for developers, it’s like learning the basic mechanics of a car. You don’t need to be a mechanic to drive, but knowing what’s under the hood helps you make better decisions and troubleshoot when things go wrong.&lt;/p&gt;

&lt;p&gt;This guide breaks down the essential building blocks of the internet and web, explaining not just what they are, but how they work together to make our connected world possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Foundation: Servers and Clients
&lt;/h3&gt;

&lt;p&gt;At its core, the internet operates on a simple relationship: one computer asks for something, and another computer provides it. The computer doing the asking is called a &lt;strong&gt;client&lt;/strong&gt; (the browser in your phone or computer), and the computer providing the resource is called a &lt;strong&gt;server&lt;/strong&gt;.&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%2Fwxjz6azu6igdlm49nhix.png" 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%2Fwxjz6azu6igdlm49nhix.png" alt="Client-Server Relationship" width="800" height="534"&gt;&lt;/a&gt;Source: &lt;a href="https://www.oreilly.com/library/view/distributed-computing-in/9781787126992/e4c12f79-583c-4c6b-b0ed-f62fa23be081.xhtml" rel="noopener noreferrer"&gt;O’Reilly Media&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A server isn’t necessarily a massive machine in a data center, it’s any computer configured to respond to requests. Your laptop could be a server if you set it up that way. What makes something a server is its role, not its hardware.&lt;/p&gt;

&lt;p&gt;When you type “google.com” into your browser, you’re using a client to request Google’s homepage from their servers. The server processes your request and sends back the HTML, CSS, and JavaScript that your browser assembles into the page you see.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Computers Find Each Other
&lt;/h3&gt;

&lt;h4&gt;
  
  
  IP Addresses
&lt;/h4&gt;

&lt;p&gt;Every device connected to the internet needs an address, just like every house needs a street address for mail delivery. That’s where &lt;strong&gt;IP addresses&lt;/strong&gt; come in. An IP address is a unique numerical identifier like &lt;code&gt;192.168.1.1&lt;/code&gt; (IPv4) or the longer &lt;code&gt;2001:0db8:85a3:0000:0000:8a2e:0370:7334&lt;/code&gt; (IPv6).&lt;/p&gt;

&lt;p&gt;Think of it this way: if the internet is a massive city, IP addresses are the exact coordinates of every building. But there’s a problem, we humans aren’t great at remembering long strings of numbers.&lt;/p&gt;

&lt;h4&gt;
  
  
  Domain Names and DNS
&lt;/h4&gt;

&lt;p&gt;This is where &lt;strong&gt;domain names&lt;/strong&gt; and the &lt;strong&gt;Domain Name System (DNS)&lt;/strong&gt; save the day. A domain name like &lt;code&gt;amazon.com&lt;/code&gt; is simply a human-friendly alias for an IP address. When you type a domain name, your computer asks a DNS server, "What's the IP address for this domain?" The DNS server responds with the actual IP address, and your computer uses that to connect.&lt;/p&gt;

&lt;p&gt;DNS is often called the “phone book of the internet,” but it’s more like a constantly updated GPS system that helps you find the right destination every time.&lt;/p&gt;

&lt;p&gt;Here’s a fun exercise to try out; Type &lt;code&gt;216.58.223.238&lt;/code&gt; in your browser and see where that leads you.&lt;/p&gt;

&lt;h4&gt;
  
  
  URLs: The Complete Address
&lt;/h4&gt;

&lt;p&gt;A &lt;strong&gt;URL (Uniform Resource Locator)&lt;/strong&gt; is the full address you see in your browser’s address bar. It contains several parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Protocol&lt;/strong&gt;: &lt;code&gt;https://&lt;/code&gt; tells your browser how to communicate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Domain&lt;/strong&gt;: &lt;code&gt;bbc.com&lt;/code&gt; identifies the server&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port&lt;/strong&gt;: &lt;code&gt;:443&lt;/code&gt; (often hidden) specifies which service to use&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Path&lt;/strong&gt;: &lt;code&gt;/sport/football&lt;/code&gt; points to a specific resource&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So &lt;code&gt;https://bbc.com:443/sport/football&lt;/code&gt; is a complete instruction set for retrieving football content from BBC’s server.&lt;/p&gt;

&lt;h4&gt;
  
  
  Ports: Multiple Services, One Address
&lt;/h4&gt;

&lt;p&gt;Here’s where things get interesting. A single server with one IP address can run dozens of different services simultaneously; a web server, an email server, a file transfer server, and more. How does incoming traffic know which service to reach? &lt;strong&gt;Ports&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of an IP address as a large apartment building, and ports as individual apartment numbers. Port numbers range from &lt;strong&gt;0 to 65,535&lt;/strong&gt;, and certain numbers are standardized for specific services:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Port 80&lt;/strong&gt;: HTTP (unsecured web traffic)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port 443&lt;/strong&gt;: HTTPS (secured web traffic)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port 25&lt;/strong&gt;: SMTP (email sending)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port 22&lt;/strong&gt;: SSH (secure remote access)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port 3306&lt;/strong&gt;: MySQL database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you visit a website, your browser automatically connects to port 443 (for HTTPS) or port 80 (for HTTP) unless you specify otherwise. This combination of IP address and port number creates a &lt;strong&gt;socket&lt;/strong&gt; which is a unique endpoint for communication.&lt;/p&gt;

&lt;h3&gt;
  
  
  Protocols: The Language of the Internet
&lt;/h3&gt;

&lt;p&gt;Computers need to speak the same language to understand each other. &lt;strong&gt;Protocols&lt;/strong&gt; are simply agreed-upon rules and standards for communication. These protocols define everything from how data is packaged and addressed to how errors are handled and connections are maintained.&lt;/p&gt;

&lt;p&gt;When you send data, it doesn’t travel as one piece. Instead, it passes through multiple layers, with each adding its own header information in a process called &lt;strong&gt;encapsulation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Data movement across a network follows the &lt;strong&gt;Open Systems Interconnection (OSI) Model,&lt;/strong&gt; a seven-layer framework. As data travels down the layers (encapsulation), each layer adds a &lt;strong&gt;header&lt;/strong&gt; with its specific control information (e.g., ports at &lt;em&gt;Transport layer&lt;/em&gt;, IP addresses at &lt;em&gt;Network layer&lt;/em&gt;). At the destination, the process reverses (decapsulation).&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%2Ft9cqw74ivmxez5742ui4.png" 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%2Ft9cqw74ivmxez5742ui4.png" alt="The OSI Model" width="800" height="626"&gt;&lt;/a&gt;Source: &lt;a href="https://netbeez.net/blog/osi-model-beginners-guide-for-techies/" rel="noopener noreferrer"&gt;NetBeez&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the &lt;em&gt;Transport Layer&lt;/em&gt; (Layer 4) of the OSI model, data is managed by two primary protocols: &lt;strong&gt;TCP&lt;/strong&gt; and &lt;strong&gt;UDP&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  TCP: The Reliable Data Transfer Protocol
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;TCP (Transmission Control Protocol)&lt;/strong&gt; is a connection-oriented protocol that prioritizes reliability over speed. Before any data is transmitted, TCP performs a three-way handshake (SYN, SYN-ACK, ACK) to establish a connection between client and server. It then breaks data into segments, numbers each one sequentially, and transmits them. The receiving end sends acknowledgments (ACKs) for received segments, and TCP retransmits any that go missing or arrive corrupted.&lt;/p&gt;

&lt;p&gt;TCP also handles congestion control, automatically adjusting transmission rates based on network conditions. TCP is used for web pages (HTTP/HTTPS), email (SMTP, IMAP), file transfers (FTP), and any application where data integrity is critical.&lt;/p&gt;

&lt;h4&gt;
  
  
  UDP: The Fast Data Transfer Protocol
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;UDP (User Datagram Protocol)&lt;/strong&gt; is a fast, minimalist transport protocol used on the internet. It works by sending small units of data, called &lt;strong&gt;datagrams&lt;/strong&gt;, to a destination without setting up a formal connection first (it’s connectionless).&lt;/p&gt;

&lt;p&gt;Because of its speed, UDP is perfect for time-sensitive applications where a little lost data is better than waiting to recover it. UDP is used in video streaming, DNS queries and online gaming.&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%2Fk5lpx1mi6cnbz6667z25.png" 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%2Fk5lpx1mi6cnbz6667z25.png" alt="UDP vs TCP" width="800" height="424"&gt;&lt;/a&gt;Source: &lt;a href="https://www.stormstreaming.com/blog/tcp-and-upd-what-are-they/" rel="noopener noreferrer"&gt;Storm Streaming&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  HTTP: Web Traffic
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;HTTP (Hypertext Transfer Protocol)&lt;/strong&gt; is an application-layer (layer 7) protocol that defines the structure of requests and responses between web clients and servers. It’s a stateless protocol, meaning each request-response pair is independent i.e., the server doesn’t inherently remember previous interactions. &lt;/p&gt;

&lt;p&gt;An HTTP request consists of a request line (method, URI, and HTTP version), headers (metadata like Content-Type, User-Agent, and Accept), and an optional body. Common HTTP methods include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt;: Retrieve a resource&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;POST&lt;/strong&gt;: Submit data to be processed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PUT&lt;/strong&gt;: Update or create a resource&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DELETE&lt;/strong&gt;: Remove a resource&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HEAD&lt;/strong&gt;: Retrieve headers only (useful for checking if content has changed)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The server responds with a status line (HTTP version, status code, and reason phrase), response headers, and usually a body containing the requested content. Status codes are grouped by category: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;200–299 for success&lt;/li&gt;
&lt;li&gt;300–399 for redirects&lt;/li&gt;
&lt;li&gt;400–499 for client errors, (where the infamous 404 lives)&lt;/li&gt;
&lt;li&gt;500–599 for server errors..&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  HTTPS: Secure Web Traffic
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;HTTPS&lt;/strong&gt; is HTTP secured with &lt;strong&gt;TLS&lt;/strong&gt; (Transport Layer Security), the successor to &lt;strong&gt;SSL&lt;/strong&gt; (Secure Sockets Layer). When you connect to an HTTPS site, your browser and the server perform a TLS handshake. The server presents a digital certificate proving its identity, and both sides use a complex, slow process (&lt;strong&gt;asymmetric encryption,&lt;/strong&gt; like RSA) to securely agree on a secret code, called a &lt;strong&gt;session key&lt;/strong&gt;. All subsequent data (the actual website content) is encrypted using the shared &lt;strong&gt;session key&lt;/strong&gt; in a faster method (&lt;strong&gt;symmetric encryption&lt;/strong&gt;, like AES). &lt;/p&gt;

&lt;p&gt;This provides three critical security properties: &lt;strong&gt;confidentiality&lt;/strong&gt; (data can’t be read by eavesdroppers), &lt;strong&gt;integrity&lt;/strong&gt; (data can’t be modified without detection), and &lt;strong&gt;authentication&lt;/strong&gt; (you’re communicating with the real server, not an impostor).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The padlock icon 🔒 in your browser verifies that the server’s certificate is valid and trusted.&lt;/p&gt;
&lt;/blockquote&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%2Fryrr45e6g9s7u4ubj5xo.png" 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%2Fryrr45e6g9s7u4ubj5xo.png" alt="HTTP vs HTTPS" width="800" height="478"&gt;&lt;/a&gt;Source: &lt;a href="https://nix-united.com/blog/http-vs-https-google-chrome-update/" rel="noopener noreferrer"&gt;Nix United&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a nutshell, &lt;strong&gt;HTTP&lt;/strong&gt; is an &lt;strong&gt;insecure connection&lt;/strong&gt; that can be intercepted and read by anyone, but &lt;strong&gt;HTTPS&lt;/strong&gt; is &lt;strong&gt;HTTP secured with TLS encryption&lt;/strong&gt;, meaning the data is scrambled and protected from eavesdroppers. &lt;/p&gt;

&lt;h4&gt;
  
  
  IP: Routing Protocol
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;IP (Internet Protocol)&lt;/strong&gt; operates at the network layer (Layer 3) and is responsible for addressing and routing packets across networks. IP gives every device a unique &lt;strong&gt;IP address&lt;/strong&gt; (its label or “street address”) and uses that address to figure out the best path for data transfer.&lt;/p&gt;

&lt;p&gt;When you send data, IP breaks it into packets and forwards each one toward its destination. Each IP packet contains a header with source and destination IP addresses, a &lt;strong&gt;time-to-live (TTL)&lt;/strong&gt; value, protocol identifier (indicating whether the payload is TCP, UDP, or something else), and other control information. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Routers&lt;/strong&gt; read the destination address on the packet’s header to decide where to send it next (next hop). The TTL value of each packet counts down with every hop. If the TTL reaches zero, the packet is thrown away. This prevents packets from endlessly circulating the network if a routing error occurs.&lt;/p&gt;

&lt;p&gt;IP is connectionless and unreliable by design. It makes a best-effort attempt to deliver packets but provides no guarantees. Packets might arrive out of order, get duplicated, or be dropped entirely due to network congestion or routing failures. Higher-layer protocols like TCP handle reliability concerns.&lt;/p&gt;

&lt;p&gt;There are two versions in use: &lt;strong&gt;IPv4&lt;/strong&gt; uses 32-bit addresses (about 4.3 billion possible addresses) and is running out of available addresses, while &lt;strong&gt;IPv6&lt;/strong&gt; uses 128-bit addresses (enough for trillions of trillions of addresses) and is gradually being adopted worldwide.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Request-Response Cycle
&lt;/h3&gt;

&lt;p&gt;Let’s put it all together with what happens when you visit a website:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You type &lt;code&gt;https://example.com/blog&lt;/code&gt; into your browser&lt;/li&gt;
&lt;li&gt;Your browser asks a DNS server for the IP address of &lt;code&gt;example.com&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;DNS responds with the IP address (e.g., &lt;code&gt;93.184.216.34&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Your browser establishes a TCP connection to that IP address on port 443&lt;/li&gt;
&lt;li&gt;An SSL/TLS handshake encrypts the connection&lt;/li&gt;
&lt;li&gt;Your browser sends an HTTP GET request for &lt;code&gt;/blog&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The server processes the request and sends back an HTTP response with the HTML content&lt;/li&gt;
&lt;li&gt;Your browser receives the response, renders the page, and makes additional requests for images, stylesheets, and scripts&lt;/li&gt;
&lt;li&gt;The TCP connection eventually closes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This entire process typically happens in under a second, with multiple requests happening simultaneously.&lt;/p&gt;

&lt;h3&gt;
  
  
  APIs and Modern Web Architecture
&lt;/h3&gt;

&lt;p&gt;As the web evolved, servers started doing more than just serving HTML pages. &lt;strong&gt;APIs (Application Programming Interfaces)&lt;/strong&gt; allow different programs to communicate with servers using structured requests and responses. This communication typically involves sending and receiving &lt;strong&gt;JSON&lt;/strong&gt; (JavaScript Object Notation) data, which is faster and lighter than sending a full webpage.&lt;/p&gt;

&lt;p&gt;For instance, instead of the server sending an entire blog page (with headers, footers, and sidebars), it can use an API to send only the raw &lt;strong&gt;content&lt;/strong&gt; of the blog post. This data can then be used by a mobile app, a separate web application, or a browser to display the content how it needs to.&lt;/p&gt;

&lt;h4&gt;
  
  
  REST API
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;REST API (Representational State Transfer)&lt;/strong&gt; is a popular architectural style for building APIs. It uses standard HTTP methods (GET, POST, PUT, DELETE) to perform operations on resources identified by URLs. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET /api/users/123&lt;/code&gt; retrieves user data&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST /api/users&lt;/code&gt; creates a new user&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PUT /api/users/123&lt;/code&gt; updates user data&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DELETE /api/users/123&lt;/code&gt; deletes a user&lt;/li&gt;
&lt;/ul&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%2F9unwzm26hty4w97sgyxe.png" 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%2F9unwzm26hty4w97sgyxe.png" alt="REST Api structure" width="800" height="352"&gt;&lt;/a&gt;Source: &lt;a href="https://idempiere.org/blog/2025/03/27/understanding-rest-api-and-its-importance-in-idempiere-development/" rel="noopener noreferrer"&gt;iDempiere&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This approach powers most modern web applications, mobile apps, and connected services. &lt;/p&gt;

&lt;h3&gt;
  
  
  Network Management and Security
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Firewalls
&lt;/h4&gt;

&lt;p&gt;A &lt;strong&gt;firewall&lt;/strong&gt; acts as a security guard, monitoring network traffic and blocking potentially harmful connections based on predefined rules. It can block specific ports, IP addresses, or types of traffic to protect servers from attacks.&lt;/p&gt;

&lt;h4&gt;
  
  
  Load Balancers
&lt;/h4&gt;

&lt;p&gt;When a website becomes popular, one server isn’t enough. A &lt;strong&gt;load balancer&lt;/strong&gt; sits in front of multiple servers and distributes incoming requests among them. If one server fails, the load balancer routes traffic to the healthy servers, ensuring the website stays online.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Proxy Servers&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;A &lt;strong&gt;proxy server&lt;/strong&gt; acts as an intermediary between clients and servers. It can cache content to improve performance, filter requests for security, or mask the client’s IP address for privacy. Many organizations use proxy servers to control and monitor internet access.&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance and Optimization
&lt;/h3&gt;

&lt;p&gt;Several factors affect how fast and reliably data travels across the internet:&lt;/p&gt;

&lt;h4&gt;
  
  
  Bandwidth
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Bandwidth&lt;/strong&gt; is the maximum amount of data that can be transferred in a given time period, like the width of a highway. More bandwidth means more data can flow simultaneously.&lt;/p&gt;

&lt;h4&gt;
  
  
  Latency
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Latency&lt;/strong&gt; is the time it takes for data to travel from one point to another, like the speed limit on that highway. Lower latency means faster response times. Geographic distance, network congestion, and the number of intermediate routers all affect latency.&lt;/p&gt;

&lt;h4&gt;
  
  
  Packets
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Packets&lt;/strong&gt; are the small chunks that data gets broken into for transmission. Each packet contains both the actual data (payload) and information about where it’s going and where it came from. Breaking data into packets allows for more efficient use of network resources and better error handling.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The internet might seem like magic, but it’s really a carefully orchestrated system of protocols, addresses, and services working together. Servers and clients communicate through well-defined ports using agreed-upon protocols. Domain names make the internet human-friendly, while IP addresses and routing make it functional. Security layers like HTTPS protect our data, and architectural patterns like REST make building connected applications possible.&lt;/p&gt;

&lt;p&gt;Understanding these fundamentals doesn’t just satisfy curiosity, it empowers you to troubleshoot problems, make informed decisions about web technologies, and appreciate the remarkable engineering that connects billions of devices worldwide. The next time you load a webpage in a fraction of a second, you’ll know the invisible symphony of technology making it happen.&lt;/p&gt;

&lt;p&gt;These concepts form the foundation of everything that happens online. The internet is complex, but at its core, it’s just computers talking to each other and now you know their language.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>architecture</category>
      <category>learning</category>
      <category>website</category>
    </item>
    <item>
      <title>Understanding Shallow Copy vs Deep Copy in JavaScript</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Tue, 07 Oct 2025 10:51:07 +0000</pubDate>
      <link>https://dev.to/rowleks/understanding-shallow-copy-vs-deep-copy-in-javascript-50ep</link>
      <guid>https://dev.to/rowleks/understanding-shallow-copy-vs-deep-copy-in-javascript-50ep</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;If you've ever worked with objects or arrays in JavaScript, you might have run into unexpected mutations. You modify what you thought was a copy, only to find that the original data has changed too. This behavior can lead to subtle bugs that are hard to track down.&lt;/p&gt;

&lt;p&gt;The root cause of this problem lies in how JavaScript handles references to complex data types. When you work with objects and arrays, you're often working with references rather than actual copies of the data. Understanding the difference between shallow and deep copying is crucial for writing predictable, bug-free code.&lt;/p&gt;

&lt;p&gt;In this guide, we'll explore what shallow and deep copies are, how to create them, when to use each approach, and common pitfalls to avoid when copying data structures in JavaScript.&lt;/p&gt;




&lt;h4&gt;
  
  
  Methods of Copying in JavaScript?
&lt;/h4&gt;

&lt;p&gt;In JavaScript, there are two main approaches to copying objects and arrays:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shallow Copy&lt;/strong&gt; creates a new object or array, but only copies the top-level properties. Any nested objects or arrays inside it are still referenced rather than copied. This means that if you modify a nested structure in the copied version, it also affects the original.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deep Copy&lt;/strong&gt; creates an entirely new object or array by recursively copying all nested structures. This results in a fully independent clone where changes in the copied structure do not affect the original at all.&lt;/p&gt;

&lt;h4&gt;
  
  
  Shallow Copy Techniques
&lt;/h4&gt;

&lt;p&gt;A shallow copy creates a new object or array at the top level, but nested objects or arrays remain as references to the original data. Let's explore the different ways to create shallow copies.&lt;/p&gt;

&lt;h5&gt;
  
  
  Shallow Copying Objects
&lt;/h5&gt;

&lt;p&gt;There are two primary methods for shallow copying objects:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using the Spread Operator (&lt;code&gt;...&lt;/code&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The spread operator is the most modern and concise way to create a shallow copy of an object:&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;originalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;New York&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10001&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;const&lt;/span&gt; &lt;span class="nx"&gt;shallowCopy&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;originalUser&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Modifying top-level property (safe)&lt;/span&gt;
&lt;span class="nx"&gt;shallowCopy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&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="nx"&gt;originalUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "John" - unchanged&lt;/span&gt;

&lt;span class="c1"&gt;// Modifying nested property (affects original!)&lt;/span&gt;
&lt;span class="nx"&gt;shallowCopy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Los Angeles&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="nx"&gt;originalUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Los Angeles" - changed!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using &lt;code&gt;Object.assign()&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Object.assign()&lt;/code&gt; method copies all enumerable properties from one or more source objects to a target object:&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;originalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;New York&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10001&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;const&lt;/span&gt; &lt;span class="nx"&gt;shallowCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;originalUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Same behavior as spread operator&lt;/span&gt;
&lt;span class="nx"&gt;shallowCopy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Safe - only affects copy&lt;/span&gt;
&lt;span class="nx"&gt;shallowCopy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;zip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;90001&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Affects original!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Shallow Copying Arrays
&lt;/h5&gt;

&lt;p&gt;Arrays can also be shallow copied using several methods:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using the Spread Operator (&lt;code&gt;...&lt;/code&gt;)&lt;/strong&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;originalArray&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;const&lt;/span&gt; &lt;span class="nx"&gt;shallowCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;originalArray&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Modifying top-level element (safe)&lt;/span&gt;
&lt;span class="nx"&gt;shallowCopy&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;99&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;originalArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 1 - unchanged&lt;/span&gt;

&lt;span class="c1"&gt;// Modifying nested array (affects original!)&lt;/span&gt;
&lt;span class="nx"&gt;shallowCopy&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;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;99&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;originalArray&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;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 99 - changed!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using &lt;code&gt;slice()&lt;/code&gt; Method&lt;/strong&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;originalArray&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;const&lt;/span&gt; &lt;span class="nx"&gt;shallowCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;originalArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Same shallow copy behavior&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using &lt;code&gt;concat()&lt;/code&gt; Method&lt;/strong&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;originalArray&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;const&lt;/span&gt; &lt;span class="nx"&gt;shallowCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[].&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Same shallow copy behavior&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Deep Copy Techniques
&lt;/h4&gt;

&lt;p&gt;A deep copy recursively copies all nested structures, creating a fully independent clone. Let's explore the methods available for deep copying.&lt;/p&gt;

&lt;h5&gt;
  
  
  Using &lt;code&gt;structuredClone()&lt;/code&gt;
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;structuredClone()&lt;/code&gt; method is a modern, built-in way to create deep copies. It works with both objects and arrays and handles most data types correctly:&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;originalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;New York&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10001&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;hobbies&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;reading&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="s2"&gt;gaming&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;const&lt;/span&gt; &lt;span class="nx"&gt;deepCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;structuredClone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Modifying nested properties (safe)&lt;/span&gt;
&lt;span class="nx"&gt;deepCopy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Los Angeles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;deepCopy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hobbies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cooking&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="nx"&gt;originalUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "New York" - unchanged&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;originalUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hobbies&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ["reading", "gaming"] - unchanged&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;structuredClone()&lt;/code&gt; also works seamlessly with arrays:&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;originalArray&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&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="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;const&lt;/span&gt; &lt;span class="nx"&gt;deepCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;structuredClone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;deepCopy&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="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;deepCopy&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;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;99&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;originalArray&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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "John" - unchanged&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;originalArray&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;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 3 - unchanged&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Using Lodash &lt;code&gt;_.cloneDeep()&lt;/code&gt;
&lt;/h5&gt;

&lt;p&gt;If you need to support older browsers or have more complex cloning requirements, Lodash's &lt;code&gt;_.cloneDeep()&lt;/code&gt; method is a reliable third-party solution:&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;// First, install lodash with npm install lodash before importing&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash&lt;/span&gt;&lt;span class="dl"&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;originalData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;notifications&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;email&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="na"&gt;push&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deepCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cloneDeep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;deepCopy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;notifications&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&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;originalData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;notifications&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true - unchanged&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  When to Use Each Approach
&lt;/h4&gt;

&lt;p&gt;Choosing between shallow and deep copy depends on your data structure and use case:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Shallow Copy when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You only need to copy the first level of an object or array&lt;/li&gt;
&lt;li&gt;Your data structure doesn't contain nested objects or arrays&lt;/li&gt;
&lt;li&gt;You want better performance (shallow copies are faster)&lt;/li&gt;
&lt;li&gt;You're working with simple data structures like configuration objects with primitive values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Deep Copy when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You're working with nested objects or arrays&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You need complete independence from the original data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You're implementing features like undo/redo functionality&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You're managing state in applications where mutations could cause bugs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You need to clone complex data structures without side effects&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Common Pitfalls to Avoid
&lt;/h4&gt;

&lt;p&gt;Even experienced developers can fall into traps when copying objects and arrays. Here are some common pitfalls and how to avoid them.&lt;/p&gt;

&lt;h5&gt;
  
  
  (1) Using Assignment Instead of Copying
&lt;/h5&gt;

&lt;p&gt;The most common mistake is thinking that assignment (&lt;code&gt;=&lt;/code&gt;) creates a copy:&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;original&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&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;copy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This is NOT a copy!&lt;/span&gt;

&lt;span class="nx"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&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="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Bob" - both variables point to the same object!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Always use proper copying methods like spread operator or &lt;code&gt;Object.assign()&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;original&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&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;copy&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;original&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// Now it's a real copy&lt;/span&gt;

&lt;span class="nx"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&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="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "John" - unchanged&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  (2) Using Shallow Copy for Nested Structures
&lt;/h5&gt;

&lt;p&gt;Using shallow copy methods when you actually need a deep copy leads to unexpected mutations:&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;preferences&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en&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;const&lt;/span&gt; &lt;span class="nx"&gt;updatedUser&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;user&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// Shallow copy&lt;/span&gt;
&lt;span class="nx"&gt;updatedUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preferences&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Oops! Affects original&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preferences&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "light" - unexpected change!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Use &lt;code&gt;structuredClone()&lt;/code&gt; or &lt;code&gt;_.cloneDeep()&lt;/code&gt; for nested structures:&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;preferences&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en&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;const&lt;/span&gt; &lt;span class="nx"&gt;updatedUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;structuredClone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Deep copy&lt;/span&gt;
&lt;span class="nx"&gt;updatedUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preferences&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;light&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preferences&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "dark" - original unchanged&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  (3) Limitations of &lt;code&gt;structuredClone()&lt;/code&gt;
&lt;/h5&gt;

&lt;p&gt;While &lt;code&gt;structuredClone()&lt;/code&gt; is powerful, it has limitations. It cannot clone:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;li&gt;DOM nodes&lt;/li&gt;
&lt;li&gt;Symbols&lt;/li&gt;
&lt;li&gt;Objects with getters/setters
&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="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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;greet&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;return&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="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// Function won't be cloned&lt;/span&gt;
  &lt;span class="na"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Symbol won't be cloned&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;clone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;structuredClone&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;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;clone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined - function was not cloned&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; For objects with functions, use &lt;code&gt;_.cloneDeep()&lt;/code&gt; from Lodash or implement custom cloning logic.&lt;/p&gt;

&lt;h5&gt;
  
  
  (4) Using &lt;code&gt;JSON.parse(JSON.stringify())&lt;/code&gt; for Deep Copying
&lt;/h5&gt;

&lt;p&gt;Some developers use &lt;code&gt;JSON.parse(JSON.stringify(obj))&lt;/code&gt; as a quick deep copy solution, but this approach has significant drawbacks:&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;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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="na"&gt;greet&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;return&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="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;undef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&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;clone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&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;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;clone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// String, not a Date object&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;clone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined - function was lost&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;clone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;undef&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined - property was removed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method fails with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functions (completely removed)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;undefined&lt;/code&gt; values (removed)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Date&lt;/code&gt; objects (converted to strings)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Infinity&lt;/code&gt; and &lt;code&gt;NaN&lt;/code&gt; (converted to &lt;code&gt;null&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Circular references (throws error)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Use &lt;code&gt;structuredClone()&lt;/code&gt; or &lt;code&gt;_.cloneDeep()&lt;/code&gt; instead for reliable deep copying.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Understanding the difference between shallow and deep copying is essential for writing reliable JavaScript code. While shallow copies are sufficient for simple, flat data structures, deep copies are necessary when working with nested objects and arrays to ensure complete independence from the original data.&lt;/p&gt;

&lt;p&gt;The key takeaways are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Shallow copies&lt;/strong&gt; only duplicate the top level, leaving nested structures as references to the original data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deep copies&lt;/strong&gt; recursively duplicate all levels, creating fully independent clones&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;structuredClone()&lt;/code&gt; for modern deep copying needs, or &lt;code&gt;_.cloneDeep()&lt;/code&gt; for more complex scenarios&lt;/li&gt;
&lt;li&gt;Always be aware of which copying method you're using and whether it matches your data structure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By mastering these copying techniques, you'll avoid common bugs related to unexpected mutations and write more predictable, maintainable code. Whether you're managing application state, implementing undo functionality, or simply working with complex data structures, knowing when and how to properly copy your data is a fundamental skill that will serve you throughout your JavaScript journey.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding Promises in JavaScript</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Thu, 02 Oct 2025 19:03:03 +0000</pubDate>
      <link>https://dev.to/rowleks/understanding-promises-in-javascript-18in</link>
      <guid>https://dev.to/rowleks/understanding-promises-in-javascript-18in</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;JavaScript is single-threaded, meaning it executes one operation at a time. But in the real world, applications constantly need to wait for API responses, file loading, or complex computations.&lt;/p&gt;

&lt;p&gt;Historically, this reliance on waiting led to complex, nested callback functions, often referred to as “Callback Hell.” &lt;strong&gt;Promises&lt;/strong&gt; were introduced to solve this exact problem, making asynchronous code cleaner, more readable, and far more efficient.&lt;/p&gt;

&lt;p&gt;In this guide, we’ll cover the fundamentals of Promises, how to create and consume them, and some common pitfalls to avoid when using Promises&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;What is a Promise?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;A &lt;strong&gt;Promise&lt;/strong&gt; in JavaScript is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value. It acts as a placeholder for a value that is not yet available.&lt;/p&gt;

&lt;p&gt;Every Promise exists in one of three mutually exclusive states:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pending:&lt;/strong&gt; The initial state; the operation is still ongoing and has neither completed nor failed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fulfilled (or Resolved):&lt;/strong&gt; The operation completed successfully, and the Promise now holds a resulting value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rejected:&lt;/strong&gt; The operation failed, and the Promise holds a reason for the failure (usually an Error object).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Think of a Promise like ordering food:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You &lt;strong&gt;place the order&lt;/strong&gt; (Promise is created — &lt;strong&gt;Pending&lt;/strong&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The food arrives successfully (Promise is &lt;strong&gt;Fulfilled&lt;/strong&gt; — &lt;code&gt;resolve&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The kitchen can run out of ingredients (Promise is &lt;strong&gt;Rejected&lt;/strong&gt; — &lt;code&gt;reject&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  How to create a Promise
&lt;/h4&gt;

&lt;p&gt;You create a Promise using the &lt;code&gt;Promise&lt;/code&gt; constructor, which takes one argument: an &lt;strong&gt;executor function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The executor function is responsible for starting the asynchronous work. It receives two arguments, both of which are callback functions and is immediately executed when the promise constructor is called.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;resolve&lt;/code&gt;: Call this function when the asynchronous operation succeeds.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reject&lt;/code&gt;: Call this function when the asynchronous operation fails.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a simple example of using Promises&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;function&lt;/span&gt; &lt;span class="nf"&gt;myExecutor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// async operation and logic to call resolve or reject goes here&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;success&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="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;setTimeout&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Operation succeeded, pass the result&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Operation completed successfully!&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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Operation failed, pass the reason/error&lt;/span&gt;
      &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Operation failed&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;span class="mi"&gt;2000&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;myPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myExecutor&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;myPromise&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, &lt;code&gt;setTimeout&lt;/code&gt; is the asynchronous operation that calls (after 2 seconds) the &lt;code&gt;resolve&lt;/code&gt; callback function when the &lt;em&gt;success&lt;/em&gt; variable is &lt;code&gt;true&lt;/code&gt; otherwise it calls the &lt;code&gt;reject&lt;/code&gt; callback function when &lt;em&gt;success&lt;/em&gt; is &lt;code&gt;false&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;myExecutor&lt;/code&gt; &lt;em&gt;named&lt;/em&gt; callback function is passed to the &lt;code&gt;Promise&lt;/code&gt; constructor, but we could just as well used an inline &lt;em&gt;anonymous&lt;/em&gt; or &lt;em&gt;arrow&lt;/em&gt; callback function. Feel free to brush up on callback functions in my previous post on &lt;a href="https://dev.to/rowleks/understanding-callback-functions-in-javascript-1fk0"&gt;Understanding Callback Functions in JavaScript&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Side Note:&lt;/strong&gt; Going forward, I’ll be declaring callback functions using anonymous functions. I thing it’s easier to tell that an argument is a function if it literally has the &lt;strong&gt;function&lt;/strong&gt; keyword in it. You can follow along using your preferred method.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Return Value of a Promise
&lt;/h4&gt;

&lt;p&gt;A Promise does not return a value like a traditional function does, instead it provides a state which represents the eventual completion (success or failure)&lt;/p&gt;

&lt;p&gt;This is what will get logged to the console from the last line by running the code snippet above;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise {&amp;lt;pending&amp;gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s a Promise object with the &lt;code&gt;pending&lt;/code&gt; state because that’s the initial state of every Promise, clicking on it in a browser console expands the object to show other possible states and return values.&lt;/p&gt;

&lt;h4&gt;
  
  
  Consuming Promises
&lt;/h4&gt;

&lt;p&gt;Once a Promise is created, you use methods like &lt;code&gt;.then()&lt;/code&gt; and &lt;code&gt;.catch()&lt;/code&gt; to "consume" its eventual value. These methods allow you to define what happens when the Promise transitions from &lt;strong&gt;Pending&lt;/strong&gt; to either &lt;strong&gt;Fulfilled&lt;/strong&gt; or &lt;strong&gt;Rejected&lt;/strong&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  The &lt;code&gt;.then()&lt;/code&gt; Method
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;.then()&lt;/code&gt; method takes up to two handler functions as arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first function (&lt;code&gt;onFulfilled&lt;/code&gt;) accepts as an argument the result ( &lt;em&gt;value returned from the&lt;/em&gt; &lt;code&gt;*resolve*&lt;/code&gt; &lt;em&gt;callback function&lt;/em&gt; ) and runs when the Promise is &lt;strong&gt;Fulfilled&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The second function (&lt;code&gt;onRejected&lt;/code&gt;) runs when the Promise is &lt;strong&gt;Rejected&lt;/strong&gt; but it is optional and often skipped in favor of &lt;code&gt;.catch()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using the simple example from before, we can consume the value returned by the resolve or rejected callback function by using the &lt;code&gt;.then&lt;/code&gt; method and the handler functions ( &lt;code&gt;onFulfilled&lt;/code&gt; / &lt;code&gt;onRejected&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="nx"&gt;myPromise&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;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// This is the onFulfilled handler that runs if resolve() was called.&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs "Operation completed successfully!"&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;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// This is the onRejected handler runs if reject() was called.&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//  Logs "Operation failed"&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;blockquote&gt;
&lt;p&gt;Notice we log &lt;code&gt;error.message&lt;/code&gt; to the console not just &lt;code&gt;error&lt;/code&gt; because we passed an &lt;code&gt;Error&lt;/code&gt; object to the &lt;code&gt;reject()&lt;/code&gt; callback function not a string.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  The &lt;code&gt;.catch()&lt;/code&gt; Method
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;.catch()&lt;/code&gt; method is syntactic sugar for &lt;code&gt;.then(null, onRejected)&lt;/code&gt;. It is the standard way to handle errors in Promises and is cleaner than putting the rejection handler inside &lt;code&gt;.then()&lt;/code&gt;. We simply just moved the &lt;code&gt;onRejected&lt;/code&gt; handler function to a different method.&lt;/p&gt;

&lt;p&gt;The code snippet above can be rewritten as;&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="nx"&gt;myPromise&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;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// This is the onFulfilled handler that runs if resolve() was called&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs "Operation completed successfully!"&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&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;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// This runs if reject() was called.&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Logs "Operation failed"&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How Promises Work
&lt;/h4&gt;

&lt;p&gt;The image below can help visualize how Promises work&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%2Fjhqmg7b4zg75kj6jkx7q.png" 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%2Fjhqmg7b4zg75kj6jkx7q.png" alt="How Promises work" width="800" height="628"&gt;&lt;/a&gt;Source: &lt;a href="https://medium.com/@bytefish?source=post_page---byline--3da2bdea1d97---------------------------------------" rel="noopener noreferrer"&gt;Shuai Li&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the diagram above, we can see that the state changes from &lt;strong&gt;pending&lt;/strong&gt; to &lt;strong&gt;fulfilled&lt;/strong&gt; if the async operation &lt;code&gt;resolve&lt;/code&gt; with a value, that value is further passed to the &lt;code&gt;.then()&lt;/code&gt; method for further operations.&lt;/p&gt;

&lt;p&gt;The state can also change from &lt;strong&gt;pending&lt;/strong&gt; to &lt;strong&gt;rejected&lt;/strong&gt; if the async operation &lt;code&gt;reject&lt;/code&gt; with an Error, that Error is further passed to the &lt;code&gt;.catch()&lt;/code&gt; method for further handling&lt;/p&gt;

&lt;h4&gt;
  
  
  Chaining Promises
&lt;/h4&gt;

&lt;p&gt;The greatest power of Promises is their ability to be chained. Because the &lt;code&gt;.then()&lt;/code&gt; method always returns a &lt;strong&gt;new Promise&lt;/strong&gt;, you can link multiple asynchronous operations together sequentially.&lt;/p&gt;

&lt;p&gt;This is the key mechanism for escaping “Callback Hell,” as you can transform complex, deeply nested code into a flat, readable sequence.&lt;/p&gt;

&lt;p&gt;A common example of this is the chaining done when fetching data using the fetch API:&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;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://example.com/api/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// fetch() returns Promise 1&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="c1"&gt;// Process response from Promise 1 and return Promise 2 &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="c1"&gt;// response.json() returns a Promise&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="c1"&gt;// Use the result of Promise 2&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="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&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;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Catches any error from all steps in the chain&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Something went wrong:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&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;h4&gt;
  
  
  Common Pitfalls to Avoid When Using Promises
&lt;/h4&gt;

&lt;p&gt;Promises are a powerful tool for handling asynchronous operations in JavaScript, but they can be tricky to use correctly. Here are some common pitfalls to watch out for when working with Promises and how to avoid them.&lt;/p&gt;

&lt;h5&gt;
  
  
  (1) Forgetting That Promises Are Asynchronous:
&lt;/h5&gt;

&lt;p&gt;Although promises can make our code look and feel synchronous, they are still asynchronous in nature, one can easily make the mistake of trying to access the resolved value 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getData&lt;/span&gt; &lt;span class="o"&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="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://example.com/api/users&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Expecting to use the data outside the getData 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;catch&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;error&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;error&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// getData returns a Promise&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Logs Promise { &amp;lt;pending&amp;gt; }, not the data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because &lt;code&gt;getData()&lt;/code&gt; function contains a Promise, it runs asynchronously so when the JavaScript engine reaches the line &lt;code&gt;const result = getData()&lt;/code&gt; it simply assigns the initial pending state Promise Object to &lt;em&gt;result&lt;/em&gt; variable before proceeding to the next line and not the data we expected.&lt;/p&gt;

&lt;p&gt;To fix this, we can;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;chain &lt;code&gt;getData()&lt;/code&gt; with another &lt;code&gt;.then()&lt;/code&gt; method without assigning  it to a variable
&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getData&lt;/span&gt; &lt;span class="o"&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="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://example.com/api/users&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="k"&gt;return&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="k"&gt;catch&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;error&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;error&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;getData&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;result&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Logs the data&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;or use the top level &lt;code&gt;await&lt;/code&gt; keyword before calling &lt;code&gt;getData()&lt;/code&gt; like this:
&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getData&lt;/span&gt; &lt;span class="o"&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="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://example.com/api/users&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="k"&gt;return&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="k"&gt;catch&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;error&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;error&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// JS awaits the data before proceeding to the next line&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Logs the data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  (2) Not Returning Promises in &lt;code&gt;.then()&lt;/code&gt; Chains:
&lt;/h5&gt;

&lt;p&gt;When chaining &lt;code&gt;.then()&lt;/code&gt; calls, forgetting to return a Promise inside a &lt;code&gt;.then()&lt;/code&gt; handler breaks the chain and can cause unexpected behavior.&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;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://example.com/api/users&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="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="c1"&gt;// forgetting the return keyword&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="c1"&gt;// data is undefined because the previous chain did not return a promise&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&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;error&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Something went wrong:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&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;To fix this; always return a Promise or value from &lt;code&gt;.then()&lt;/code&gt; handlers.&lt;/p&gt;

&lt;h5&gt;
  
  
  (3) Failing to handle Promise Rejections/Failures:
&lt;/h5&gt;

&lt;p&gt;If a Promise rejects and you don’t handle the error, it can cause unhandled Promise rejection warnings or crashes.&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;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://exmpl.com/api/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Typo that'll trigger reject()&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;// No .catch() to handle errors&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Promises are one of the most important features in modern JavaScript because they simplify how we deal with asynchronous code. Instead of juggling nested callbacks and messy logic, Promises give us a clean, structured way to handle success and failure. They’re the foundation for even more powerful tools like &lt;code&gt;async/await&lt;/code&gt;, which build on the same concepts but make code look synchronous.  &lt;/p&gt;

&lt;p&gt;As you work with Promises, remember the key ideas: a Promise represents a value that may be available now, later, or never. It can be &lt;strong&gt;pending&lt;/strong&gt;, &lt;strong&gt;fulfilled&lt;/strong&gt;, or &lt;strong&gt;rejected&lt;/strong&gt;—and how you handle those outcomes determines how reliable your code will be.  &lt;/p&gt;

&lt;p&gt;By mastering Promises, you’ll not only write more maintainable and bug-free code, but you’ll also set yourself up to understand advanced JavaScript concepts with ease. The next time you fetch data, process files, or chain multiple async operations, you’ll see just how powerful Promises really are.  &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>promises</category>
    </item>
    <item>
      <title>Understanding Callback Functions in JavaScript</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Thu, 25 Sep 2025 13:11:30 +0000</pubDate>
      <link>https://dev.to/rowleks/understanding-callback-functions-in-javascript-1fk0</link>
      <guid>https://dev.to/rowleks/understanding-callback-functions-in-javascript-1fk0</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Have you ever wondered how JavaScript knows what to do &lt;strong&gt;after&lt;/strong&gt; completing a task like waiting for a timer, handling user clicks, or fetching data from a server? That’s where &lt;strong&gt;callback functions&lt;/strong&gt; come in. They are foundational to writing effective, non-blocking, and clean JavaScript code.&lt;/p&gt;

&lt;p&gt;In this post, we’ll dive into the essentials of callbacks, covering what they are, the different ways to write them, and the crucial distinction between synchronous and asynchronous callbacks.&lt;/p&gt;




&lt;h3&gt;
  
  
  What is a Callback Function?
&lt;/h3&gt;

&lt;p&gt;In JavaScript, functions are &lt;strong&gt;first-class citizens&lt;/strong&gt;, meaning they can be passed around like variables.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;callback&lt;/strong&gt; is simply a function passed as an argument to another function, which then gets executed later, either immediately or after a specific event or operation.&lt;/p&gt;

&lt;p&gt;Here’s a simple 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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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="s2"&gt;Hello, &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&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="nf"&gt;runCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rowland&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// invoke the callback function&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;runCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: Hello, Rowland&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this code, the &lt;code&gt;greet&lt;/code&gt; function is the callback, passed as an argument to &lt;code&gt;runCallback&lt;/code&gt; and executed inside it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Important Note:&lt;/p&gt;

&lt;p&gt;When passing the function as a callback, you pass its &lt;strong&gt;reference&lt;/strong&gt; by not including parentheses like this; &lt;code&gt;runCallback(greet)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Adding parentheses as in &lt;code&gt;runCallback(greet())&lt;/code&gt; would execute &lt;code&gt;greet&lt;/code&gt; immediately and pass its returned value (which would be &lt;code&gt;undefined&lt;/code&gt; in this case) as the argument, which is incorrect for a callback.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Ways of Writing Callback Functions
&lt;/h3&gt;

&lt;p&gt;Callbacks can be defined in a few different ways:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Named Callbacks&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;A named callback is a regular function definition that has an explicit name, like the &lt;code&gt;greet&lt;/code&gt; function in the example above.&lt;/p&gt;

&lt;p&gt;Benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reusability: You can use the same function as a callback in multiple places.&lt;/li&gt;
&lt;li&gt;Readability: Clearer code structure when the logic is complex.&lt;/li&gt;
&lt;li&gt;Debugging: The function’s name appears in the call stack, making debugging easier.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Anonymous Callbacks&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;An anonymous callback is a function without a name that is passed directly as an argument, usually defined inline.&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;function&lt;/span&gt; &lt;span class="nf"&gt;runCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rowland&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// invoke the callback function&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;runCallback&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;name&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="s2"&gt;Hello, &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// Output: Hello, Rowland&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;&lt;strong&gt;Arrow Function Callbacks&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;This is similar to anonymous callbacks but uses the modern ES6 &lt;strong&gt;arrow function&lt;/strong&gt; syntax for a more concise and readable style.&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;function&lt;/span&gt; &lt;span class="nf"&gt;runCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rowland&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Invoke the callback function&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;runCallback&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;name&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="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&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;span class="c1"&gt;// Output: Hello, Rowland&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Types of Callback Functions
&lt;/h3&gt;

&lt;p&gt;Callbacks primarily fall into two categories, based on &lt;em&gt;when&lt;/em&gt; they are executed:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Synchronous Callbacks&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;A synchronous callback is executed &lt;strong&gt;immediately&lt;/strong&gt; within the function it’s passed to, completing before the next line of code runs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Use Cases:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Array Methods: &lt;code&gt;forEach&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;Custom utility functions&lt;/li&gt;
&lt;li&gt;Higher-Order Functions (HOF).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example with &lt;code&gt;map&lt;/code&gt; array method:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doubleNumber&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubled&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="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doubleNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// The callback runs immediately for each item.&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;doubled&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2, 4, 6]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;doubleNumber&lt;/code&gt; is a named callback passed to the &lt;code&gt;arr.map()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Asynchronous Callbacks&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;An asynchronous callback is executed &lt;strong&gt;after an asynchronous operation completes&lt;/strong&gt;, not immediately. This allows the JavaScript engine to move on to the next line of code while the operation (like a timer or network request) is pending.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Asynchronous Operations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;setTimeout&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Event Listeners (e.g., &lt;code&gt;document.addEventListener(‘event’, callback)&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;HTTP Requests (e.g., using &lt;code&gt;fetch&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example with &lt;code&gt;setTimeout&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="nf"&gt;setTimeout&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="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;Runs after 2 seconds&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="mi"&gt;2000&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;This will execute first&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;Output in the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
This will execute first
Runs after 2 seconds

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

&lt;/div&gt;



&lt;p&gt;The JS engine doesn’t wait for the timer, so the second &lt;code&gt;console.log&lt;/code&gt; executes immediately, and the callback inside &lt;code&gt;setTimeout&lt;/code&gt; runs only after the 2000ms (2s) delay.&lt;/p&gt;




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

&lt;p&gt;Callback functions are a core concept in JavaScript, crucial for understanding how the language manages function execution, especially with asynchronous tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To Recap:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Callbacks are functions passed into other functions to be invoked later.&lt;/li&gt;
&lt;li&gt;They can be written as named, anonymous, or arrow functions.&lt;/li&gt;
&lt;li&gt;Synchronous callbacks run immediately (e.g., in array methods like map and forEach).&lt;/li&gt;
&lt;li&gt;Asynchronous callbacks run after an operation completes (e.g., setTimeout, event listeners, fetch).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding how and when callbacks execute is essential and sets a strong foundation for learning deeper concepts like &lt;strong&gt;Promises&lt;/strong&gt; and &lt;strong&gt;async/await.&lt;/strong&gt; You’ll find callbacks are everywhere as you continue to write more JavaScript!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Assignment by Value vs Assignment by Reference in JavaScript</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Wed, 24 Sep 2025 13:47:47 +0000</pubDate>
      <link>https://dev.to/rowleks/assignment-by-value-vs-assignment-by-reference-in-javascript-d64</link>
      <guid>https://dev.to/rowleks/assignment-by-value-vs-assignment-by-reference-in-javascript-d64</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;It’s very common in programming to assign a variable with the value of another variable as in &lt;code&gt;let x = y&lt;/code&gt; . You assume that &lt;code&gt;y&lt;/code&gt; now holds a copy of &lt;code&gt;x&lt;/code&gt;’s value. And for the most part, you'd be right. But then, you run into a bug where changing &lt;code&gt;y&lt;/code&gt; also mysteriously changes &lt;code&gt;x&lt;/code&gt; . Why is that?&lt;/p&gt;

&lt;p&gt;The answer lies in one of JavaScript’s most fundamental, and often misunderstood, concepts: &lt;strong&gt;assignment by value vs. assignment by reference.&lt;/strong&gt; While the syntax for assignment is always the same, what happens behind the scenes depends on the type of data you’re working with. Understanding this difference is the key to preventing a whole class of frustrating bugs.&lt;/p&gt;

&lt;p&gt;This post is a continuation from my previous post on &lt;a href="https://dev.to/rowleks/understanding-mutability-in-javascript-4ab7"&gt;Understanding Mutability in JavaScript&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this post we’ll cover how assignment is handled in the different data types in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assigning to a primitive data types
&lt;/h3&gt;

&lt;p&gt;Primitive data types in JavaScript like &lt;code&gt;strings&lt;/code&gt;, &lt;code&gt;numbers&lt;/code&gt;, &lt;code&gt;booleans&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, and &lt;code&gt;undefined&lt;/code&gt; are &lt;strong&gt;immutable&lt;/strong&gt;. This means their value cannot be changed after creation. When you assign a primitive from one variable to another, JavaScript creates a new, independent copy of that value.&lt;/p&gt;

&lt;p&gt;Think of it like making a photocopy. The original document remains unchanged, and any marks you make on the copy won’t transfer back to the original.&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;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="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="nx"&gt;x&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="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 10, 10&lt;/span&gt;

&lt;span class="nx"&gt;y&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;// Re-assigned to another value&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="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 10, 20 ( x remains unchanged )&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Drawing from my previous diagram of variables acting as pointers, let’s take a look at what happens inside the computer when we run the code above;&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%2Fmdabmz8brh8g8s5s7sao.png" 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%2Fmdabmz8brh8g8s5s7sao.png" alt="Image illustrating assignment by value in primitives" width="353" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the first 2 lines of code runs, the value (10) that variable &lt;em&gt;x&lt;/em&gt; holds is copied to another location in memory and variable &lt;em&gt;y&lt;/em&gt; points to that location and so both variables holds the same value (10) but points to different location in memory.&lt;/p&gt;

&lt;p&gt;After &lt;code&gt;y = 20&lt;/code&gt;, the y pointer is moved and points to another location in memory that holds the value 20. This happens because primitive types are immutable and now both x and y points to different locations holding different values.&lt;/p&gt;

&lt;p&gt;This same process happens in all primitive data types. When you assign a variable to the value of another variable, both variables remain independent of each other and any operation to one does not affect the other.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assigning to an object types
&lt;/h3&gt;

&lt;p&gt;Objects (including arrays and functions) are &lt;strong&gt;mutable&lt;/strong&gt;, their internal properties can be changed. When you assign an object from one variable to another, JavaScript does not copy the entire object. Instead, it copies the &lt;strong&gt;reference&lt;/strong&gt; to the object (its memory address). This means both variables are now pointing to the &lt;strong&gt;same object in memory.&lt;/strong&gt; If a property is changed by one of the variables, that change will be reflected in the other.&lt;/p&gt;

&lt;p&gt;Imagine you have a shared Google Doc. You send the link to a friend. You and your friend are both accessing the same document. If your friend makes changes, you’ll see them, and vice versa, because you’re both looking at the same, single document. The link is the reference, and the Google Doc is the object in memory.&lt;/p&gt;

&lt;p&gt;Consider the example below;&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&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;admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// A copy of the reference is assigned&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;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// { name: "John", role: "user" }&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;admin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: "John", role: "user" }&lt;/span&gt;

&lt;span class="c1"&gt;// Change a property of the object via the 'admin' variable&lt;/span&gt;
&lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&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="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: "John", role: "admin" }&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;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// { name: "John", role: "admin" } -&amp;gt; The original object was changed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see what happens in the computer memory when we run this code in the diagram below;&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%2F8e13u5t3upgat0nflnwv.png" 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%2F8e13u5t3upgat0nflnwv.png" alt="Image illustrating assignment by value in object types" width="499" height="862"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the first two lines of code, both variables points to the same location in memory unlike in primitives where the value is copied to another location in memory. Because objects are mutable, we can directly alter the properties using the dot notation.&lt;/p&gt;

&lt;p&gt;So after the line &lt;code&gt;admin.role = “admin”;&lt;/code&gt; the &lt;em&gt;admin&lt;/em&gt; variable altered the object in that location in memory and because both variables points to the same address, the change is also reflected in the &lt;em&gt;user&lt;/em&gt; variable&lt;/p&gt;

&lt;p&gt;This works the same for other object types like &lt;code&gt;array&lt;/code&gt; and &lt;code&gt;function&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Breaking the link
&lt;/h4&gt;

&lt;p&gt;A good question now is; “How do we copy the contents of the object to a different address in memory and break the link?” There are a couple of ways to do that, one of which is to simply assign the &lt;em&gt;admin&lt;/em&gt; variable to another object that is similar to the first then alter the property we want to change.&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&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;admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// A new object is created at a different memory address&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;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// { name: "John", role: "user" }&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;admin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: "John", role: "user" }&lt;/span&gt;

&lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&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="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: "John", role: "user" }&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;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// { name: "John", role: "admin" } -&amp;gt; The original object remains unchanged&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although this works, you can tell this method is quite verbose and prone to errors. What if the initial object had over 20 properties? Instead we can use the &lt;code&gt;spread operator (...)&lt;/code&gt; to copy the contents of the first object to another location in memory thereby breaking the link.&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&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;admin&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;user&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// A copies the contents to a different memory address&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;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// { name: "John", role: "user" }&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;admin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: "John", role: "user" }&lt;/span&gt;

&lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&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="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: "John", role: "user" }&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;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// { name: "John", role: "admin" } -&amp;gt; The original object remains unchanged&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The image below illustrates what happens in the computer when we run the code above;&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%2Foeiup31xjlr6zsm1rf0s.png" 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%2Foeiup31xjlr6zsm1rf0s.png" alt="Image illustrating assignment by value in object types using the spread operator" width="505" height="867"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we use the &lt;code&gt;spread operator (...)&lt;/code&gt;, it copies the entire properties of the first object to a different address in memory. You will notice the two variables both points to different locations in memory, now a change to either one will not affect the other.&lt;/p&gt;

&lt;p&gt;I would like to point out some extra information about how the &lt;code&gt;spread operator (...)&lt;/code&gt; works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It works the same way for arrays as it does for objects.&lt;/li&gt;
&lt;li&gt;It does shallow copying, meaning only the first level of the object is copied to a different address, for deeper nested levels, just the reference is copied.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Comparisons in different data types
&lt;/h3&gt;

&lt;p&gt;JavaScript considers two objects as distinct when they don’t point to the same memory address even if the properties or contents of the objects are exactly the same&lt;/p&gt;

&lt;p&gt;You can confirm this by running;&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&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;admin&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;user&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;user&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false ( Same properties, different location in memory )&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This produces &lt;code&gt;false&lt;/code&gt; because JavaScript compares object types by &lt;strong&gt;reference&lt;/strong&gt; and not the contents of the object. So unless both objects point to the same location in memory, doing comparisons like this will always output &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is in contrast with primitive types where comparisons is done by &lt;strong&gt;value&lt;/strong&gt;;&lt;/p&gt;

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

&lt;p&gt;In conclusion, a solid understanding of how JavaScript handles variable assignment for different data types is crucial for writing robust and bug-free code. Remember that for &lt;strong&gt;primitive types&lt;/strong&gt;, assignments and comparisons are always done by &lt;strong&gt;value&lt;/strong&gt;, creating a new, independent copy of the data. This means a change to one variable will never affect the original.&lt;/p&gt;

&lt;p&gt;However, for &lt;strong&gt;object types&lt;/strong&gt;, the story is different. Assignment and comparison are handled by &lt;strong&gt;reference&lt;/strong&gt;. When you assign one object variable to another, they both point to the same memory location. Any property you alter on one will affect the other, as you are both modifying the same underlying object. To break this shared reference and create a new, distinct object, you can use the &lt;code&gt;spread operator (...)&lt;/code&gt;. It creates a shallow copy, which is a great starting point for making safe modifications without unintended side effects.&lt;/p&gt;

&lt;p&gt;While the spread operator is powerful, it has its limitations, particularly with nested objects. In subsequent posts, we’ll delve deeper into more comprehensive methods for copying objects and explore how these same rules of pass-by-value and pass-by-reference apply when passing variables to functions. Knowing the difference between a copy and a shared reference is a fundamental skill that will help you avoid countless headaches in your coding journey.&lt;/p&gt;

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

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding Mutability in JavaScript</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Mon, 22 Sep 2025 04:09:12 +0000</pubDate>
      <link>https://dev.to/rowleks/understanding-mutability-in-javascript-4ab7</link>
      <guid>https://dev.to/rowleks/understanding-mutability-in-javascript-4ab7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Ever wondered what goes on inside the computer when you create and mutate variables? What exactly is the difference between &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;? Why you’re able to alter an object or array even after declaring it with &lt;code&gt;const&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;In this post, I’ll break down all these concepts, focusing on mutability of the different data types with simple diagrams to illustrate how variables work in JavaScript.&lt;/p&gt;

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

&lt;p&gt;Mutability refers to whether a data type’s value can be changed after it’s been created. This is a fundamental concept that separates the two main categories of data types in the JavaScript: primitive types and object types.&lt;/p&gt;

&lt;p&gt;To better understand this concept, lets take a moment to understand what variables really are and how they work in JavaScript&lt;/p&gt;

&lt;h3&gt;
  
  
  What are variables?
&lt;/h3&gt;

&lt;p&gt;In modern JavaScript, values are created and stored as a variable using the &lt;code&gt;let&lt;/code&gt; &amp;amp; &lt;code&gt;const&lt;/code&gt; keywords. both of which acts as pointers to a location in the computer’s memory where a value is stored. The only difference is how flexible they can be. &lt;code&gt;let&lt;/code&gt; is flexible as it can be moved to point to another location in memory, different from the one it was initialized with but &lt;code&gt;const&lt;/code&gt; can only point to the address it was initialized with.&lt;/p&gt;

&lt;p&gt;Now let’s get back to understanding the mutability of primitive and object data types in JavaScript. &lt;/p&gt;

&lt;h3&gt;
  
  
  Mutability of primitive data types
&lt;/h3&gt;

&lt;p&gt;Primitive types are &lt;strong&gt;immutable&lt;/strong&gt; meaning their values cannot be altered after creation. When you perform an operation on a primitive, you are not changing the original value, you are simply creating a new one.&lt;/p&gt;

&lt;p&gt;Some of the common primitive data types are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Number&lt;/li&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Null&lt;/li&gt;
&lt;li&gt;Undefined&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s take a look at a few examples to understand;&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;x&lt;/span&gt; &lt;span class="o"&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;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;// 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above code snippet, you might think “didn’t we just mutate the value of x from 5 to 10?” Well, we didn’t.&lt;/p&gt;

&lt;p&gt;The image below can better illustrate what actually happened inside the computer&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%2F3fkxu5a1hl7mpil9tt01.png" 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%2F3fkxu5a1hl7mpil9tt01.png" alt="Diagram illustrating how primitive variables are stored using let" width="384" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we did was move the pointer from the location in the computer memory that stored the value 5 to another location that stored the value 10. You will notice that the value 5 is still being stored at its original location but now nothing is pointing to it so it will get flushed out by JavaScript’s garbage collector to create space for another value to occupy that spot later on.&lt;/p&gt;

&lt;p&gt;What happens when we try this with &lt;code&gt;const&lt;/code&gt; variable?&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;x&lt;/span&gt; &lt;span class="o"&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;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="c1"&gt;// Uncaught TypeError: Assignment to constant 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;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will not run&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we try to move the pointer to another location, we get an error because variables initialized with &lt;code&gt;const&lt;/code&gt; cannot be moved to another location in memory and because primitives are immutable, there is no way for us to change the value 5 to another value.&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%2Fgb9gcy9v3rl10x9kx5d6.png" 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%2Fgb9gcy9v3rl10x9kx5d6.png" alt="Diagram illustrating how primitive variables are stored using const" width="399" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s another example with strings&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;msg&lt;/span&gt; &lt;span class="o"&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="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// This method returns a new string "HELLO"&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;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// The original string remains "hello"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above, you might expect the &lt;em&gt;msg&lt;/em&gt; variable to become upper cased but it won’t because of the immutability of strings. However, the method &lt;code&gt;toUpperCase()&lt;/code&gt; returns a new string value with all the letters in uppercase which we can re-assign back to the &lt;em&gt;msg&lt;/em&gt; variable.&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;msg&lt;/span&gt; &lt;span class="o"&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="nx"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// The return value is re-assigned to msg&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;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "HELLO"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is a diagram to help illustrate;&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%2Fglcktp6gmav21wkqqr1h.png" 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%2Fglcktp6gmav21wkqqr1h.png" alt="Diagram illustrating how primitive variables are stored using let" width="400" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will notice the original “hello” string was not modified but a new string with all the letters in uppercase was created and the &lt;em&gt;msg&lt;/em&gt; pointer was moved to the new location storing the upper-cased value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mutability of object types
&lt;/h3&gt;

&lt;p&gt;Object types are &lt;strong&gt;mutable&lt;/strong&gt;, meaning their properties can be changed without reassigning the variable. This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Objects&lt;/li&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you have a variable that holds an object, it’s not holding the object itself, but a reference to its location in computer memory. Imagine it like a label on a storage box. The variable is the label, and the object is the contents inside the box.&lt;/p&gt;

&lt;p&gt;Because objects are mutable, you can change their contents directly, without changing or moving the label.&lt;/p&gt;

&lt;p&gt;Consider this example below;&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;bio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&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;bio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 30&lt;/span&gt;

&lt;span class="nx"&gt;bio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// We're directly changing a property on bio&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;bio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will log 50!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above, we can see that modifying the object’s properties directly changes the data at that memory location. We didn’t have to re-assign the &lt;em&gt;bio&lt;/em&gt; variable to point to another location, we simply altered the value that it was pointing to.&lt;/p&gt;

&lt;p&gt;The diagram below help illustrate that point;&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%2Ftxzbryuwh8hyptd0mx8r.png" 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%2Ftxzbryuwh8hyptd0mx8r.png" alt="Diagram illustrating how objects are stored" width="466" height="827"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;bio&lt;/em&gt; variable points to the same address in both figures, but the contents inside changed. It doesn’t matter if we used &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; to initialize the variable. Since we’re not moving the pointer, altering the contents of the object would still work the same.&lt;/p&gt;

&lt;p&gt;Where we’ll run into issues is when we initialize the variable with &lt;code&gt;const&lt;/code&gt; and then try to re-assign it to another value (point to another location)&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;bio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;bio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// Throws an error&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;bio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will not run&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we tried to re-assign &lt;em&gt;bio&lt;/em&gt; to another object but run into an error, because &lt;em&gt;bio&lt;/em&gt; was initialized with &lt;code&gt;const&lt;/code&gt;. We would still run into the same error if we tried to re-assign it to a primitive type. This would work fine if we initialized the variable with &lt;code&gt;let&lt;/code&gt; and then re-assign it to another value regardless of the data type we are re-assigning to.&lt;/p&gt;

&lt;p&gt;Let’s consider another example with an array which is a type of object;&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;colors&lt;/span&gt; &lt;span class="o"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yellow&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;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="cm"&gt;/*  We can add or remove items directly from the 'colors' array
 using methods that mutate it, like .push() or .pop() */&lt;/span&gt;

&lt;span class="nx"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&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="nx"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: ['red', 'yellow', 'blue', 'green']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similar to the &lt;em&gt;bio&lt;/em&gt; variable, we did not move the colors pointer but mutated the contents of the address it was pointing to using the &lt;code&gt;.push()&lt;/code&gt; method. This is the essence of mutability: the ability to change the contents of a data structure without creating a new one.&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%2Fikkkxhz79mwwo8fjasum.png" 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%2Fikkkxhz79mwwo8fjasum.png" alt="Diagram illustrating how arrays are stored" width="521" height="835"&gt;&lt;/a&gt;&lt;/p&gt;




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

&lt;p&gt;Understanding mutability is a milestone in any JavaScript developer’s journey. By now, you should have a clear understanding that primitives are &lt;strong&gt;immutable&lt;/strong&gt;; operations on them create new values while objects are &lt;strong&gt;mutable&lt;/strong&gt;, allowing you to change their contents directly.&lt;/p&gt;

&lt;p&gt;Variables are pointers and declaring a variable with &lt;code&gt;let&lt;/code&gt; means you can point it to another location in memory after creation but those declared with &lt;code&gt;const&lt;/code&gt; are immovable and always points to the same location in memory.&lt;/p&gt;

&lt;p&gt;In subsequent posts I will explore other concepts such as &lt;em&gt;assigning by value&lt;/em&gt; and &lt;em&gt;assigning by reference&lt;/em&gt; and embracing an immutable-first approach, especially with objects and arrays, to prevent unexpected side effects and simplify your code.&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Link git to GitHub via SSH on Windows</title>
      <dc:creator>Rowland</dc:creator>
      <pubDate>Wed, 08 Jan 2025 17:40:16 +0000</pubDate>
      <link>https://dev.to/rowleks/how-to-link-git-to-github-via-ssh-on-windows-1067</link>
      <guid>https://dev.to/rowleks/how-to-link-git-to-github-via-ssh-on-windows-1067</guid>
      <description>&lt;p&gt;As a developer, learning about github can feel great at first, I mean who doesn't need a  secure cloud storehouse for their codes? The importance and use of git and GitHub cannot be overemphasized but there come a point in time where you just get tired and bored of always;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Heading to github.com, &lt;/li&gt;
&lt;li&gt;Locating your repo&lt;/li&gt;
&lt;li&gt;Adding new files to the repo &lt;/li&gt;
&lt;li&gt;Clicking and dragging the files from your PC into GitHub&lt;/li&gt;
&lt;li&gt;And finally writing a commit message.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All these just to push your code to GitHub, what if you needed to make just a minor change? You'd have to go through that mini hell again? Actually, you don't have to because all these steps can be shortened to;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"minor fix"&lt;/span&gt;
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yeah that's right, just these 3 commands above does all that work for you and fast.&lt;/p&gt;

&lt;p&gt;But before you acquire such power, there's a price you'd have to pay. and that is, you'd have to setup a connection between git and GitHub first which luckily isn't that hard at all. I am going to share those steps in this post.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Download and install git
&lt;/h2&gt;

&lt;p&gt;Alright, the first step is to have git installed in your PC, if you already have it, great! If not just &lt;a href="https://git-scm.com/downloads/win" rel="noopener noreferrer"&gt;click here&lt;/a&gt; and download a version suitable for you PC&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%2Fjs6v95huzsg6mb3kjmjw.png" 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%2Fjs6v95huzsg6mb3kjmjw.png" alt="git website for downloading git" width="800" height="489"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After download, start the install wizard and follow the prompts to install.&lt;/p&gt;

&lt;p&gt;Upon installation of git, 3 other applications would also be automatically installed in your PC: &lt;em&gt;Git cmd&lt;/em&gt;, &lt;em&gt;Git GUI&lt;/em&gt; and &lt;em&gt;Git Bash&lt;/em&gt;. These are just the different ways of interacting with git.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring git
&lt;/h3&gt;

&lt;p&gt;Open &lt;em&gt;Git Bash&lt;/em&gt;, which should look similar to the image below&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%2Fhsiaflhq98sz28fros5b.png" 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%2Fhsiaflhq98sz28fros5b.png" alt="Git Bash for windows" width="800" height="524"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run these commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.name &lt;span class="s2"&gt;"Your Name"&lt;/span&gt;

&lt;span class="c"&gt;#This name will be associated with your commits &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.email &lt;span class="s2"&gt;"Your email Address"&lt;/span&gt;

&lt;span class="c"&gt;#This email will be associated with your commits &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can verify that these has been set by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--list&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Generate SSH Key
&lt;/h2&gt;

&lt;p&gt;Installing and configuring git was so easy right, now to an easy but not so easy step, generating an SSH key;&lt;/p&gt;

&lt;p&gt;What is an SSH key and why do we need it?&lt;/p&gt;

&lt;p&gt;To answer that let's first understand what SSH even mean. &lt;br&gt;
SSH stands for Secure Shell, and it's a network protocol (like HTTPS but different) that allows secure communication between devices over an insecure connection.&lt;/p&gt;

&lt;p&gt;Meanwhile an SSH key is a pair of cryptographic keys (a public key and a private key) used to authenticate a secure connection between your computer and a remote server (in this case, GitHub). Put simply, it's a file containing encrypted texts used for authetication.&lt;br&gt;
We need it to authenticate to GitHub without our username or password.&lt;/p&gt;

&lt;p&gt;To generate the key, open &lt;em&gt;Git Bash&lt;/em&gt; and run these commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-keygen &lt;span class="nt"&gt;-t&lt;/span&gt; rsa &lt;span class="nt"&gt;-b&lt;/span&gt; 4096 &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="s2"&gt;"your-email@example.com"&lt;/span&gt;

&lt;span class="c"&gt;#Replace "your-email@example.com" with the email address associated with your GitHub account.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's breakdown some of the command arguments above:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;ssh-keygen&lt;/strong&gt; - This generates a new SSH key pair (a public key and a private key)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-t rsa&lt;/strong&gt; - This sets the type of SSH encryption to rsa (Rivest-Shamir-Adleman) which is the most widely used. Other types includes; &lt;br&gt;
ECDSA (Elliptic Curve Digital Signature Algorithm)&lt;br&gt;
ED25519 (Edwards-curve Digital Signature Algorithm 25519)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-b 4096&lt;/strong&gt; - This sets the number of bits of the generated key to 4096. The other possible value for rsa encryption is 2048 which is less secure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;span&gt; -C your-email@example.com&lt;/span&gt;&lt;/strong&gt; - This adds a comment to the key (usually your email address)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When prompted after running the above command, press Enter to accept the default file location for the file containing the generated SSH key.&lt;/p&gt;

&lt;p&gt;Next, you can optionally set a passphrase for extra security or simply leave the field blank and press Enter twice.&lt;/p&gt;

&lt;p&gt;Lastly, copy the public key to your clipboard with the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clip &amp;lt; ~/.ssh/id_rsa.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&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; ~/.ssh/id_rsa.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will display the public key on your Git Bash terminal, then you can manually highlight and copy the public SSH key&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Add SSH Key to Github and link to Github repository locally
&lt;/h2&gt;

&lt;p&gt;Now for the third and final step, do these:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Log in to your GitHub account.&lt;/li&gt;
&lt;li&gt;Go to Settings &amp;gt; SSH and GPG keys.&lt;/li&gt;
&lt;li&gt;Click New SSH key.&lt;/li&gt;
&lt;li&gt;In the Title field, add a descriptive name (e.g., "git ssh")&lt;/li&gt;
&lt;li&gt;Paste your public key into the Key field. (The one you copied earlier)&lt;/li&gt;
&lt;li&gt;Click Add SSH key. &lt;/li&gt;
&lt;/ol&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%2Fgei3wx4xhhn7de0ga678.png" 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%2Fgei3wx4xhhn7de0ga678.png" alt="Adding ssh key to github" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Confirm successfull connection
&lt;/h3&gt;

&lt;p&gt;To confirm the connection between git and GitHub, open Git Bash and run the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh &lt;span class="nt"&gt;-T&lt;/span&gt; git@github.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if connection was successful you'll a message similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Hi &amp;lt;username&amp;gt;! You&lt;span class="s1"&gt;'ve successfully authenticated, but GitHub does not provide shell access.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Else simply reply &lt;strong&gt;yes&lt;/strong&gt; multiple times when prompted. Afterwards, run the first command again to reconfirm.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linking to a GitHub repo from VS code terminal
&lt;/h3&gt;

&lt;p&gt;Congratulations on Linking your git to your GitHub via SSH. Goodnews is you only need to go through all those linking steps once. Now comes the easiest part.&lt;/p&gt;

&lt;p&gt;Goto GitHub and create a new repo or open an existing repo&lt;br&gt;
Click on SSH &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%2F3k6t7abylvf8t2dsy4e1.png" 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%2F3k6t7abylvf8t2dsy4e1.png" alt="GitHub new repo" width="800" height="323"&gt;&lt;/a&gt;&lt;br&gt;
For a new repo without any files that looks like the above pic:&lt;br&gt;
Click on SSH and copy the url&lt;/p&gt;

&lt;p&gt;For an already existing repo with files, open the repo, click Code &amp;gt; click on the ssh tab and copy the url&lt;br&gt;
Open your project folder in VS and click on Terminal &amp;gt; New Terminal&lt;br&gt;
run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone git@github.com:rowleks/test-repo.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next navigate to the recently cloned repo (folder) on your PC project folder and open the newly cloned repo in VS code &lt;/p&gt;

&lt;p&gt;Open the VS code terminal and run the following commands&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Initial commit"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git branch &lt;span class="nt"&gt;-M&lt;/span&gt; main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it.&lt;/p&gt;

&lt;p&gt;Subsequently all you need to do to push to GitHub from that repository are the commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"commit message"&lt;/span&gt;
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you for reading! I hope this helped.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
