<?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: Beatris Ilieva</title>
    <description>The latest articles on DEV Community by Beatris Ilieva (@beatrisilieva).</description>
    <link>https://dev.to/beatrisilieva</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%2F2925486%2Fe172c476-e8c0-4a5e-9ed8-063bdf27069d.png</url>
      <title>DEV Community: Beatris Ilieva</title>
      <link>https://dev.to/beatrisilieva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/beatrisilieva"/>
    <language>en</language>
    <item>
      <title>Python Functions vs Methods: Differences, Definition, and Examples</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Sat, 18 Oct 2025 17:54:29 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/python-functions-vs-methods-differences-definition-and-examples-h9l</link>
      <guid>https://dev.to/beatrisilieva/python-functions-vs-methods-differences-definition-and-examples-h9l</guid>
      <description>&lt;p&gt;Functions and methods are both callable code blocks in Python, but they operate differently. Functions are standalone blocks of code, while methods are functions bound to objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition: Functions and Methods
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;function&lt;/strong&gt; is a standalone block of reusable code that performs a specific task. Functions are called directly without reference to an object:&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="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;sum&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;strong&gt;method&lt;/strong&gt; is a function that belongs to an object. Methods are called using dot notation on that specific object:&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="n"&gt;my_list&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="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;my_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dot (&lt;code&gt;.&lt;/code&gt;) is the indicator that we are accessing a method attached to an object.&lt;/p&gt;

&lt;p&gt;Methods are defined within a class definition. When we create an object (an instance) from that class, the object has access to all methods defined in its class. This principle applies to both built-in types and custom classes we write. For example, the &lt;code&gt;list&lt;/code&gt; type is defined as a class in Python's source code, and methods like &lt;code&gt;.pop()&lt;/code&gt; and &lt;code&gt;.append()&lt;/code&gt; are defined within that class. When we create a list object, we create an instance of the &lt;code&gt;list&lt;/code&gt; class and access its methods. The same applies to strings, dictionaries, and all other built-in types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Difference: Object Attachment
&lt;/h2&gt;

&lt;p&gt;The primary distinction between functions and methods lies in their attachment to objects. Methods are defined within a class and operate on instances of that class. Functions exist independently in the namespace.&lt;/p&gt;

&lt;h3&gt;
  
  
  Methods Belong to Specific Data Types
&lt;/h3&gt;

&lt;p&gt;Each data type in Python has methods designed specifically for that type:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;String methods:&lt;/strong&gt;&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="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;              &lt;span class="c1"&gt;# Returns "HELLO"
&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;              &lt;span class="c1"&gt;# Returns "hello"
&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;l&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;x&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# Returns "hexxo"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;List methods:&lt;/strong&gt;&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="n"&gt;my_list&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="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&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="c1"&gt;# Adds 4 to the list
&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;         &lt;span class="c1"&gt;# Removes 2 from the list
&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;             &lt;span class="c1"&gt;# Removes and returns the last item
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Dictionary methods:&lt;/strong&gt;&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="n"&gt;my_dict&lt;/span&gt; &lt;span class="o"&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;name&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;Alice&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;age&lt;/span&gt;&lt;span class="sh"&gt;"&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="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;            &lt;span class="c1"&gt;# Returns the keys
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;          &lt;span class="c1"&gt;# Returns the values
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Functions Work Across Multiple Data Types
&lt;/h3&gt;

&lt;p&gt;Functions are designed as general-purpose tools that operate on multiple types of data:&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="nf"&gt;len&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="c1"&gt;# Works with lists
&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;              &lt;span class="c1"&gt;# Works with strings
&lt;/span&gt;&lt;span class="nf"&gt;len&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="c1"&gt;# Works with tuples
&lt;/span&gt;
&lt;span class="nf"&gt;sum&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="c1"&gt;# Works with lists
&lt;/span&gt;&lt;span class="nf"&gt;sum&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="c1"&gt;# Works with tuples
&lt;/span&gt;
&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;# Works with lists
&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;        &lt;span class="c1"&gt;# Works with tuples
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Mutating and Non-Mutating Operations
&lt;/h2&gt;

&lt;p&gt;A critical distinction exists between methods and functions that modify data and those that do not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mutating Methods
&lt;/h3&gt;

&lt;p&gt;Some methods alter the object they are called upon. The original object is modified:&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="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&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;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="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;            &lt;span class="c1"&gt;# Modifies my_list
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# Output: [1, 2, 3]
&lt;/span&gt;
&lt;span class="n"&gt;my_list&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="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;             &lt;span class="c1"&gt;# Removes the last item
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# Output: [1, 2]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Non-Mutating Methods and Functions
&lt;/h3&gt;

&lt;p&gt;Other methods and most functions return a value or create a new object without modifying the original:&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="n"&gt;my_list&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="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# Does not modify my_list
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                &lt;span class="c1"&gt;# Output: [1, 2, 3]
&lt;/span&gt;
&lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&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;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="n"&gt;sorted_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Returns a new list
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                &lt;span class="c1"&gt;# Output: [3, 1, 2] - unchanged
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sorted_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# Output: [1, 2, 3] - new list
&lt;/span&gt;
&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&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="c1"&gt;# Returns a value
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Functions&lt;/strong&gt; are standalone code blocks that can be called directly without reference to an object&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Methods&lt;/strong&gt; are functions bound to specific objects and accessed using dot notation&lt;/li&gt;
&lt;li&gt;  Functions typically work across multiple data types, while methods are specific to their data type&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Mutating operations&lt;/strong&gt; modify the original object, while &lt;strong&gt;non-mutating operations&lt;/strong&gt; return a new value or object without changing the original&lt;/li&gt;
&lt;li&gt;  The dot (&lt;code&gt;.&lt;/code&gt;) notation indicates that we are calling a method on an object&lt;/li&gt;
&lt;li&gt;  When choosing between a method and a function, consider whether the operation should modify the original data&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>List Comprehension in Python: Syntax, Examples, and When to Use Regular For Loops</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Sat, 18 Oct 2025 17:18:31 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/list-comprehension-in-python-syntax-examples-and-when-to-use-regular-for-loops-279k</link>
      <guid>https://dev.to/beatrisilieva/list-comprehension-in-python-syntax-examples-and-when-to-use-regular-for-loops-279k</guid>
      <description>&lt;h2&gt;
  
  
  What is List Comprehension?
&lt;/h2&gt;

&lt;p&gt;List comprehension is a concise syntax for creating a new list by applying an expression to each item in an iterable. An iterable is any object that can be looped through, such as a list, range, or string. List comprehension is faster and more Pythonic than regular for loops.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is List Comprehension Faster?
&lt;/h3&gt;

&lt;p&gt;Python itself is written in the C programming language. The Python interpreter—the program that reads and executes our Python code—was created using C and then compiled into machine code. When we use list comprehension, we call optimized C code that has already been compiled into machine code. In contrast, a regular for loop requires Python to interpret each line and translate it to machine code one step at a time. Since the C code is already compiled and ready to execute, list comprehension runs much faster, especially with large lists.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Use List Comprehension
&lt;/h2&gt;

&lt;p&gt;List comprehension works best in specific situations. We should use list comprehension for simple transformations such as multiplying, filtering, or converting values. It is ideal when the code fits on one line and remains easy to read. We should also use list comprehension when we need a new list as the result and when we want better performance with simple operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Use Regular For Loops
&lt;/h2&gt;

&lt;p&gt;There are cases where a regular for loop is more appropriate. We should use a regular for loop for complex logic with multiple conditions or multiple operations inside the loop. We should also use a regular for loop when readability would suffer from compression. Additionally, we should use a regular for loop when we do not need to create a new list and are just processing data. Finally, we should use a regular for loop for deep nesting with more than two levels.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples of List Comprehension and For Loops
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Example 1: Simple Transformation with List Comprehension
&lt;/h3&gt;

&lt;p&gt;When we need to transform each element in a list, list comprehension provides a clean and efficient solution.&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="c1"&gt;# Example 1: Simple transformation - squaring numbers
# With list comprehension (concise and fast)
&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;squared&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;squared&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: [1, 4, 9, 16, 25]
&lt;/span&gt;
&lt;span class="c1"&gt;# With regular for loop (more verbose)
&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;squared&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;squared&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;squared&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: [1, 4, 9, 16, 25]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both approaches produce the same result. The list comprehension approach requires fewer lines of code and executes faster.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Filtering with a Condition
&lt;/h3&gt;

&lt;p&gt;List comprehension becomes even more valuable when we need to filter items based on a condition.&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="c1"&gt;# Example 2: With condition - filtering even numbers
&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;evens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;evens&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: [2, 4, 6, 8]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The condition &lt;code&gt;if n % 2 == 0&lt;/code&gt; filters the list to include only even numbers. This single line accomplishes both filtering and list creation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3: When Not to Use List Comprehension
&lt;/h3&gt;

&lt;p&gt;When logic becomes too complex, a regular for loop provides better readability.&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="c1"&gt;# Example 3: When NOT to use list comprehension - complex logic
# Bad (hard to read)
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

&lt;span class="c1"&gt;# Good (clear and readable)
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&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="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: [0, 3, 4, 9, 8, 15, 12, 21, 16, 27]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second version with a regular for loop is much easier to understand. We can see exactly what conditions are checked and what actions are taken in each case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;List comprehension&lt;/strong&gt; is a concise syntax for creating a new list by applying an expression to each item in an iterable such as a list, range, or string&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;List comprehension is faster and more Pythonic&lt;/strong&gt; than regular for loops for simple transformations&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Python is written in C,&lt;/strong&gt; and list comprehension calls optimized C code that has already been compiled into machine code&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Use list comprehension&lt;/strong&gt; for simple transformations, filtering, or converting values when the code fits on one line and is easy to read&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Use regular for loops&lt;/strong&gt; for complex logic with multiple conditions, multiple operations inside the loop, or deep nesting with more than two levels&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Readability is important:&lt;/strong&gt; a slower but readable solution is often better than a faster solution that is difficult to understand&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;List comprehension creates a new list,&lt;/strong&gt; while regular for loops can be used just for processing data without creating a new list&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Performance benefit of list comprehension&lt;/strong&gt; comes from its optimized implementation, but clarity should always take priority in code design&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Python Lists vs Tuples: Mutable vs Immutable Data Structures and When to Use Each</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Sat, 18 Oct 2025 16:13:35 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/python-lists-vs-tuples-mutable-vs-immutable-data-structures-and-when-to-use-each-3790</link>
      <guid>https://dev.to/beatrisilieva/python-lists-vs-tuples-mutable-vs-immutable-data-structures-and-when-to-use-each-3790</guid>
      <description>&lt;p&gt;When we work with collections of data in Python, we have several options available. Two of the most fundamental data structures we encounter are lists and tuples. While they appear similar at first glance, they have important differences that make each one suitable for different situations.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Makes Lists and Tuples Different?
&lt;/h2&gt;

&lt;p&gt;The core difference between lists and tuples relates to a concept called &lt;strong&gt;mutability&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;list is mutable&lt;/strong&gt;, which means we can add, remove, or modify elements after the list has been created.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;tuple is immutable&lt;/strong&gt;, which means once we create it, we cannot change it. The data stays exactly as we set it up. If we try to modify a tuple, Python will stop us with an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Should We Use Lists?
&lt;/h2&gt;

&lt;p&gt;We should use a &lt;strong&gt;list&lt;/strong&gt; when we need a collection that will change over time. Lists are perfect for situations where we add, remove, or update information.&lt;/p&gt;

&lt;p&gt;A practical example is a to-do list application. We start with some tasks, and throughout the day we add new tasks, mark some as complete (and remove them), or change task descriptions.&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="c1"&gt;# List - mutable
&lt;/span&gt;&lt;span class="n"&gt;tasks&lt;/span&gt; &lt;span class="o"&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;write code&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;review PR&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;deploy&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Works fine
&lt;/span&gt;&lt;span class="n"&gt;tasks&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;refactor code&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;  &lt;span class="c1"&gt;# Can modify
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# ['refactor code', 'review PR', 'deploy']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When Should We Use Tuples?
&lt;/h2&gt;

&lt;p&gt;We should use a &lt;strong&gt;tuple&lt;/strong&gt; for data that should not change. Tuples are perfect for fixed data that represents something that should stay constant throughout our program.&lt;/p&gt;

&lt;p&gt;Tuples have two additional advantages. First, tuples are slightly faster than lists because they are immutable. Python does not need to manage changes or reindex elements when we add or remove items. Second, tuples serve as a clear signal to other developers reading our code. When someone sees a tuple, they understand that this data is meant to stay constant and should not be modified.&lt;/p&gt;

&lt;p&gt;A practical example is storing geographic coordinates. The latitude and longitude of a city do not change, so we can safely store them in a tuple to indicate they are fixed data.&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="c1"&gt;# Tuple - immutable
&lt;/span&gt;&lt;span class="n"&gt;coordinates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;40.7128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;74.0060&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# NYC latitude, longitude
&lt;/span&gt;&lt;span class="n"&gt;coordinates&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="mf"&gt;41.8781&lt;/span&gt;  &lt;span class="c1"&gt;# TypeError: 'tuple' object does not support item assignment
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important detail to understand:&lt;/strong&gt; if a tuple contains mutable objects like lists inside it, those objects themselves can still be modified. The tuple stores references (addresses) to objects, not the objects themselves. The immutability of the tuple protects the references it holds—we cannot change which objects the tuple points to, add new references, or remove them. However, if an object that the tuple points to is mutable, we can modify that object directly. The tuple itself does not change—we still cannot add or remove elements from the tuple—but the mutable objects within it can be changed.&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="c1"&gt;# Tuple with mutable object inside
&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&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;production&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&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;server1&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;server2&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;config&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;server3&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# This works - modifying the list inside
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# ('production', ['server1', 'server2', 'server3'])
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Lists are mutable:&lt;/strong&gt; we can add, remove, and modify elements after creation&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Tuples are immutable:&lt;/strong&gt; once created, they cannot be changed (though mutable objects inside them can be)&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Use lists&lt;/strong&gt; for collections that change, such as to-do lists or shopping lists&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Use tuples&lt;/strong&gt; for fixed data that should not change, such as coordinates or configuration values&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Tuples offer a slight performance benefit&lt;/strong&gt; because Python does not need to manage changes&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Tuples communicate intent:&lt;/strong&gt; other developers understand that the data is meant to be constant&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>What is the Difference Between Mutable and Immutable Objects in Python? What is a Hash Table and Why Does Python Use Them?</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Sat, 18 Oct 2025 15:20:01 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/what-is-the-difference-between-mutable-and-immutable-objects-in-python-what-is-a-hash-table-and-4pmk</link>
      <guid>https://dev.to/beatrisilieva/what-is-the-difference-between-mutable-and-immutable-objects-in-python-what-is-a-hash-table-and-4pmk</guid>
      <description>&lt;h2&gt;
  
  
  Understanding Mutable and Immutable Objects
&lt;/h2&gt;

&lt;p&gt;Mutable objects in Python are objects that can be changed after creation, like lists, dictionaries, and sets. Immutable objects cannot be changed after creation, like strings, integers, floats, and tuples.&lt;/p&gt;

&lt;p&gt;The key difference is what happens when we try to modify them. With immutable objects, we do not actually change the value. A new object is created in memory instead. For example, if we define a variable &lt;code&gt;x&lt;/code&gt; equal to 5 and then perform &lt;code&gt;x = x + 1&lt;/code&gt;, the original integer has not been changed. A new integer object with value 6 has been created, and now &lt;code&gt;x&lt;/code&gt; points to this new object. The garbage collector will eventually clear the old integer object because no variable refers to it anymore.&lt;/p&gt;

&lt;p&gt;With mutable objects, when we modify the content, the object stays in the same place in memory. If we have a list and append an item to it, the same list object is being modified, not creating a new one.&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="c1"&gt;# Immutable objects - creates new objects
&lt;/span&gt;&lt;span class="n"&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Output: 140234567891234 (example ID)
&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Output: 140234567891264 (different ID - new object!)
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# Output: 6
&lt;/span&gt;
&lt;span class="c1"&gt;# Mutable objects - modifies the same object
&lt;/span&gt;&lt;span class="n"&gt;my_list&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Output: 140234567892345
&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Output: 140234567892345 (same ID - same object!)
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# Output: [1, 2, 3, 4]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How Function Behavior Differs with Mutable and Immutable Objects
&lt;/h2&gt;

&lt;p&gt;This distinction is important because it affects how objects behave when we pass them to functions. When we pass an immutable object to a function and modify it within that function, the modifications do not affect the original variable. When we pass a mutable object to a function and modify it within that function, the modifications do affect the original object.&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="c1"&gt;# Function behavior with mutable vs immutable
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;modify_immutable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="c1"&gt;# Creates new object
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;modify_mutable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lst&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;lst&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&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="c1"&gt;# Modifies same object
&lt;/span&gt;
&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;modify_immutable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# Output: 5 (unchanged)
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: 6
&lt;/span&gt;
&lt;span class="n"&gt;my_list&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="nf"&gt;modify_mutable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: [1, 2, 3, 4] (changed!)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding Hash Tables and Why Mutability Matters
&lt;/h2&gt;

&lt;p&gt;To understand why mutability is so important in Python, we need to explore how dictionaries actually work internally. Python dictionaries use a data structure called a &lt;strong&gt;hash table&lt;/strong&gt;, which relies on a fundamental principle: keys must never change.&lt;/p&gt;

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

&lt;p&gt;A &lt;strong&gt;hash function&lt;/strong&gt; is a mathematical function that takes any input—like a string or number—and converts it into a fixed-size number called a &lt;strong&gt;hash value&lt;/strong&gt;. The key characteristic is that it is &lt;strong&gt;deterministic&lt;/strong&gt;, meaning the same input always produces the same hash value. For example, "name" might always convert to 2834572, and "age" might convert to 9184736.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Hash Tables Provide Fast Lookups
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;hash table&lt;/strong&gt; is a data structure that uses a hash function to map keys to specific memory locations. When we store a key like "name", Python converts it into a number that tells Python exactly where to store or find the value in memory. Instead of searching through items one by one, Python jumps directly to the right location.&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="c1"&gt;# Dictionary
&lt;/span&gt;&lt;span class="n"&gt;user_ages&lt;/span&gt; &lt;span class="o"&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;Alice&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Bob&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Charlie&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_ages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Bob&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# Instant lookup, no matter how many users
&lt;/span&gt;
&lt;span class="c1"&gt;# Behind the scenes (simplified):
# hash('Bob') -&amp;gt; 9184736 -&amp;gt; go directly to that memory location -&amp;gt; find 30
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python dictionaries and sets use hash tables internally. This is why dictionary lookups are so fast—O(1) on average—even with millions of items.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Keys Must Be Immutable
&lt;/h3&gt;

&lt;p&gt;Keys must be immutable objects like strings, numbers, or tuples. If a key could change after being stored, its hash value would change too, and Python would not be able to find it anymore.&lt;/p&gt;

&lt;p&gt;Since mutable objects can be modified after creation, their hash values are not stable. That is why we cannot use mutable objects—such as lists or dictionaries—as dictionary keys. Only immutable objects can serve as keys because their hash values remain constant throughout their lifetime.&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="c1"&gt;# Why keys must be immutable
&lt;/span&gt;&lt;span class="n"&gt;valid_key&lt;/span&gt; &lt;span class="o"&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;latitude&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;longitude&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Tuple - immutable, works fine
&lt;/span&gt;&lt;span class="n"&gt;coordinates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;valid_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;NYC&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;invalid_key&lt;/span&gt; &lt;span class="o"&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;latitude&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;longitude&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# List - mutable
# locations = {invalid_key: 'NYC'}  # TypeError: unhashable type: 'list'
&lt;/span&gt;
&lt;span class="c1"&gt;# Valid - immutable keys
&lt;/span&gt;&lt;span class="n"&gt;valid_dict&lt;/span&gt; &lt;span class="o"&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;name&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;John&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;# String - immutable ✓
&lt;/span&gt;    &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;answer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="c1"&gt;# Integer - immutable ✓
&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tuple&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;      &lt;span class="c1"&gt;# Tuple - immutable ✓
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Invalid - mutable keys
&lt;/span&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;invalid_dict&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;list&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;# List - mutable ✗
&lt;/span&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;TypeError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: unhashable type: 'list'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Mutable objects&lt;/strong&gt; can be changed after creation. When modified, the object in memory is altered, not replaced&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Immutable objects&lt;/strong&gt; cannot be changed after creation. Attempts to modify them result in new objects being created&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Function parameters&lt;/strong&gt; behave differently based on mutability. Mutable objects passed to functions can be modified externally; immutable objects cannot&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Hash functions&lt;/strong&gt; take any input and convert it into a fixed-size number called a hash value in a deterministic way&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Hash tables&lt;/strong&gt; use hash functions to map keys to memory locations, enabling very fast O(1) lookups&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Dictionary keys must be immutable&lt;/strong&gt; because mutable objects have unstable hash values. If a key changed, its hash would change and Python could no longer find it&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Is Python a Statically or Dynamically Typed Language? What's the Difference?</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Sat, 18 Oct 2025 13:53:05 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/is-python-a-statically-or-dynamically-typed-language-whats-the-difference-2b4n</link>
      <guid>https://dev.to/beatrisilieva/is-python-a-statically-or-dynamically-typed-language-whats-the-difference-2b4n</guid>
      <description>&lt;p&gt;Python is a &lt;strong&gt;dynamically typed language&lt;/strong&gt;. This means developers do not need to specify what type of data a variable will hold when creating it. Instead, Python determines the type during runtime as the code executes. In contrast, statically typed languages such as C require developers to declare the type of every variable upfront, before the code runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Dynamic Typing?
&lt;/h2&gt;

&lt;p&gt;With dynamic typing, any value can be assigned to a variable, and Python automatically determines its type:&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="c1"&gt;# Python (dynamically typed)
&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;           &lt;span class="c1"&gt;# This is a string
&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;                &lt;span class="c1"&gt;# Now it is an integer - no problem
&lt;/span&gt;&lt;span class="n"&gt;name&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="c1"&gt;# Now it is a list - still acceptable
&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14&lt;/span&gt;              &lt;span class="c1"&gt;# Now it is a float - no error
&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;        &lt;span class="c1"&gt;# Output: &amp;lt;class 'float'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interpreter determines what type each variable holds as it encounters it. Developers have complete freedom to change a variable's type at any point.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Static Typing?
&lt;/h2&gt;

&lt;p&gt;With static typing, a variable's type must be declared when it is created. Once declared, that variable can only hold values of that specific type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// C (statically typed)&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// This is an integer&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// COMPILATION ERROR - compilation stops here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler checks all type declarations before the program runs. If there is a type mismatch, compilation fails and the program cannot start. The compilation process stops immediately at the first error, so subsequent lines are never reached or evaluated.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Key Difference: When Are Errors Caught?
&lt;/h2&gt;

&lt;p&gt;This is the fundamental distinction between the two approaches:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Statically typed languages (C):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type errors are caught during &lt;strong&gt;compilation&lt;/strong&gt; (before the program runs)&lt;/li&gt;
&lt;li&gt;If there is a type mismatch, the code fails to compile and the program cannot run&lt;/li&gt;
&lt;li&gt;Errors are prevented &lt;em&gt;before&lt;/em&gt; deployment to production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Dynamically typed languages (Python):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type errors are caught during &lt;strong&gt;runtime&lt;/strong&gt; (while the program is running)&lt;/li&gt;
&lt;li&gt;The program starts successfully, but crashes when execution reaches a line with incompatible types&lt;/li&gt;
&lt;li&gt;Errors are only discovered when that specific code path is executed&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When Does the Error Appear? A Practical Example
&lt;/h2&gt;

&lt;p&gt;With a statically typed language like C, a type error prevents the entire program from running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// C (statically typed)&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&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="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// COMPILATION ERROR - compilation stops here&lt;/span&gt;
                        &lt;span class="c1"&gt;// The program never runs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Python, the program runs until the problematic line is executed:&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="c1"&gt;# Python - error caught at runtime
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;           &lt;span class="c1"&gt;# Executes successfully: 15
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# RUNTIME ERROR: TypeError
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Python, the first call executes successfully. The error only appears when the program reaches the second call with incompatible types. The program runs without issue until that specific line is executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Safety Trade-offs
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Statically typed languages are "safer" in one important sense:&lt;/strong&gt; Type errors can never reach production because they are caught during compilation. This is why many mission-critical systems (banks, aerospace, medical devices) use statically typed languages like C, C++, or Java.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, dynamically typed languages offer distinct advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Faster development:&lt;/strong&gt; Code can be written more quickly without explicit type declarations&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility:&lt;/strong&gt; Generic functions can be written that work with multiple data types&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier prototyping:&lt;/strong&gt; Experimentation and iteration can proceed more rapidly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dynamic typing represents a trade-off: developers gain flexibility and development speed, but sacrifice early error detection through compilation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python's Evolution: The Best of Both Worlds
&lt;/h2&gt;

&lt;p&gt;In recent years, Python has evolved to include &lt;strong&gt;type hints&lt;/strong&gt; (introduced in Python 3.5). Type hints allow developers to optionally annotate variable and function types, though Python remains dynamically typed at runtime:&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="c1"&gt;# Modern Python with type hints (optional)
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;           &lt;span class="c1"&gt;# Works: 15
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# Python executes this line without objection
&lt;/span&gt;                                       &lt;span class="c1"&gt;# but raises TypeError at runtime
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type hints are purely optional annotations; Python does not enforce them during execution. However, separate static analysis tools such as &lt;strong&gt;mypy&lt;/strong&gt; can examine the code before runtime and detect type inconsistencies:&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="c"&gt;# mypy checks types before execution&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;mypy your_file.py
error: Argument 1 to &lt;span class="s2"&gt;"add_numbers"&lt;/span&gt; has incompatible &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="s2"&gt;"str"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; expected &lt;span class="s2"&gt;"int"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach provides the advantages of both paradigms: developers retain the flexibility and rapid development speed of dynamic typing, while gaining access to type checking before runtime through optional tooling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Important Note: Design Choice, Not Necessity
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Static versus dynamic typing is independent from compiled versus interpreted.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python could theoretically be statically typed. For example, a statically typed version of Python could require developers to declare types upfront, and the interpreter could check those types during execution.&lt;br&gt;
Similarly, C could theoretically be dynamically typed. A dynamically typed version of C could allow developers to create variables without declaring types, and the compiler could check the types of the values that variables hold during compilation instead of requiring upfront type declarations. For example, instead of requiring &lt;code&gt;int age = 25;&lt;/code&gt;, a dynamically typed compiled language could allow &lt;code&gt;age = 25;&lt;/code&gt; and automatically determine that the variable age currently holds an integer value.&lt;/p&gt;

&lt;p&gt;The actual choices made by language creators reflect different priorities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Python's designers&lt;/strong&gt; chose dynamic typing to prioritize ease of use and rapid development&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C's designers&lt;/strong&gt; chose static typing to prioritize performance and early error detection&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each choice reflects different priorities and intended use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Python is dynamically typed:&lt;/strong&gt; Types are checked during runtime, not at compile-time&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In dynamically typed languages type declarations are not required:&lt;/strong&gt; although type hints can be optionally added&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In dynamically typed languages type errors appear during execution:&lt;/strong&gt; when incompatible operations are attempted&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Static typing catches errors earlier:&lt;/strong&gt; but requires explicit type declarations upfront&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Python's type hints with mypy&lt;/strong&gt; provide type checking without sacrificing dynamic flexibility&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Is Python an interpreted or compiled language? How does Python code execution work?</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Sat, 18 Oct 2025 13:50:34 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/is-python-an-interpreted-or-compiled-language-how-does-python-code-execution-work-18cj</link>
      <guid>https://dev.to/beatrisilieva/is-python-an-interpreted-or-compiled-language-how-does-python-code-execution-work-18cj</guid>
      <description>&lt;p&gt;When learning to program, one of the fundamental concepts developers encounter is the distinction between compiled and interpreted languages. Languages like C are often described as "compiled," while Python is called "interpreted." But what does this distinction actually mean? The answer lies in understanding how code is transformed from human-readable instructions into machine code that the CPU (Central Processing Unit) can execute.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the CPU
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;CPU&lt;/strong&gt; is the brain of the computer. It does all the work—it reads instructions, does calculations, saves information, and makes decisions. The most important thing to know is that the CPU only understands machine code. The CPU cannot understand the code we write in Python or C.&lt;/p&gt;

&lt;p&gt;This is the main reason we need compilers and interpreters. They translate our code into machine code so the CPU can understand and follow our instructions.&lt;/p&gt;

&lt;p&gt;Another important fact: different computers have different CPUs that speak different "languages." For example, a program written for an Apple MacBook will not work on a Windows laptop without changes. Each computer needs its own special machine code.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Compilation?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Compilation is the process of translating an entire program from source code into machine code before execution begins.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a developer writes code in a compiled language like C, the compiler reads all the source code and translates it completely into machine code. This machine code is specific to the target CPU. The resulting program can then run and the CPU executes it directly.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Compilation Process in C
&lt;/h3&gt;

&lt;p&gt;Imagine a developer writes a program in C. The compiler reads the entire program and translates it all into machine code before anything runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Code (original program)
    ↓ [Compiler translates everything]
Machine Code (executable program)
    ↓ [CPU executes it]
Output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This happens only once. After the compilation is done, the program is ready to use. Every time someone runs the program, the CPU executes the machine code directly—no more translation is needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Characteristics of Compilation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Everything is translated first:&lt;/strong&gt; Before the program runs, the entire code is changed into machine code&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Errors are found before running:&lt;/strong&gt; If there are problems with the code, the compiler finds them and stops. The program will not run until the errors are fixed&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Machine code is made for one type of CPU:&lt;/strong&gt; If we need the program to work on a different CPU type, the code must be compiled again for that CPU&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Programs run very fast:&lt;/strong&gt; Once the program is translated, the CPU runs it directly without anything slowing it down&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Interpretation?
&lt;/h2&gt;

&lt;p&gt;An interpreter is a program that reads our code and follows the instructions one line at a time. Unlike a compiler, an interpreter does not translate everything first. Instead, it reads each line, understands it, and tells the CPU what to do—all while the program is running.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Interpretation Process in Python
&lt;/h3&gt;

&lt;p&gt;A developer writes a Python program:&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="c1"&gt;# hello.py
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then runs it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python hello.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Code (hello.py)
    ↓ [Python interpreter reads each line]
    ↓ [Interpreter tells CPU what to do]
Output: "Hello, World!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interpreter reads the code line by line while the program is running. It translates each line and immediately tells the CPU what to do.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Characteristics of Interpretation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Line by line:&lt;/strong&gt; The code is read and executed one line at a time while the program runs&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Errors appear during running:&lt;/strong&gt; If there is a problem in the code, you only find out when the program reaches that line&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Works on any computer:&lt;/strong&gt; The same code can run on Mac, Windows, etc. without any changes&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Slower execution:&lt;/strong&gt; The interpreter is working while the program runs, so it is slower than compiled programs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Development Experience: A Practical Comparison
&lt;/h2&gt;

&lt;p&gt;The differences between compiled and interpreted languages become very apparent during development:&lt;/p&gt;

&lt;h3&gt;
  
  
  Compiled Language Workflow (C)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Write code in C
2. Translate it all to machine code using compiler
3. If errors exist → Fix code → Go back to step 2
4. If no errors → Run the program
5. Test and look at results
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The problem:&lt;/strong&gt; Every time you change the code, you must translate it all again. For very large programs, this can take a long time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interpreted Language Workflow (Python)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Write code in Python
2. Run interpreter: python program.py
3. Observe output immediately
4. If errors exist → Fix code → Go to step 2
5. Code runs immediately; no waiting for compilation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantage:&lt;/strong&gt; Developers see results immediately, enabling rapid iteration and experimentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python's Hybrid Approach: The Best of Both Worlds
&lt;/h2&gt;

&lt;p&gt;Python is neither purely compiled nor purely interpreted. Instead, it uses a &lt;strong&gt;hybrid approach&lt;/strong&gt; that combines benefits of both.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Bits and Bytes
&lt;/h3&gt;

&lt;p&gt;Before exploring how code is translated, it is important to understand the basic units of computer information.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;bit&lt;/strong&gt; is the smallest unit of information a computer can understand. It is either 0 or 1 (on or off).&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;byte&lt;/strong&gt; is a group of 8 bits. It is the smallest unit of information that has its own address. This means the computer can locate and save one byte at a specific place in memory. Bytes are used to store all data—numbers, letters, colors, sounds—everything in your computer is made of bytes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The letter "A" = 1 byte = 8 bits = &lt;code&gt;01000001&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  The number "5" = 1 byte = 8 bits = &lt;code&gt;00000101&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these bits and bytes form &lt;strong&gt;machine code&lt;/strong&gt;—the special language that computers understand, made only of 0s and 1s, like &lt;code&gt;10110000&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Python Actually Works
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Code (.py file)
    ↓ [Python compiler - first time only]
Bytecode (.pyc file - cached in __pycache__)
    ↓ [Python interpreter - every execution]
Machine Code (executed by CPU)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 1: Compilation to Bytecode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a Python module is imported for the first time, Python compiles the source code to &lt;strong&gt;bytecode&lt;/strong&gt;—an intermediate format that is simpler than source code but not executable by the CPU.&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="c1"&gt;# user.py
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When this module is imported, Python creates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user.py → compiled to → user.cpython-313.pyc (cached bytecode)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Interpretation of Bytecode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every time the Python program runs, the Python interpreter reads the cached bytecode and translates it to machine code instructions that the CPU can execute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python program.py &lt;span class="o"&gt;(&lt;/span&gt;first run&lt;span class="o"&gt;)&lt;/span&gt;
    → Uses cached .pyc file
    → Interpreter translates bytecode to machine code
    → CPU executes

python program.py &lt;span class="o"&gt;(&lt;/span&gt;second run&lt;span class="o"&gt;)&lt;/span&gt;
    → Uses same cached .pyc file
    → Interpreter translates bytecode to machine code again
    → CPU executes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important note:&lt;/strong&gt; The &lt;code&gt;__pycache__&lt;/code&gt; directory and &lt;code&gt;.pyc&lt;/code&gt; files are created only for &lt;strong&gt;imported modules&lt;/strong&gt;, not for the main script being executed. Python automatically updates these cached bytecode files whenever the source code changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why This Hybrid Approach?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Portability:&lt;/strong&gt; Bytecode is universal across platforms. The same &lt;code&gt;.pyc&lt;/code&gt; file works on Mac, Windows, etc. Different Python interpreters on each platform translate that bytecode to their platform's native machine code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user.cpython-313.pyc (universal bytecode)
    ↓
    ├─→ [Interpreter on Mac] → machine code → CPU executes
    ├─→ [Interpreter on Windows] → machine code → CPU executes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Development speed:&lt;/strong&gt; Developers write Python code and see results immediately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caching performance:&lt;/strong&gt; By caching bytecode, Python avoids recompiling the same code repeatedly, providing a performance boost on subsequent runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Compilation&lt;/strong&gt; translates an entire program to machine code before execution; execution is fast but development iteration is slow&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Interpretation&lt;/strong&gt; translates and executes code during runtime; development is fast but execution is slower&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Machine code&lt;/strong&gt; is CPU-specific; different computers require different machine code&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Bytecode&lt;/strong&gt; is a universal intermediate format that can be translated to different machine code for different computers&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Python uses a hybrid approach:&lt;/strong&gt; code is compiled to bytecode once (and cached), then interpreted to machine code each time it runs&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;The &lt;code&gt;__pycache__&lt;/code&gt; directory&lt;/strong&gt; contains cached bytecode for imported modules, automatically updated when source code changes&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Understanding these concepts&lt;/strong&gt; helps explain why Python is faster to develop with but slower to execute than C&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Understanding First-Class Functions and Higher-Order Functions in JavaScript</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Tue, 18 Mar 2025 13:56:48 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/understanding-first-class-functions-and-higher-order-functions-in-javascript-4jlj</link>
      <guid>https://dev.to/beatrisilieva/understanding-first-class-functions-and-higher-order-functions-in-javascript-4jlj</guid>
      <description>&lt;h2&gt;
  
  
  📋 Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What is a First-Class Function in JavaScript&lt;/li&gt;
&lt;li&gt;What is a Higher-Order Function in JavaScript&lt;/li&gt;
&lt;li&gt;Built-in Higher-Order Functions in JavaScript&lt;/li&gt;
&lt;li&gt;What is a Pure Function?&lt;/li&gt;
&lt;li&gt;Key Takeaways&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What is a First-Class Function in JavaScript
&lt;/h2&gt;

&lt;p&gt;JavaScript treats functions as &lt;strong&gt;first-class citizens&lt;/strong&gt;, meaning functions can be used just like any other value. This characteristic allows functions to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Be assigned to variables.&lt;/li&gt;
&lt;li&gt;Be passed as arguments to other functions.&lt;/li&gt;
&lt;li&gt;Be returned as results from other functions.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Passing a Function as an Argument
&lt;/h3&gt;

&lt;p&gt;One of the most common applications of first-class functions is passing them as arguments to other functions:&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;operandA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;operandB&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="nf"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;operandA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;operandB&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;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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="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;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;sum&lt;/code&gt; is a function that gets passed into &lt;code&gt;execute&lt;/code&gt; as an argument, demonstrating the first-class function concept.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Higher-Order Function in JavaScript
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;higher-order function&lt;/strong&gt; is a function that either:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Accepts another function as an argument, &lt;strong&gt;or&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Returns a function as a result, &lt;strong&gt;or&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Does both.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;code&gt;execute&lt;/code&gt; function in the previous example is a higher-order function because it takes another function (&lt;code&gt;operation&lt;/code&gt;) as an argument.&lt;/p&gt;

&lt;p&gt;Let's now create an example where a function returns another function:&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;greetingBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;salutation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&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="nf"&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="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;salutation&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="nx"&gt;title&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="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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greetAWoman&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;greetingBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mrs.&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;greetAMan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;greetingBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mr.&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="nf"&gt;greetAWoman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sarah&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Hello, Mrs. Sarah!&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="nf"&gt;greetAMan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Hello, Mr. John!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why Use Higher-Order Functions
&lt;/h3&gt;

&lt;p&gt;Higher-order functions allow code reuse and abstraction. In the example above, &lt;code&gt;greetingBuilder&lt;/code&gt; is a reusable function that generates different greeting functions based on the given parameters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Built-in Higher-Order Functions in JavaScript
&lt;/h2&gt;

&lt;p&gt;JavaScript provides built-in higher-order functions such as &lt;code&gt;map()&lt;/code&gt; and &lt;code&gt;reduce()&lt;/code&gt;. These methods require a function as an argument to determine their 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mappedResult&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;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;element&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="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;mappedResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [2, 4, 6, 8, 10]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reducedResult&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;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;curr&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;reducedResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;map()&lt;/code&gt; and &lt;code&gt;reduce()&lt;/code&gt; accept functions to perform operations on array elements, making them excellent examples of built-in higher-order functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Pure Function
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;pure function&lt;/strong&gt; is a function that always returns the same output for the same input and has no side effects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example of an Impure Function
&lt;/h3&gt;

&lt;p&gt;The function below is &lt;strong&gt;impure&lt;/strong&gt; because its result depends on an external factor (&lt;code&gt;Math.random()&lt;/code&gt;), making it unpredictable:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;notPure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;randomNum&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;randomNum&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="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example of a Pure Function
&lt;/h3&gt;

&lt;p&gt;In contrast, this function is &lt;strong&gt;pure&lt;/strong&gt; because it always returns the same output for the same input:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;pure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;pure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;First-Class Functions&lt;/strong&gt;: Functions in JavaScript can be assigned to variables, passed as arguments, and returned from other functions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Higher-Order Functions&lt;/strong&gt;: Functions that either accept other functions as arguments or return functions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Built-in Higher-Order Functions&lt;/strong&gt;: Methods like &lt;code&gt;map()&lt;/code&gt; and &lt;code&gt;reduce()&lt;/code&gt; allow efficient data manipulation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pure Functions&lt;/strong&gt;: Functions that always return the same result for the same input and do not produce side effects.&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;I would be grateful to understand your opinion.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How the Call Stack, Heap, and Closures Work in JavaScript</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Tue, 18 Mar 2025 13:55:32 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/how-the-call-stack-heap-and-closures-work-in-javascript-1anb</link>
      <guid>https://dev.to/beatrisilieva/how-the-call-stack-heap-and-closures-work-in-javascript-1anb</guid>
      <description>&lt;h2&gt;
  
  
  📋 Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;The Call Stack and Execution Contexts&lt;/li&gt;
&lt;li&gt;The Stack and the Heap&lt;/li&gt;
&lt;li&gt;Closure vs No Closure&lt;/li&gt;
&lt;li&gt;Understanding Closures and Garbage Collection&lt;/li&gt;
&lt;li&gt;Key Takeaways&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Before you continue, you might want to read &lt;a href="https://dev.to/beatrisilieva/understanding-execution-context-and-the-this-keyword-in-javascript-5cgg"&gt;Understanding Execution Context and the &lt;code&gt;This&lt;/code&gt; Keyword in JavaScript&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In JavaScript, the &lt;strong&gt;call stack&lt;/strong&gt; is a critical concept in how functions are executed. It stores the execution contexts of functions as they are called, and the way JavaScript manages memory through the &lt;strong&gt;stack&lt;/strong&gt; and &lt;strong&gt;heap&lt;/strong&gt; directly influences the behavior of variables and closures. Let’s break down the core concepts step by step.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Call Stack and Execution Contexts
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;call stack&lt;/strong&gt; is a data structure that stores execution contexts in a last-in, first-out (LIFO) manner. The &lt;strong&gt;call stack&lt;/strong&gt; is where the JavaScript engine keeps track of function calls. When one function calls another, the &lt;strong&gt;execution context&lt;/strong&gt; of the calling function is pushed onto the call stack. A function's execution context stays on the call stack until the function finishes executing and returns, at which point its context is popped off the stack.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;second&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;second&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello from second!&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;first&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s how the call stack would look:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;first()&lt;/code&gt;&lt;/strong&gt; is called, so its execution context is pushed onto the stack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;second()&lt;/code&gt;&lt;/strong&gt; is called inside &lt;code&gt;first()&lt;/code&gt;, so the execution context of &lt;code&gt;second()&lt;/code&gt; is pushed onto the stack.&lt;/li&gt;
&lt;li&gt;After &lt;code&gt;second()&lt;/code&gt; finishes executing, its context is popped off the stack, and the flow returns to &lt;code&gt;first()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The Stack and the Heap
&lt;/h2&gt;

&lt;p&gt;JavaScript uses two types of memory for storing values: the &lt;strong&gt;stack&lt;/strong&gt; and the &lt;strong&gt;heap&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;stack&lt;/strong&gt; is a small, fast memory area where &lt;strong&gt;primitive data types&lt;/strong&gt; (such as numbers, strings, and booleans) are stored. Variables containing primitive values are pushed and popped from the stack quickly.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;heap&lt;/strong&gt; is a much larger memory area used for storing &lt;strong&gt;reference types&lt;/strong&gt; (such as objects, arrays, and functions). When a reference type is created, a pointer to its memory location is stored on the stack, while the actual data resides in the heap. If an object is nested within another object, the reference to the nested object points to another location in the heap.&lt;/p&gt;

&lt;h3&gt;
  
  
  Primitive vs Reference Types
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Primitive data types&lt;/strong&gt; (e.g., numbers, strings, booleans) have their values stored directly in the stack.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&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;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Stored in the Stack&lt;/span&gt;
&lt;span class="kd"&gt;let&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="s1"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Stored in the Stack&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Reference data types&lt;/strong&gt; (e.g., objects, arrays, and functions) have their &lt;strong&gt;memory locations&lt;/strong&gt; (pointers) stored in the stack, while the actual data is stored in the heap. If an object is nested within another object, the reference to the nested object points to another location in the heap.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&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="s1"&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;25&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// The object itself is stored in the Heap&lt;/span&gt;
&lt;span class="c1"&gt;// The variable 'person' in the Stack holds a reference to it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Closures&lt;/strong&gt; also reside in the Heap because they retain access to outer scope variables even after their execution context has been removed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closure vs No Closure
&lt;/h2&gt;

&lt;p&gt;Now, let's compare two similar functions to see the difference between closures and functions that don't form closures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&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;function&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;inner&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;number&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;innerFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2:&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;function&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;inner&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;number&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation of Differences:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;In Example 1&lt;/strong&gt;, the &lt;code&gt;inner&lt;/code&gt; function is returned by the &lt;code&gt;outer&lt;/code&gt; function, and &lt;strong&gt;keeps a reference to &lt;code&gt;number&lt;/code&gt; even after &lt;code&gt;outer&lt;/code&gt; has finished executing&lt;/strong&gt;. This forms a &lt;strong&gt;closure&lt;/strong&gt;. As a result, each call to &lt;code&gt;innerFunction()&lt;/code&gt; increments the value of &lt;code&gt;number&lt;/code&gt; from where it left off.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In Example 2&lt;/strong&gt;, the &lt;code&gt;inner&lt;/code&gt; function is called &lt;strong&gt;immediately&lt;/strong&gt; within the &lt;code&gt;outer2&lt;/code&gt; function. Once &lt;code&gt;inner&lt;/code&gt; is called, the value of &lt;code&gt;number&lt;/code&gt; is logged and &lt;code&gt;inner&lt;/code&gt; finishes execution. Since no closure is formed, each time &lt;code&gt;outer2&lt;/code&gt; is called, the value of &lt;code&gt;number&lt;/code&gt; starts at 1 again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why This Happens:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Closures (Example 1)&lt;/strong&gt; allow a function to retain access to variables in its outer scope, even after the outer function has finished execution. The variable &lt;code&gt;number&lt;/code&gt; is part of the closure, and its value persists across calls to &lt;code&gt;innerFunction&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Non-closures (Example 2)&lt;/strong&gt; do not retain the state of variables between function calls. The &lt;code&gt;number&lt;/code&gt; variable is created and used within the scope of &lt;code&gt;outer2&lt;/code&gt;, and since the function is called anew each time, the &lt;code&gt;number&lt;/code&gt; variable is reset to 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Closures and Garbage Collection
&lt;/h2&gt;

&lt;p&gt;In the previous examples, we saw that the closure holds onto the variables of the outer function, even after the outer function’s execution context has been removed from the stack. This happens because the closure keeps a &lt;strong&gt;reference&lt;/strong&gt; to the variable, preventing it from being garbage collected. The garbage collector will only remove variables that are no longer reachable, and in the case of closures, the variable remains accessible.&lt;/p&gt;

&lt;p&gt;For example, in the following case:&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;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;inner&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;number&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;innerFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;outer&lt;/code&gt; function creates a local variable &lt;code&gt;number&lt;/code&gt; and returns the &lt;code&gt;inner&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;inner&lt;/code&gt; function forms a &lt;strong&gt;closure&lt;/strong&gt; over &lt;code&gt;number&lt;/code&gt;—it retains access to the &lt;code&gt;number&lt;/code&gt; variable even after &lt;code&gt;outer&lt;/code&gt; has returned.&lt;/li&gt;
&lt;li&gt;The execution context of &lt;code&gt;outer&lt;/code&gt; is removed from the call stack, but the variable &lt;code&gt;number&lt;/code&gt; stays in the heap as part of the closure.&lt;/li&gt;
&lt;li&gt;Every time &lt;code&gt;innerFunction()&lt;/code&gt; is called, it accesses the &lt;code&gt;number&lt;/code&gt; variable from the closure, incrementing its value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Although the execution context of &lt;code&gt;outer&lt;/code&gt; is removed from the call stack after the &lt;code&gt;inner&lt;/code&gt; function is returned, the variable &lt;code&gt;number&lt;/code&gt; remains in memory due to the closure created by &lt;code&gt;inner&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;call stack&lt;/strong&gt; manages function execution contexts.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Stack Memory&lt;/strong&gt; stores primitive values and references to objects.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Heap Memory&lt;/strong&gt; stores actual objects, arrays, functions, and closures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Closures&lt;/strong&gt; retain variables from their outer function in the Heap to ensure accessibility.&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;I would be grateful to understand your opinion.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>closure</category>
      <category>stack</category>
      <category>heap</category>
    </item>
    <item>
      <title>Understanding Execution Context and the `This` Keyword in JavaScript</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Tue, 18 Mar 2025 13:43:32 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/understanding-execution-context-and-the-this-keyword-in-javascript-5cgg</link>
      <guid>https://dev.to/beatrisilieva/understanding-execution-context-and-the-this-keyword-in-javascript-5cgg</guid>
      <description>&lt;h2&gt;
  
  
  📋 Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Execution Context&lt;/li&gt;
&lt;li&gt;Function Context&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;This&lt;/code&gt; Keyword&lt;/li&gt;
&lt;li&gt;Key Takeaways&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Two important concepts in JavaScript that often confuse developers, especially those new to the language, are &lt;strong&gt;Execution Context&lt;/strong&gt; and the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt; keyword&lt;/strong&gt;. In this article, we will break these concepts down, explain how the function context is set, and explore how &lt;code&gt;this&lt;/code&gt; behaves in different situations.&lt;/p&gt;




&lt;h2&gt;
  
  
  Execution Context
&lt;/h2&gt;

&lt;p&gt;Every function in JavaScript is executed in its own &lt;strong&gt;execution context&lt;/strong&gt;. The &lt;strong&gt;execution context&lt;/strong&gt; is essentially the environment in which the function is executed. It holds the scope of variables, the &lt;code&gt;this&lt;/code&gt; binding, and the chain of scopes for a function.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is inside the Execution Context?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Variables&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
The execution context contains all the variables declared within the function. These variables are created when the function is called and cease to exist once the function finishes executing. This is because they are tied to the execution context, and when the function returns, the execution context is removed from the call stack, taking its variables with it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;This&lt;/code&gt; Binding&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
The execution context also defines what &lt;code&gt;this&lt;/code&gt; refers to. The value of &lt;code&gt;this&lt;/code&gt; is determined based on how a function is invoked. It refers to the &lt;strong&gt;Function Context&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scope Chaining&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
In JavaScript, inner functions can access variables from their outer scopes. The execution context helps set up the scope chain, ensuring that when a variable is accessed in an inner function, the JavaScript engine knows where to look for it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Function Context
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;function context&lt;/strong&gt; refers to the object that is being referenced by the &lt;code&gt;this&lt;/code&gt; keyword. In other words, &lt;code&gt;this&lt;/code&gt; refers to the &lt;strong&gt;Function Context&lt;/strong&gt;, which is the object that the function is currently operating within.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Points
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;execution context&lt;/strong&gt; is where the function’s variables, scope, and &lt;code&gt;this&lt;/code&gt; binding are defined.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;function context&lt;/strong&gt; is where the &lt;code&gt;this&lt;/code&gt; keyword refers to an object, and it is set depending on how the function is invoked.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  &lt;code&gt;This&lt;/code&gt; Keyword
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;this&lt;/code&gt; keyword&lt;/strong&gt; refers to the object that is executing the current function. The context of &lt;code&gt;this&lt;/code&gt; depends on &lt;strong&gt;how&lt;/strong&gt; the function is called, not &lt;strong&gt;where&lt;/strong&gt; it is defined. Let's take a closer look at different ways in which a function can be invoked and how the &lt;code&gt;this&lt;/code&gt; context changes in each case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ways to Invoke a Function
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;As a &lt;strong&gt;global function&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;As an &lt;strong&gt;object method&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;As a &lt;strong&gt;callback function&lt;/strong&gt; passed to an event listener&lt;/li&gt;
&lt;li&gt;Using &lt;strong&gt;call()&lt;/strong&gt;, &lt;strong&gt;apply()&lt;/strong&gt;, and &lt;strong&gt;bind()&lt;/strong&gt; methods&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's examine each case in detail.&lt;/p&gt;




&lt;h3&gt;
  
  
  Invoke as Global Function
&lt;/h3&gt;

&lt;p&gt;When a function is invoked in the global scope, the &lt;code&gt;this&lt;/code&gt; keyword refers to the &lt;strong&gt;global object&lt;/strong&gt; (in Node.js, it refers to the &lt;code&gt;module&lt;/code&gt;).&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&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="s2"&gt;`Hi, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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;// Invoke as global function&lt;/span&gt;
&lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hi, my name is undefined!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;this.name&lt;/code&gt; returns &lt;code&gt;undefined&lt;/code&gt; because the global context does not have a &lt;code&gt;name&lt;/code&gt; property.&lt;/p&gt;




&lt;h3&gt;
  
  
  Invoke as a Method
&lt;/h3&gt;

&lt;p&gt;When a function is invoked as a method of an object, the &lt;code&gt;this&lt;/code&gt; keyword refers to that &lt;strong&gt;object&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&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="s2"&gt;`Hi, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&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="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;sayHi&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hi, my name is John!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;this&lt;/code&gt; refers to the &lt;code&gt;person&lt;/code&gt; object, so &lt;code&gt;this.name&lt;/code&gt; is &lt;code&gt;John&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, if the function is invoked as an inner function of a method, the context may 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;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&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="s2"&gt;`Hi, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&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="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;sayHi&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="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hi, my name is undefined!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;this.name&lt;/code&gt; returns &lt;code&gt;undefined&lt;/code&gt; because &lt;code&gt;sayHi&lt;/code&gt; is invoked in the global context, not as part of the &lt;code&gt;person&lt;/code&gt; object.&lt;/p&gt;




&lt;h3&gt;
  
  
  Invoke as a Callback
&lt;/h3&gt;

&lt;p&gt;When a function is passed as a callback to an event listener, the &lt;code&gt;this&lt;/code&gt; context refers to the &lt;strong&gt;DOM element&lt;/strong&gt; that the event is attached to.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"button"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Click"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"Button"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inputElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;inputElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sayHi&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;sayHi&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="s2"&gt;`Hi, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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="c1"&gt;// Hi, my name is Button!&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;this&lt;/code&gt; refers to the input element, and &lt;code&gt;this.name&lt;/code&gt; is &lt;code&gt;Button&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Arrow Function Context
&lt;/h3&gt;

&lt;p&gt;Arrow functions behave differently than regular functions. They do not have their own &lt;code&gt;this&lt;/code&gt; context. Instead, they inherit the &lt;code&gt;this&lt;/code&gt; context from their &lt;strong&gt;parent function&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sayHiOuterArrow&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="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;`Hi, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&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="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;greet&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;sayHi&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="s2"&gt;`Hi, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sayHiArrow&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="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;`Hi, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// John&lt;/span&gt;

        &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hi, my name is undefined!&lt;/span&gt;
        &lt;span class="nf"&gt;sayHiArrow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hi, my name is John!&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;sayHiArrow()&lt;/code&gt; correctly logs &lt;code&gt;John&lt;/code&gt; because it inherits &lt;code&gt;this&lt;/code&gt; from the &lt;code&gt;greet&lt;/code&gt; method, while &lt;code&gt;sayHi()&lt;/code&gt; does not.&lt;/p&gt;




&lt;h3&gt;
  
  
  Explicit Binding
&lt;/h3&gt;

&lt;p&gt;JavaScript provides three methods for explicitly binding the &lt;code&gt;this&lt;/code&gt; context: &lt;strong&gt;&lt;code&gt;call()&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;apply()&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;bind()&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Call
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;call()&lt;/code&gt;&lt;/strong&gt; method invokes a function with a specified &lt;code&gt;this&lt;/code&gt; context.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&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="s2"&gt;`Hi, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newContext&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="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hi, my name is John!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Apply
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;apply()&lt;/code&gt;&lt;/strong&gt; method is similar to &lt;code&gt;call()&lt;/code&gt;, but instead of passing arguments individually, we pass them as an array.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;salutation&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="s2"&gt;`Hi &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;salutation&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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newContext&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="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newContext&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="s1"&gt;dear&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;Michel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Hi dear Michel, my name is John!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Bind
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;bind()&lt;/code&gt;&lt;/strong&gt; method returns a new function with the &lt;code&gt;this&lt;/code&gt; context set, which can be invoked later.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;salutation&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="s2"&gt;`Hi &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;salutation&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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newContext&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="s1"&gt;John&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;modifiedSayHi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;modifiedSayHi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dear&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;Michel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hi dear Michel, my name is John!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Execution Context&lt;/strong&gt;: Each function runs in its own execution context, which includes variables, &lt;code&gt;this&lt;/code&gt; binding, and scope chaining.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function Context&lt;/strong&gt;: The object that &lt;code&gt;this&lt;/code&gt; refers to depends on how the function is invoked.
&lt;strong&gt;&lt;code&gt;this&lt;/code&gt; Keyword&lt;/strong&gt;: 1. Always refers to an object. 2. Its value is determined at the time of function invocation.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Ways to Invoke a Function&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Global Function&lt;/strong&gt;: &lt;code&gt;this&lt;/code&gt; refers to the global object (&lt;code&gt;window&lt;/code&gt; in browsers, &lt;code&gt;global&lt;/code&gt; in Node.js).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method Call&lt;/strong&gt;: &lt;code&gt;this&lt;/code&gt; refers to the object the method belongs to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Callback Function (Event Listener)&lt;/strong&gt;: &lt;code&gt;this&lt;/code&gt; refers to the element that triggered the event.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arrow Function&lt;/strong&gt;: Inherits &lt;code&gt;this&lt;/code&gt; from its enclosing scope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explicit Binding&lt;/strong&gt;:
&lt;code&gt;call()&lt;/code&gt;: Calls a function with a specified &lt;code&gt;this&lt;/code&gt; value.
&lt;code&gt;apply()&lt;/code&gt;: Works like &lt;code&gt;call()&lt;/code&gt;, but accepts an array of arguments.
&lt;code&gt;bind()&lt;/code&gt;: Returns a new function with &lt;code&gt;this&lt;/code&gt; bound, which can be called later.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;




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

&lt;p&gt;I would be grateful to understand your opinion.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>this</category>
      <category>executioncontext</category>
      <category>contextbinding</category>
    </item>
    <item>
      <title>Asynchronous Programming in JavaScript: A Beginner’s Guide</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Mon, 17 Mar 2025 16:10:01 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/asynchronous-programming-in-javascript-a-beginners-guide-3hd1</link>
      <guid>https://dev.to/beatrisilieva/asynchronous-programming-in-javascript-a-beginners-guide-3hd1</guid>
      <description>&lt;h2&gt;
  
  
  📋 Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Multi-Threaded vs Single-Threaded in JavaScript&lt;/li&gt;
&lt;li&gt;AJAX&lt;/li&gt;
&lt;li&gt;Synchronous vs Asynchronous Programming&lt;/li&gt;
&lt;li&gt;Fetch API&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Abbreviations
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;JS: JavaScript&lt;/li&gt;
&lt;li&gt;AJAX: Asynchronous JavaScript and XML&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Before you continue, you might want to read &lt;a href="https://dev.to/beatrisilieva/a-beginners-guide-to-http-and-rest-services-3j92"&gt;A Beginner’s Guide to HTTP and REST Services&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A request to the backend is an asynchronous operation. When we send a request to the backend to fetch content from a database, we have no control over how long it will take to process. The backend first receives the request, processes it, queries the database, processes the retrieved data, and only then returns a response. The total time required depends on various factors, such as server load, network congestion, and internet speed.&lt;/p&gt;

&lt;p&gt;In some cases, the process may take a few seconds, which is a long time in the context of software applications. If users are forced to wait without being able to interact with the application, it results in a poor user experience—such as a frozen screen until the response arrives.&lt;/p&gt;

&lt;p&gt;This is where asynchronous programming comes into play. It allows the code to continue executing normally while the request is processed in the background. Once the request is complete, the result is displayed without interrupting the user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multi-Threaded vs Single-Threaded in JavaScript
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Before you continue, you might want to read &lt;a href="https://dev.to/beatrisilieva/understanding-the-event-loop-the-heart-of-asynchronous-javascript-3g0i"&gt;Understanding the Event Loop: The Heart of Asynchronous JavaScript&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In general, multi-threaded means that multiple processes can run simultaneously, with two programs executing in parallel, independently of each other. On the other hand, synchronous code runs sequentially, within a single process.&lt;/p&gt;

&lt;p&gt;JS, however, is a single-threaded language, meaning it executes code in one process at a time. But here’s the interesting part: despite being single-threaded, JS allows asynchronous operations. This means that even though JS processes tasks one at a time, it can still handle tasks like network requests, allowing other code to run in the meantime.&lt;/p&gt;

&lt;p&gt;So, how can JS be asynchronous if it’s not multi-threaded? The key lies in the event loop mechanism, which lets JS offload tasks to be executed later, without blocking the main thread. This gives the illusion of parallel execution while maintaining a single thread of execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  AJAX
&lt;/h2&gt;

&lt;p&gt;AJAX is a technique for fetching data from a remote server in the background without disrupting the user experience. It allows web applications to send and receive data asynchronously, meaning the page doesn’t need to reload to update its content.&lt;/p&gt;

&lt;p&gt;With AJAX, JS instructs the browser to send requests to the server, retrieve data, and dynamically update the webpage without requiring a full refresh. This approach enhances user experience by enabling smooth interactions, such as loading new content, submitting forms, or fetching search results without interruptions.&lt;/p&gt;

&lt;h3&gt;
  
  
  AJAX Workflow
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Initial Page Load
&lt;/h4&gt;

&lt;p&gt;When a user visits a webpage, the browser makes an HTTP request to the server to fetch the necessary resources, such as HTML, CSS, JS, images, and fonts. This initial page load involves multiple requests, not just one, as each resource must be fetched separately. Once all these resources are loaded, the web application becomes interactive and starts running in the browser. At this point, the client-side JS is ready to handle dynamic content and further user interactions.&lt;/p&gt;

&lt;h4&gt;
  
  
  User Interaction
&lt;/h4&gt;

&lt;p&gt;Once the page is loaded, JS is responsible for making the application interactive. When a user triggers an event—such as clicking a button or submitting a form—the application does not reload the entire page. Instead, an AJAX request is initiated to communicate with the server in the background.&lt;/p&gt;

&lt;h4&gt;
  
  
  Request to the RESTful Server
&lt;/h4&gt;

&lt;p&gt;The AJAX request is sent to the server, which processes the request and sends back a response. Most of the time, the server responds with data in a lightweight format like JSON.&lt;/p&gt;

&lt;h4&gt;
  
  
  Processing and Rendering
&lt;/h4&gt;

&lt;p&gt;After the data is received, the client processes the response and dynamically updates the page as needed. This could involve rendering new content, updating UI elements, or displaying messages to the user. The processing of data happens asynchronously, ensuring the application remains responsive while handling the background request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Synchronous vs Asynchronous Programming
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Synchronous Programming
&lt;/h3&gt;

&lt;p&gt;Synchronous programming means that the code is executed line by line, one command after the other. Each operation must complete before the next one can begin. This results in a predictable flow of execution, but it can be inefficient if some operations take a long time to complete (e.g., network requests). Here’s an example of synchronous code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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;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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the numbers will be printed one after another in the exact order.&lt;/p&gt;

&lt;h3&gt;
  
  
  Asynchronous Programming
&lt;/h3&gt;

&lt;p&gt;Asynchronous programming allows certain tasks, like fetching data from a server, to run in the background while other code continues to execute. This prevents blocking the entire program, keeping the application responsive. In JS, asynchronous code can execute concurrently, meaning the total execution time depends on the longest task, and the program remains functional while waiting for the result.&lt;/p&gt;

&lt;h4&gt;
  
  
  Approaches to asynchronous programming
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Callbacks
&lt;/h5&gt;

&lt;p&gt;Let's look at an example using the built-in setTimeout() function. This function accepts two arguments: a callback (a function to execute) and a delay (the time after which the callback will be executed). With setTimeout(), the callback is executed asynchronously after the specified time.&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Before&lt;/span&gt;&lt;span class="dl"&gt;'&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="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Meanwhile&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="s1"&gt;After&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a synchronous context, we'd expect to see the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;'Before'&lt;/li&gt;
&lt;li&gt;Wait for 2 seconds&lt;/li&gt;
&lt;li&gt;'Meanwhile'&lt;/li&gt;
&lt;li&gt;'After'&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, the actual output is:&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;Before&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;After&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;Meanwhile&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;console.log('Before') is executed immediately.&lt;/li&gt;
&lt;li&gt;setTimeout() delegates the callback function to be executed later (after 2 seconds).&lt;/li&gt;
&lt;li&gt;console.log('After') is executed immediately after setTimeout() is called, even before the callback runs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, what if we set the callback to execute after 0 seconds?&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Before&lt;/span&gt;&lt;span class="dl"&gt;'&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="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Meanwhile&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;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;After&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;The output remains the same:&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;Before&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;After&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;Meanwhile&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This happens because synchronous actions always execute before asynchronous ones, even if the delay is set to 0. The callback is still placed in the event queue and will only be executed after the current stack is cleared.&lt;/p&gt;

&lt;h5&gt;
  
  
  Promises
&lt;/h5&gt;

&lt;p&gt;A Promise is a JS object that represents a value that may be available in the future, or never. It is commonly used to handle asynchronous operations, such as fetching data from a server.&lt;/p&gt;

&lt;h6&gt;
  
  
  Promise States
&lt;/h6&gt;

&lt;p&gt;A Promise can be in one of three states:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pending → The operation is still in progress.&lt;/li&gt;
&lt;li&gt;Fulfilled → The operation was successful, and we have the result.&lt;/li&gt;
&lt;li&gt;Rejected → An error occurred, and the operation failed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h6&gt;
  
  
  Creating a Promise
&lt;/h6&gt;

&lt;p&gt;To create a Promise, we use the Promise class and provide an executor function. This function takes two parameters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;resolve(value) → Called when the operation succeeds.&lt;/li&gt;
&lt;li&gt;reject(reason) → Called when the operation fails.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s an example simulating a rocket launch mission 🚀:&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;launchRocket&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="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;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="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🚀 Initiating launch sequence...&lt;/span&gt;&lt;span class="dl"&gt;'&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;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;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="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="s1"&gt;🎉 The rocket has successfully landed on Mars! 🏆&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="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🔥 Mission failed! The rocket exploded in space. 💥&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;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Simulating a 3-second delay&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;h6&gt;
  
  
  Using a Promise
&lt;/h6&gt;

&lt;p&gt;Once we have a Promise, we handle its outcome using:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;.then(successCallback) → Executes when the Promise is fulfilled.&lt;/li&gt;
&lt;li&gt;.catch(errorCallback) → Executes when the Promise is rejected.
&lt;/li&gt;
&lt;/ol&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;mission&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;launchRocket&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;mission&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Runs if the mission succeeds&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="nx"&gt;error&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Runs if the mission fails&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;📡 Monitoring rocket status...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This runs immediately (non-blocking)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If success:&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="err"&gt;🚀&lt;/span&gt; &lt;span class="nx"&gt;Initiating&lt;/span&gt; &lt;span class="nx"&gt;launch&lt;/span&gt; &lt;span class="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="err"&gt;📡&lt;/span&gt; &lt;span class="nx"&gt;Monitoring&lt;/span&gt; &lt;span class="nx"&gt;rocket&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Wait&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;...)&lt;/span&gt;
&lt;span class="err"&gt;🎉&lt;/span&gt; &lt;span class="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;rocket&lt;/span&gt; &lt;span class="nx"&gt;has&lt;/span&gt; &lt;span class="nx"&gt;successfully&lt;/span&gt; &lt;span class="nx"&gt;landed&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;Mars&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="err"&gt;🏆&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Otherwise:&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="err"&gt;🚀&lt;/span&gt; &lt;span class="nx"&gt;Initiating&lt;/span&gt; &lt;span class="nx"&gt;launch&lt;/span&gt; &lt;span class="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="err"&gt;📡&lt;/span&gt; &lt;span class="nx"&gt;Monitoring&lt;/span&gt; &lt;span class="nx"&gt;rocket&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Wait&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;...)&lt;/span&gt;
&lt;span class="err"&gt;🔥&lt;/span&gt; &lt;span class="nx"&gt;Mission&lt;/span&gt; &lt;span class="nx"&gt;failed&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;rocket&lt;/span&gt; &lt;span class="nx"&gt;exploded&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;space&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="err"&gt;💥&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Async/Await
&lt;/h5&gt;

&lt;p&gt;Async/Await is a more readable way to work with promises in JavaScript. It provides a cleaner and more intuitive syntax compared to using .then() and .catch().&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;async functions always return a promise, even if you don't explicitly return one. If a value is returned, it's automatically wrapped in a resolved promise.&lt;/li&gt;
&lt;li&gt;The await keyword can only be used inside async functions. It pauses the execution of the function at that point until the promise resolves or rejects.&lt;/li&gt;
&lt;li&gt;While the code inside an async function looks synchronous, it is non-blocking and will not freeze the rest of the program. It still allows asynchronous operations to run, but the function execution itself pauses at each await until the promise is settled.
&lt;/li&gt;
&lt;/ol&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;launchRocket&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="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;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="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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🚀 Initiating launch sequence...&lt;/span&gt;&lt;span class="dl"&gt;'&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;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;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="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="s1"&gt;🎉 The rocket has successfully landed on Mars! 🏆&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="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🔥 Mission failed! The rocket exploded in space. 💥&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;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;startMission&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&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;launchRocket&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="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="nx"&gt;err&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;err&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;startMission&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Key Points
&lt;/h6&gt;

&lt;ol&gt;
&lt;li&gt;async keyword: Declares a function as asynchronous, which will always return a promise.&lt;/li&gt;
&lt;li&gt;await keyword: Pauses the execution of the function until the promise resolves or rejects.&lt;/li&gt;
&lt;li&gt;Synchronous-looking code: Inside an async function, code looks like it's executing synchronously, but it still works asynchronously behind the scenes.&lt;/li&gt;
&lt;li&gt;Error handling: Use try/catch to handle errors with async/await, just like synchronous code.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Fetch API
&lt;/h2&gt;

&lt;p&gt;The Fetch API is a way to make HTTP requests in JavaScript, and it’s built on Promises.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Promise: fetch() returns a promise that resolves to the response of the request.&lt;/li&gt;
&lt;li&gt;Response: The response from a fetch() request is a Stream, which means it’s processed asynchronously.&lt;/li&gt;
&lt;li&gt;Default Method: By default, fetch() uses the GET method, so we don’t need to specify it unless we're making a request with another HTTP method (like POST or PUT).&lt;/li&gt;
&lt;li&gt;Options Parameter: When sending a request with a body (e.g., POST or PUT), we need to pass an options object where we specify the method, headers, and body.&lt;/li&gt;
&lt;li&gt;Body and Headers: When sending data in the body of a request, it should be a JSON string. We must also include headers to tell the server that we're sending JSON data (e.g., 'Content-Type': 'application/json').&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  GET Request
&lt;/h3&gt;

&lt;p&gt;A GET request is used to retrieve data from a server.&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://jsonplaceholder.typicode.com/posts/1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns a promise&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// returns a promise&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;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;// handle errors&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;userId&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="nx"&gt;id&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="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sunt aut facere repellat provident occaecati excepturi optio reprehenderit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;quia et suscipit&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;suscipit recusandae consequuntur expedita et cum&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;reprehenderit molestiae ut ut quas totam&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nostrum rerum est autem sunt rem eveniet architecto&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;h3&gt;
  
  
  POST REQUEST
&lt;/h3&gt;

&lt;p&gt;A POST request is used to send data to the server, such as creating a new resource.&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://jsonplaceholder.typicode.com/posts&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;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;headers&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="s1"&gt;Content-Type&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;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;body&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="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;My First Post&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is a test post.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;userId&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="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error:&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;My First Post&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is a test post.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;userId&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="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;101&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  PUT Request
&lt;/h3&gt;

&lt;p&gt;A PUT request is used to update an existing resource on the server.&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://jsonplaceholder.typicode.com/posts/1&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;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PUT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;headers&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="s1"&gt;Content-Type&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;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;body&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="na"&gt;id&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="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Updated title&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Updated content&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;userId&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="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error:&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result&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="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;id&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="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Updated title&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Updated content&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userId&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  DELETE Request
&lt;/h4&gt;

&lt;p&gt;A DELETE request is used to delete a resource from the server.&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://jsonplaceholder.typicode.com/posts/1&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;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DELETE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Deleted:&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;ok&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="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;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;Error:&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&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;Deleted&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using the Fetch API, we can perform asynchronous operations like retrieving and sending data to a server without blocking the rest of our code.&lt;/p&gt;

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

&lt;p&gt;In this guide, we've explored the key concepts and techniques involved in asynchronous programming in JS.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Asynchronous Programming allows us to perform tasks like network requests without blocking the rest of our code, which improves user experience by keeping applications responsive.&lt;/li&gt;
&lt;li&gt;JS is single-threaded, yet through asynchronous operations and the event loop, it can handle tasks concurrently.&lt;/li&gt;
&lt;li&gt;AJAX is a set of techniques that allows JS to instruct the browser to fetch data from the server in the background and update the UI dynamically, without reloading the page, ensuring smooth interactions.&lt;/li&gt;
&lt;li&gt;Synchronous vs Asynchronous programming contrasts how tasks are executed: synchronously (sequentially) and asynchronously (non-blocking).&lt;/li&gt;
&lt;li&gt;Promises are a foundational concept in handling asynchronous operations, offering methods like .then() and .catch() to manage success and error cases.&lt;/li&gt;
&lt;li&gt;Async/Await provides a cleaner and more intuitive syntax for working with Promises, making asynchronous code look and behave more synchronously.&lt;/li&gt;
&lt;li&gt;Fetch API allows us to make HTTP requests, such as GET, POST, PUT, and DELETE, leveraging Promises to handle responses asynchronously.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;I would be grateful to understand your opinion.&lt;/p&gt;

</description>
      <category>ajax</category>
      <category>promises</category>
      <category>asynchronousprogramming</category>
    </item>
    <item>
      <title>Understanding the Event Loop: The Heart of Asynchronous JavaScript</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Mon, 17 Mar 2025 15:52:19 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/understanding-the-event-loop-the-heart-of-asynchronous-javascript-3g0i</link>
      <guid>https://dev.to/beatrisilieva/understanding-the-event-loop-the-heart-of-asynchronous-javascript-3g0i</guid>
      <description>&lt;h2&gt;
  
  
  📋 Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Step-by-Step Explanation of the JavaScript Event Loop Execution&lt;/li&gt;
&lt;li&gt;Data Structures: Call Stack and Event Queue&lt;/li&gt;
&lt;li&gt;Key Takeaways&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🔗 &lt;strong&gt;Live Demo&lt;/strong&gt;: &lt;a href="https://beatrisilieva.github.io/event-loop" rel="noopener noreferrer"&gt;Click Here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🎥 &lt;strong&gt;Demo Video&lt;/strong&gt;:&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=LX0Mky7DvFc" rel="noopener noreferrer"&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%2F8lqdzqisy6dv2e0gj2ml.jpg" alt="Watch the video" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;JavaScript is a &lt;strong&gt;single-threaded&lt;/strong&gt; language, meaning it can only execute one task at a time on the main thread. However, this doesn't mean JavaScript can't handle multiple tasks simultaneously. The key to understanding this behavior lies in the &lt;strong&gt;Event Loop&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Event Loop allows JavaScript to perform asynchronous operations, such as waiting for a network request or timer, without blocking the main thread. By offloading tasks to be executed later, JavaScript creates the illusion of parallel execution while still maintaining its single-threaded nature.&lt;/p&gt;

&lt;p&gt;In this article, we’ll take a closer look at how the Event Loop manages both &lt;strong&gt;synchronous&lt;/strong&gt; and &lt;strong&gt;asynchronous&lt;/strong&gt; tasks. We’ll walk through how JavaScript handles operations, manages callbacks, and uses the Event Loop to ensure that asynchronous tasks don't block the execution of synchronous code.&lt;/p&gt;

&lt;p&gt;To help visualize this process, we provide a &lt;strong&gt;series of visualizations&lt;/strong&gt; that illustrate each step of how JavaScript handles tasks. These visualizations are based on a specific code example, which is shown throughout the images to simplify the explanation. Each visualization is accompanied by a &lt;strong&gt;description&lt;/strong&gt; to clarify what's happening at each step. This approach helps us see how tasks move through the &lt;strong&gt;Call Stack&lt;/strong&gt;, the &lt;strong&gt;Event Queue&lt;/strong&gt;, and how the &lt;strong&gt;Event Loop&lt;/strong&gt; orchestrates everything.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Structures: Call Stack and Event Queue
&lt;/h2&gt;

&lt;p&gt;In this article, we’ll also briefly touch on the two main data structures that help JavaScript manage synchronous and asynchronous tasks:&lt;/p&gt;

&lt;p&gt;🍽️🍽️🍽️ &lt;strong&gt;Call Stack&lt;/strong&gt;: The &lt;em&gt;Call Stack&lt;/em&gt; is where JavaScript keeps track of &lt;strong&gt;synchronous&lt;/strong&gt; functions that are executing. It works like a stack of plates in the kitchen sink — as each plate gets added, it’s placed on top of the stack. When the sink gets full, the plate that is &lt;strong&gt;added last is the first to be removed&lt;/strong&gt;. Similarly, when a function is called, it’s added on top of the stack, and once it finishes executing, it’s removed from the top.&lt;/p&gt;

&lt;p&gt;☕🚶‍♂️🚶‍♀️ &lt;strong&gt;Event Queue&lt;/strong&gt;: The &lt;em&gt;Event Queue&lt;/em&gt; holds &lt;strong&gt;asynchronous&lt;/strong&gt; tasks waiting to be processed. It works like a line at a coffee shop. The first person in line is the first to get coffee, meaning the &lt;strong&gt;first task added in the queue will be the first one to be processed&lt;/strong&gt; once the &lt;em&gt;Call Stack&lt;/em&gt; is clear.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step-by-Step Explanation of the JavaScript Event Loop Execution
&lt;/h2&gt;

&lt;p&gt;For better visualization, all synchronous execution contexts are pushed onto the call stack, ready to be executed.&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%2F0je2py03u030te3pz83g.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%2F0je2py03u030te3pz83g.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🚀 &lt;code&gt;console.log('Start');&lt;/code&gt; is executed immediately, producing &lt;code&gt;"Start"&lt;/code&gt; as output.&lt;/p&gt;

&lt;p&gt;⏸️ The &lt;em&gt;Event Loop&lt;/em&gt; is &lt;strong&gt;paused&lt;/strong&gt; while synchronous code runs in the main thread.&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%2Fcaxg6vc9gjbopos026ty.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%2Fcaxg6vc9gjbopos026ty.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;⏳ The &lt;code&gt;setTimeout(() =&amp;gt; { zeroSecondsLater(); }, 0);&lt;/code&gt; function is &lt;strong&gt;asynchronous&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;🔄 The JavaScript engine &lt;strong&gt;delegates&lt;/strong&gt; it to the &lt;em&gt;Browser API&lt;/em&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%2Flej2fn4sp7vntmi7pw0m.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%2Flej2fn4sp7vntmi7pw0m.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📩 Since the delay is 0ms, the &lt;em&gt;Browser API&lt;/em&gt; processes the request immediately and moves the &lt;code&gt;zeroSecondsLater callback&lt;/code&gt; to the &lt;em&gt;Event Queue&lt;/em&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%2Frepf1wfbe9w28t52ca5d.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%2Frepf1wfbe9w28t52ca5d.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔄 &lt;code&gt;setTimeout(() =&amp;gt; { console.log('3 seconds later'); }, 3000);&lt;/code&gt; is &lt;strong&gt;delegated&lt;/strong&gt; to the &lt;em&gt;Browser API&lt;/em&gt;, which starts a 3-second timer.&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%2Fovd42981og7xrbf9uaw4.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%2Fovd42981og7xrbf9uaw4.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;⏳ Similarly, &lt;code&gt;setTimeout(() =&amp;gt; { console.log('4 seconds later'); }, 4000);&lt;/code&gt; is &lt;strong&gt;delegated&lt;/strong&gt; to the &lt;em&gt;Browser API&lt;/em&gt;, which starts a 4-second timer.&lt;/p&gt;

&lt;p&gt;⏩ Meanwhile, &lt;strong&gt;synchronous code continues executing in the main thread without waiting for these timers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;🛑 Important: The &lt;strong&gt;Call Stack executes only synchronous functions&lt;/strong&gt; and must complete all synchronous tasks before handling anything else. The &lt;em&gt;Event Loop&lt;/em&gt; does not move callbacks from the &lt;em&gt;Event Queue&lt;/em&gt; to the &lt;em&gt;Call Stack&lt;/em&gt; until all synchronous code has finished executing.&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%2Fm4dnvxe2g5w8uiitxhwt.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%2Fm4dnvxe2g5w8uiitxhwt.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🚀 &lt;code&gt;console.log('End');&lt;/code&gt; is executed immediately, producing &lt;code&gt;"End"&lt;/code&gt; as output.&lt;/p&gt;

&lt;p&gt;👀 At this point, the &lt;em&gt;Call Stack&lt;/em&gt; is empty, &lt;strong&gt;signaling&lt;/strong&gt; the &lt;em&gt;Event Loop&lt;/em&gt; to check the event queue.&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%2F53apmd7c3dtgjrsii0ls.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%2F53apmd7c3dtgjrsii0ls.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🎧 The &lt;em&gt;Event Loop&lt;/em&gt; waits for the &lt;em&gt;Call Stack&lt;/em&gt; to become empty and then moves the &lt;strong&gt;first&lt;/strong&gt; callback from the &lt;em&gt;Event Queue&lt;/em&gt; to the &lt;em&gt;Call Stack&lt;/em&gt; for execution.&lt;/p&gt;

&lt;p&gt;🔁 The &lt;code&gt;zeroSecondsLater();&lt;/code&gt; callback is moved from the event queue to the call stack.&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%2F09jwmi4xpjedysw1w9sc.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%2F09jwmi4xpjedysw1w9sc.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📩 The &lt;em&gt;Browser API&lt;/em&gt; completes the 3-second timer and moves &lt;code&gt;console.log('3 seconds later');&lt;/code&gt; to the &lt;em&gt;Event Queue&lt;/em&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%2F44d120rhkhirokk3ikd3.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%2F44d120rhkhirokk3ikd3.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📩 One more second has passed so the &lt;em&gt;Browser API&lt;/em&gt; moves &lt;code&gt;console.log('4 seconds later');&lt;/code&gt; to the &lt;em&gt;Event Queue&lt;/em&gt;, placing it after the &lt;code&gt;console.log('3 seconds later');&lt;/code&gt; callback.&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%2Fziyv468oa1e8c2an9m3o.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%2Fziyv468oa1e8c2an9m3o.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔄 The &lt;code&gt;zeroSecondsLater();&lt;/code&gt; callback invokes &lt;code&gt;oneSecondLater();&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%2F2mwfscjvcpbwbu3o5xnw.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%2F2mwfscjvcpbwbu3o5xnw.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔄 &lt;code&gt;oneSecondLater();&lt;/code&gt; invokes &lt;code&gt;console.log&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%2Fndmvw38df2ll5k5u94g9.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%2Fndmvw38df2ll5k5u94g9.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;💬 &lt;code&gt;"1 second later"&lt;/code&gt; is printed in the console.&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%2Fm621m0qa7p79hjtrib0q.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%2Fm621m0qa7p79hjtrib0q.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔄 Then, &lt;code&gt;zeroSecondsLater();&lt;/code&gt; invokes &lt;code&gt;twoSecondsLater();&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%2Fkoqiyeidwkk21nlu0iq4.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%2Fkoqiyeidwkk21nlu0iq4.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔄 &lt;code&gt;twoSecondsLater();&lt;/code&gt; invokes &lt;code&gt;console.log&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%2Fi0qh9u8o62ai9mxdsopb.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%2Fi0qh9u8o62ai9mxdsopb.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;💬 &lt;code&gt;"2 seconds later"&lt;/code&gt; is printed in the console. All the synchronous code has been executed and the callback function &lt;code&gt;zeroSecondsLater()&lt;/code&gt; completes its execution. At this point, the &lt;em&gt;Call Stack&lt;/em&gt; becomes empty, signaling the &lt;em&gt;Event Loop&lt;/em&gt; to begin processing callbacks from the &lt;em&gt;Event Queue&lt;/em&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%2F3ruu4yumc5c0nswad0kj.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%2F3ruu4yumc5c0nswad0kj.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👀 The &lt;em&gt;Event Loop&lt;/em&gt; moves &lt;code&gt;console.log('3 seconds later');&lt;/code&gt; from the &lt;em&gt;Event Queue&lt;/em&gt; to the &lt;em&gt;Call Stack&lt;/em&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%2F8breq8k5l52wfuf7vct0.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%2F8breq8k5l52wfuf7vct0.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🚀 The function executes, producing &lt;code&gt;"3 seconds later"&lt;/code&gt; in the console.&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%2F29ezuighnjct6k17oces.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%2F29ezuighnjct6k17oces.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👀 Again, the &lt;em&gt;Call Stack&lt;/em&gt; is empty, allowing the &lt;em&gt;Event Loop&lt;/em&gt; to move &lt;code&gt;console.log('4 seconds later');&lt;/code&gt; from the &lt;em&gt;Event Queue&lt;/em&gt; to the &lt;em&gt;Call Stack&lt;/em&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%2F9zbgbptlrpcsnsjdibi6.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%2F9zbgbptlrpcsnsjdibi6.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🚀 The function executes, producing &lt;code&gt;"4 seconds later"&lt;/code&gt; in the console.&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%2F5uwms5of2e407bbwhu9x.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%2F5uwms5of2e407bbwhu9x.png" alt="Image Alt Text" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;✅ &lt;strong&gt;Only synchronous code is executed in the Call Stack&lt;/strong&gt; (Execution Stack) on the &lt;strong&gt;main thread&lt;/strong&gt;. The &lt;em&gt;Event Loop&lt;/em&gt; does not move callbacks from the &lt;em&gt;Event Queue&lt;/em&gt; to the &lt;em&gt;Call Stack&lt;/em&gt; until all synchronous code has finished executing&lt;/p&gt;

&lt;p&gt;✅ Asynchronous functions (e.g., setTimeout) are &lt;strong&gt;delegated&lt;/strong&gt; to the &lt;em&gt;Browser API&lt;/em&gt;, which processes them in &lt;strong&gt;parallel&lt;/strong&gt; while synchronous code continues.&lt;/p&gt;

&lt;p&gt;✅ Callbacks enter the &lt;em&gt;Event Queue&lt;/em&gt; based on when they were &lt;strong&gt;processed&lt;/strong&gt; by the &lt;em&gt;Browser API&lt;/em&gt;, not necessarily in the order they were requested.&lt;/p&gt;

&lt;p&gt;✅ The &lt;em&gt;Event Loop&lt;/em&gt; waits for the &lt;em&gt;Call Stack&lt;/em&gt; to be &lt;strong&gt;empty&lt;/strong&gt; before moving a callback from the &lt;em&gt;Event Queue&lt;/em&gt; to the &lt;em&gt;Call Stack&lt;/em&gt;.&lt;/p&gt;




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

&lt;p&gt;I would be grateful to understand your opinion.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>eventloop</category>
    </item>
    <item>
      <title>A Beginner’s Guide to HTTP and REST Services</title>
      <dc:creator>Beatris Ilieva</dc:creator>
      <pubDate>Fri, 14 Mar 2025 11:37:09 +0000</pubDate>
      <link>https://dev.to/beatrisilieva/a-beginners-guide-to-http-and-rest-services-3j92</link>
      <guid>https://dev.to/beatrisilieva/a-beginners-guide-to-http-and-rest-services-3j92</guid>
      <description>&lt;h2&gt;
  
  
  📋 Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Abbreviations&lt;/li&gt;
&lt;li&gt;HTTP&lt;/li&gt;
&lt;li&gt;Web Client and Web Server&lt;/li&gt;
&lt;li&gt;HTTP Request Methods&lt;/li&gt;
&lt;li&gt;HTTP Request&lt;/li&gt;
&lt;li&gt;HTTP Response&lt;/li&gt;
&lt;li&gt;Status Codes&lt;/li&gt;
&lt;li&gt;REST Services&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Abbreviations
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;WWW (World Wide Web)&lt;/strong&gt; – A system of interlinked web pages and resources accessible via the internet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTTP (Hypertext Transfer Protocol)&lt;/strong&gt; – The foundation of web communication, enabling clients (browsers) and servers to exchange data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CRUD (Create, Read, Update, Delete)&lt;/strong&gt; – The four basic operations performed on data in an application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;REST (Representational State Transfer)&lt;/strong&gt; – An architectural style for designing web services based on standard HTTP methods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;URI (Uniform Resource Identifier)&lt;/strong&gt; – A string used to uniquely identify a resource on the web.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;When we talk about the web, we often use the terms &lt;code&gt;Internet&lt;/code&gt; and &lt;code&gt;WWW&lt;/code&gt; interchangeably, but they refer to different things. The Internet is the vast global &lt;strong&gt;network&lt;/strong&gt; that connects computers and devices, while the &lt;code&gt;WWW&lt;/code&gt; is a &lt;strong&gt;service&lt;/strong&gt; that runs on top of this network, allowing us to access websites and exchange information through &lt;code&gt;HTTP&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;WWW&lt;/code&gt; is a &lt;strong&gt;service&lt;/strong&gt; on the Internet that enables communication between a &lt;code&gt;Client&lt;/code&gt; (such as a web browser) and a &lt;code&gt;Server&lt;/code&gt;. This interaction happens &lt;strong&gt;through HTTP&lt;/strong&gt;, a &lt;strong&gt;text-based protocol&lt;/strong&gt; that defines the &lt;em&gt;rules for transferring web resources&lt;/em&gt; like HTML, CSS, JavaScript, JSON, images, and fonts—powering the web as we know it. Without following these rules, communication between the client and server would fail.&lt;/p&gt;

&lt;p&gt;Regardless of the backend or frontend technology we use, we must always follow &lt;code&gt;HTTP&lt;/code&gt; rules to &lt;strong&gt;make requests from the client&lt;/strong&gt; and &lt;strong&gt;send responses from the server&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;To put it simply, &lt;code&gt;HTTP&lt;/code&gt; is a text-based protocol that defines how communication between a &lt;code&gt;Client&lt;/code&gt; and a &lt;code&gt;Server&lt;/code&gt; occurs through the &lt;code&gt;WWW&lt;/code&gt; service. The &lt;strong&gt;goal is to transfer web resources&lt;/strong&gt; such as JSON, HTML, JavaScript, CSS, images, fonts, and more—between the client and the server.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;HTTP operates on a &lt;strong&gt;request-response model&lt;/strong&gt;. The client initiates the communication by making a request, and the server responds. Each request has only one corresponding response.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web Client and Web Server
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;Web Client&lt;/code&gt; is &lt;strong&gt;software&lt;/strong&gt; that interacts with a &lt;code&gt;Web Server&lt;/code&gt;. The most common web client is a Web Browser (e.g., Chrome, Firefox, Safari).&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Web Server&lt;/code&gt; is &lt;strong&gt;software&lt;/strong&gt; that listens for incoming requests on a specific port. It processes these requests and returns an appropriate response to the client. The server runs continuously awaiting requests.&lt;/p&gt;

&lt;p&gt;Together, the &lt;code&gt;Web Client&lt;/code&gt; and &lt;code&gt;Web Server&lt;/code&gt; communicate to deliver content and functionality on the web.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Request Methods
&lt;/h2&gt;

&lt;p&gt;With &lt;code&gt;HTTP methods&lt;/code&gt;, we specify the &lt;strong&gt;action&lt;/strong&gt; that should be performed on the server. &lt;em&gt;A method is a property of the request that tells the server the client's intention&lt;/em&gt;, such as creating, retrieving, updating, or deleting data.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;🔍 &lt;strong&gt;GET&lt;/strong&gt;: By sending a GET request, we ask the server to &lt;strong&gt;retrieve a resource&lt;/strong&gt;. An example of a GET request is opening a website’s home page. The server responds by sending all the necessary resources (HTML, CSS, JavaScript, fonts, etc.) so we can see the page content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✉️ &lt;strong&gt;POST&lt;/strong&gt;: By making a POST request, we tell the server that we intend to &lt;strong&gt;create new data&lt;/strong&gt;. For example, when a user registers on a website, a POST request is sent with their username, email, and password. The server processes this data and creates the account.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🔄 &lt;strong&gt;PUT&lt;/strong&gt;: By making a PUT request, we indicate that we want to &lt;strong&gt;modify existing data&lt;/strong&gt;. For example, if a user submits an incorrect first name during registration and needs to correct it, they can send a PUT request with the updated information.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;🛠️ &lt;strong&gt;PATCH&lt;/strong&gt;: A PATCH request is used for &lt;strong&gt;making partial updates&lt;/strong&gt; to a resource. The key difference between PATCH and PUT is that PUT requires sending the entire updated resource, whereas PATCH allows updating only specific fields. For example, if a blog post has a title, author, and description, and we want to update only the title:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;PUT&lt;/strong&gt;: We must send the entire updated resource, including the title, author, and description.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PATCH&lt;/strong&gt;: We can send only the new title, leaving the other fields unchanged.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;🗑️ &lt;strong&gt;DELETE&lt;/strong&gt;: By sending a DELETE request, we instruct the server to &lt;strong&gt;remove a resource&lt;/strong&gt;. For example, if a user decides to close their account, a DELETE request can be sent to remove their data.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;🔎 &lt;strong&gt;HEAD&lt;/strong&gt;: A HEAD request is used to retrieve &lt;strong&gt;only the headers of a response&lt;/strong&gt;, without the response body. This is useful for checking metadata (such as content length or last modified date) before making a full request.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;⚙️ &lt;strong&gt;OPTIONS&lt;/strong&gt;: The OPTIONS request may be used to &lt;strong&gt;check which HTTP methods&lt;/strong&gt; are supported by the server for a specific URL.&lt;/p&gt;&lt;/li&gt;

&lt;/ol&gt;

&lt;h2&gt;
  
  
  HTTP Request
&lt;/h2&gt;

&lt;p&gt;An HTTP request consists of two main parts:&lt;/p&gt;

&lt;h3&gt;
  
  
  Request Headers
&lt;/h3&gt;

&lt;p&gt;Contain metadata about the request, ensuring that it reaches the server with the necessary information.&lt;/p&gt;

&lt;h4&gt;
  
  
  Request Line
&lt;/h4&gt;

&lt;p&gt;The first line of the request, which includes the HTTP method, the path (URL), and the HTTP version being used:&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;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Username&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;RepoName&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;issues&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;HTTP&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Host Header
&lt;/h4&gt;

&lt;p&gt;Specifies the domain name of the server. The client first checks with a Name Server (DNS), which resolves the domain into an IP address before making the request:&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;Host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;github&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;com&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Other Request Headers
&lt;/h4&gt;

&lt;p&gt;Additional headers provide extra information about the request. In this example, we specify that the request body is formatted as JSON, allowing the server to correctly parse the received text:&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;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Request Body
&lt;/h3&gt;

&lt;p&gt;The actual content of the request, typically included in methods like POST and PUT, but not in GET requests. In the example below, the body contains the title and description of an issue, formatted as a JSON string:&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CRLF&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;indicates&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;line&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;title&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;Found a bug&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;description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Working on it&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CRLF&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  HTTP Response
&lt;/h2&gt;

&lt;p&gt;An HTTP response has a structure similar to a request.&lt;/p&gt;

&lt;h3&gt;
  
  
  Response Headers
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Status Line
&lt;/h4&gt;

&lt;p&gt;The status code indicates whether the request was successful or not. It consists of a numeric status code and a corresponding status message, informing the client about the outcome of the request.&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;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="nx"&gt;OK&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Other Response Headers
&lt;/h4&gt;

&lt;p&gt;In this example, the server specifies that the returned content is in HTML format so that the browser knows how to parse and render it 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="nx"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Response Body
&lt;/h3&gt;

&lt;p&gt;The response body contains the actual content sent by the server.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CRLF&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Some&lt;/span&gt; &lt;span class="nx"&gt;Title&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/title&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/head&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Test&lt;/span&gt; &lt;span class="nx"&gt;HTML&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/body&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/html&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CRLF&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Status code
&lt;/h2&gt;

&lt;p&gt;HTTP status codes &lt;strong&gt;indicate the outcome of a request&lt;/strong&gt;. They are grouped into different ranges based on their meaning:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;200&lt;/strong&gt; (and similar) – The request was successfully received, processed, and responded to by the server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;301/302&lt;/strong&gt; - The requested resource has been moved to a different location&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;400&lt;/strong&gt; (and similar) – The request was received and understood, but there was an &lt;strong&gt;issue on the client's side&lt;/strong&gt; (e.g., missing resource, bad request format). These are not server errors but rather issues caused by the request itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;500&lt;/strong&gt; (and similar) – The request was sent correctly, but an internal server error occurred while processing it. This indicates a &lt;strong&gt;problem on the server's side&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  REST Services
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;REST&lt;/code&gt; is an &lt;strong&gt;architectural&lt;/strong&gt; style for &lt;strong&gt;client-server&lt;/strong&gt; communication &lt;strong&gt;over HTTP&lt;/strong&gt;. It provides a set of best practices that, when followed, create a &lt;strong&gt;consistent and predictable&lt;/strong&gt; structure for web applications. By adhering to REST principles, backend developers build APIs that are easy to understand and work with, while frontend developers can construct requests with a standardized approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resource
&lt;/h3&gt;

&lt;p&gt;A resource is a &lt;strong&gt;logically distinct&lt;/strong&gt; part of an application. For example, in a social media platform, resources might include users, photos, posts, and comments.&lt;/p&gt;

&lt;p&gt;Resources are typically named as &lt;strong&gt;plural nouns&lt;/strong&gt;, and we can perform CRUD (Create, Read, Update, Delete) operations on them using HTTP methods:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create&lt;/strong&gt; -&amp;gt; POST&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read&lt;/strong&gt; -&amp;gt; GET&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update&lt;/strong&gt; -&amp;gt; PUT/PATCH&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delete&lt;/strong&gt; -&amp;gt; DELETE&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;REST allows us by only knowing the name of the resource to be able to perform all CRUD operations.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  RESTful API / RESTful Service
&lt;/h3&gt;

&lt;p&gt;A RESTful API or RESTful Service refers to a server that follows REST principles.&lt;/p&gt;

&lt;h4&gt;
  
  
  Examples
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Create a new article (POST request to create a resource)
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//some-service.org/articles&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Get all articles (Retrieve all resources)
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//some-service.org/articles&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Get a specific article (Use a unique identifier)
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//some-service.org/articles/73635&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Delete an article (Remove a resource)
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;DELETE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//some-service.org/articles/73635&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Replace an existing article (Update the entire resource)
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;PUT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//some-service.org/articles/73635&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Modify an existing article (Partial update)
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;PATCH&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//some-service.org/articles/73635&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As seen in the examples above, &lt;em&gt;REST allows us to perform all CRUD operations using a consistent URI structure&lt;/em&gt;. By simply knowing the resource name and following REST principles, we can easily interact with the API.&lt;/p&gt;

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

&lt;p&gt;In this article, we explored the fundamentals of &lt;code&gt;HTTP&lt;/code&gt; and &lt;code&gt;REST&lt;/code&gt; services, essential concepts for web communication. We started with an overview of &lt;code&gt;HTTP&lt;/code&gt;, understanding how clients and servers interact using requests and responses.&lt;/p&gt;

&lt;p&gt;We then examined different &lt;code&gt;HTTP request&lt;/code&gt; methods like &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;, and &lt;code&gt;PATCH&lt;/code&gt;, along with how headers and &lt;code&gt;status codes&lt;/code&gt; work.&lt;/p&gt;

&lt;p&gt;Next, we introduced &lt;code&gt;REST&lt;/code&gt;, a structured approach to designing APIs that promotes scalability and consistency.&lt;/p&gt;

&lt;p&gt;By understanding these concepts, we can confidently work with web services, interact with APIs, and build scalable applications that follow web standards. 🚀&lt;/p&gt;




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

&lt;p&gt;I would be grateful to understand your opinion.&lt;/p&gt;

</description>
      <category>http</category>
      <category>rest</category>
    </item>
  </channel>
</rss>
