<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jaya Sudha</title>
    <description>The latest articles on Forem by Jaya Sudha (@jaya_sudha_96fce1e511efee).</description>
    <link>https://forem.com/jaya_sudha_96fce1e511efee</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%2F3689412%2F357fd5ec-b174-4454-9fbb-cb8a4bcf414b.png</url>
      <title>Forem: Jaya Sudha</title>
      <link>https://forem.com/jaya_sudha_96fce1e511efee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jaya_sudha_96fce1e511efee"/>
    <language>en</language>
    <item>
      <title>The Simplest Way to Understand Asynchronous JavaScript</title>
      <dc:creator>Jaya Sudha</dc:creator>
      <pubDate>Sat, 28 Mar 2026 12:28:46 +0000</pubDate>
      <link>https://forem.com/jaya_sudha_96fce1e511efee/understanding-asynchronous-javascript-1745</link>
      <guid>https://forem.com/jaya_sudha_96fce1e511efee/understanding-asynchronous-javascript-1745</guid>
      <description>&lt;p&gt;JavaScript is one of the most popular programming languages used to build websites and web applications. But one thing that often confuses beginners is &lt;strong&gt;asynchronous programming&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Don't worry — in this article we’ll understand it in the &lt;strong&gt;simplest way possible with real-life examples and practical scenarios&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;First, Let’s Understand a Simple Problem&lt;/p&gt;

&lt;p&gt;Imagine you are at a &lt;strong&gt;restaurant&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You place an order for food. The food will take &lt;strong&gt;15 minutes&lt;/strong&gt; to prepare.&lt;/p&gt;

&lt;p&gt;What would you do?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stand near the kitchen for 15 minutes doing nothing&lt;/li&gt;
&lt;li&gt;Sit at your table, talk with friends, and wait until the food is ready&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Of course, you will choose &lt;strong&gt;option 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You continue doing other things &lt;strong&gt;while the food is being prepared&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is exactly how &lt;strong&gt;asynchronous JavaScript works&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What is Asynchronous JavaScript?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Asynchronous programming allows JavaScript to &lt;strong&gt;do other tasks while waiting for a long operation to finish&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Some tasks take time, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetching data from an API&lt;/li&gt;
&lt;li&gt;Loading images&lt;/li&gt;
&lt;li&gt;Waiting for user clicks&lt;/li&gt;
&lt;li&gt;Uploading files&lt;/li&gt;
&lt;li&gt;Timers and delays&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of &lt;strong&gt;blocking the program&lt;/strong&gt;, JavaScript continues executing other code.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Synchronous vs Asynchronous&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Synchronous Code (Blocking)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In synchronous code, each line waits for the previous line to finish.&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="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;slowTask&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
(wait...)
End
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program &lt;strong&gt;stops and waits&lt;/strong&gt; until &lt;code&gt;slowTask&lt;/code&gt; finishes.&lt;/p&gt;

&lt;p&gt;This can make the website &lt;strong&gt;freeze&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Asynchronous Code (Non-Blocking)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now let’s see asynchronous 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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task completed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
Task completed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript does &lt;strong&gt;not wait&lt;/strong&gt; for the timeout to finish.&lt;/p&gt;

&lt;p&gt;It continues running the rest of the code.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Example 1: Ordering Food Online&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you order food on an app like Swiggy or Zomato:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You place the order&lt;/li&gt;
&lt;li&gt;The restaurant prepares the food&lt;/li&gt;
&lt;li&gt;The delivery person picks it up&lt;/li&gt;
&lt;li&gt;You track the delivery&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;During this time, &lt;strong&gt;you continue using your phone&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Your phone doesn’t freeze until the food arrives.&lt;/p&gt;

&lt;p&gt;That is &lt;strong&gt;asynchronous behavior&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Example 2: Loading Data From an API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most websites load data from a server.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Weather apps&lt;/li&gt;
&lt;li&gt;News websites&lt;/li&gt;
&lt;li&gt;E-commerce sites&lt;/li&gt;
&lt;li&gt;Social media feeds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript fetches data asynchronously.&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="s2"&gt;https://api.example.com/users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;data&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;data&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here JavaScript:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sends a request to the server&lt;/li&gt;
&lt;li&gt;Continues running other code&lt;/li&gt;
&lt;li&gt;Displays the data when it arrives&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;Example 3: User Click Events&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a user clicks a button, JavaScript waits for that event.&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;button&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="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Button clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function runs &lt;strong&gt;only when the user clicks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Until then, JavaScript simply &lt;strong&gt;waits in the background&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Example 4: Timers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes we want something to happen &lt;strong&gt;after a delay&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example: Showing a notification after a few 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="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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Reminder!&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;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message appears &lt;strong&gt;after 3 seconds&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;The Problem With Callbacks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before modern JavaScript, asynchronous tasks were handled using &lt;strong&gt;callbacks&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="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nf"&gt;getOrders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nf"&gt;getPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payment&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;payment&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This becomes deeply nested and hard to read.&lt;/p&gt;

&lt;p&gt;This problem is known as &lt;strong&gt;Callback Hell&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;The Modern Solution: Promises&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Promises make asynchronous code easier to manage.&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="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="s2"&gt;/api/users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;data&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;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Promises represent a &lt;strong&gt;value that will be available in the future&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;They can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pending&lt;/li&gt;
&lt;li&gt;Fulfilled&lt;/li&gt;
&lt;li&gt;Rejected&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Even Better: Async / Await&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Modern JavaScript introduced &lt;strong&gt;async/await&lt;/strong&gt;, which makes asynchronous code look cleaner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUsers&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;response&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/api/users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks almost like &lt;strong&gt;normal synchronous code&lt;/strong&gt;, but it still runs asynchronously.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Where Asynchronous JavaScript is Used&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Almost every modern web application uses asynchronous programming.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Loading API data&lt;/li&gt;
&lt;li&gt;Chat applications&lt;/li&gt;
&lt;li&gt;File uploads&lt;/li&gt;
&lt;li&gt;Payment processing&lt;/li&gt;
&lt;li&gt;Notifications&lt;/li&gt;
&lt;li&gt;Real-time updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without asynchronous programming, websites would feel &lt;strong&gt;slow and unresponsive&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Asynchronous programming is a technique in JavaScript that allows the program to perform other tasks while waiting for a long operation to complete. It is commonly implemented using concepts like &lt;strong&gt;callbacks, event listeners, timers, promises, and async/await&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is what makes modern web applications &lt;strong&gt;fast, interactive, and user-friendly&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you are learning JavaScript, understanding asynchronous concepts like &lt;strong&gt;callbacks, promises, and async/await&lt;/strong&gt; will significantly improve your ability to build real-world applications.&lt;/p&gt;




&lt;p&gt;Thanks for reading! If this article helped you understand asynchronous JavaScript better, feel free to share your thoughts.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Closures in JavaScript: A Beginner-Friendly Guide</title>
      <dc:creator>Jaya Sudha</dc:creator>
      <pubDate>Fri, 27 Mar 2026 06:18:14 +0000</pubDate>
      <link>https://forem.com/jaya_sudha_96fce1e511efee/understanding-closures-in-javascript-explained-for-beginners-1b8c</link>
      <guid>https://forem.com/jaya_sudha_96fce1e511efee/understanding-closures-in-javascript-explained-for-beginners-1b8c</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. What is a Closure?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In simple words:&lt;/p&gt;

&lt;p&gt;A Closure is when a &lt;strong&gt;function remembers the variables from its outer function&lt;/strong&gt; even after the outer function has finished executing.&lt;/p&gt;

&lt;p&gt;Think of it like a &lt;strong&gt;school bag&lt;/strong&gt; 🎒.&lt;/p&gt;

&lt;p&gt;A student goes home.&lt;br&gt;
But the &lt;strong&gt;bag still contains books from school&lt;/strong&gt;.&lt;br&gt;
Even outside school, the books are still accessible.&lt;/p&gt;

&lt;p&gt;Similarly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A function can &lt;strong&gt;remember variables from where it was created&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Simple Example&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;outerFunction&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello Students&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;innerFunction&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;message&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;innerFunction&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;myFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;myFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Students
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. How This Works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;outerFunction()&lt;/code&gt; runs.&lt;/li&gt;
&lt;li&gt;It creates a variable &lt;code&gt;message&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;innerFunction()&lt;/code&gt; is created inside it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;outerFunction()&lt;/code&gt; returns innerFunction.&lt;/li&gt;
&lt;li&gt;Even after &lt;code&gt;outerFunction()&lt;/code&gt; finishes, &lt;code&gt;innerFunction()&lt;/code&gt; &lt;strong&gt;still remembers message&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This memory is called a &lt;strong&gt;Closure&lt;/strong&gt;.&lt;br&gt;
&lt;strong&gt;"That ability of remembering the outer variables is called a Closure."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Real-Life Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine a &lt;strong&gt;cookie jar&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;cookieJar&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;cookies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;takeCookie&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;cookies&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cookies left:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cookies&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;takeCookie&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;eatCookie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;cookieJar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nf"&gt;eatCookie&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;eatCookie&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;eatCookie&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cookies left: 4
Cookies left: 3
Cookies left: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;cookieJar()&lt;/code&gt; finished running, &lt;strong&gt;the function still remembers the &lt;code&gt;cookies&lt;/code&gt; variable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Why Closures Are Useful&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Closures help us to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remember data&lt;/li&gt;
&lt;li&gt;Protect variables&lt;/li&gt;
&lt;li&gt;Create private variables&lt;/li&gt;
&lt;li&gt;Build powerful JavaScript features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many libraries like &lt;strong&gt;React&lt;/strong&gt; and &lt;strong&gt;Node.js&lt;/strong&gt; use closures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Key Points to Remember&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;✔ A closure is created when&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A function is inside another function&lt;/li&gt;
&lt;li&gt;The inner function uses variables from the outer function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✔ The inner function remembers those variables even after the outer function ends.&lt;/p&gt;

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

&lt;p&gt;Closures may look confusing at first, but they are just about &lt;strong&gt;functions remembering their environment&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Whenever an &lt;strong&gt;inner function accesses variables from an outer function, a closure is created&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Understanding closures will make you &lt;strong&gt;much stronger in JavaScript&lt;/strong&gt;, because many advanced concepts depend on them.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript Events Made Simple: A Beginner-Friendly Guide</title>
      <dc:creator>Jaya Sudha</dc:creator>
      <pubDate>Thu, 26 Mar 2026 17:17:45 +0000</pubDate>
      <link>https://forem.com/jaya_sudha_96fce1e511efee/understanding-events-in-javascript-beginner-friendly-guide-49bk</link>
      <guid>https://forem.com/jaya_sudha_96fce1e511efee/understanding-events-in-javascript-beginner-friendly-guide-49bk</guid>
      <description>&lt;p&gt;Modern websites are &lt;strong&gt;interactive&lt;/strong&gt;. When you click a button, type in a search box, or submit a form, something happens.&lt;/p&gt;

&lt;p&gt;This behavior is possible because of &lt;strong&gt;JavaScript Events&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this blog, we will learn:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Introduction to Events&lt;/li&gt;
&lt;li&gt;How Events Work&lt;/li&gt;
&lt;li&gt;Common Event Types&lt;/li&gt;
&lt;li&gt;The Event Object&lt;/li&gt;
&lt;li&gt;Event Propagation&lt;/li&gt;
&lt;li&gt;Event Delegation&lt;/li&gt;
&lt;li&gt;Removing Event Listeners&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s start.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Introduction to Events&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;event&lt;/strong&gt; is an action that happens in the browser.&lt;/p&gt;

&lt;p&gt;Events can be triggered by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User actions&lt;/li&gt;
&lt;li&gt;Browser actions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Examples of events&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Event&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Clicking a button&lt;/td&gt;
&lt;td&gt;click&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typing in keyboard&lt;/td&gt;
&lt;td&gt;keydown&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Submitting a form&lt;/td&gt;
&lt;td&gt;submit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Moving mouse over element&lt;/td&gt;
&lt;td&gt;mouseover&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Page loaded&lt;/td&gt;
&lt;td&gt;load&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&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;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Click Me&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Button clicked!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When the user &lt;strong&gt;clicks the button&lt;/strong&gt;, an alert message appears.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;interactivity&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Without events, websites would be &lt;strong&gt;static&lt;/strong&gt; like a newspaper page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. How Events Work&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript uses an &lt;strong&gt;event-driven model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This means the &lt;strong&gt;browser waits for events&lt;/strong&gt; and then runs code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The process&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;User performs an action&lt;/li&gt;
&lt;li&gt;Browser detects the event&lt;/li&gt;
&lt;li&gt;JavaScript runs the event handler&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Syntax of addEventListener&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="nx"&gt;element&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="s2"&gt;event&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&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;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"btn"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Click&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;btn&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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;btn&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="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Button was clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Button was clicked
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This message appears in the &lt;strong&gt;console&lt;/strong&gt; when the button is clicked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Common Event Types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript has many event types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Mouse Events&lt;/strong&gt;&lt;br&gt;
Triggered by mouse actions.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Event&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;click&lt;/td&gt;
&lt;td&gt;user clicks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;dblclick&lt;/td&gt;
&lt;td&gt;double click&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;mouseover&lt;/td&gt;
&lt;td&gt;mouse enters element&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;mouseout&lt;/td&gt;
&lt;td&gt;mouse leaves element&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&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;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hover over me&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;box&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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;box&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;box&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="s2"&gt;mouseover&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;box&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="s2"&gt;mouseout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;white&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
When the mouse &lt;strong&gt;enters the box → yellow background&lt;/strong&gt;&lt;br&gt;
When the mouse &lt;strong&gt;leaves → white background&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Keyboard Events&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Triggered when keys are pressed.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Event&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;keydown&lt;/td&gt;
&lt;td&gt;key pressed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;keyup&lt;/td&gt;
&lt;td&gt;key released&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;keypress&lt;/td&gt;
&lt;td&gt;key pressed (older event)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&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;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"inputBox"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;input&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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;inputBox&lt;/span&gt;&lt;span class="dl"&gt;"&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;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;keydown&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Key pressed:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Typing &lt;strong&gt;A B C&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Console shows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Key pressed: A
Key pressed: B
Key pressed: C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Form Events&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Used when working with forms.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Event&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;submit&lt;/td&gt;
&lt;td&gt;form submitted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;change&lt;/td&gt;
&lt;td&gt;value changed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;input&lt;/td&gt;
&lt;td&gt;typing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;focus&lt;/td&gt;
&lt;td&gt;field selected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;blur&lt;/td&gt;
&lt;td&gt;field left&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&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;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"form"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;"text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;form&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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;form&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;form&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="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Form submitted!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
The alert appears &lt;strong&gt;without refreshing the page.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Window / Document Events&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These events happen on the browser window.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Event&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;load&lt;/td&gt;
&lt;td&gt;page fully loaded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DOMContentLoaded&lt;/td&gt;
&lt;td&gt;HTML loaded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;resize&lt;/td&gt;
&lt;td&gt;window resized&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;scroll&lt;/td&gt;
&lt;td&gt;page scrolled&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example&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="nb"&gt;window&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="s2"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User is scrolling&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. The Event Object&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Whenever an event occurs, JavaScript creates an &lt;strong&gt;event object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This object contains information about the event.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&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="nb"&gt;document&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="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important Properties&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Property&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;event.target&lt;/td&gt;
&lt;td&gt;element clicked&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;event.type&lt;/td&gt;
&lt;td&gt;type of event&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;event.key&lt;/td&gt;
&lt;td&gt;key pressed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;event.clientX&lt;/td&gt;
&lt;td&gt;mouse X position&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;event.clientY&lt;/td&gt;
&lt;td&gt;mouse Y position&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&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="nb"&gt;document&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="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Clicked element:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Event type:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Prevent Default Behaviour&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some elements have default actions.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Form reload&lt;/li&gt;
&lt;li&gt;Link navigation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can stop it using:&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;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://google.com"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"link"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Go to Google&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;link&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Link prevented!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Event Propagation&lt;/strong&gt;                        &lt;/p&gt;

&lt;p&gt;When an event happens inside nested elements, it travels through the DOM.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;event propagation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are two phases:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Event Bubbling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Event goes from &lt;strong&gt;child → parent → document&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 html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"parent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Click&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Child clicked&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;parent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Parent clicked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Child clicked
Parent clicked

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

&lt;/div&gt;



&lt;p&gt;Because &lt;strong&gt;event bubbles&lt;/strong&gt; up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Event Capturing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Event goes from &lt;strong&gt;parent → child&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="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="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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;&lt;strong&gt;Example&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;parent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Parent capturing&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="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;&lt;strong&gt;stopPropagation()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stops the event from moving further.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stopPropagation&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Child only&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Child only
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Parent event will not run.&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 plaintext"&gt;&lt;code&gt;document
   ↓
body
   ↓
parent
   ↓
child (clicked)
   ↑
parent
   ↑
body
   ↑
document
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Capturing → top to bottom&lt;/li&gt;
&lt;li&gt;Bubbling → bottom to top&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6. Event Delegation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of adding many event listeners, we can add &lt;strong&gt;one listener&lt;/strong&gt; to the parent.&lt;/p&gt;

&lt;p&gt;This technique is called &lt;strong&gt;event delegation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It improves performance.&lt;/p&gt;

&lt;p&gt;Example 1&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;ul&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"list"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Item 1&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Item 2&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Item 3&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;list&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tagName&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;LI&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You clicked:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Clicking any item shows:(The exact element that was clicked by the user)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You clicked: Item 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if new &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; is added later, it still works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Removing Event Listeners&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can remove an event listener using:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;removeEventListener()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But the function must be named.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&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;sayHello&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="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;btn&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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;btn&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="s2"&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;sayHello&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;sayHello&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anonymous functions cannot be removed easily.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
JavaScript events are essential for &lt;strong&gt;creating interactive web applications&lt;/strong&gt;. Understanding how events work helps developers handle user actions effectively and build dynamic user interfaces.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding the DOM in JavaScript: A Complete Beginner’s Guide</title>
      <dc:creator>Jaya Sudha</dc:creator>
      <pubDate>Wed, 25 Mar 2026 16:42:42 +0000</pubDate>
      <link>https://forem.com/jaya_sudha_96fce1e511efee/dom-in-javascript-26ji</link>
      <guid>https://forem.com/jaya_sudha_96fce1e511efee/dom-in-javascript-26ji</guid>
      <description>&lt;p&gt;Every modern website we interact with today is dynamic. When we click a button, type into a search box, or see new content appear without refreshing the page, JavaScript is working behind the scenes to update the webpage instantly. But how does JavaScript know which part of the page to update?&lt;/p&gt;

&lt;p&gt;This is where the &lt;strong&gt;Document Object Model (DOM)&lt;/strong&gt; comes into play.&lt;/p&gt;

&lt;p&gt;By the end of this guide, you will have a solid understanding of how the DOM works and why it is one of the most important concepts in front-end development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. What is the DOM?&lt;/strong&gt;&lt;br&gt;
The DOM (&lt;strong&gt;Document Object Model&lt;/strong&gt;) is a programming interface that represents an HTML document as a &lt;strong&gt;tree structure of objects.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When the browser loads a webpage, it converts the HTML into a DOM tree, which JavaScript can access and modify.&lt;/p&gt;

&lt;p&gt;Example HTML:&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;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Welcome&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello Students&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;DOM Tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Document
   |
  body
  /   \
 h1    p
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each HTML element becomes a &lt;strong&gt;node&lt;/strong&gt; in the DOM tree.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Types of DOM Nodes&lt;/strong&gt;&lt;br&gt;
The DOM consists of different types of nodes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Document Node&lt;/strong&gt;&lt;br&gt;
The &lt;strong&gt;root of the entire page.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Element Nodes&lt;/strong&gt;&lt;br&gt;
All HTML elements are element nodes.&lt;/p&gt;

&lt;p&gt;Examples:&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;h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Text Nodes&lt;/strong&gt;&lt;br&gt;
The text inside elements.&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;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; → element node&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Hello World&lt;/code&gt; → text node&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Attribute Nodes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Attributes attached to elements.&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;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"image.jpg"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"picture"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Attributes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;src&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;alt&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Accessing the DOM&lt;/strong&gt;&lt;br&gt;
The browser provides the** document object** to interact with the DOM.&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="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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This displays the full DOM structure in the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Selecting Elements (DOM Selection)&lt;/strong&gt;&lt;br&gt;
Before modifying anything, JavaScript must select the element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;getElementById()&lt;/strong&gt;&lt;br&gt;
Select element using ID.&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;h1&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title&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;&lt;strong&gt;getElementsByClassName()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Select elements with a specific class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByClassName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;box&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;Returns an &lt;strong&gt;HTMLCollection&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;getElementsByTagName()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Select elements by tag name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p&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;Returns an &lt;strong&gt;HTMLCollection&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;querySelector()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Returns the &lt;strong&gt;first matching element&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="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="s2"&gt;.box&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;&lt;strong&gt;querySelectorAll()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Returns &lt;strong&gt;all matching elements&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.box&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;Returns a &lt;strong&gt;NodeList&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. DOM Manipulation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;DOM manipulation means changing the webpage dynamically using JavaScript.&lt;/p&gt;

&lt;p&gt;Main categories include:&lt;/p&gt;

&lt;p&gt;• Change text&lt;br&gt;
• Change styles&lt;br&gt;
• Add or remove elements&lt;br&gt;
• Respond to user actions&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Changing Text Content&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;innerText&lt;/strong&gt;  - Changes visible text.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;textContent&lt;/strong&gt; -Changes all text including hidden content.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;innerHTML&lt;/strong&gt; - Changes HTML inside the element.&lt;/li&gt;
&lt;/ul&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;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Hello &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"display:none;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;World&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&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="nx"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// Hello&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Hello World&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;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Hello &amp;lt;span style="display:none;"&amp;gt;World&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7. Changing Styles&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript can directly change CSS properties.&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="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fontSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;20px&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;Important rule:&lt;/p&gt;

&lt;p&gt;CSS property → &lt;strong&gt;camelCase in JavaScript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Manipulating Classes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;classList&lt;/strong&gt; property helps manage CSS classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add a class&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="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&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;&lt;strong&gt;Remove a class&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="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&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;&lt;strong&gt;Toggle a class&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="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&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;&lt;strong&gt;Check if class exists&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="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&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;&lt;strong&gt;9. Creating Elements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript can &lt;strong&gt;create new HTML elements&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;let&lt;/span&gt; &lt;span class="nx"&gt;div&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&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;&lt;strong&gt;Add text:&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="nx"&gt;div&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;10. Adding Elements to the DOM&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are several methods to &lt;strong&gt;insert elements&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;append()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Adds element at the end.&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;parent&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="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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="nb"&gt;document&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;appendChild()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Also adds element at the end.&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;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Difference:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;appendChild → only one node&lt;/li&gt;
&lt;li&gt;append → multiple nodes and text allowed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;prepend()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Adds element at the beginning.&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;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;before()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Insert element before another element.&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;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;before&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;after()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Insert element after another element.&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;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;after&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;insertAdjacentHTML()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Insert HTML at specific positions.&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;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertAdjacentHTML&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;beforeend&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;&amp;lt;p&amp;gt;Hello&amp;lt;/p&amp;gt;&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;Positions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;beforebegin
afterbegin
beforeend
afterend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;11. Removing Elements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;remove()&lt;/strong&gt;&lt;br&gt;
Removes element directly.&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;element&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;removeChild()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Removes a child from a parent.&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;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;12. Replacing Elements&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;replaceWith()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Replace an element with another.&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;oldElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replaceWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;replaceChild()&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="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replaceChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newChild&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;oldChild&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;13. Attribute Manipulation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript can change HTML attributes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;setAttribute()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add or update attribute.&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;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;class&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;box&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;&lt;strong&gt;getAttribute()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Get attribute value.&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;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;class&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;&lt;strong&gt;removeAttribute()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Remove attribute.&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;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;class&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;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The DOM provides a structured way for JavaScript to interact with HTML documents. By understanding how &lt;strong&gt;nodes, elements, traversal, and manipulation&lt;/strong&gt; work, developers can dynamically control webpage content and structure. Learning the DOM is a &lt;strong&gt;foundational skill&lt;/strong&gt; for building interactive and modern web applications.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Array Methods Explained in Simple Words (With Examples)</title>
      <dc:creator>Jaya Sudha</dc:creator>
      <pubDate>Wed, 04 Mar 2026 07:09:40 +0000</pubDate>
      <link>https://forem.com/jaya_sudha_96fce1e511efee/javascript-array-methods-explained-in-simple-words-with-examples-e80</link>
      <guid>https://forem.com/jaya_sudha_96fce1e511efee/javascript-array-methods-explained-in-simple-words-with-examples-e80</guid>
      <description>&lt;p&gt;Arrays are used to store multiple values in one variable.&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 plaintext"&gt;&lt;code&gt;const fruits = ["Apple", "Banana", "Mango"];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But how do we add, remove, or change items inside an array?&lt;/p&gt;

&lt;p&gt;JavaScript gives us array methods to do that easily.&lt;/p&gt;

&lt;p&gt;Let’s learn the most important ones step by step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Adding &amp;amp; Removing Elements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;push() – Add at the End&lt;/strong&gt;&lt;br&gt;
The push() method adds a new element to the end of an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ["Apple", "Banana"];
fruits.push("Mango");

console.log(fruits);
// ["Apple", "Banana", "Mango"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It changes the original array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pop() – Remove from the End&lt;/strong&gt;&lt;br&gt;
The pop() method removes the last element from an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ["Apple", "Banana", "Mango"];
fruits.pop();

console.log(fruits);
// ["Apple", "Banana"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It changes the original array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;unshift() – Add at the Beginning&lt;/strong&gt;&lt;br&gt;
The unshift() method adds a new element at the beginning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ["Banana", "Mango"];
fruits.unshift("Apple");

console.log(fruits);
// ["Apple", "Banana", "Mango"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It changes the original array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;shift() – Remove from the Beginning&lt;/strong&gt;&lt;br&gt;
The shift() method removes the first element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ["Apple", "Banana", "Mango"];
fruits.shift();

console.log(fruits);
// ["Banana", "Mango"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It changes the original array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.Copying &amp;amp; Modifying Arrays&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;slice() – Copy Part of an Array&lt;/strong&gt;&lt;br&gt;
The slice() method returns a new array containing selected elements from the original array without modifying it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.slice(startIndex, endIndex)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;startIndex → Where to start (included)&lt;/li&gt;
&lt;li&gt;endIndex → Where to stop (NOT included)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Important: endIndex is NOT included.&lt;/strong&gt;&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 plaintext"&gt;&lt;code&gt;const numbers = [10, 20, 30, 40];

const result = numbers.slice(1, 3);

console.log(result);
// [20, 30]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;br&gt;
Start at index 1 → 20&lt;br&gt;
Stop before index 3 → 40 not included&lt;br&gt;
So output = [20, 30]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2 – Copy Full Array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3];
const copy = numbers.slice();

console.log(copy);
// [1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called a &lt;strong&gt;shallow copy&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;splice() – Add or Remove Anywhere&lt;/strong&gt;&lt;br&gt;
The splice() method is used to add, remove, or replace elements in an array.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It can change the array at any position.&lt;/li&gt;
&lt;li&gt;It modifies the original array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.splice(startIndex, deleteCount, item1, item2, ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;startIndex → Where to start&lt;/li&gt;
&lt;li&gt;deleteCount → How many elements to remove&lt;/li&gt;
&lt;li&gt;item1, item2... → (Optional) Items to add&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Remove Elements&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4];

numbers.splice(1, 2);

console.log(numbers);
// [1, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Start at index 1 → value 2&lt;br&gt;
Remove 2 elements → 2 and 3&lt;br&gt;
Result: [1, 4]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add Elements&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 4];

numbers.splice(1, 0, 2, 3);

console.log(numbers);
// [1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Start at index 1&lt;/li&gt;
&lt;li&gt;Remove 0 elements&lt;/li&gt;
&lt;li&gt;Add 2 and 3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Important rule:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;splice() inserts items at the &lt;strong&gt;startIndex position&lt;/strong&gt;, and the existing element at that index shifts to the right.&lt;/li&gt;
&lt;li&gt;It does NOT insert after it.&lt;/li&gt;
&lt;li&gt;It inserts before the element at that index.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3.Searching Methods&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;includes() – Check if Value Exists&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ["Apple", "Banana"];

console.log(fruits.includes("Apple"));
// true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returns true or false.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;indexOf() – Find Position&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ["Apple", "Banana"];

console.log(fruits.indexOf("Banana"));
// 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Returns the index of the element.&lt;/li&gt;
&lt;li&gt;If not found, it returns -1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;find() – Find First Matching Element&lt;/strong&gt;&lt;br&gt;
The find() method is used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find the first element that matches a condition&lt;/li&gt;
&lt;li&gt;It returns the element itself&lt;/li&gt;
&lt;li&gt;If nothing matches, it returns undefined&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.find(function(element) {
  return condition;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [5, 10, 15, 20];

const result = numbers.find(function(num) {
  return num &amp;gt; 10;
});

console.log(result);
// 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It stops immediately and returns 15.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Rule&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;find() returns the first matching element only&lt;/li&gt;
&lt;li&gt;It stops searching after that&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4.Looping Methods&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;forEach() – Loop Through Array&lt;/strong&gt;&lt;br&gt;
The forEach() method is used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run a function for each element in the array&lt;/li&gt;
&lt;li&gt;It does NOT return a new array&lt;/li&gt;
&lt;li&gt;It does NOT change the original array (unless you modify it 
manually)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.forEach(function(element, index, array) {
  // code to run
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Parameters:&lt;br&gt;
element → current value&lt;br&gt;
index → position (optional)&lt;br&gt;
array → full array (optional)&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 plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3];

numbers.forEach(function(num) {
  console.log(num);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important Rule&lt;/strong&gt;&lt;br&gt;
forEach():&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loops through entire array&lt;/li&gt;
&lt;li&gt;Does NOT return anything&lt;/li&gt;
&lt;li&gt;Always returns undefined&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2 – With Index&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ["Apple", "Banana", "Mango"];

fruits.forEach(function(fruit, index) {
  console.log(index, fruit);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 Apple
1 Banana
2 Mango
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;map() – Create a New Array&lt;/strong&gt;&lt;br&gt;
The map() method is used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transform each element in an array&lt;/li&gt;
&lt;li&gt;Return a new array&lt;/li&gt;
&lt;li&gt;It does NOT change the original array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.map(function(element, index, array) {
  return newValue;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;element → current value&lt;br&gt;
index → position (optional)&lt;br&gt;
array → full array (optional)&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 plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3];

const doubled = numbers.map(function(num) {
  return num * 2;
});

console.log(doubled);
// [2, 4, 6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What happened?&lt;/strong&gt;&lt;br&gt;
1 → 1 × 2 = 2&lt;br&gt;
2 → 2 × 2 = 4&lt;br&gt;
3 → 3 × 2 = 6&lt;/p&gt;

&lt;p&gt;map() collects all returned values into a new array.&lt;/p&gt;

&lt;p&gt;Important Rule&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;map() ALWAYS returns a new array&lt;/li&gt;
&lt;li&gt;It must have a return statement&lt;/li&gt;
&lt;li&gt;It does NOT modify the original array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2 – With Arrow Function (Modern Way)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3];

const doubled = numbers.map(num =&amp;gt; num * 2);

console.log(doubled);
// [2, 4, 6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;filter() – Filter Based on Condition&lt;/strong&gt;&lt;br&gt;
The filter() method is used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select elements that match a condition&lt;/li&gt;
&lt;li&gt;Return a new array&lt;/li&gt;
&lt;li&gt;It does NOT change the original array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.filter(function(element, index, array) {
  return condition;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the condition is:&lt;br&gt;
true → element is kept&lt;br&gt;
false → element is removed&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1 – Basic Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4];

const even = numbers.filter(function(num) {
  return num % 2 === 0;
});

console.log(even);
// [2, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only even numbers are kept.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Rule&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;filter() always returns a new array&lt;/li&gt;
&lt;li&gt;It may return fewer elements&lt;/li&gt;
&lt;li&gt;It can even return an empty array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2 – Using Arrow Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [5, 10, 15, 20];
const greaterThan10 = numbers.filter(n =&amp;gt; n &amp;gt; 10);

console.log(greaterThan10);
// [15, 20]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;reduce() – Combine All Values into One&lt;/strong&gt;&lt;br&gt;
The reduce() method is used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take all array elements&lt;/li&gt;
&lt;li&gt;Combine them into a single value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It can return:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A number&lt;/li&gt;
&lt;li&gt;A string&lt;/li&gt;
&lt;li&gt;An object&lt;/li&gt;
&lt;li&gt;Even another array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.reduce(function(accumulator, currentValue) {
  return updatedValue;
}, initialValue);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;accumulator&lt;/strong&gt; → Stores the result step by step&lt;br&gt;
&lt;strong&gt;currentValue&lt;/strong&gt; → Current element in loop&lt;br&gt;
&lt;strong&gt;initialValue&lt;/strong&gt; → Starting value&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1 – Add All Numbers&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4];
const total = numbers.reduce(function(sum, num) {
  return sum + num;
}, 0);

console.log(total);
// 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2 – Multiply Numbers&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [2, 3, 4];
const result = numbers.reduce(function(total, num) {
  return total * num;
}, 1);

console.log(result);
// 24
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript Array Methods – Quick Comparison&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%2Fe9ejhxxapp5opc0ri703.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%2Fe9ejhxxapp5opc0ri703.png" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This table helps you quickly understand which methods modify the array and which return a new one.&lt;/p&gt;

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

&lt;p&gt;JavaScript array methods may look overwhelming at first, but once you understand how they are grouped, everything becomes much clearer.&lt;/p&gt;

&lt;p&gt;Some methods modify the original array (like &lt;strong&gt;push(), pop(),&lt;/strong&gt; and &lt;strong&gt;splice()&lt;/strong&gt;), while others return a new array or value without changing the original (like &lt;strong&gt;slice(), map(), filter(), and find()&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;The key is to remember:&lt;/p&gt;

&lt;p&gt;🔹 Use &lt;strong&gt;mutating methods&lt;/strong&gt; when you want to change the existing array.&lt;br&gt;
🔹 Use &lt;strong&gt;non-mutating methods&lt;/strong&gt; when you want to keep your original data safe.&lt;br&gt;
🔹 Use iteration methods like map(), filter(), and reduce() for cleaner and more readable code.&lt;br&gt;
🔹 Use search methods like includes(), find(), and indexOf() to quickly locate data.&lt;/p&gt;

&lt;p&gt;Start practicing them in small examples, and soon you’ll use them naturally in real-world projects.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript Arrays: A Beginner’s Guide with Examples</title>
      <dc:creator>Jaya Sudha</dc:creator>
      <pubDate>Thu, 26 Feb 2026 18:36:20 +0000</pubDate>
      <link>https://forem.com/jaya_sudha_96fce1e511efee/javascript-arrays-a-beginners-guide-with-examples-3p04</link>
      <guid>https://forem.com/jaya_sudha_96fce1e511efee/javascript-arrays-a-beginners-guide-with-examples-3p04</guid>
      <description>&lt;p&gt;Arrays are one of the most important data structures in JavaScript. They allow us to store multiple values in a single variable instead of creating separate variables for each value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Is an Array?&lt;/strong&gt;&lt;br&gt;
An array is a special variable that can hold multiple values.&lt;br&gt;
Each value inside an array is called an element.&lt;/p&gt;

&lt;p&gt;Arrays use zero-based indexing, which means the first element starts at index 0.&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 plaintext"&gt;&lt;code&gt;let arr = [10, 20, 30];

10 → index 0
20 → index 1
30 → index 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Different Ways to Create an Array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Using Array Literal (Most Common Method)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the simplest and most commonly used method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Using the Array Constructor&lt;/strong&gt;&lt;br&gt;
The Array() constructor has two main syntaxes, depending on the arguments provided.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To create an array with specific elements: Pass the elements as arguments.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = new Array(1, 2, 3);

//Result: [1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although this works, using array literals ([]) is usually cleaner and easier to read.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To create an empty array of a specified length: Pass a single, non-negative integer argument.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = new Array(4);
// Result: an array with a length of 4 empty slots, not [undefined, undefined, undefined, undefined]
// arr[0] is undefined, but the slot is "empty" (sparse array)

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Differences from Array Literals&lt;/strong&gt;&lt;br&gt;
While new Array() and [] seem similar, they differ in the following ways:&lt;br&gt;
&lt;strong&gt;Single Numeric Argument&lt;/strong&gt;: The most significant difference is how a single numeric argument is handled.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;new Array(5) creates an array with a length of 5 empty slots.&lt;/li&gt;
&lt;li&gt;[5] creates an array with one element, the number 5.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;array literal&lt;/strong&gt; notation is generally considered more efficient, concise, and easier to read.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Creating Empty Array&lt;/strong&gt;&lt;br&gt;
Creating a empty array and push the values to them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then add values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr[0] = 10;
arr.push(20);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Using Array.of()&lt;/strong&gt;&lt;br&gt;
Array.of() creates a new array from the given arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = Array.of(1, 2, 3);
let arr1 = Array.of(3);
console.log(arr);
console.log(arr1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3]
[3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Using Array.from()&lt;/strong&gt;&lt;br&gt;
Creates a new array from an iterable or array-like object.&lt;br&gt;
Used to convert something into array.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = "Apple";
let arr = Array.from(str);
console.log(arr);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["A", "P", "P", "L", "E"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = Array.from({ length: 5 });
console.log(arr);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[undefined, undefined, undefined, undefined, undefined]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Using Spread Operator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spread operator:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;We can also create a new array by copying another array using the spread operator (...).&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = [1, 2, 3];
let b = [...a];

console.log(b);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Creates a shallow copy&lt;/li&gt;
&lt;li&gt;Does NOT modify original array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example 2&lt;br&gt;
&lt;strong&gt;Merge Arrays&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = [1, 2];
let b = [3, 4];

let c = [...a, ...b];

console.log(c);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Accessing Array Elements&lt;/strong&gt;&lt;br&gt;
We access elements using their index number.&lt;br&gt;
&lt;strong&gt;Important: Array index always starts from 0.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let fruits = ["apple", "orange", "guava", "grapes"];

console.log(fruits[0]); // apple
console.log(fruits[1]); // orange
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;br&gt;
fruits[0] → First element&lt;br&gt;
fruits[1] → Second element&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accessing the Last Element&lt;/strong&gt;&lt;br&gt;
To access the last element dynamically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(names[fruits.length - 1]); // grapes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why length - 1?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;length is a property&lt;/strong&gt;&lt;br&gt;
The length property returns the size of the array. It represents the highest index in the array plus one.&lt;/p&gt;

&lt;p&gt;In most cases, it shows how many elements are inside the array. However, JavaScript arrays can contain empty slots (also called sparse arrays). In such cases, length reflects the array size, not necessarily the number of filled elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, , , 4];

console.log(arr.length); // 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though the array length is 4, only 2 elements actually contain values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Updating Array Elements&lt;/strong&gt;&lt;br&gt;
Arrays in JavaScript are &lt;strong&gt;mutable&lt;/strong&gt;, which means we can change their values.&lt;/p&gt;

&lt;p&gt;We can update an element by assigning a new value to a specific index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruits[1] = "Banana";

console.log(fruits);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["apple", "Banana", "guava", "grapes"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we updated the second element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Updating All Elements Using Loop&lt;/strong&gt;&lt;br&gt;
If we want to modify every element, we can use a loop.&lt;/p&gt;

&lt;p&gt;Example: Convert all fruits name to uppercase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; fruits.length; i++) {
  fruits[i] = fruits[i].toUpperCase();
}

console.log(fruits);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["APPLE", "BANANA", "GUAVA", "GRAPES"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using forEach (Modern Way)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruits.forEach((value, index) =&amp;gt; {
  fruits[index] = value.toUpperCase();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is cleaner and more readable.&lt;/p&gt;

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

&lt;p&gt;Arrays are a fundamental part of JavaScript. They help us store and manage multiple values efficiently. Understanding how to create, access, update, and manipulate arrays is essential for building real-world applications.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript Fundamentals Every Beginner Should Know</title>
      <dc:creator>Jaya Sudha</dc:creator>
      <pubDate>Thu, 12 Feb 2026 04:21:55 +0000</pubDate>
      <link>https://forem.com/jaya_sudha_96fce1e511efee/javascript-o07</link>
      <guid>https://forem.com/jaya_sudha_96fce1e511efee/javascript-o07</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is JavaScript&lt;/strong&gt;&lt;br&gt;
JavaScript (JS) is a programming language used to make websites interactive and dynamic.(e.g., having complex animations, clickable buttons, popup menus, etc.).&lt;/p&gt;

&lt;p&gt;If HTML is the &lt;strong&gt;structure of a website&lt;/strong&gt; and CSS is the &lt;strong&gt;design&lt;/strong&gt;, then JavaScript is the &lt;strong&gt;brain that adds behavior&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Founder of JavaScript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript was created by &lt;strong&gt;Brendan Eich&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Created at: &lt;strong&gt;Netscape&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Year: &lt;strong&gt;1995&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Developed in: &lt;strong&gt;Just 10 days!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Original name: &lt;strong&gt;Mocha&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Later renamed to: &lt;strong&gt;LiveScript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally renamed to: &lt;strong&gt;JavaScript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why JavaScript is called JavaScript&lt;/strong&gt;&lt;br&gt;
JavaScript was originally called LiveScript. In 1995, it was renamed to JavaScript as a marketing strategy to take advantage of Java’s popularity, even though the two languages are entirely different in design and functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How JavaScript Code Runs in the Browser&lt;/strong&gt;&lt;br&gt;
JavaScript runs inside the browser’s JavaScript engine (like V8 in Chrome). The engine first parses the code, then uses Just-In-Time (JIT) compilation to convert it into machine code, which is executed by the CPU.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple Flow Diagram&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our Code&lt;br&gt;
⬇&lt;br&gt;
JavaScript Engine (V8) (inside our browser)&lt;br&gt;
⬇&lt;br&gt;
Machine Code (0s &amp;amp; 1s)&lt;br&gt;
⬇&lt;br&gt;
CPU Executes&lt;br&gt;
⬇&lt;br&gt;
Output&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is JavaScript compiled or interpreted?&lt;/strong&gt;&lt;br&gt;
JavaScript was traditionally considered an interpreted language, but modern engines like V8 use &lt;strong&gt;Just-In-Time (JIT)&lt;/strong&gt; compilation. So it is technically both compiled and interpreted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Just-In-Time (JIT)&lt;/strong&gt;&lt;br&gt;
Just-In-Time (JIT) compilation is a technique used by modern JavaScript engines (like &lt;strong&gt;Google Chrome's V8 and Firefox's SpiderMonkey)&lt;/strong&gt; to improve performance by dynamically compiling JavaScript code into native, optimized machine code during execution, rather than interpreting it line by line every time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
JavaScript is a powerful language that plays a major role in modern web development. Understanding how it works inside the browser helps developers write efficient and optimized code.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>HTML Basics: Understanding the Basic Structure of an HTML Document</title>
      <dc:creator>Jaya Sudha</dc:creator>
      <pubDate>Tue, 06 Jan 2026 17:51:57 +0000</pubDate>
      <link>https://forem.com/jaya_sudha_96fce1e511efee/understanding-the-basic-structure-of-an-html-document-5bop</link>
      <guid>https://forem.com/jaya_sudha_96fce1e511efee/understanding-the-basic-structure-of-an-html-document-5bop</guid>
      <description>&lt;p&gt;&lt;strong&gt;HTML&lt;/strong&gt;&lt;br&gt;
HTML (HyperText Markup Language) is the standard language used to create webpages. It tells the browser how to display content using tags.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Structure of an HTML Document&lt;/strong&gt;&lt;br&gt;
An HTML document is a file that contains HTML code. It has a predefined structure that tells the browser how to display the web page.&lt;br&gt;
Let’s understand the structure using a simple example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Structure of HTML Document&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;!-- Main content of website --&amp;gt;
    &amp;lt;h1&amp;gt;Welcome&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;Beginner friendly blogs&amp;lt;/p&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;1. &lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
This tag indicates the version of the HTML. In this case, it indicates that the document is written in HTML5, which is the latest version of HTML.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The &lt;strong&gt;&lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;&lt;/strong&gt; tag is the root element of an HTML document, and the &lt;strong&gt;lang="en"&lt;/strong&gt; attribute specifies that the content language is English, which improves accessibility and SEO.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The &lt;strong&gt;&lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;&lt;/strong&gt; section contains configuration information such as the page title, styles, and scripts that are required for the webpage but are not visible to users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. &lt;code&gt;&amp;lt;title&amp;gt;Structure of HTML Document&amp;lt;/title&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; tag defines the title of the webpage shown in the browser tab.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tag contains all the content that is visible to users on the webpage. It includes text, images, buttons, forms, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;!-- Main content of website --&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
HTML comments are used to document code and it is not displayed in the browser. It is used for &lt;strong&gt;code readability&lt;/strong&gt; and &lt;strong&gt;notes for developers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Tags Found Inside the&lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Text and Structure&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; to &lt;code&gt;&amp;lt;h6&amp;gt;&lt;/code&gt;&lt;/strong&gt; are HTML heading tags used to define headings in a web page.&lt;br&gt;
They represent different levels of importance, where:&lt;br&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;&lt;/strong&gt; is the most important and &lt;strong&gt;largest&lt;/strong&gt; heading&lt;br&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;h6&amp;gt;&lt;/code&gt;&lt;/strong&gt; is the least important and &lt;strong&gt;smallest&lt;/strong&gt; heading&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;:&lt;/strong&gt; Paragraphs of text.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;:&lt;/strong&gt; A general-purpose container for grouping other elements for styling with CSS or manipulation with JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;:&lt;/strong&gt; An inline element for grouping small parts of text for styling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;br&amp;gt;&lt;/code&gt;:&lt;/strong&gt; Inserts a single line break.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;hr&amp;gt;&lt;/code&gt;:&lt;/strong&gt; Represents a horizontal line.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lists&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;Unordered&lt;/strong&gt; (bulleted) lists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;ol&amp;gt;&lt;/code&gt;&lt;/strong&gt;: &lt;strong&gt;Ordered&lt;/strong&gt; (numbered) lists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt;&lt;/strong&gt;: Individual list items within &lt;strong&gt;&lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;&amp;lt;ol&amp;gt;&lt;/code&gt;&lt;/strong&gt; tags. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Links and Media&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;a&amp;gt;:&lt;/code&gt;&lt;/strong&gt; &lt;strong&gt;Hyperlinks&lt;/strong&gt;, used to link to other pages or resources.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;&lt;/strong&gt;: Embeds an &lt;strong&gt;image&lt;/strong&gt; in the document.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;video&amp;gt;&lt;/code&gt;&lt;/strong&gt;: Embeds &lt;strong&gt;video&lt;/strong&gt; content.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;audio&amp;gt;&lt;/code&gt;&lt;/strong&gt;: Embeds &lt;strong&gt;audio&lt;/strong&gt; content&lt;/p&gt;

&lt;p&gt;Note: &lt;strong&gt;Embeds&lt;/strong&gt; are used to display external resources inside a web page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tables&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;table&amp;gt;&lt;/code&gt;&lt;/strong&gt;: Defines a table for displaying tabular data.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;tr&amp;gt;, &amp;lt;th&amp;gt;, &amp;lt;td&amp;gt;&lt;/code&gt;&lt;/strong&gt;: Table rows, headers, and data cells, respectively. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Semantic Elements&lt;/strong&gt;&lt;br&gt;
Semantic tags make the code easy to understand, help &lt;strong&gt;screen readers&lt;/strong&gt;, and improve &lt;strong&gt;SEO&lt;/strong&gt;.&lt;br&gt;
&lt;strong&gt;For example,&lt;/strong&gt; replacing &lt;strong&gt;&lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;&lt;/strong&gt;tags with semantic tags like &lt;strong&gt;&lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;&amp;lt;article&amp;gt;&lt;/code&gt;&lt;/strong&gt; makes the code more readable, accessible, and SEO-friendly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Screen readers&lt;/strong&gt; are software programs that read web content aloud for visually impaired users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SEO&lt;/strong&gt; stands for &lt;strong&gt;Search Engine Optimization&lt;/strong&gt;. It’s the practice of improving a website so that search engines like Google can understand it and show it higher in search results, helping more people visit your site.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Commonly used semantic HTML tags:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt;&lt;/strong&gt;– Represents the header of a page or section (usually contains navigation, logo, etc.).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt;&lt;/strong&gt; – Defines a navigation menu with links.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;main&amp;gt;&lt;/code&gt;&lt;/strong&gt; – Represents the main content of the page (should be unique per page).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;section&amp;gt;&lt;/code&gt;&lt;/strong&gt; – Defines a thematic section of content, usually with a heading.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;article&amp;gt;&lt;/code&gt;&lt;/strong&gt; – Represents independent content, like blog posts, news articles, or forum posts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;aside&amp;gt;&lt;/code&gt;&lt;/strong&gt; – Defines content aside from the main content, like sidebars, ads, or widgets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;footer&amp;gt;&lt;/code&gt;&lt;/strong&gt; – Represents the footer of a page or section (usually contains copyright, links, contact info).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of semantic HTML:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Improves SEO – search engines understand your content.&lt;/li&gt;
&lt;li&gt;Increases accessibility – screen readers can read content aloud.&lt;/li&gt;
&lt;li&gt;Makes code organized and readable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Understanding the &lt;strong&gt;basic structure of an HTML document&lt;/strong&gt; is essential for beginners. Use semantic tags, write clear headings, and make your pages accessible. These practices improve both &lt;strong&gt;user experience&lt;/strong&gt; and &lt;strong&gt;SEO&lt;/strong&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Git Basics for Beginners: Understanding Version Control and Git Workflow</title>
      <dc:creator>Jaya Sudha</dc:creator>
      <pubDate>Sun, 04 Jan 2026 16:45:43 +0000</pubDate>
      <link>https://forem.com/jaya_sudha_96fce1e511efee/git-learning-1i65</link>
      <guid>https://forem.com/jaya_sudha_96fce1e511efee/git-learning-1i65</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is GIT&lt;/strong&gt;&lt;br&gt;
Git(&lt;strong&gt;Global Information Tracker&lt;/strong&gt;) is a distributed version control system used to track changes in source code.&lt;br&gt;
It helps us understand what changes were made, who made them, and when they were made by maintaining a complete version history of files.&lt;/p&gt;

&lt;p&gt;Git was created by &lt;strong&gt;Linus Torvalds&lt;/strong&gt; in 2005.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Version control system&lt;/strong&gt;&lt;br&gt;
A Version Control System (VCS) is a software tool that tracks and manages changes to files, primarily source code.&lt;br&gt;
It acts as a history log for a project, allowing developers to compare versions and revert to earlier states if errors occur.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Version Control Systems&lt;/strong&gt;&lt;br&gt;
There are three types of version control systems: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Local VCS&lt;/strong&gt;&lt;br&gt;
In a Local VCS, versions are stored only on a single computer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
Manual file backups (copy–paste folders)&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;No collaboration&lt;/li&gt;
&lt;li&gt;Risk of data loss&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Centralized VCS&lt;/strong&gt;&lt;br&gt;
In a Centralized VCS, a single central server stores the project.&lt;br&gt;
All developers connect to this server to get and update code.&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%2F83v5p5t5jifw0sezbvdx.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%2F83v5p5t5jifw0sezbvdx.png" alt=" " width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
SVN (Subversion)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
One central server contains the code.&lt;br&gt;
Developers checkout, commit, and update files from the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Distributed VCS&lt;/strong&gt;&lt;br&gt;
In a Distributed VCS, every developer has a full copy of the repository, including the complete history.&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%2Fnjbxg8w3m90it09hhh0k.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%2Fnjbxg8w3m90it09hhh0k.png" alt=" " width="800" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;Each developer has complete history&lt;/li&gt;
&lt;li&gt;Can work offline&lt;/li&gt;
&lt;li&gt;Changes are pushed later to platforms like GitHub or GitLab&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How git works&lt;/strong&gt;&lt;br&gt;
A Git process flow diagram illustrates how changes move between different environments using specific commands. &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%2Fhg8n7l5ni4egwprlhfb6.webp" 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%2Fhg8n7l5ni4egwprlhfb6.webp" alt=" " width="800" height="568"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Working Directory → Staging Area → Local Repository → Remote Repository&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Files are edited in the &lt;strong&gt;Working Directory&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;git add&lt;/code&gt;&lt;/strong&gt; moves changes to the &lt;strong&gt;Staging Area&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;git commit&lt;/code&gt;&lt;/strong&gt; saves changes into the &lt;strong&gt;Local Repository&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;git push&lt;/code&gt;&lt;/strong&gt; sends commits to the &lt;strong&gt;Remote Repository (GitHub/GitLab)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Working Directory&lt;/strong&gt;&lt;br&gt;
The working directory is the place where developers modify project files before staging and committing them to Git.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Staging Area&lt;/strong&gt;&lt;br&gt;
The staging area is an intermediate area where changes are reviewed and added before committing them to the local repository.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Repository&lt;/strong&gt;&lt;br&gt;
A repository is a &lt;strong&gt;storage location&lt;/strong&gt; where Git tracks all versions of a project’s files and maintains its full history.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Local repository:&lt;/strong&gt;&lt;br&gt;
A local repository is a Git repository that exists on our computer where changes are committed before being pushed to a remote repository.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remote repository:&lt;/strong&gt;&lt;br&gt;
A remote repository is an online Git repository hosted on platforms like GitHub or GitLab.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is GitLab&lt;/strong&gt;&lt;br&gt;
GitLab is an online platform that hosts Git repositories, allowing developers to manage code, collaborate on our Git projects online.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to connect Git with GitLab&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Create a repository on GitLab&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; Connect your local repo:&lt;br&gt;
git remote add origin &lt;a href="https://gitlab.com/username/portfolio.git" rel="noopener noreferrer"&gt;https://gitlab.com/username/portfolio.git&lt;/a&gt;&lt;br&gt;
git push -u origin main&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Now your local changes are visible on GitLab.&lt;/p&gt;

</description>
      <category>git</category>
      <category>gitlab</category>
      <category>learning</category>
    </item>
    <item>
      <title>My First Notes on HTML</title>
      <dc:creator>Jaya Sudha</dc:creator>
      <pubDate>Fri, 02 Jan 2026 08:35:26 +0000</pubDate>
      <link>https://forem.com/jaya_sudha_96fce1e511efee/my-first-notes-on-html-2p3l</link>
      <guid>https://forem.com/jaya_sudha_96fce1e511efee/my-first-notes-on-html-2p3l</guid>
      <description>&lt;p&gt;&lt;strong&gt;HTML&lt;/strong&gt;&lt;br&gt;
HTML stands for HyperText Markup Language. It is used to communicate with the browser using tags. These tags define the structure of a web page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Founder of HTML&lt;/strong&gt;&lt;br&gt;
HTML was created by &lt;strong&gt;Tim Berners-Lee&lt;/strong&gt; in 1991. It is the standard markup language used to create web pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is markup language?&lt;/strong&gt;&lt;br&gt;
A markup language is a language used to structure and format content using tags. It tells the browser how to display text, images, links, and other elements on a web page.&lt;br&gt;
Examples of markup languages: &lt;strong&gt;HTML, XML, XHTML&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difference between HTML vs Markup Language vs Programming language&lt;/strong&gt;&lt;br&gt;
Markup language structures content, HTML is one markup language used for web pages, and programming languages add logic and functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
A markup language is like a vehicle category, and HTML is a bike in that category and the programming language is like a engine that makes the bike run.&lt;br&gt;
&lt;strong&gt;Markup Language&lt;/strong&gt; → Vehicle category&lt;br&gt;
&lt;strong&gt;HTML&lt;/strong&gt; → Bike&lt;br&gt;
&lt;strong&gt;Programming Language&lt;/strong&gt; → Engine that makes the bike run.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scripting Language&lt;/strong&gt;&lt;br&gt;
A scripting language is a type of programming language used to write small programs (scripts) that automate tasks and add dynamic behavior to applications, especially web pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scripting Language vs Programming Language&lt;/strong&gt;&lt;br&gt;
A scripting language is used for automation and interaction, while a programming language is used to develop complete software applications.&lt;/p&gt;

</description>
      <category>html</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
