<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Esther Christopher - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Esther Christopher - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 14 May 2026 17:33:13 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/EstherChristopher/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ CSS Units – When to Use rem, em, px, and More ]]>
                </title>
                <description>
                    <![CDATA[ CSS units allow you to measure and specify different property values. You use them to modify CSS properties such as margins, padding, height, and width to make them compatible with devices of all screen sizes. CSS units have two basic types: Absolut... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/css-units-when-to-use-each-one/</link>
                <guid isPermaLink="false">66bb9398d2bda3e4315491f7</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Esther Christopher ]]>
                </dc:creator>
                <pubDate>Thu, 25 Jan 2024 00:58:27 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/01/pexels-adonyi-ga-bor-1409216.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>CSS units allow you to measure and specify different property values. You use them to modify CSS properties such as margins, padding, height, and width to make them compatible with devices of all screen sizes.</p>
<p>CSS units have two basic types:</p>
<ul>
<li>Absolute units</li>
<li>Relative units</li>
</ul>
<p>Absolute units are fixed and do not depend on the size of the parent element or the viewport. Examples of absolute units are pixels (px), points (pt), and centimeters (cm). </p>
<p>Relative units, on the other hand, are flexible – and just as the name implies, they are relative to the parent element’s size, the viewport’s size, or the root element’s font size.</p>
<p>Since there are a number of CSS unit types, you might have trouble deciding which unit to use for a particular measurement. This article will demonstrate the best use cases for each of these units. We'll focus on the most important and frequently used CSS units here: <code>rem</code>, <code>em</code>, <code>vh</code>, <code>vw</code>, <code>%</code>, and the all too familiar absolute unit – <code>px</code>.</p>
<h2 id="heading-table-of-contents">Table of Contents:</h2>
<ol>
<li><a class="post-section-overview" href="#heading-rem-rem">Rem (<code>rem</code>)</a></li>
<li><a class="post-section-overview" href="#heading-em-em">Em (<code>em</code>)</a></li>
<li><a class="post-section-overview" href="#heading-percentages">Percentages (<code>%</code>)</a></li>
<li><a class="post-section-overview" href="#heading-viewport-height-vh">Viewport height (<code>vh</code>)</a></li>
<li><a class="post-section-overview" href="#heading-viewport-width-vw">Viewport width (<code>vw</code>)</a></li>
<li><code>[ch](#heading-ch)</code></li>
<li><a class="post-section-overview" href="#heading-pixels-px">Pixels (<code>px</code>)</a></li>
</ol>
<h2 id="heading-rem-rem">Rem (<code>rem</code>)</h2>
<p>The <code>rem</code> unit in CSS stands for "root <code>em</code>". It is a relative unit of measurement that is relative to the font size of the root element. One <code>rem</code> is equal to the font size of the root element. The root element defaults to <code>16px</code> in many browsers, so <code>1rem</code> is equal to <code>16px</code>.</p>
<p>Let's look at an example to see how you can use <code>rem</code>:</p>
<pre><code class="lang-html">    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Best Practices for CSS units<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is a paragraph with font size set to 2rem<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-tag">html</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
}
<span class="hljs-selector-class">.container</span> {
   <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span>;
   <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
   <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#ddd</span>;
}
<span class="hljs-selector-tag">h1</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#0077cc</span>;
}
<span class="hljs-selector-tag">p</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1rem</span>;
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#0077cc</span>;
}
</code></pre>
<p>The <code>h1</code> element is set to <code>2rem</code> which means that it's two times the root element, the html element. 2 x <code>16px</code> = 32, so the <code>h1</code> element is <code>32px</code>.</p>
<p>Here's the root element at <code>16px</code>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/4AF00C2B-DB5A-463D-836F-8EF72530EB04_4_5005_c-2.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>The font-size when the root element is at 16px</em></p>
<p>Then in the below image, the root element was set to <code>20px</code> so <code>2rem</code> of the <code>h1</code> element is 2 x <code>20px</code> making the <code>h1</code> text <code>40px</code>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/4A7E8EE7-683F-4F83-A5C0-1D2FF4872DBD_4_5005_c-1.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>The font-size when the root element is at 20px</em></p>
<p>In your code editor, as you change the base font size in the HTML selector of the CSS file, you can see how the font sizes defined in rem units also adjusts equally.</p>
<p>By using <code>rem</code> units, you can easily scale the font size of the body elements proportionally to the HTML font size.</p>
<p>It's a good idea to use <code>rem</code> to set font sizes because it is designed to adapt to the user's browser preferences. This helps with accessibility. It is also good for consistent scaling, as changing the HTML font size will affect the elements with rem units.</p>
<p>Using <code>rem</code> or <code>em</code> for padding or margin also offers advantages in terms of providing a scalable and maintainable design. When you change the font size at the top level, all <code>rem</code> values automatically gets updated and adjusts according to the base front size. <code>rem</code> or <code>em</code> should be used for margin or padding depending on if you want the element to be relative to the root element or the parent.</p>
<h2 id="heading-em-em">Em (<code>em</code>)</h2>
<p>Similar to <code>rem</code>, <code>em</code> is a relative unit of measurement. But unlike <code>rem</code>, <code>em</code> is relative to the font size of the parent element or the font-size of the nearest parent with a defined font size.</p>
<p>Let's look at an example:</p>
<pre><code class="lang-htmlembedded">    &lt;div class="container"&gt;
      &lt;p&gt;This is a paragraph with the font size set to 2em&lt;/p&gt;
    &lt;/div&gt;
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
     <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
}
<span class="hljs-selector-class">.container</span> {
   <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span>;
   <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
   <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#ddd</span>;
}
<span class="hljs-selector-tag">p</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2em</span>;
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#0077cc</span>;
}
</code></pre>
<p>The <code>p</code> element is set to <code>2em</code> which means it is 2 times the font size of the parent element. 2 x 20 is <code>40px</code> so the p element is <code>40px</code>. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/A8CF43A8-8685-476A-A566-2C9E60836CAC_4_5005_c.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>The font-size of the paragraph element set at 2em</em></p>
<p>Let's look at another example:</p>
<pre><code class="lang-htmlembedded">    &lt;div class="parent-element"&gt;
      This is the nearest parent
      &lt;div class="child-element"&gt;
        &lt;p&gt;This is the child&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.parent-element</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#ddd</span>;
}
<span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2em</span>;
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#0077cc</span>;
}
</code></pre>
<p>Here, the font size of the <code>child-element</code> is set to 2 times that of the <code>parent element</code>. That makes it <code>40px</code> (2 x 20).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/AFF1051E-8AFF-43DE-A90B-DD49CE020AA4_4_5005_c.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>The font-size of the child-element at 2em of the parent-element</em></p>
<p>If you need to scale an element to be consistent with the parent, then <code>em</code> is the right direction to go in. <code>em</code> is good for creating scalable and responsive designs.</p>
<p>It is recommended to use <code>em</code> for setting margin and padding. When you use em for margin and padding, they adjust according to the font size of the parent element. This creates a consistent design, especially when users adjust their browser's default font size. When you set margin or padding with em, the layouts also become more flexible and adaptable, and elements can scale in proportion.</p>
<p><code>em</code> is also important for media queries to enhance responsiveness and adaptability.</p>
<h2 id="heading-percentages">Percentages (<code>%</code>)</h2>
<p>Percentages are relative units that render an element relative to the size of the element's parent. They serve as a percentage of their containing block and are always relative to their nearest parent.</p>
<p>Here's an example:</p>
<pre><code class="lang-htmlembedded">&lt;div class="container"&gt;
    &lt;div class="box"&gt;&lt;/div&gt;
&lt;/div&gt;
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">80%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">80%</span>;
  <span class="hljs-attribute">margin</span>: auto;
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">bottom</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">right</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#000000</span>;
}
<span class="hljs-selector-class">.box</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">50%</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0077cc</span>;
}
</code></pre>
<p>Here, the box element is <code>50%</code> the width and height of the parent container, so it takes up half of the container.</p>
<p>The container is <code>80%</code> of the viewport by width and height. If you try to resize the browser, you will notice that the percentage is maintained. Similarly, the box within the container is <code>50%</code> of the container by width and height.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/B73FE4D6-BFF6-410D-8713-848BD92BFD3F_1_201_a.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>The box element at 50% of its containing element</em></p>
<p>When you want an element to take up a certain amount of the containing block, then you should go with percentages. </p>
<p>Percentages can be used for positioning elements relative to their containing element. This comes in handy when creating layouts where elements need to be positioned based on a percentage of the parent.</p>
<p>Setting widths and heights in percentages also allows elements to scale relative to their containing element.</p>
<h2 id="heading-viewport-height-vh">Viewport height (<code>vh</code>)</h2>
<p><code>vh</code> is a relative unit of measurement that represents the visible height area of the browser window.</p>
<p>Here's an example of how it works:</p>
<pre><code class="lang-htmlembedded"> &lt;div class="viewport-height"&gt;
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.viewport-height</span> {
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
    <span class="hljs-attribute">background-color</span>: bisque;
}
</code></pre>
<p>This code snippet shows the <code>viewport-height</code> element set t0 <code>100vh</code> so it covers all of the viewport height.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/191234F1-D770-46AB-837B-3C0FFCD338C3_1_201_a.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>The viewport area covered by 100vh</em></p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">50vw</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">30vh</span>;
  <span class="hljs-attribute">background-color</span>: antiquewhite;
}
</code></pre>
<p>Let's change it slightly:</p>
<pre><code class="lang-htmlembedded">&lt;div class="relative-height"&gt;&lt;/div&gt;
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.relative-height</span> {
    <span class="hljs-attribute">height</span>: <span class="hljs-number">40vh</span>;
    <span class="hljs-attribute">background-color</span>: bisque;
}
</code></pre>
<p>In the above example, the <code>relative-height</code> element is set to <code>40vh</code>, so it covers that fraction of the viewport height. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/F45DD133-6DC6-4869-A3A7-844737B7F18F_1_201_a.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>The viewport area covered by 40vh</em></p>
<p>Using <code>vh</code> for height allows elements to maintain the set height of the viewport. This is particularly useful when you want an element to take up a certain percentage of the screen's height.</p>
<p><code>vh</code> can also be used to set font sizes that are responsive to the viewport height. This is useful for responsive typography. When you don't want an element to be relative to the parent, you can use <code>vh</code>. </p>
<p>There can be inconsistencies in the way devices interpret the <code>vh</code> unit due to variation in browsers so make sure you use it with caution to prevent problems.</p>
<h2 id="heading-viewport-width-vw">Viewport width (<code>vw</code>)</h2>
<p><code>vw</code> is similar to <code>vh</code> but <code>vw</code> refers to the width of the browser window. It is used to set elements based on the horizontal axis of the browser window.</p>
<p>Here's an example:</p>
<pre><code class="lang-htmlembedded">  &lt;body&gt;
    &lt;div class="box"&gt;&lt;/div&gt;
  &lt;/body&gt;
</code></pre>
<p>Here, the <code>box</code> is specified as <code>50vw</code> of the overall viewport width. As the browser shrinks and increases, watch how the box adjusts to maintain the specified width.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/EE2681DC-B4DB-4D9F-84E7-B7CBC435684B_1_201_a.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>The box element occupying 50% of the viewport width</em></p>
<p><code>vw</code> is particularly useful and encouraged when you want an element to take a certain width of the viewport. <code>vw</code> is also used for font sizes in media query for responsive typography. </p>
<h2 id="heading-ch"><code>ch</code></h2>
<p><code>ch</code> is a relative unit that scales based on the width of characters. <code>1ch</code> is equal to the width of the "0" character in the current font.</p>
<pre><code class="lang-htmlembedded">    &lt;div&gt;
      &lt;p&gt;Some text specified with ch unit&lt;/p&gt;
    &lt;/div&gt;
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">'Courier New'</span>, monospace; 
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
}
<span class="hljs-selector-tag">p</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2ch</span>; 
    <span class="hljs-attribute">line-height</span>: <span class="hljs-number">1.5</span>; 
  }
</code></pre>
<p>The font size of the text inside the <code>&lt;p&gt;</code> element is set to be twice the width of the "0" character in the current font. </p>
<p><code>ch</code> is particularly useful for defining the max character length on a single line. It provides a size relative to the width of characters, making it flexible, adaptable, and enhances readability. It allows you to set a max-width for text containers based on the width of characters.</p>
<h2 id="heading-pixels-px">Pixels (<code>px</code>)</h2>
<p><code>px</code> is an absolute unit of measurement used to specify length and sizes. <code>px</code> is a fixed unit and should be used sparingly and with caution for responsive design.</p>
<pre><code class="lang-htmlembedded">    &lt;div class="container"&gt;
      &lt;p&gt;This element has a fixed size using px units.&lt;/p&gt;
    &lt;/div&gt;
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">300px</span>; 
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#f0f0f0</span>;
  }

  <span class="hljs-selector-tag">p</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#333</span>;
  }
</code></pre>
<p>In this snippet, the size of the <code>&lt;p&gt;</code> text remains a certain size. It remains fixed at <code>16px</code> and doesn't change regardless of whether the size of the browser changes. </p>
<p>When you resize the browser, you will observe that the container doesn't scale according to the viewport, and neither does the text scale according to the container.</p>
<p>The main reason why using <code>px</code> is not always recommended for responsive design lies in its fixed nature. Unlike relative units, such as percentages, <code>em</code>, <code>rem</code>, and viewport units (<code>vw</code>, <code>vh</code>), <code>px</code> does not adjust based on the user's preferences or the size of the viewport.</p>
<p><code>px</code> is useful when you want to specify a fixed size of an element, such as a border size or an image size.</p>
<p>An example:</p>
<pre><code class="lang-htmlembedded">img {
    width: 200px;
    height: 150px;
}
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>These are the most frequently used units in CSS, so it's important to be familiar with when to use them.</p>
<p>To set font sizes, you'll commonly use <code>rem</code> or <code>ch</code>. For width, percentages are often recommended. You can also consider using <code>vw</code> and <code>ch</code>. For height, consider using <code>%</code>, <code>rem</code>, or <code>vh</code>.</p>
<p>For padding or margin, you'll typically use <code>rem</code> or <code>em</code> depending on your specific requirements.</p>
<p>Feel free to refer back to this article whenever you need guidance on the best units for setting measurements or lengths.</p>
<p>Thanks for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How Does JavaScript Work Behind the Scenes? JS Engine and Runtime Explained ]]>
                </title>
                <description>
                    <![CDATA[ So you may know that your code somehow compiles and executes in your browser to display the beautiful web application you’ve built. But are you aware of all the components that come into play to enable the output?  Let’s dive a little into JavaScript... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-javascript-works-behind-the-scenes/</link>
                <guid isPermaLink="false">66bb939cadd24ba427325115</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Esther Christopher ]]>
                </dc:creator>
                <pubDate>Tue, 30 May 2023 18:41:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/05/CFF8A164-D6C4-4A88-85F2-388E83A2E3E7.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>So you may know that your code somehow compiles and executes in your browser to display the beautiful web application you’ve built. But are you aware of all the components that come into play to enable the output? </p>
<p>Let’s dive a little into JavaScript behind the scenes. The abstract part that you can’t exactly see. </p>
<p>Why should a seemingly abstract subject matter to you? An understanding of the inner workings of JavaScript allows you to explore the language beyond the surface level and from a deeper perspective. </p>
<p>It provides contextual information on the language and how the JavaScript engine optimizes code. This will give you some important foundational knowledge which shapes the way you write code. It also helps you write more efficient, scalable, and maintainable code. </p>
<h2 id="heading-the-javascript-engine">The JavaScript Engine</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/09BA18A6-3F7A-4DBE-AA43-C482725CA5E4.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>JavaScript Engine showing the Call stack and the Heap</em></p>
<p>The JavaScript engine is simply a computer program that interprets JavaScript code. The engine is responsible for executing the code. </p>
<p>Every major browser has a JavaScript engine that executes JavaScript code. The most popular one is the Google Chrome <a target="_blank" href="https://en.wikipedia.org/wiki/JavaScript_engine">V8</a> engine. Google’s V8 powers Google Chrome and <a target="_blank" href="https://nodejs.org/en/docs">Node.js</a>, a back-end JavaScript runtime environment used to build server-side applications.</p>
<p>Other major browser engines include:</p>
<ul>
<li>SpiderMonkey developed by Mozilla for Firefox</li>
<li>JavaScriptCore which powers the Safari browser</li>
<li>Chakra which powers Internet Explorer</li>
</ul>
<p>Any JavaScript engine typically contains a call stack and a heap. The call stack is where the code is executed. The heap is an unstructured memory pool that stores all the objects needed for the application.</p>
<p>Since the computer’s processor only understands binary, 0’s and 1’s, the code has to be translated to 0’s and 1’s. </p>
<p>When a code snippet passes into the engine, the code is initially parsed, that is read. The code is subsequently parsed to a data structure called the abstract syntax tree (AST). The resulting tree is then used to create machine codes. </p>
<p>Execution happens in the JavaScript engine call stack using the execution context. This is the environment where JavaScript code is executed.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/FA4EDBD9-0348-4445-B795-8D1FEF904CBE.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>A diagram illustration showing the JavaScript execution process</em></p>
<h2 id="heading-the-javascript-runtime">The JavaScript Runtime</h2>
<p>Think of the JavaScript runtime as the house that encompasses all the components needed to run JavaScript. This house comprises the JavaScript engine, Web APIs, and the callback queue. </p>
<p>Web APIs are functionalities that are provided to the engine but are not part of the JavaScript language. They are accessible to the engine through the browser and help access data or enhance browser functionality. Examples are the Document Object Model (DOM) and Fetch APIs. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/CDFBBA53-5533-478E-91CE-5904714E1043.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>A diagram of JavaScript Runtime in the Browser containing the JavaScript Engine, WEB APIs, and the Callback Queue</em></p>
<p>The callback queue includes callback functions that are ready to be executed. The callback queue ensures that callbacks are executed in the First-In-First-Out (FIFO) method and they get passed into the stack when it’s empty. </p>
<p>The browser runtime and Node.js are examples of runtime environments.</p>
<p>When JavaScript executes within a web browser it is operating within the browser’s runtime environment. The browser runtime environment provides access to the DOM which enables interaction with web page elements, handling events, and manipulating the page structure.  </p>
<p>Node.js provides a server-side runtime environment for executing JavaScript outside the browser. Because it executes JavaScript outside the browser, it does not have access to the web APIs. Instead, the Node.js runtime environment replaces it with something called C++ bindings and the thread pool. </p>
<h2 id="heading-javascript-optimization-strategies">JavaScript Optimization Strategies</h2>
<p>Modern JavaScript engines have some optimization strategies put in place to enhance the performance of code execution. These optimizations occur dynamically during the execution process. Let's look at some of these strategies. </p>
<h3 id="heading-just-in-time-compilation">Just-in-Time compilation</h3>
<p>The process that involves the translation of JavaScript code into machine code occurs using compilation and interpretation. </p>
<p>In compilation, the entire source code is converted into machine code at once and written into a binary file to be executed by the computer. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/EB039874-52DC-4CD8-B95C-F9E75F2D2283_4_5005_c.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>A diagram showing the code compilation process</em></p>
<p>In contrast, during interpretation, the interpreter goes through the source code and interprets it line by line, executing each line as it encounters it.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/D3DC97A6-3D79-46E0-A2F2-BB3FA694F0EF_4_5005_c.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>A diagram showing the code interpretation process</em></p>
<p>JavaScript used to be an interpreted language, but interpreted languages are slower compared to compiled languages.</p>
<p>In order to optimize the performance of web applications, JavaScript combines both compilation and interpretation. This is called Just-in-Time compilation. This method compiles the entire code into machine code all at once and executes it. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/E2BA4399-5F52-408C-B2AB-A9E6F74B3238_4_5005_c.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>A diagram showing Just-in-Time compilation of code</em></p>
<p>Just-in-Time compilation involves the same two processes as regular compilation, but here the machine code isn’t written into a binary file. The code is also executed right away after compilation.</p>
<p>This has had a significant impact on the speed of code execution in JavaScript. So hopefully this helps dispel the notion that JavaScript is a purely interpreted language.</p>
<p>To fully optimize JavaScript code, the engine first creates an unoptimized version of the machine code so it can start executing immediately. While that is ongoing, the code is being re-optimized and recompiled in the background of the currently running program execution. This is done multiple times to produce the final, most optimized version. </p>
<p>The process of parsing, compilation, and execution happens in some special thread in the engine that can’t be accessed from the code.</p>
<h3 id="heading-what-is-inlining">What is Inlining?</h3>
<p>Inlining is another optimization technique JavaScript employs to improve performance and speed.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-keyword">return</span> a + b;
}

<span class="hljs-keyword">let</span> result = <span class="hljs-number">0</span>;
result = result + <span class="hljs-number">5</span>;
result = result + <span class="hljs-number">3</span>;

<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">//</span>
</code></pre>
<p>In this snippet, the original <code>add()</code> function is not directly called. Instead, the code inside the function <code>return a + b;</code> is inserted at the call site.</p>
<p>This optimization is done especially for functions that are called repeatedly.  The JavaScript engine will run the function as it normally would. But as the function gets called often, the engine replaces the function call with the actual code of the function at the call site. This helps to prevent several function calls and improve performance.</p>
<h3 id="heading-performance-considerations">Performance Considerations</h3>
<p>Several factors affect the performance of your web application. As the JavaScript engine employs some strategies to ensure optimization, there are also some best practices to be taken into consideration by developers for efficient execution. </p>
<p>Techniques such as minimizing DOM manipulation and reducing function calls enhance code performance. </p>
<p>Frequent access and interaction to the DOM slows down the rendering of web pages and contributes to performance lag. Since you can’t altogether avoid interacting with the DOM, you can minimize interaction by batching DOM updates to reduce overhead.</p>
<p>Additionally, reducing function calls takes up the performance a notch. By reducing function calls, you streamline your code and make it more efficient making your JavaScript applications faster and more responsive.</p>
<pre><code class="lang-js"><span class="hljs-comment">// Inefficient code with unnecessary function calls</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">calculateTotal</span>(<span class="hljs-params">a, b, c</span>) </span>{
  <span class="hljs-keyword">return</span> addNumbers(a, b) + multiplyNumbers(c, b);
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addNumbers</span>(<span class="hljs-params">x, y</span>) </span>{
  <span class="hljs-keyword">return</span> x + y;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyNumbers</span>(<span class="hljs-params">x, y</span>) </span>{
  <span class="hljs-keyword">return</span> x * y;
}
<span class="hljs-comment">// Improved code with reduced function calls</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">calculateTotal</span>(<span class="hljs-params">a, b, c</span>) </span>{
  <span class="hljs-keyword">const</span> sum = a + b;
  <span class="hljs-keyword">return</span> sum + c * b;
}
<span class="hljs-built_in">console</span>.log(calculateTotal(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>)); <span class="hljs-comment">// Output: 23</span>
</code></pre>
<p>In the inefficient code, the <code>calculateTotal()</code> function makes separate function calls to <code>addNumbers()</code> and <code>multiplyNumbers()</code>. This causes function call overhead.</p>
<p>In the improved code, the function calls are reduced by directly performing the addition and multiplication operations within the <code>calculateTotal()</code> function. By reducing function calls, the code becomes more efficient and improves execution speed.</p>
<h2 id="heading-future-javascript-developments-and-trends">Future JavaScript Developments and Trends</h2>
<p>There will continue to be improvements and advancements in JavaScript engines and runtime environments. These changes are geared towards improving the performance of web applications. </p>
<p>One such advancement is the rise of <a target="_blank" href="https://webassembly.org/">WebAssembly</a>. WebAssembly brings near-native performance to web applications and supports multiple languages. It opens up new possibilities for performance optimization and execution speed. </p>
<p>It is important for JavaScript developers to stay updated with these trends and adapt new coding best practices accordingly. </p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>So many processes are involved in how your JavaScript code is being parsed until it renders a functional web application. </p>
<p>This article provides a high-level overview of the main concepts. It explains how the JavaScript engine executes code, the runtime, and its components. It also goes on to explain optimization strategies and highlight performance considerations. </p>
<p>Understanding how JavaScript operates behind the scenes shapes the way developers approach problems and write more efficient codes. It also helps them stay ahead of the learning curve and adapt easily to future changes in JavaScript features.</p>
<p>For more in-depth learning, you can visit these resources: </p>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/execution-context-how-javascript-works-behind-the-scenes/">Execution context</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/javascript-concurrency-model-and-event-loop/">Event loop</a></li>
<li><a target="_blank" href="https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/">Microtask queueing</a></li>
</ul>
<p>If you enjoyed reading this article, then connect with me on <a target="_blank" href="https://twitter.com/thedocsgirl">Twitter</a> and <a target="_blank" href="https://www.linkedin.com/in/esther-christopher-a0909419b">LinkedIn</a> where I share my knowledge.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
