<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged modernjavascript</title>
<link>https://coderlegion.com/tag/modernjavascript</link>
<description>Powered by Question2Answer</description>
<item>
<title>forEach Doesn't Wait for Your await: The Bug That Makes APIs Reply Before the Work Is Don</title>
<link>https://coderlegion.com/31828/foreach-doesnt-wait-for-your-await-the-bug-that-makes-apis-reply-before-the-work-is-don</link>
<description>&lt;p&gt;Your endpoint sends 50 emails, marks 50 orders as shipped, and returns &lt;code&gt;{ success: true }&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In your tests, it works. In production, the response comes back in 8 milliseconds, half the orders are still &lt;code&gt;pending&lt;/code&gt;, and one failed email crashed the whole Node process with an error nobody can trace to a request.&lt;/p&gt;
&lt;p&gt;The culprit is usually one innocent line.&lt;/p&gt;
&lt;h3 id=&quot;the-code-that-looks-fine&quot;&gt;The Code That Looks Fine&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;app.post('/api/ship', async (req, res) =&amp;gt; {
  const orders = await Order.find({ status: 'paid' });

  orders.forEach(async (order) =&amp;gt; {
    await sendShippingEmail(order);
    await Order.updateOne({ _id: order._id }, { status: 'shipped' });
  });

  res.json({ success: true });
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is an &lt;code&gt;await&lt;/code&gt; inside, so it must wait. It does not.&lt;/p&gt;
&lt;h3 id=&quot;why-it-returns-early&quot;&gt;Why It Returns Early&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;forEach&lt;/code&gt; was written before promises existed. It calls your function for each item and &lt;strong&gt;ignores whatever the function returns&lt;/strong&gt;. An &lt;code&gt;async&lt;/code&gt; function returns a promise, and &lt;code&gt;forEach&lt;/code&gt; throws that promise away without looking at it.&lt;/p&gt;
&lt;p&gt;So the loop starts 50 async tasks, finishes in one tick, and moves on. The &lt;code&gt;await&lt;/code&gt; inside each callback only pauses that callback, not the loop around it, and not your handler.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;console.log('start');
[1, 2, 3].forEach(async (n) =&amp;gt; {
  await new Promise((r) =&amp;gt; setTimeout(r, 100));
  console.log('done', n);
});
console.log('end');

// start
// end
// done 1
// done 2
// done 3
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;end&lt;/code&gt; prints before any work finishes. That is your &lt;code&gt;res.json()&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;why-it-is-hard-to-catch&quot;&gt;Why It Is Hard to Catch&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fast tests hide it.&lt;/strong&gt; With a local database and a mocked email service, the work finishes before your next assertion runs. It passes by luck.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The failure is somewhere else.&lt;/strong&gt; The response says success, so nothing points back to this line.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Errors escape.&lt;/strong&gt; If a callback throws, nobody is awaiting that promise, so there is no &lt;code&gt;try/catch&lt;/code&gt; that can catch it. Since Node 15, an unhandled rejection crashes the process by default. One bad email address can take down the server, and the stack trace will not mention the request that caused it.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;fix-1-need-it-sequential-use-for-of&quot;&gt;Fix 1: Need It Sequential? Use for...of&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;for (const order of orders) {
  await sendShippingEmail(order);
  await Order.updateOne({ _id: order._id }, { status: 'shipped' });
}

res.json({ success: true });
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;for...of&lt;/code&gt; is real control flow, so &lt;code&gt;await&lt;/code&gt; pauses the whole loop. Errors land in your &lt;code&gt;try/catch&lt;/code&gt;. Use this when order matters, or when you must not hit a rate-limited API in parallel.&lt;/p&gt;
&lt;h3 id=&quot;fix-2-need-it-fast-promise-all-with-map&quot;&gt;Fix 2: Need It Fast? Promise.all With map&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;await Promise.all(
  orders.map(async (order) =&amp;gt; {
    await sendShippingEmail(order);
    await Order.updateOne({ _id: order._id }, { status: 'shipped' });
  })
);

res.json({ success: true });
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;map&lt;/code&gt; returns the array of promises that &lt;code&gt;forEach&lt;/code&gt; was throwing away, and &lt;code&gt;Promise.all&lt;/code&gt; waits for all of them. Same callback, one different method.&lt;/p&gt;
&lt;h3 id=&quot;fix-3-some-failures-are-acceptable-use-allsettled&quot;&gt;Fix 3: Some Failures Are Acceptable? Use allSettled&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;Promise.all&lt;/code&gt; rejects as soon as one promise fails, but &lt;strong&gt;the other tasks keep running&lt;/strong&gt;. You get an error back while half the work still completes in the background.&lt;/p&gt;
&lt;p&gt;If you want every task to finish and then look at what failed:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const results = await Promise.allSettled(orders.map(shipOrder));

const failed = results
  .map((r, i) =&amp;gt; ({ r, order: orders[i] }))
  .filter(({ r }) =&amp;gt; r.status === 'rejected');

res.json({
  shipped: results.length - failed.length,
  failed: failed.map(({ order }) =&amp;gt; order._id),
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now one bad email does not hide the 49 that worked, and the response tells the caller exactly what to retry.&lt;/p&gt;
&lt;h3 id=&quot;fix-4-thousands-of-items-limit-the-concurrency&quot;&gt;Fix 4: Thousands of Items? Limit the Concurrency&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;Promise.all(items.map(...))&lt;/code&gt; starts everything at once. With 10,000 items, that is 10,000 simultaneous database calls or HTTP requests, which can exhaust your connection pool or get you rate limited.&lt;/p&gt;
&lt;p&gt;Process in batches:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;async function inBatches(items, size, fn) {
  const results = [];
  for (let i = 0; i &amp;lt; items.length; i += size) {
    const batch = items.slice(i, i + size);
    results.push(...(await Promise.all(batch.map(fn))));
  }
  return results;
}

await inBatches(orders, 10, shipOrder);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ten at a time is fast enough for most jobs and gentle on whatever is on the other end.&lt;/p&gt;
&lt;h3 id=&quot;same-trap-in-filter-some-every-and-reduce&quot;&gt;Same Trap in filter, some, every and reduce&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;forEach&lt;/code&gt; is not the only method that does not understand promises.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// Always keeps everything. A Promise is truthy.
const valid = users.filter(async (u) =&amp;gt; await isActive(u));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The callback returns a promise for every user, and a promise object is always truthy, so nothing is ever filtered out. &lt;code&gt;some&lt;/code&gt; and &lt;code&gt;every&lt;/code&gt; have the same problem and return &lt;code&gt;true&lt;/code&gt; immediately.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// Resolve first, then filter
const flags = await Promise.all(users.map(isActive));
const valid = users.filter((_, i) =&amp;gt; flags[i]);
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;the-rule&quot;&gt;The Rule&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Array methods like &lt;code&gt;forEach&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;some&lt;/code&gt; and &lt;code&gt;every&lt;/code&gt; do not await. If an &lt;code&gt;async&lt;/code&gt; function sits inside one, the work is detached from your request.&lt;/strong&gt; Use &lt;code&gt;for...of&lt;/code&gt; to run steps in order, and &lt;code&gt;Promise.all&lt;/code&gt; or &lt;code&gt;Promise.allSettled&lt;/code&gt; with &lt;code&gt;map&lt;/code&gt; to run them together.&lt;/p&gt;
&lt;p&gt;Quick audit: search your project for &lt;code&gt;.forEach(async&lt;/code&gt;. In a route handler, a job or a script that exits when it finishes, each hit is either a bug or a race that has not lost yet.&lt;/p&gt;
&lt;p&gt;I use this pattern in the bulk operations behind my &lt;a rel=&quot;nofollow&quot; href=&quot;https://pixelanas.com/blog/mongodb-mongoose-patterns-2026&quot;&gt;Next.js and MongoDB templates on pixelanas.com&lt;/a&gt;, where seeding, imports and batch updates must finish before the response goes out.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Get the templates:&lt;/strong&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;https://pixelanas.gumroad.com&quot;&gt;https://pixelanas.gumroad.com&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;How many &lt;code&gt;.forEach(async&lt;/code&gt; do you find in your codebase? Drop the number in the comments, and tell me if one of them turned out to be a real bug.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;em&gt;Anas, full-stack Next.js developer building SaaS products and premium templates. X: &lt;a rel=&quot;nofollow&quot; href=&quot;https://x.com/ASheikh69751&quot;&gt;@ASheikh69751&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/31828/foreach-doesnt-wait-for-your-await-the-bug-that-makes-apis-reply-before-the-work-is-don</guid>
<pubDate>Tue, 20 Oct 2026 10:16:43 +0000</pubDate>
</item>
<item>
<title>9 Steps to Build an IPTV Web Player With JavaScript: From M3U Playlist to Live Playback</title>
<link>https://coderlegion.com/26427/9-steps-to-build-an-iptv-web-player-with-javascript-from-m3u-playlist-to-live-playback</link>
<description>&lt;p&gt;Building a web-based IPTV player with JavaScript is a practical way to understand how modern live video applications work in the browser. What looks like a simple interface with a channel list and a video window actually involves several different technologies working together: playlist parsing, HTTP-based media delivery, HLS manifests, HTML5 video, browser APIs, network requests, and playback state management.&lt;/p&gt;
&lt;p&gt;In this guide, we will build the foundation of a JavaScript IPTV web player that can read a channel list, extract stream information, display channels dynamically, and play HLS live streams in a browser.&lt;/p&gt;
&lt;p&gt;The examples are designed around publicly accessible or self-hosted streams that you are authorized to use. The purpose is to explain the engineering behind an IPTV-style application rather than provide unauthorized access to copyrighted broadcasts.&lt;/p&gt;
&lt;p&gt;The project will start with plain HTML, CSS, and JavaScript. Once the basic architecture is understood, the same concepts can be transferred to React, Vue, Angular, or another frontend framework.&lt;/p&gt;
&lt;h3 id=&quot;what-is-an-iptv-web-player-&quot;&gt;What Is an IPTV Web Player?&lt;/h3&gt;
&lt;p&gt;An IPTV web player is essentially a web application designed to organize and play live media streams delivered over an IP network.&lt;/p&gt;
&lt;p&gt;From a developer's perspective, the term IPTV describes the application and distribution context, while the actual video playback may rely on technologies such as HLS, MPEG-DASH, HTML5 video, Media Source Extensions, or WebRTC depending on the requirements.&lt;/p&gt;
&lt;p&gt;A basic IPTV web player can be represented as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;M3U Playlist
     ↓
JavaScript Parser
     ↓
Channel Objects
     ↓
Channel List
     ↓
Selected Stream
     ↓
HLS / M3U8
     ↓
HTML5 Video
     ↓
Live Playback
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This architecture is simple enough to understand but also provides a foundation for much larger streaming applications.&lt;/p&gt;
&lt;p&gt;A production application can later add search, categories, favorites, EPG data, authentication, analytics, multiple audio tracks, subtitles, quality selection, and responsive interfaces.&lt;/p&gt;
&lt;h3 id=&quot;understanding-m3u-and-m3u8-playlists&quot;&gt;Understanding M3U and M3U8 Playlists&lt;/h3&gt;
&lt;p&gt;Before writing JavaScript, it is important to understand what an M3U playlist actually contains.&lt;/p&gt;
&lt;p&gt;M3U is a playlist format originally used to describe collections of media resources. In IPTV applications, an M3U file is frequently used to store channel metadata and stream URLs.&lt;/p&gt;
&lt;p&gt;A simplified playlist may look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;#EXTM3U

#EXTINF:-1,Example News
https://example.com/live/news/index.m3u8

#EXTINF:-1,Example Sports
https://example.com/live/sports/index.m3u8

#EXTINF:-1,Example Music
https://example.com/live/music/index.m3u8
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;#EXTM3U&lt;/code&gt; line identifies the playlist.&lt;/p&gt;
&lt;p&gt;Each &lt;code&gt;#EXTINF&lt;/code&gt; line contains metadata describing the following media URL.&lt;/p&gt;
&lt;p&gt;The URL may point to an HLS manifest ending in &lt;code&gt;.m3u8&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;That distinction is important because the M3U playlist and the HLS manifest serve different purposes.&lt;/p&gt;
&lt;p&gt;An M3U playlist might describe hundreds of channels:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;Channel A → stream-a.m3u8
Channel B → stream-b.m3u8
Channel C → stream-c.m3u8
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An HLS manifest, on the other hand, describes the media presentation itself.&lt;/p&gt;
&lt;p&gt;A master HLS playlist can reference different quality levels:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;1080p
720p
480p
360p
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The browser or playback library can then select an appropriate representation.&lt;/p&gt;
&lt;h3 id=&quot;why-use-javascript-to-build-an-iptv-player-&quot;&gt;Why Use JavaScript to Build an IPTV Player?&lt;/h3&gt;
&lt;p&gt;JavaScript provides everything needed to create the application layer of a basic IPTV web player.&lt;/p&gt;
&lt;p&gt;It can:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Fetch playlist files&lt;/li&gt;
&lt;li&gt;Parse text responses&lt;/li&gt;
&lt;li&gt;Extract channel metadata&lt;/li&gt;
&lt;li&gt;Build dynamic interfaces&lt;/li&gt;
&lt;li&gt;Handle user interaction&lt;/li&gt;
&lt;li&gt;Change the current stream&lt;/li&gt;
&lt;li&gt;Communicate with APIs&lt;/li&gt;
&lt;li&gt;Monitor playback events&lt;/li&gt;
&lt;li&gt;Display errors&lt;/li&gt;
&lt;li&gt;Store favorites&lt;/li&gt;
&lt;li&gt;Implement search and filtering&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The HTML5 &lt;code&gt;&amp;lt;video&amp;gt;&lt;/code&gt; element handles the actual media element, while JavaScript controls how the application interacts with it.&lt;/p&gt;
&lt;p&gt;This separation is important.&lt;/p&gt;
&lt;p&gt;You should think of the application as two layers:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;Application Layer
├── Playlist
├── Channels
├── Search
├── Categories
├── Favorites
└── User Interface

Playback Layer
├── HLS
├── M3U8
├── Video Element
├── Buffering
└── Media Events
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Keeping these responsibilities separate makes the project easier to extend later.&lt;/p&gt;
&lt;h3 id=&quot;step-1-create-the-basic-iptv-web-player-structure&quot;&gt;Step 1: Create the Basic IPTV Web Player Structure&lt;/h3&gt;
&lt;p&gt;Start with a simple HTML document:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang=&quot;en&quot;&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;meta charset=&quot;UTF-8&quot;&amp;gt;
  &amp;lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&amp;gt;
  &amp;lt;title&amp;gt;JavaScript IPTV Player&amp;lt;/title&amp;gt;
  &amp;lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;

  &amp;lt;main class=&quot;player-app&quot;&amp;gt;

    &amp;lt;section class=&quot;video-container&quot;&amp;gt;
      &amp;lt;video
        id=&quot;videoPlayer&quot;
        controls
        playsinline&amp;gt;
      &amp;lt;/video&amp;gt;
    &amp;lt;/section&amp;gt;

    &amp;lt;aside class=&quot;channel-panel&quot;&amp;gt;
      &amp;lt;input
        id=&quot;channelSearch&quot;
        type=&quot;search&quot;
        placeholder=&quot;Search channels...&quot;
      &amp;gt;

      &amp;lt;div id=&quot;channelList&quot;&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;/aside&amp;gt;

  &amp;lt;/main&amp;gt;

  &amp;lt;script src=&quot;app.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This gives us three important elements:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A video element&lt;/li&gt;
&lt;li&gt;A search field&lt;/li&gt;
&lt;li&gt;A dynamic channel container&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The JavaScript application will populate the channel list later.&lt;/p&gt;
&lt;p&gt;A basic stylesheet could be:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-css&quot;&gt;body {
  margin: 0;
  font-family: system-ui, sans-serif;
  background: #111;
  color: #fff;
}

.player-app {
  display: grid;
  grid-template-columns: 1fr 320px;
  min-height: 100vh;
}

.video-container {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
}

video {
  width: 100%;
  max-height: 90vh;
}

.channel-panel {
  padding: 20px;
  overflow-y: auto;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At this stage, there is no IPTV functionality yet.&lt;/p&gt;
&lt;p&gt;We have simply created the application shell.&lt;/p&gt;
&lt;h3 id=&quot;step-2-represent-iptv-channels-as-javascript-objects&quot;&gt;Step 2: Represent IPTV Channels as JavaScript Objects&lt;/h3&gt;
&lt;p&gt;Before connecting an external playlist, it is useful to understand the data model.&lt;/p&gt;
&lt;p&gt;A channel can be represented as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const channels = [
  {
    id: 1,
    name: &quot;Example News&quot;,
    category: &quot;News&quot;,
    logo: &quot;https://example.com/news.png&quot;,
    streamUrl: &quot;https://example.com/live/news/index.m3u8&quot;
  },
  {
    id: 2,
    name: &quot;Example Sports&quot;,
    category: &quot;Sports&quot;,
    logo: &quot;https://example.com/sports.png&quot;,
    streamUrl: &quot;https://example.com/live/sports/index.m3u8&quot;
  }
];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This structure is considerably easier to work with than raw playlist text.&lt;/p&gt;
&lt;p&gt;The application can now treat every channel as an object.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;console.log(channels[0].name);
console.log(channels[0].streamUrl);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result is:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;Example News
https://example.com/live/news/index.m3u8
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This leads to an important design principle:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Parse the playlist once, then work with structured data.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Do not repeatedly parse the entire M3U file every time the user clicks a channel.&lt;/p&gt;
&lt;h3 id=&quot;step-3-parse-an-m3u-playlist-with-javascript&quot;&gt;Step 3: Parse an M3U Playlist With JavaScript&lt;/h3&gt;
&lt;p&gt;Now we can implement a simple M3U parser.&lt;/p&gt;
&lt;p&gt;Consider this playlist:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;#EXTM3U

#EXTINF:-1,Example News
https://example.com/live/news/index.m3u8

#EXTINF:-1,Example Sports
https://example.com/live/sports/index.m3u8
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A basic parser can process the lines:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function parseM3U(content) {
  const lines = content
    .split(/\r?\n/)
    .map(line =&amp;gt; line.trim())
    .filter(Boolean);

  const channels = [];

  for (let i = 0; i &amp;lt; lines.length; i++) {
    const line = lines[i];

    if (line.startsWith(&quot;#EXTINF&quot;)) {
      const name = line.substring(
        line.lastIndexOf(&quot;,&quot;) + 1
      );

      const streamUrl = lines[i + 1];

      if (streamUrl &amp;amp;&amp;amp; !streamUrl.startsWith(&quot;#&quot;)) {
        channels.push({
          id: channels.length + 1,
          name,
          streamUrl
        });
      }
    }
  }

  return channels;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is intentionally simple.&lt;/p&gt;
&lt;p&gt;Real-world M3U playlists can contain additional attributes such as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;group-title
tvg-id
tvg-name
tvg-logo
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;#EXTINF:-1 tvg-id=&quot;news.example&quot; tvg-name=&quot;Example News&quot; tvg-logo=&quot;https://example.com/logo.png&quot; group-title=&quot;News&quot;,Example News
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A production parser should extract those fields rather than only reading the text after the comma.&lt;/p&gt;
&lt;h3 id=&quot;step-4-fetch-an-m3u-playlist&quot;&gt;Step 4: Fetch an M3U Playlist&lt;/h3&gt;
&lt;p&gt;Once the parser exists, JavaScript can retrieve a playlist using the Fetch API.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;async function loadPlaylist(url) {
  const response = await fetch(url);

  if (!response.ok) {
    throw new Error(
      `Playlist request failed: ${response.status}`
    );
  }

  const content = await response.text();

  return parseM3U(content);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;loadPlaylist(&quot;/playlists/channels.m3u&quot;)
  .then(channels =&amp;gt; {
    console.log(channels);
  })
  .catch(error =&amp;gt; {
    console.error(error);
  });
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is an important browser consideration here.&lt;/p&gt;
&lt;p&gt;If the playlist is hosted on another domain, the server must allow the browser application to access it through appropriate CORS configuration.&lt;/p&gt;
&lt;p&gt;For a streaming application you control, configure CORS at the server or CDN rather than attempting to bypass browser security from JavaScript.&lt;/p&gt;
&lt;p&gt;This is a common source of confusion when developing M3U playlist applications.&lt;/p&gt;
&lt;h3 id=&quot;step-5-render-the-iptv-channel-list&quot;&gt;Step 5: Render the IPTV Channel List&lt;/h3&gt;
&lt;p&gt;Once the playlist has been converted into JavaScript objects, the interface can be generated dynamically.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const channelList =
  document.getElementById(&quot;channelList&quot;);

function renderChannels(channels) {
  channelList.innerHTML = &quot;&quot;;

  channels.forEach(channel =&amp;gt; {
    const button = document.createElement(&quot;button&quot;);

    button.className = &quot;channel-item&quot;;
    button.textContent = channel.name;

    button.addEventListener(&quot;click&quot;, () =&amp;gt; {
      playChannel(channel);
    });

    channelList.appendChild(button);
  });
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This creates one button for each channel.&lt;/p&gt;
&lt;p&gt;The important part is that the UI is generated from data.&lt;/p&gt;
&lt;p&gt;That means the same interface can display 10 channels or 10,000 channels without manually writing HTML for every entry.&lt;/p&gt;
&lt;p&gt;A more advanced channel item could include:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;[Logo] Example News
       News
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The same principle applies.&lt;/p&gt;
&lt;h3 id=&quot;step-6-play-an-hls-stream-in-the-browser&quot;&gt;Step 6: Play an HLS Stream in the Browser&lt;/h3&gt;
&lt;p&gt;Now we reach the actual playback layer.&lt;/p&gt;
&lt;p&gt;Modern web browsers use different mechanisms for HLS playback depending on the platform.&lt;/p&gt;
&lt;p&gt;Some browsers provide native HLS support through the HTML5 video element. Others can use Media Source Extensions with a JavaScript playback library such as HLS.js.&lt;/p&gt;
&lt;p&gt;For a browser-compatible implementation, HLS.js can be loaded into the application.&lt;/p&gt;
&lt;p&gt;Using a module-based installation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;npm install hls.js
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;import Hls from &quot;hls.js&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The player logic can be structured as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const video = document.getElementById(
  &quot;videoPlayer&quot;
);

let hls = null;

function playChannel(channel) {

  if (hls) {
    hls.destroy();
    hls = null;
  }

  if (Hls.isSupported()) {

    hls = new Hls();

    hls.loadSource(channel.streamUrl);
    hls.attachMedia(video);

  } else if (
    video.canPlayType(
      &quot;application/vnd.apple.mpegurl&quot;
    )
  ) {

    video.src = channel.streamUrl;

  } else {

    showError(
      &quot;This browser does not support HLS playback.&quot;
    );

  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This pattern is useful because it handles both HLS.js playback and native HLS playback.&lt;/p&gt;
&lt;p&gt;HLS.js documents this feature-detection approach and uses Media Source Extensions for browsers where the required support is available. (&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/video-dev/hls.js?utm_source=chatgpt.com&quot;&gt;github.com&lt;/a&gt;)&lt;/p&gt;
&lt;h3 id=&quot;step-7-connect-the-playlist-to-the-player&quot;&gt;Step 7: Connect the Playlist to the Player&lt;/h3&gt;
&lt;p&gt;Now the main application can bring everything together.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;let allChannels = [];

async function initializePlayer() {
  try {

    allChannels = await loadPlaylist(
      &quot;/playlists/channels.m3u&quot;
    );

    renderChannels(allChannels);

  } catch (error) {

    showError(
      &quot;Unable to load the channel playlist.&quot;
    );

    console.error(error);
  }
}

initializePlayer();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The complete flow becomes:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;Page loads
    ↓
JavaScript starts
    ↓
Fetch M3U playlist
    ↓
Parse playlist
    ↓
Create channel objects
    ↓
Render channel list
    ↓
User selects channel
    ↓
Load M3U8 stream
    ↓
HLS.js / Native HLS
    ↓
HTML5 Video
    ↓
Live playback
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the basic architecture behind an IPTV web player.&lt;/p&gt;
&lt;h3 id=&quot;step-8-add-channel-search&quot;&gt;Step 8: Add Channel Search&lt;/h3&gt;
&lt;p&gt;A channel list becomes difficult to use when it contains hundreds or thousands of entries.&lt;/p&gt;
&lt;p&gt;Search is therefore one of the first features worth adding.&lt;/p&gt;
&lt;p&gt;The HTML:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;input
  id=&quot;channelSearch&quot;
  type=&quot;search&quot;
  placeholder=&quot;Search channels...&quot;
&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;JavaScript:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const searchInput =
  document.getElementById(&quot;channelSearch&quot;);

searchInput.addEventListener(&quot;input&quot;, () =&amp;gt; {

  const query =
    searchInput.value.toLowerCase().trim();

  const filtered = allChannels.filter(channel =&amp;gt;
    channel.name
      .toLowerCase()
      .includes(query)
  );

  renderChannels(filtered);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the user can type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;news
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and the interface will display only channels containing that term.&lt;/p&gt;
&lt;p&gt;For a large application, you could extend this to search:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Channel name&lt;/li&gt;
&lt;li&gt;Category&lt;/li&gt;
&lt;li&gt;Country&lt;/li&gt;
&lt;li&gt;EPG metadata&lt;/li&gt;
&lt;li&gt;Tags&lt;/li&gt;
&lt;li&gt;Language&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The key is to perform filtering against the structured channel data rather than reparsing the M3U file.&lt;/p&gt;
&lt;h3 id=&quot;step-9-add-categories-and-channel-metadata&quot;&gt;Step 9: Add Categories and Channel Metadata&lt;/h3&gt;
&lt;p&gt;M3U playlists often contain metadata that can improve the user experience.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;group-title=&quot;Sports&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;can be converted into:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;{
  name: &quot;Example Sports&quot;,
  category: &quot;Sports&quot;,
  streamUrl: &quot;...&quot;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The interface can then provide:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;All
News
Sports
Movies
Music
Kids
Documentary
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Selecting a category simply filters the channel array.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function filterByCategory(category) {

  if (category === &quot;All&quot;) {
    renderChannels(allChannels);
    return;
  }

  const filtered = allChannels.filter(
    channel =&amp;gt; channel.category === category
  );

  renderChannels(filtered);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This approach scales much better than building separate pages for every category.&lt;/p&gt;
&lt;h3 id=&quot;handling-hls-playback-errors&quot;&gt;Handling HLS Playback Errors&lt;/h3&gt;
&lt;p&gt;A serious JavaScript IPTV player needs error handling.&lt;/p&gt;
&lt;p&gt;Streaming failures can happen for reasons outside the frontend application.&lt;/p&gt;
&lt;p&gt;Possible causes include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Network interruption&lt;/li&gt;
&lt;li&gt;HTTP errors&lt;/li&gt;
&lt;li&gt;Missing media segments&lt;/li&gt;
&lt;li&gt;Invalid HLS manifests&lt;/li&gt;
&lt;li&gt;Unsupported codecs&lt;/li&gt;
&lt;li&gt;CORS configuration&lt;/li&gt;
&lt;li&gt;Expired URLs&lt;/li&gt;
&lt;li&gt;Server overload&lt;/li&gt;
&lt;li&gt;CDN problems&lt;/li&gt;
&lt;li&gt;Decoder failures&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;HLS.js provides error events that can be monitored.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;hls.on(Hls.Events.ERROR, (event, data) =&amp;gt; {

  console.error(
    &quot;HLS error:&quot;,
    data
  );

});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Fatal errors can be treated differently from recoverable problems.&lt;/p&gt;
&lt;p&gt;A simplified example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;hls.on(Hls.Events.ERROR, (event, data) =&amp;gt; {

  if (!data.fatal) {
    return;
  }

  switch (data.type) {

    case Hls.ErrorTypes.NETWORK_ERROR:
      hls.startLoad();
      break;

    case Hls.ErrorTypes.MEDIA_ERROR:
      hls.recoverMediaError();
      break;

    default:
      hls.destroy();
      break;
  }
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The appropriate recovery strategy depends on the application.&lt;/p&gt;
&lt;p&gt;Blindly retrying every request is not a good solution because it can create additional traffic while hiding an underlying server problem.&lt;/p&gt;
&lt;h3 id=&quot;understanding-iptv-buffering&quot;&gt;Understanding IPTV Buffering&lt;/h3&gt;
&lt;p&gt;Buffering is one of the most misunderstood parts of web video playback.&lt;/p&gt;
&lt;p&gt;A player may buffer because of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Insufficient bandwidth&lt;/li&gt;
&lt;li&gt;Network instability&lt;/li&gt;
&lt;li&gt;Slow server response&lt;/li&gt;
&lt;li&gt;CDN congestion&lt;/li&gt;
&lt;li&gt;Large segment sizes&lt;/li&gt;
&lt;li&gt;High bitrate&lt;/li&gt;
&lt;li&gt;Encoding problems&lt;/li&gt;
&lt;li&gt;Player buffer configuration&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The frontend is only one part of the equation.&lt;/p&gt;
&lt;p&gt;Consider the complete path:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;Encoder
   ↓
Streaming Packager
   ↓
Origin
   ↓
CDN
   ↓
Internet
   ↓
Browser
   ↓
JavaScript Player
   ↓
Video Element
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the origin server is slow, changing the JavaScript interface will not solve the problem.&lt;/p&gt;
&lt;p&gt;If the CDN is performing poorly, increasing frontend retries may make the situation worse.&lt;/p&gt;
&lt;p&gt;This is why video streaming problems should be diagnosed across the complete delivery pipeline.&lt;/p&gt;
&lt;h3 id=&quot;improving-iptv-channel-switching&quot;&gt;Improving IPTV Channel Switching&lt;/h3&gt;
&lt;p&gt;Fast channel switching is one of the most noticeable quality improvements in an IPTV web player.&lt;/p&gt;
&lt;p&gt;When a user selects another channel, the application should:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Stop the previous playback pipeline&lt;/li&gt;
&lt;li&gt;Release the old HLS instance when necessary&lt;/li&gt;
&lt;li&gt;Load the new manifest&lt;/li&gt;
&lt;li&gt;Attach the new media source&lt;/li&gt;
&lt;li&gt;Start playback when permitted by the browser&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A clean implementation prevents multiple HLS instances from remaining active.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function destroyPlayer() {

  if (hls) {
    hls.destroy();
    hls = null;
  }

  video.removeAttribute(&quot;src&quot;);
  video.load();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then before switching:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;destroyPlayer();
playChannel(channel);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is especially important for applications where users switch channels frequently.&lt;/p&gt;
&lt;h3 id=&quot;autoplay-restrictions-in-modern-browsers&quot;&gt;Autoplay Restrictions in Modern Browsers&lt;/h3&gt;
&lt;p&gt;Another issue developers often discover is that a player works when clicking a button but fails to start automatically.&lt;/p&gt;
&lt;p&gt;This is usually related to browser autoplay policies.&lt;/p&gt;
&lt;p&gt;Modern browsers can restrict autoplay, particularly when media includes audio.&lt;/p&gt;
&lt;p&gt;A reliable application should therefore design around user interaction.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;button.addEventListener(&quot;click&quot;, () =&amp;gt; {
  playChannel(channel);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The user's click provides a natural interaction point for starting playback.&lt;/p&gt;
&lt;p&gt;Do not assume that every browser will allow a newly loaded live stream to begin automatically with audio.&lt;/p&gt;
&lt;h3 id=&quot;building-a-more-maintainable-javascript-iptv-player&quot;&gt;Building a More Maintainable JavaScript IPTV Player&lt;/h3&gt;
&lt;p&gt;As the project grows, a single &lt;code&gt;app.js&lt;/code&gt; file can quickly become difficult to maintain.&lt;/p&gt;
&lt;p&gt;A better structure might be:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;src/
├── player/
│   ├── hlsPlayer.js
│   └── playbackEvents.js
│
├── playlist/
│   ├── parser.js
│   └── loader.js
│
├── channels/
│   ├── channelList.js
│   ├── search.js
│   └── categories.js
│
├── ui/
│   ├── notifications.js
│   └── loading.js
│
└── main.js
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This gives every part of the application a clear responsibility.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;parser.js&lt;/code&gt; handles M3U parsing.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;loader.js&lt;/code&gt; handles network requests.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;hlsPlayer.js&lt;/code&gt; handles playback.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;channelList.js&lt;/code&gt; handles channel rendering.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;search.js&lt;/code&gt; handles filtering.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;main.js&lt;/code&gt; connects everything together.&lt;/p&gt;
&lt;p&gt;This architecture also makes future migration to React or another framework easier.&lt;/p&gt;
&lt;h3 id=&quot;security-considerations-for-iptv-web-applications&quot;&gt;Security Considerations for IPTV Web Applications&lt;/h3&gt;
&lt;p&gt;A web player should never expose secrets in frontend JavaScript.&lt;/p&gt;
&lt;p&gt;Do not place:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;API keys&lt;/li&gt;
&lt;li&gt;Database passwords&lt;/li&gt;
&lt;li&gt;Private credentials&lt;/li&gt;
&lt;li&gt;Administrative tokens&lt;/li&gt;
&lt;li&gt;Origin server credentials&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;inside client-side code.&lt;/p&gt;
&lt;p&gt;Anything shipped to the browser should be considered visible to the user.&lt;/p&gt;
&lt;p&gt;If your application requires authentication, sensitive operations should be handled through a backend service.&lt;/p&gt;
&lt;p&gt;The frontend can receive a controlled response from the backend rather than directly exposing internal infrastructure.&lt;/p&gt;
&lt;p&gt;This is especially important when building commercial streaming applications.&lt;/p&gt;
&lt;h3 id=&quot;cors-and-m3u-playlist-access&quot;&gt;CORS and M3U Playlist Access&lt;/h3&gt;
&lt;p&gt;CORS deserves special attention because it is a common issue when loading external M3U playlists.&lt;/p&gt;
&lt;p&gt;Suppose your player is hosted at:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;https://player.example.com
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;while the playlist is hosted at:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;https://cdn.example.net/channels.m3u
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The browser treats these as different origins.&lt;/p&gt;
&lt;p&gt;If the server does not provide an appropriate CORS response, JavaScript may be prevented from reading the playlist.&lt;/p&gt;
&lt;p&gt;The same consideration can apply to HLS manifests and media resources depending on how the streaming infrastructure is configured.&lt;/p&gt;
&lt;p&gt;For infrastructure that you control, solve this at the server or CDN level.&lt;/p&gt;
&lt;p&gt;Do not attempt to defeat browser security restrictions with client-side workarounds.&lt;/p&gt;
&lt;h3 id=&quot;measuring-iptv-player-performance&quot;&gt;Measuring IPTV Player Performance&lt;/h3&gt;
&lt;p&gt;Once the player is working, performance should be measured rather than guessed.&lt;/p&gt;
&lt;p&gt;Useful metrics include:&lt;/p&gt;
&lt;h4 id=&quot;startup-time&quot;&gt;Startup time&lt;/h4&gt;
&lt;p&gt;How long does it take from selecting a channel until the first frame appears?&lt;/p&gt;
&lt;h4 id=&quot;channel-switching-time&quot;&gt;Channel switching time&lt;/h4&gt;
&lt;p&gt;How long does it take to move from one stream to another?&lt;/p&gt;
&lt;h4 id=&quot;rebuffering-ratio&quot;&gt;Rebuffering ratio&lt;/h4&gt;
&lt;p&gt;How frequently does playback stop because additional media is unavailable?&lt;/p&gt;
&lt;h4 id=&quot;manifest-loading-time&quot;&gt;Manifest loading time&lt;/h4&gt;
&lt;p&gt;How quickly does the browser receive the HLS manifest?&lt;/p&gt;
&lt;h4 id=&quot;segment-download-time&quot;&gt;Segment download time&lt;/h4&gt;
&lt;p&gt;How quickly can the player retrieve media segments?&lt;/p&gt;
&lt;h4 id=&quot;playback-errors&quot;&gt;Playback errors&lt;/h4&gt;
&lt;p&gt;How many sessions encounter fatal playback errors?&lt;/p&gt;
&lt;p&gt;These measurements help identify whether a problem originates from the frontend, network, CDN, or streaming infrastructure.&lt;/p&gt;
&lt;h3 id=&quot;common-mistakes-when-building-a-javascript-iptv-player&quot;&gt;Common Mistakes When Building a JavaScript IPTV Player&lt;/h3&gt;
&lt;h4 id=&quot;using-the-m3u-file-as-the-player-source&quot;&gt;Using the M3U file as the player source&lt;/h4&gt;
&lt;p&gt;The M3U playlist is generally a collection of metadata and stream references.&lt;/p&gt;
&lt;p&gt;The actual video player normally consumes an individual media stream, such as an HLS manifest.&lt;/p&gt;
&lt;h4 id=&quot;assuming-every-browser-supports-hls-in-the-same-way&quot;&gt;Assuming every browser supports HLS in the same way&lt;/h4&gt;
&lt;p&gt;Browser capabilities differ.&lt;/p&gt;
&lt;p&gt;Feature detection is safer than assuming universal support.&lt;/p&gt;
&lt;h4 id=&quot;ignoring-cleanup&quot;&gt;Ignoring cleanup&lt;/h4&gt;
&lt;p&gt;If every channel switch creates a new HLS instance without destroying the old one, unnecessary resources can remain active.&lt;/p&gt;
&lt;h4 id=&quot;loading-massive-playlists-unnecessarily&quot;&gt;Loading massive playlists unnecessarily&lt;/h4&gt;
&lt;p&gt;A very large playlist can increase startup time and memory usage.&lt;/p&gt;
&lt;p&gt;For large applications, consider pagination, server-side filtering, or an API that returns only the required channel data.&lt;/p&gt;
&lt;h4 id=&quot;putting-playlist-parsing-inside-the-video-component&quot;&gt;Putting playlist parsing inside the video component&lt;/h4&gt;
&lt;p&gt;Playlist parsing and media playback are separate responsibilities.&lt;/p&gt;
&lt;p&gt;Keep them independent.&lt;/p&gt;
&lt;h4 id=&quot;ignoring-network-errors&quot;&gt;Ignoring network errors&lt;/h4&gt;
&lt;p&gt;A video player should provide meaningful feedback when a stream cannot be loaded.&lt;/p&gt;
&lt;h4 id=&quot;testing-only-on-a-fast-connection&quot;&gt;Testing only on a fast connection&lt;/h4&gt;
&lt;p&gt;A player that works perfectly on a high-speed fiber connection may behave very differently on mobile or congested networks.&lt;/p&gt;
&lt;p&gt;Testing should include slower and unstable network conditions.&lt;/p&gt;
&lt;h3 id=&quot;taking-the-iptv-web-player-further&quot;&gt;Taking the IPTV Web Player Further&lt;/h3&gt;
&lt;p&gt;Once the basic JavaScript IPTV player is working, many additional features become possible.&lt;/p&gt;
&lt;h4 id=&quot;electronic-program-guide&quot;&gt;Electronic Program Guide&lt;/h4&gt;
&lt;p&gt;An EPG can add program information to the channel interface.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;Example News

18:00  Evening News
19:00  World Report
20:00  Documentary
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The EPG data can be retrieved from an API or another authorized data source.&lt;/p&gt;
&lt;h4 id=&quot;favorites&quot;&gt;Favorites&lt;/h4&gt;
&lt;p&gt;Favorites can be stored locally:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;localStorage.setItem(
  &quot;favorites&quot;,
  JSON.stringify(favorites)
);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For authenticated applications, favorites can instead be stored server-side.&lt;/p&gt;
&lt;h4 id=&quot;recently-watched-channels&quot;&gt;Recently watched channels&lt;/h4&gt;
&lt;p&gt;The application can keep track of recently selected channels and display them separately.&lt;/p&gt;
&lt;h4 id=&quot;responsive-design&quot;&gt;Responsive design&lt;/h4&gt;
&lt;p&gt;A desktop interface can use a two-column layout, while mobile devices can switch to:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;Video
↓
Channel Search
↓
Categories
↓
Channel List
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;keyboard-navigation&quot;&gt;Keyboard navigation&lt;/h4&gt;
&lt;p&gt;Accessibility should also be considered.&lt;/p&gt;
&lt;p&gt;Users should be able to navigate important controls without relying entirely on a mouse or touchscreen.&lt;/p&gt;
&lt;h3 id=&quot;from-javascript-prototype-to-production-iptv-platform&quot;&gt;From JavaScript Prototype to Production IPTV Platform&lt;/h3&gt;
&lt;p&gt;A prototype can be built entirely on the frontend, but a larger streaming platform will usually require backend services.&lt;/p&gt;
&lt;p&gt;A more complete architecture might look like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;                    ┌─────────────────┐
                    │ JavaScript Web  │
                    │     Player      │
                    └────────┬────────┘
                             │
                       HTTPS / API
                             │
                    ┌────────▼────────┐
                    │    Backend API  │
                    └────────┬────────┘
                             │
              ┌──────────────┼──────────────┐
              │              │              │
          Channels          EPG         User Data
              │              │              │
              └──────────────┼──────────────┘
                             │
                            CDN
                             │
                       Media Origin
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This architecture separates application data from media delivery.&lt;/p&gt;
&lt;p&gt;The frontend does not need to know every internal detail of the streaming infrastructure.&lt;/p&gt;
&lt;p&gt;Instead, it receives the information required to build the user experience.&lt;/p&gt;
&lt;p&gt;This pattern is much easier to maintain as the platform grows.&lt;/p&gt;
&lt;h3 id=&quot;why-m3u-hls-is-a-useful-developer-project&quot;&gt;Why M3U + HLS Is a Useful Developer Project&lt;/h3&gt;
&lt;p&gt;Building an IPTV web player may seem like a niche project, but it teaches several concepts that apply far beyond IPTV.&lt;/p&gt;
&lt;p&gt;You learn how to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Parse structured text&lt;/li&gt;
&lt;li&gt;Consume remote resources&lt;/li&gt;
&lt;li&gt;Work with asynchronous JavaScript&lt;/li&gt;
&lt;li&gt;Manage browser media APIs&lt;/li&gt;
&lt;li&gt;Handle network failures&lt;/li&gt;
&lt;li&gt;Build dynamic interfaces&lt;/li&gt;
&lt;li&gt;Design reusable components&lt;/li&gt;
&lt;li&gt;Work with HTTP streaming&lt;/li&gt;
&lt;li&gt;Debug playback problems&lt;/li&gt;
&lt;li&gt;Measure frontend performance&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The same skills are useful when building video-on-demand platforms, live event applications, media dashboards, educational platforms, and other streaming products.&lt;/p&gt;
&lt;p&gt;The IPTV use case simply provides a concrete environment where all these concepts come together.&lt;/p&gt;
&lt;h3 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;/h3&gt;
&lt;p&gt;Building an IPTV web player with JavaScript is a good practical exercise for developers who want to understand the relationship between frontend applications and modern video streaming.&lt;/p&gt;
&lt;p&gt;The basic architecture is straightforward:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;M3U
 ↓
JavaScript Parser
 ↓
Channel Data
 ↓
Web Interface
 ↓
M3U8 Stream
 ↓
HLS.js / Native HLS
 ↓
HTML5 Video
 ↓
Live Playback
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But a reliable application requires more than simply placing a stream URL inside a video element.&lt;/p&gt;
&lt;p&gt;You need to think about browser compatibility, playlist parsing, CORS, network failures, HLS errors, buffering, channel switching, autoplay policies, application architecture, and performance monitoring.&lt;/p&gt;
&lt;p&gt;The biggest lesson is that the player is only one part of the streaming system.&lt;/p&gt;
&lt;p&gt;When playback is slow or unstable, developers should investigate the complete chain from media encoding and packaging through the origin server and CDN to the browser.&lt;/p&gt;
&lt;p&gt;Starting with a small JavaScript prototype is therefore a good approach. Build playback first, then add playlist parsing, channel management, search, categories, error handling, and performance monitoring one step at a time.&lt;/p&gt;
&lt;p&gt;Once the fundamentals are working, the same architecture can evolve into a React application, a Smart TV interface, a mobile application, or a much larger streaming platform.&lt;/p&gt;
&lt;p&gt;For readers exploring the broader IPTV ecosystem, &lt;a rel=&quot;nofollow&quot; href=&quot;http://iptvfoxpro.com?utm_source=chatgpt.com&quot;&gt;IPTV FOX PRO&lt;/a&gt;, &lt;a rel=&quot;nofollow&quot; href=&quot;https://vastiptv.com?utm_source=chatgpt.com&quot;&gt;VAST IPTV&lt;/a&gt;, and &lt;a rel=&quot;nofollow&quot; href=&quot;https://foxiptvpremium.com?utm_source=chatgpt.com&quot;&gt;FOX IPTV Premium&lt;/a&gt; can be referenced as examples of IPTV-oriented services. These resources are supplementary to the technical discussion; the core of this guide remains the engineering behind playlist processing and browser-based video playback.&lt;/p&gt;
&lt;p&gt;The most important principle is simple: &lt;strong&gt;build the data layer, playback layer, and user interface as separate components.&lt;/strong&gt; Once those boundaries are clear, an IPTV web player becomes much easier to develop, debug, and scale.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/26427/9-steps-to-build-an-iptv-web-player-with-javascript-from-m3u-playlist-to-live-playback</guid>
<pubDate>Mon, 07 Sep 2026 16:58:04 +0000</pubDate>
</item>
<item>
<title>React Roadmap 2026 for Beginners</title>
<link>https://coderlegion.com/9349/react-roadmap-2026-for-beginners</link>
<description>&lt;p&gt;React remains one of the most in-demand frontend libraries and shows no signs of slowing down going into 2026 since it’s widely used for both small and large-scale applications.&lt;/p&gt;
&lt;p&gt;This roadmap is designed with a clear, modern learning path without getting overwhelmed and just with the essentials. It reflects current industry practices and focuses on skills that actually matter in real-world React projects. Let’s get started!&lt;/p&gt;
&lt;h3 id=&quot;1-javascript-fundamentals&quot;&gt;1. JavaScript Fundamentals&lt;/h3&gt;
&lt;p&gt;This one is a &lt;strong&gt;Non-Negotiable&lt;/strong&gt;. Before jumping into React, you need a solid understanding of &lt;strong&gt;modern Javascript&lt;/strong&gt;. React assumes you’re comfortable with &lt;em&gt;core JS concepts and ES6+ syntax&lt;/em&gt;. So you should focus on:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Variables (&lt;strong&gt;let&lt;/strong&gt;, &lt;strong&gt;const&lt;/strong&gt;)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Functions and arrow functions&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Conditionals and loops&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Objects&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Array Functions&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Object &amp;amp; array destructuring&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Spread and rest operators&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Optional chaining (&lt;strong&gt;?.&lt;/strong&gt;)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Asynchronous JavaScript (&lt;strong&gt;Promises, Async/Await&lt;/strong&gt;)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;These concepts will drive you directly to how React components manage data, render UI, and handle user interactions.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Some Recommended Resources from my blog:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://www.codeqazone.com/javascript-object-destructuring/&quot;&gt;Javascript Object Destructuring&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://www.codeqazone.com/complete-javascript-array-methods-guide/&quot;&gt;Complete Javascript Array Methods Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://www.codeqazone.com/must-known-javascript-array-methods/&quot;&gt;6 Must‑Know JS Array Methods&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-html-amp-css-fundamentals&quot;&gt;2. HTML &amp;amp; CSS Fundamentals&lt;/h3&gt;
&lt;p&gt;React doesn’t replace HTML or CSS, it &lt;strong&gt;builds on top of them&lt;/strong&gt;. That's why you should be comfortable with topics like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Semantic HTML&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Flexbox and Grid&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Responsive design&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For a styling approach, some CSS frameworks and tools you’ll find the most:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;strong&gt;Tailwind CSS&lt;/strong&gt; (very popular in modern projects)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;strong&gt;CSS Modules&lt;/strong&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Styled-components (still used, but no longer dominant)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You could also take a look at CSS frameworks, these can be really helpful, the one I will always recommend is &lt;a rel=&quot;nofollow&quot; href=&quot;https://getbootstrap.com/&quot;&gt;Bootstrap&lt;/a&gt; or its react version: &lt;a rel=&quot;nofollow&quot; href=&quot;https://react-bootstrap.github.io/&quot;&gt;React Bootstrap&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;The key is understanding &lt;strong&gt;how to style components&lt;/strong&gt;, not locking yourself into one tool.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;3-understanding-rest-apis&quot;&gt;3. Understanding REST APIs&lt;/h3&gt;
&lt;p&gt;Most React applications revolve around &lt;strong&gt;data from APIs&lt;/strong&gt;. That's why you should make sure to understand:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;What REST APIs are&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;HTTP methods (&lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Status codes (At least the most common use)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Fetching data and handling loading/error states&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It's also important to learn how to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Fetch data using &lt;strong&gt;fetch&lt;/strong&gt; or &lt;strong&gt;axios&lt;/strong&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Display API data in components&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Handle errors gracefully&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Recommended Resource from my blog:&lt;/strong&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.codeqazone.com/rest-api-http-methods/&quot;&gt;When to Use GET, POST, PUT, DELETE and PATCH in REST APIs&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;4-learning-react-fundamentals&quot;&gt;4. Learning React Fundamentals&lt;/h3&gt;
&lt;p&gt;Before jumping into frameworks or meta-tools, focus on just &lt;strong&gt;learning React itself&lt;/strong&gt;. Start with the official documentation, it’s well-maintained and beginner-friendly. Then, explore other tutorials that match your learning style. &lt;a rel=&quot;nofollow&quot; href=&quot;https://react.dev/learn&quot;&gt;Check the officila React Docs&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&quot;core-react-concepts-to-master&quot;&gt;Core React Concepts to Master&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Functional components&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;JSX&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Props&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Routing&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;State management with &lt;strong&gt;useState&lt;/strong&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Side effects with &lt;strong&gt;useEffect&lt;/strong&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Controlled inputs and forms&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Conditional rendering&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rendering lists&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Event handling&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Hooks (most importantly: &lt;strong&gt;useState, useEffect, useRef, useContext&lt;/strong&gt;)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Server-Side Rendering (SSR) (just the basics to start)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;5-typescript-strongly-recommended-&quot;&gt;5. TypeScript (Strongly Recommended)&lt;/h3&gt;
&lt;p&gt;TypeScript is no longer optional in most professional React environments. You don’t need to master it immediately, but you should understand:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Basic types&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Typing props and state&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Typing API responses&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;6-tooling&quot;&gt;6. Tooling&lt;/h3&gt;
&lt;p&gt;Make sure to make yourself familiar with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Version control with Git (super essential to learn the fundamentals like branching, clone, commit, push, pull, resolving merge conflicts)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;strong&gt;Vite&lt;/strong&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;npm&lt;/strong&gt;, &lt;strong&gt;yarn&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;Create React App (CRA) is deprecated and no longer recommended.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;7-get-your-hands-dirty-with-a-few-projects&quot;&gt;7. Get Your Hands Dirty with a Few Projects&lt;/h3&gt;
&lt;p&gt;Start small and grow gradually. Then move on to more complete apps like a full CRUD app with a database or local storage. If you want to go further, try building real-life projects like: A movie listing app, user registration form, a recipes search app, etc.&lt;/p&gt;
&lt;p&gt;Choose topics that genuinely interest you, it’ll keep you motivated and engaged and avoid getting stuck in tutorial hell, apply what you learn as you go. Going into the actual practice is what solidifies your skills and helps build a strong portfolio.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://www.codeqazone.com/free-public-apis-list/&quot;&gt;Check this post with free APIs for project&lt;/a&gt; you could for your next React project.&lt;/p&gt;
&lt;h3 id=&quot;8-bonus-graphql&quot;&gt;8. Bonus: GraphQL&lt;/h3&gt;
&lt;p&gt;GraphQL is powerful and used in many working places, but it’s &lt;strong&gt;not required for beginners&lt;/strong&gt;. Learn it if:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Your project or job requires it&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You already understand REST APIs well&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Think of GraphQL as an &lt;strong&gt;advanced tool&lt;/strong&gt;, not a prerequisite.&lt;/p&gt;
&lt;h3 id=&quot;9-bonus-testing-basics&quot;&gt;9. Bonus: Testing Basics&lt;/h3&gt;
&lt;p&gt;You don’t need to be a testing expert but understanding how to test components is increasingly expected in junior roles and an overall plus for you tech stack. So you should make sure to understand:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Why testing components matters&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;The role of tools like Jest and React Testing Library&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;final-thoughts-1&quot;&gt;Final Thoughts&lt;/h3&gt;
&lt;p&gt;Even when there is so much more to learn about React, you don’t need to learn everything at once. Focus on the &lt;strong&gt;fundamentals&lt;/strong&gt;, build real projects, and expand your toolset as needed.&lt;/p&gt;
&lt;p&gt;I hope this roadmap will be a good starting point for you and help you on your own journey. I made it based on my own experience and some research. If you know somebody whom this could be of help please share!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Happy learning and thank you for reading!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/9349/react-roadmap-2026-for-beginners</guid>
<pubDate>Thu, 08 Jan 2026 12:30:00 +0000</pubDate>
</item>
<item>
<title>6 Must-Know JavaScript Array Methods (With Simple Examples)</title>
<link>https://coderlegion.com/8215/6-must-know-javascript-array-methods-with-simple-examples</link>
<description>&lt;p&gt;Javascript provides us with many array methods but only a handful are used consistently in real-world code. So in this post, we will see the &lt;strong&gt;&lt;em&gt;6 most essential array methods&lt;/em&gt;&lt;/strong&gt;, along with simple diagrams and clear examples to make them easy to understand and apply.&lt;/p&gt;
&lt;h3 id=&quot;the-main-6-array-methods&quot;&gt;The Main 6 Array Methods&lt;/h3&gt;
&lt;p&gt;These are the methods you’ll encounter the most when transforming, filtering, looping, and searching through collections:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=7303654241288535080&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;which-method-should-you-use-&quot;&gt;Which Method Should You Use?&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;https://www.codeqazone.com/images/posts/js-arrays.png&quot; alt=&quot;Diagram illustrating JavaScript array methods and their use cases&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;method-by-method-breakdown&quot;&gt;Method-by-Method Breakdown&lt;/h3&gt;
&lt;h5 id=&quot;1-map-transform-each-value&quot;&gt;1. map() → Transform Each Value&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// map() takes your array, applies a transformation to every item, and returns a brand-new array
const numbers = [1, 2, 3];
const doubled = numbers.map(n =&amp;gt; n * 2); 
// Output: [2, 4, 6]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use it when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;You want the same number of elements&lt;/em&gt;  &lt;/li&gt;
&lt;li&gt;&lt;em&gt;You're transforming values: formatting text, adding flags, extracting fields, converting types,&lt;br&gt;
 etc.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h5 id=&quot;2-filter-keep-only-what-matches&quot;&gt;2. filter() → Keep Only What Matches&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// filter() returns a new array containing only the elements that satisfy a condition
const evens = [1, 2, 3, 4].filter(n =&amp;gt; n % 2 === 0);
// Output: [2, 4]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use it when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;You’re removing unwanted items&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You’re validating or sanitizing lists&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You need a subset of the original array&lt;/em&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;h5 id=&quot;3-reduce-accumulate-into-a-single-value&quot;&gt;3. reduce() → Accumulate Into a Single Value&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// reduce() takes each value and combines it into one final output: a number, an object, an array, or any other structure
const sum = [1, 2, 3, 4].reduce((acc, n) =&amp;gt; acc + n, 0);
// Output: 10
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use it when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;You want totals, minimums, maximums&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You’re building objects or maps&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You’re transforming data into a single result&lt;/em&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;h5 id=&quot;4-find-get-the-first-matching-element&quot;&gt;4. find() → Get the First Matching Element&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// find() returns the first element that matches your condition and stops early
const user = users.find(u =&amp;gt; u.id === 2);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use it when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;You expect only one match&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You want the actual object, not a filtered array&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You want the search to stop early once found&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If no match is found, &lt;code&gt;find()&lt;/code&gt; will return &lt;code&gt;undefined&lt;/code&gt;. It's essential to handle that case in your code.&lt;/p&gt;
&lt;h5 id=&quot;5-includes-check-existence&quot;&gt;5. includes() → Check Existence&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// includes() checks if a primitive value (string, number, boolean) exists inside an array
[1, 2, 3].includes(2); // true
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use it when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;You only need a yes/no answer&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You're checking for common values like IDs, tags, or flags&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;includes()&lt;/code&gt; uses strict equality (&lt;code&gt;===&lt;/code&gt;), so &lt;code&gt;['1', 2].includes(1)&lt;/code&gt; will return &lt;code&gt;false&lt;/code&gt; because &lt;code&gt;1&lt;/code&gt; is a string and &lt;code&gt;2&lt;/code&gt; is a number.&lt;/p&gt;
&lt;h5 id=&quot;6-foreach-loop-for-side-effects&quot;&gt;6. forEach() → Loop for Side Effects&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// Use it when you’re not transforming or filtering, just doing something with each value
[1, 2, 3].forEach(n =&amp;gt; console.log(n));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use it for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Logging&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Mutating existing objects&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;DOM updates&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;External calls (APIs, analytics, etc.)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;forEach()&lt;/code&gt; doesn't return anything, and it's not ideal for chaining or transformation. For those purposes, use &lt;code&gt;map()&lt;/code&gt; or &lt;code&gt;filter()&lt;/code&gt; instead.&lt;/p&gt;
&lt;h3 id=&quot;mutating-vs-non-mutating-methods&quot;&gt;Mutating vs Non-Mutating Methods&lt;/h3&gt;
&lt;p&gt;One of the biggest pitfalls for newer developers is knowing which methods modify the original array.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Category           &lt;/th&gt;&lt;th&gt;Methods                                                                &lt;/th&gt;&lt;th&gt;Mutates?&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;❌ &lt;strong&gt;Non-Mutating&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;, &lt;code&gt;slice&lt;/code&gt;, &lt;code&gt;concat&lt;/code&gt;                           &lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;✅ &lt;strong&gt;Mutating&lt;/strong&gt;     &lt;/td&gt;&lt;td&gt;&lt;code&gt;push&lt;/code&gt;, &lt;code&gt;pop&lt;/code&gt;, &lt;code&gt;shift&lt;/code&gt;, &lt;code&gt;unshift&lt;/code&gt;, &lt;code&gt;splice&lt;/code&gt;, &lt;code&gt;sort&lt;/code&gt;, &lt;code&gt;reverse&lt;/code&gt;, &lt;code&gt;fill&lt;/code&gt; &lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Why it matters:&lt;/strong&gt; Mutating methods can cause hard-to-debug side effects, especially in React or functional codebases. Make sure to use wisely and try to avoid mutating methods when working with immutable state.&lt;/p&gt;
&lt;h3 id=&quot;quick-tips-for-using-array-methods&quot;&gt;Quick Tips for Using Array Methods&lt;/h3&gt;
&lt;h5 id=&quot;1-method-chaining-your-superpower&quot;&gt;1. Method Chaining: Your Superpower&lt;/h5&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const total = items
  .filter(x =&amp;gt; x.active)
  .map(x =&amp;gt; x.value)
  .reduce((sum, v) =&amp;gt; sum + v, 0);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; You can combine multiple array methods to build concise and expressive code in a single line.&lt;/p&gt;
&lt;h5 id=&quot;2-understand-early-exit-behavior&quot;&gt;2. Understand Early-Exit Behavior&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;find()&lt;/code&gt; → &lt;strong&gt;stops early&lt;/strong&gt; once a match is found.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;includes()&lt;/code&gt; → &lt;strong&gt;stops early&lt;/strong&gt; once the element is found.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;forEach()&lt;/code&gt; and &lt;code&gt;map()&lt;/code&gt; → &lt;strong&gt;do NOT stop early&lt;/strong&gt;, they will iterate through the entire array.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;3-performance-basics&quot;&gt;3. Performance Basics&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;forEach&lt;/code&gt; → O(n)&lt;/strong&gt;   These methods have a linear time complexity and work well for most use cases.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;sort&lt;/code&gt; → O(n log n)&lt;/strong&gt;  Sorting can become expensive as the array grows, so be cautious when working with large datasets.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;reduce&lt;/code&gt; → O(n)&lt;/strong&gt;   This is an efficient way to reduce multiple operations into a single result, and can replace multiple loops for&lt;br&gt;
optimization.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Always be mindful of performance when working with large arrays. Methods like &lt;code&gt;map()&lt;/code&gt; and &lt;code&gt;filter()&lt;/code&gt; are great for clarity, but if you're processing extremely large datasets, consider performance trade-offs.&lt;/p&gt;
&lt;h3 id=&quot;additional-resources&quot;&gt;Additional Resources&lt;/h3&gt;
&lt;p&gt;For more in-depth information on each of the array methods covered, be sure to check out the official &lt;strong&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array&quot;&gt;MDN documentation for Array methods&lt;/a&gt;&lt;/strong&gt;. It’s an excellent resource for understanding the full range of methods available to you, their syntax, and any additional features or nuances you should be aware of.&lt;/p&gt;
&lt;h3 id=&quot;final-thoughts-2&quot;&gt;Final Thoughts&lt;/h3&gt;
&lt;p&gt;Mastering these array methods can level up your JavaScript skills and improve the quality of your code. Using these methods will make your code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;More expressive&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Less error-prone&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;More maintainable&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Easier to read&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By thinking about the intent behind each array method, you can choose the right tool for the job and make your code more efficient and predictable. If you haven't already, take the time to experiment with these methods in your projects to get comfortable with them.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Happy learning and thank you for reading!&lt;/em&gt;&lt;/p&gt;
</description>
<category>Frontend Dev</category>
<guid isPermaLink="true">https://coderlegion.com/8215/6-must-know-javascript-array-methods-with-simple-examples</guid>
<pubDate>Fri, 05 Dec 2025 23:48:42 +0000</pubDate>
</item>
<item>
<title>Learning Object Destructuring in JavaScript</title>
<link>https://coderlegion.com/6053/learning-object-destructuring-in-javascript</link>
<description>&lt;p&gt;&lt;strong&gt;Object destructuring&lt;/strong&gt; is a feature in &lt;strong&gt;JavaScript&lt;/strong&gt; that allows you to &lt;strong&gt;&lt;em&gt;extract values from objects&lt;/em&gt;&lt;/strong&gt; and assign them to variables in a more concise and readable way. &lt;/p&gt;
&lt;p&gt;This feature was introduced in &lt;a rel=&quot;nofollow&quot; href=&quot;http://es6-features.org/&quot;&gt;ES6&lt;/a&gt; and has since become a popular technique among JavaScript developers.&lt;/p&gt;
&lt;p&gt;In order to use object destructuring, you can use curly braces &lt;code&gt;{}&lt;/code&gt; to specify the properties you want to extract from an object. Let's take a look at some examples of how this incredible feature can be used:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. The basic Object Destructuring&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const person = { name: 'John', age: 30 };
const { name, age } = person;
console.log(name); // Output: John
console.log(age); // Output: 30
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we declare a &lt;code&gt;person&lt;/code&gt; object with &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; properties. We then use destructuring to extract these properties and assign them to variables with the same names. We can then use them to access the values of the properties.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Renaming variables&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const person = { name: 'John', age: 30 };
const { name: fullName, age: years } = person;
console.log(fullName); // Output: John
console.log(years); // Output: 30
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we use destructuring to extract the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; properties from the &lt;code&gt;person&lt;/code&gt; object. However, we also use the colon &lt;code&gt;:&lt;/code&gt; syntax to rename the variables to &lt;code&gt;fullName&lt;/code&gt; and &lt;code&gt;years&lt;/code&gt;. This allows us to use different variable names while still extracting the values we need.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Default values&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const person = { name: 'John' };
const { name, age = 30 } = person;
console.log(name); // Output: John
console.log(age); // Output: 30
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we use destructuring to extract the &lt;code&gt;name&lt;/code&gt; property from the &lt;code&gt;person&lt;/code&gt; object. We also declare a default value of &lt;code&gt;30&lt;/code&gt; for the &lt;code&gt;age&lt;/code&gt; variable in case it is not defined in the &lt;code&gt;person&lt;/code&gt; object. This ensures that our code does not throw an error if the &lt;code&gt;age&lt;/code&gt; property is missing.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. Dynamic property names&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const helloStr = &quot;Hello world!&quot;
const { length, [length-1]: last } = str
console.log(length)  // 12
console.log(last)  // &quot;!&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we use destructuring to dynamically assing &lt;code&gt;last&lt;/code&gt; as the new property name from the &lt;code&gt;helloStr&lt;/code&gt; variable in order to get the last character from the string.&lt;/p&gt;
&lt;h3 id=&quot;final-thoughts-3&quot;&gt;Final Thoughts&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Object Destructuring&lt;/strong&gt;&lt;/em&gt; is a powerful feature that will not only make you look cool but also will help you to write cleaner and more efficient code that will be easier to maintain and understand.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Happy learning and thank you for reading!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;If this article was helpful to you, check out more in my blog &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.codeqazone.com/categories/javascript/&quot;&gt;Code &amp;amp; QA Zone&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
</description>
<category>Objects</category>
<guid isPermaLink="true">https://coderlegion.com/6053/learning-object-destructuring-in-javascript</guid>
<pubDate>Sat, 04 Oct 2025 14:15:37 +0000</pubDate>
</item>
<item>
<title>How I load javascript?</title>
<link>https://coderlegion.com/5224/how-i-load-javascript</link>
<description>&lt;h3 id=&quot;intro-&quot;&gt;Intro.&lt;/h3&gt;
&lt;p&gt;It's based on an old article that I've read a while back about &lt;em&gt;bulletproof loading of webcomponents&lt;/em&gt; and written by &lt;a rel=&quot;nofollow&quot; href=&quot;http:////https://gomakethings.com/bulletproof-web-component-loading/&quot;&gt;Chris Go Make Things&lt;/a&gt;, someone who knows a lot more about webdesign as me.&lt;br&gt;
The article was about loading  loading js in the footer or in the head and using 'async/defer' .&lt;/p&gt;
&lt;h3 id=&quot;his-approach-&quot;&gt;His Approach.&lt;/h3&gt;
&lt;p&gt;His 'bulletproof' approach was by using document.readyState and DOMContentLoaded in his component classes. &lt;/p&gt;
&lt;h3 id=&quot;my-idea-&quot;&gt;My idea.&lt;/h3&gt;
&lt;p&gt;It might be bulletproof but complex and hard to maintain The way I do that is &lt;strong&gt;Simple&lt;/strong&gt;, &lt;strong&gt;Bulletproof&lt;/strong&gt;, &lt;strong&gt;Fast&lt;/strong&gt; and hardly needs any maintenance. &lt;/p&gt;
&lt;h3 id=&quot;how-i-load-js-then-&quot;&gt;How I load js then.&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;First step is to have an index.js within the scripts folder and that contains the following:&lt;/p&gt;
&lt;pre&gt;//the imports on top. 
(async ()=&amp;gt;{
    /** 
      This is the place where all the js logic ends up but in the right order!
   */
})();
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Second step, within your index.html this: &lt;/p&gt;
&lt;p&gt;&lt;em style=&quot;background:yellow; padding: 2px&quot;&gt;&lt;br&gt;
&amp;lt;script type='module' src='./path/to/scripts/index.js'&amp;gt; &amp;lt;/script&amp;gt;&lt;br&gt;
&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;explanation&quot;&gt;Explanation&lt;/h3&gt;
&lt;p&gt;At the first step, the code has been placed in an async (Immediately Invoked Function Expression)! That iife, is the final place where all my js stuff ends up. &lt;/p&gt;
&lt;h4 id=&quot;this-way-&quot;&gt;This way.&lt;/h4&gt;
&lt;p&gt;There is no need for using 'DOMContentLoaded' because the promise is 100% fulfilled before it's passed to my index.html. So what I get is a single substance that is complete and ready to use. &lt;/p&gt;
&lt;p&gt;Actually, I rarely look at it because there is no need for! &lt;/p&gt;
&lt;h4 id=&quot;note-&quot;&gt;Note!&lt;/h4&gt;
&lt;p&gt;Placing all js stuff in the right order within you index.js is important! Otherwise, things might give errors because of lacking dependenties.&lt;/p&gt;
&lt;p&gt;More about &lt;strong&gt;IIFE&lt;/strong&gt; at &lt;a rel=&quot;nofollow&quot; href=&quot;https://developer.mozilla.org/en-US/docs/Glossary/IIFE&quot;&gt;MDN(IIFE)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That's it!&lt;/p&gt;
</description>
<category>Frontend Dev</category>
<guid isPermaLink="true">https://coderlegion.com/5224/how-i-load-javascript</guid>
<pubDate>Sun, 07 Sep 2025 09:56:58 +0000</pubDate>
</item>
<item>
<title>What is Javascript and How It Powers Modern Websites</title>
<link>https://coderlegion.com/4623/what-is-javascript-and-how-it-powers-modern-websites</link>
<description>&lt;p&gt;Picture this: You click a button on a webpage, and absolutely nothing happens .&lt;/p&gt;
&lt;p&gt;There are no dropdown menus to explore, no fun animations to enjoy, and no validations on form submissions.&lt;/p&gt;
&lt;p&gt;Basically, a &lt;em&gt;lifeless piece of the internet&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;That's what the web would look like without Javascript. &lt;/p&gt;
&lt;h2 id=&quot;what-is-javascript-&quot;&gt;What is Javascript?&lt;/h2&gt;
&lt;p&gt;Javascript, often abbreviated as &quot;JS&quot;, is the wacky genius of modern web development.&lt;/p&gt;
&lt;p&gt;It's the magic wand  that transforms static, boring HTML and CSS websites into lively, interactive, and engaging experiences that are hard to stop scrolling through.&lt;/p&gt;
&lt;p&gt;Simply put, Javascript is a programming language that adds &lt;em&gt;interactivity, animations, and dynamic features&lt;/em&gt; to websites. Whether it's &lt;em&gt;a pop-up&lt;/em&gt; (), &lt;em&gt;a dropdown menu&lt;/em&gt; (), or &lt;em&gt;an animation that dances across your screen&lt;/em&gt;—&lt;strong&gt;Javascript is behind the curtain pulling the strings&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;It's one of the &lt;strong&gt;holy trinity of web development&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;HTML – gives structure&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;CSS – makes it pretty&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Javascript – brings it to life&lt;/em&gt;&lt;/strong&gt; ⚡&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It's primarily a &lt;strong&gt;&lt;em&gt;client-side language&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;event-driven&lt;/em&gt;&lt;/strong&gt;, and runs directly in your browser without requiring installation. Magic? Nope—just good old scripting.&lt;/p&gt;
&lt;p&gt;Technically:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It's &lt;strong&gt;interpreted&lt;/strong&gt; (runs directly, no compiling)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It's &lt;strong&gt;dynamically typed&lt;/strong&gt; (you don't have to declare types — more on this in a later article)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It uses &lt;strong&gt;prototypal inheritance&lt;/strong&gt; instead of classical OOP (we'll unpack this later — no stress if that sounds unfamiliar!)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It runs using something called a &lt;strong&gt;Javascript engine&lt;/strong&gt; inside your browser (don't worry, we'll explain what that means soon)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Oh, and it also works on the server side, but we'll dive into that later.&lt;/p&gt;
&lt;h2 id=&quot;-a-brief-history-of-javascript&quot;&gt;&lt;strong&gt; A Brief History of Javascript&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Back in &lt;strong&gt;&lt;em&gt;1995&lt;/em&gt;, Brendan Eich&lt;/strong&gt; developed Javascript in just &lt;strong&gt;10 days&lt;/strong&gt;. Yes, that's faster than most of us write a to-do list .&lt;/p&gt;
&lt;p&gt;Originally called &lt;strong&gt;Mocha&lt;/strong&gt; (mmm, coffee), then &lt;strong&gt;Live Script&lt;/strong&gt;, it was finally &lt;strong&gt;rebranded as Javascript&lt;/strong&gt; because, hey, Java was trendy at the time ☕.&lt;/p&gt;
&lt;p&gt;Despite the misleading name, &lt;strong&gt;Java ≠ Javascript&lt;/strong&gt;—that's like comparing ham to a hamster.&lt;/p&gt;
&lt;p&gt;Fast-forward to today. Javascript has grown from a toy language into one of the web's core technologies, alongside HTML and CSS. It is now &lt;strong&gt;&lt;em&gt;standardised under ECMAScript&lt;/em&gt;&lt;/strong&gt; and powers everything on the web except your morning coffee .&lt;/p&gt;
&lt;h2 id=&quot;-core-features-of-javascript&quot;&gt;&lt;strong&gt;✨ Core Features of Javascript&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Javascript isn't just pretty code—it's flexible, feisty, and full of surprises.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamically Typed&lt;/strong&gt; – like, whatever &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event-Driven&lt;/strong&gt; – reacts to user clicks, scrolls, and tantrums .&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fast&lt;/strong&gt; – thanks to powerful browser engines ⚡&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Versatile&lt;/strong&gt; – works on browsers, servers, phones, fridges (not kidding !)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;It Has Superpowers (via Ecosystem) -&lt;/strong&gt; There's a massive library of tools and frameworks. React, Vue, D3, Express, Socket.io — you're never alone in Javascript land .&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It's like that colleague who can debug complex code with ease, seamlessly integrate APIs, and always finds time to collaborate on projects. &lt;/p&gt;
&lt;h2 id=&quot;how-to-run-javascript-in-a-browser&quot;&gt;&lt;strong&gt;How to Run Javascript in a Browser&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Javascript is &lt;strong&gt;built right into all major browsers&lt;/strong&gt; (&lt;em&gt;Chrome, Firefox, Safari, Edge&lt;/em&gt;), like a secret snack drawer .&lt;/p&gt;
&lt;p&gt;You can run Javascript in the browser in a few simple ways:&lt;/p&gt;
&lt;h4 id=&quot;1-the-developer-console-for-quick-testing-&quot;&gt;&lt;strong&gt;1. The Developer Console (For Quick Testing)&lt;/strong&gt;&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open &lt;em&gt;Chrome&lt;/em&gt; (or any browser).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Press &lt;code&gt;F12&lt;/code&gt; or &lt;strong&gt;right-click → Inspect&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The browser's &lt;strong&gt;developer tools&lt;/strong&gt; will open up. Go to the &lt;strong&gt;Console Tab&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Type this, Hit Enter and watch the magic unfold ✨&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log(&quot;Hello, World! &quot;);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=12213951383119350445&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h4 id=&quot;2-add-it-directly-to-html&quot;&gt;&lt;strong&gt;2. Add it directly to HTML&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Use the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag inside your HTML file - &lt;code&gt;index.html&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-xml&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
    &amp;lt;body&amp;gt;
        &amp;lt;h1&amp;gt;Javascript Test&amp;lt;/h1&amp;gt;

        &amp;lt;script&amp;gt;
            alert(&quot;Hello, World! &quot;);
        &amp;lt;/script&amp;gt;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Open the &lt;code&gt;index.html&lt;/code&gt; file in your favorite browser.&lt;/p&gt;
&lt;p&gt;Boom! JS is live and will greet you like a chatty bot!.&lt;/p&gt;
&lt;h2 id=&quot;how-javascript-gets-executed&quot;&gt;&lt;strong&gt;How Javascript gets executed&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Before getting our hands dirty with Javascript code, it is better to have a basic understanding of how Javascript gets executed. ☄️&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Javascript alone is just syntax and logic&lt;/em&gt;; just like any other programming language, &lt;strong&gt;&lt;em&gt;a compiler or an interpreter is needed to execute Javascript&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Well, there are still ongoing debates about whether Javascript is a compiled language or an interpreted language. It would be &lt;strong&gt;&lt;em&gt;more accurate to say that Javascript in modern times is both compiled and interpreted, specifically through JIT (Just-In-Time) Compilation&lt;/em&gt;&lt;/strong&gt;. More on this later.&lt;/p&gt;
&lt;p&gt;To interact with web pages, forms, user events, or fetch data, it requires &lt;strong&gt;&lt;em&gt;a host&lt;/em&gt;&lt;/strong&gt; where &lt;strong&gt;&lt;em&gt;this JIT Compiler (a Javascript engine) is embedded&lt;/em&gt;&lt;/strong&gt;, much like the engine of a car—essential for driving the experience forward and making everything run smoothly. &lt;/p&gt;
&lt;h3 id=&quot;what-is-a-hosted-environment-&quot;&gt;&lt;strong&gt;What Is a Hosted Environment? ⚙️&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;A hosted environment — or, as seasoned developers call it, &lt;strong&gt;the runtime environment&lt;/strong&gt; — is the setup provided by the host system, &lt;strong&gt;&lt;em&gt;such as a web browser or Node.js&lt;/em&gt;&lt;/strong&gt;, that embeds and interacts with the &lt;strong&gt;&lt;em&gt;Javascript engine&lt;/em&gt;&lt;/strong&gt;. This environment offers some juicy extras — such as &lt;code&gt;fetch&lt;/code&gt;, &lt;code&gt;document&lt;/code&gt;, and &lt;code&gt;setTimeout&lt;/code&gt; — that the core Javascript language doesn't include.&lt;/p&gt;
&lt;h4 id=&quot;key-examples-of-hosted-environments-for-javascript&quot;&gt;&lt;strong&gt;Key Examples of Hosted Environments for Javascript&lt;/strong&gt;&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Web Browsers (Client-side Hosted Environment)&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DOM access&lt;/strong&gt;: via &lt;code&gt;document&lt;/code&gt;, &lt;code&gt;window&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Browser APIs&lt;/strong&gt;: like &lt;code&gt;fetch()&lt;/code&gt;, &lt;code&gt;alert()&lt;/code&gt;, &lt;code&gt;console.log()&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Timers&lt;/strong&gt;: &lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;setInterval&lt;/code&gt;. etc&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt; &lt;strong&gt;Characteristics:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Code runs in the &lt;strong&gt;&lt;em&gt;context of a webpage&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Controlled by the &lt;strong&gt;&lt;em&gt;browser's security and event loop&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can access and manipulate the &lt;strong&gt;&lt;em&gt;DOM&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Node.js (Server-side Hosted Environment)&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;File System:&lt;/strong&gt; via &lt;code&gt;fs&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Network:&lt;/strong&gt; &lt;code&gt;http&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Other APIs:&lt;/strong&gt; &lt;code&gt;process&lt;/code&gt;, &lt;code&gt;require&lt;/code&gt;, etc.&lt;/p&gt;
&lt;p&gt;  &lt;strong&gt;Characteristics:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Code runs &lt;strong&gt;&lt;em&gt;on a server&lt;/em&gt;&lt;/strong&gt;, often with access to the filesystem and network.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No DOM or browser-specific features.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Includes an &lt;strong&gt;&lt;em&gt;event-driven, non-blocking I/O model&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Other Examples&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deno:&lt;/strong&gt; Secure TypeScript/Javascript runtime with modern APIs.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Edge Functions / Serverless Platforms:&lt;/strong&gt; Cloud-hosted Javascript executed in response to HTTP requests.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;All major browsers have &lt;strong&gt;&lt;em&gt;Javascript engines built into them&lt;/em&gt;&lt;/strong&gt; (Chrome, Firefox, Safari, Edge)&lt;/p&gt;
&lt;p&gt;Some well-known JS engines are,&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chrome&lt;/strong&gt; → V8 Engine &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Firefox&lt;/strong&gt; → SpiderMonkey &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Safari&lt;/strong&gt; → JavaScriptCore &lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;what-happens-when-you-load-a-web-page-&quot;&gt;&lt;strong&gt;What happens when you load a web page?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;So, how exactly does Javascript run when you load a web page? &lt;/p&gt;
&lt;p&gt;Here's a simplified overview that doesn't require a PhD:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;You load a page&lt;/strong&gt; – Browser reads the HTML and finds a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Javascript engine&lt;/strong&gt; (like V8 in Chrome or SpiderMonkey in Firefox) kicks in.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The engine parses the code&lt;/strong&gt;, converts it into something called bytecode or machine code.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;It executes the code line by line&lt;/strong&gt;, handling events, functions, loops, and more.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It interacts with the browser's APIs, including the Document Object Model (DOM) and the Browser Object Model (BOM), to enable engaging functionality on the screen.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The way everything unfolds within the browser and our Javascript engine is a fascinating process, which we will explore in more detail in a future article.&lt;/p&gt;
&lt;h2 id=&quot;-how-to-add-javascript-to-a-website&quot;&gt;&lt;strong&gt; How to Add Javascript to a Website&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;There are three classic ways to add Javascript:&lt;/p&gt;
&lt;h4 id=&quot;-1-inline-javascript&quot;&gt;&lt;strong&gt; 1. Inline Javascript&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Directly inside HTML elements.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-xml&quot;&gt;&amp;lt;button onclick=&quot;alert('You clicked!')&quot;&amp;gt;Click me&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;✅ &lt;em&gt;Easy to test. ❌ Not scalable or maintainable&lt;/em&gt;&lt;/p&gt;
&lt;h4 id=&quot;-2-internal-scripts-in-tags&quot;&gt;&lt;strong&gt; 2. Internal Scripts in  Tags&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;In a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; in the HTML.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;&amp;lt;script&amp;gt;
    console.log(&quot;This runs when the page loads.&quot;);
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Great for small scripts during development.&lt;/em&gt;&lt;/p&gt;
&lt;h4 id=&quot;-3-external-javascript-files&quot;&gt;&lt;strong&gt; 3. External Javascript Files&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;In a separate HTML file - &lt;code&gt;index.html&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-xml&quot;&gt;&amp;lt;!-- In index.html - Withing &amp;lt;body&amp;gt; tag --&amp;gt;
&amp;lt;script src=&quot;script.js&quot; defer&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Inside&lt;/strong&gt; &lt;code&gt;script.js&lt;/code&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// script.js
console.log(&quot;This is coming from an external file.&quot;);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; Use defer to make sure the script runs &lt;em&gt;after&lt;/em&gt; the HTML has loaded.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Best practice&lt;/strong&gt;: Keeping your Javascript in a separate &lt;code&gt;.js&lt;/code&gt; file helps to create cleaner and easier-to-manage code. No one likes spaghetti code. &lt;/p&gt;
&lt;h2 id=&quot;-how-javascript-powers-modern-websites&quot;&gt;&lt;strong&gt;⚡ How Javascript Powers Modern Websites&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Javascript is &lt;em&gt;the heartthrob of modern web design&lt;/em&gt; ❤️. Well, yeah, no second thoughts on that.&lt;/p&gt;
&lt;p&gt;It's everywhere, like the air we breathe , and it's lightning-fast ⚡.&lt;/p&gt;
&lt;p&gt;It transforms static pages into &lt;strong&gt;&lt;em&gt;dynamic, interactive experiences&lt;/em&gt;&lt;/strong&gt;. From simple interactions like button clicks to full-scale SPAs (Single-Page Applications) that feel like native apps, Javascript is the engine driving modern UX.&lt;/p&gt;
&lt;p&gt;With powerful frameworks like &lt;em&gt;React, Vue, and Svelte&lt;/em&gt;, and back-end tools like &lt;em&gt;Node.js&lt;/em&gt;, Javascript isn't just hanging out in the browser anymore—it's running the whole show, front to back. There's literally a JS tool for everything.&lt;/p&gt;
&lt;p&gt;If modern web design were a kitchen, HTML would be the recipe, CSS the presentation on the plate, and Javascript the chef, making the magic happen in real-time. &lt;/p&gt;
&lt;h2 id=&quot;-client-side-vs-server-side-javascript&quot;&gt;&lt;strong&gt; Client-side vs Server-side Javascript&lt;/strong&gt;&lt;/h2&gt;
&lt;h3 id=&quot;client-side-browser-&quot;&gt;&lt;strong&gt;Client-Side (Browser)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;It runs on your Devices . It controls UI and responds to events.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt; - React, Vue, Angular, etc&lt;/p&gt;
&lt;h3 id=&quot;server-side-node-js-&quot;&gt;&lt;strong&gt;Server-Side (Node.js)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;It runs on a remote server ️. It handles data, APIs and back-end logic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt; - Express, NestJS, etc&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Core Javascript  lays the foundation, and all the frameworks and libraries ️ build on top of it.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;-challenges-and-limitations&quot;&gt;&lt;strong&gt;⚠️ Challenges and Limitations&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;It's not all sunshine and syntax.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Security&lt;/strong&gt; – Cross-site scripting can be nasty ️&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Browser quirks&lt;/strong&gt; – Some browsers love being &lt;em&gt;extra&lt;/em&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Messy code&lt;/strong&gt; – Write it clean or watch your app turn into spaghetti &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance issues:&lt;/strong&gt; Heavy scripts can slow your site down to turtle speed &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Debugging hell:&lt;/strong&gt; Sometimes finding that one missing semicolon feels like searching for a needle in a haystack &lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But hey, every hero has their flaws—Javascript's just keeping things spicy ️.&lt;/p&gt;
&lt;h2 id=&quot;-the-future-of-javascript&quot;&gt;&lt;strong&gt; The Future of Javascript&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Javascript isn't going anywhere except everywhere&lt;/em&gt;&lt;/strong&gt;. It's growing wiser, faster, and maybe just a little weirder (in a good way). Here's what's cooking:&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;Web Assembly (Wasm):&lt;/strong&gt; Javascript's hitting the gym. With Wasm, it's teaming up with languages like Rust and C++ to tackle tasks it was never built for, such as hardcore gaming, video editing, and heavy-duty number crunching — all in your browser. JS + Wasm = power couple of the web.&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;TypeScript:&lt;/strong&gt; Javascript's awkward teen years are over. TypeScript is helping it adult properly, giving it types, better tooling, and fewer &quot;undefined is not a function&quot; moments.&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;AI and Smart Tooling:&lt;/strong&gt; Development tools are becoming increasingly intelligent. Imagine writing code and your editor gently whispering, &quot;Hey, you forgot a semicolon… again.&quot; AI-assisted coding (hello, Copilot and beyond) is making Javascript dev life faster, easier, and slightly creepier.&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;Framework Wars 2.0:&lt;/strong&gt; React, Vue, Svelte, Solid, Qwik, and friends are all pushing the boundaries of fast, reactive UI. The future is about smarter rendering, less Javascript shipped to the browser, and fewer &quot;why is my page load 3MB&quot; moments.&lt;/p&gt;
&lt;p&gt;⚙️ &lt;strong&gt;Server-side Evolution:&lt;/strong&gt; Node.js, Deno, Bun — everyone's racing to make server-side Javascript faster, leaner, and cooler. Serverless functions and edge computing are making it possible to run Javascript anywhere... even on a fridge (probably).&lt;/p&gt;
&lt;p&gt; &lt;strong&gt;Universal Everything:&lt;/strong&gt; Full-stack Javascript is now the norm. Front-end, back-end, mobile apps, desktop apps, even IoT — all running Javascript. Pretty soon, your toaster  might be sending JSON logs  straight to your console ️—because even breakfast needs debugging .&lt;/p&gt;
&lt;h2 id=&quot;-conclusion&quot;&gt;&lt;strong&gt; Conclusion&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Javascript isn't just code&lt;/em&gt;&lt;/strong&gt;—it's the spark that makes the web come alive ⚡. Without it, your site is basically a mannequin in a shop window . But with Javascript? Boom—now you've got Iron Man flying across your screen .&lt;/p&gt;
&lt;p&gt;So go ahead, crack open that browser console .&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Build stuff ️. Break stuff . Fix stuff . Repeat .&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;That's the Javascript way .&lt;/p&gt;
</description>
<category>Getting Started</category>
<guid isPermaLink="true">https://coderlegion.com/4623/what-is-javascript-and-how-it-powers-modern-websites</guid>
<pubDate>Tue, 12 Aug 2025 12:21:13 +0000</pubDate>
</item>
<item>
<title>JavaScript Objects: Your First Step to Data Magic</title>
<link>https://coderlegion.com/4041/javascript-objects-your-first-step-to-data-magic</link>
<description>&lt;p&gt;Ever wondered how websites store and organize information like user profiles, product details, or your favorite playlist? The secret lies in JavaScript objects – powerful containers that make coding feel like organizing a well-structured toolbox. If you're new to programming, don't worry! By the end of this guide, you'll understand objects so well that you'll wonder how you ever coded without them.&lt;/p&gt;
&lt;h3 id=&quot;follow-me-for-more-content&quot;&gt;Follow Me for More Content&lt;/h3&gt;
&lt;p&gt;Before we dive in, let's connect! I regularly share beginner-friendly coding tips and tutorials:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Portfolio&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://abdelhakim-baalla.vercel.app/&quot;&gt;abdelhakim-baalla.vercel.app&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Instagram&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.instagram.com/abdelhakim.baalla/&quot;&gt;@abdelhakim.baalla&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Twitter&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://x.com/Abdelhakim99891&quot;&gt;@Abdelhakim99891&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LinkedIn&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.linkedin.com/in/abdelhakimbaalla/&quot;&gt;abdelhakimbaalla&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now, let's unlock the magic of JavaScript objects!&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;what-are-javascript-objects-&quot;&gt;What Are JavaScript Objects?&lt;/h3&gt;
&lt;p&gt;Think of a JavaScript object as a digital filing cabinet. Just like a real filing cabinet has labeled folders containing different documents, a JavaScript object has labeled properties containing different values.&lt;/p&gt;
&lt;p&gt;In the real world, you might describe a person like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Name: Abdelhakim Baalla&lt;/li&gt;
&lt;li&gt;Age: 19&lt;/li&gt;
&lt;li&gt;City: Agadir, Morocco&lt;/li&gt;
&lt;li&gt;Profession: Full Stack Developer&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In JavaScript, we can represent this same information using an object:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const person = {
  name: &quot;Abdelhakim Baalla&quot;,
  age: 19,
  city: &quot;Agadir, Morocco&quot;,
  profession: &quot;Full Stack Developer&quot;
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Objects are everywhere in JavaScript! They're the building blocks that help us organize and manage data efficiently.&lt;/p&gt;
&lt;h3 id=&quot;why-are-objects-so-important-&quot;&gt;Why Are Objects So Important?&lt;/h3&gt;
&lt;p&gt;Objects solve a fundamental problem in programming: &lt;strong&gt;organization&lt;/strong&gt;. Imagine trying to manage information about 100 users using separate variables:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// This is messy and hard to manage!
const user1Name = &quot;Alice&quot;;
const user1Age = 30;
const user1Email = &quot;*Emails are not allowed*&quot;;

const user2Name = &quot;Bob&quot;;
const user2Age = 25;
const user2Email = &quot;*Emails are not allowed*&quot;;
// ... and so on for 100 users!
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With objects, we can organize this data much more elegantly:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const users = [
  {
    name: &quot;Alice&quot;,
    age: 30,
    email: &quot;*Emails are not allowed*&quot;
  },
  {
    name: &quot;Bob&quot;,
    age: 25,
    email: &quot;*Emails are not allowed*&quot;
  }
  // Much cleaner and scalable!
];
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;creating-your-first-javascript-object&quot;&gt;Creating Your First JavaScript Object&lt;/h3&gt;
&lt;h4 id=&quot;method-1-object-literal-syntax-most-common-&quot;&gt;Method 1: Object Literal Syntax (Most Common)&lt;/h4&gt;
&lt;p&gt;The easiest way to create an object is using curly braces &lt;code&gt;{}&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const car = {
  brand: &quot;Toyota&quot;,
  model: &quot;Camry&quot;,
  year: 2023,
  color: &quot;blue&quot;,
  isRunning: false
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;method-2-using-the-object-constructor&quot;&gt;Method 2: Using the Object Constructor&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const car = new Object();
car.brand = &quot;Toyota&quot;;
car.model = &quot;Camry&quot;;
car.year = 2023;
car.color = &quot;blue&quot;;
car.isRunning = false;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;method-3-using-object-create-&quot;&gt;Method 3: Using Object.create()&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const car = Object.create(null);
car.brand = &quot;Toyota&quot;;
car.model = &quot;Camry&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Pro Tip&lt;/strong&gt;: Method 1 (object literal) is the most popular and readable approach among developers!&lt;/p&gt;
&lt;h3 id=&quot;understanding-object-properties&quot;&gt;Understanding Object Properties&lt;/h3&gt;
&lt;p&gt;Properties are the building blocks of objects. They consist of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Key&lt;/strong&gt; (property name): The label for the data&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Value&lt;/strong&gt;: The actual data stored&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const smartphone = {
  brand: &quot;Apple&quot;,        // Key: &quot;brand&quot;, Value: &quot;Apple&quot;
  model: &quot;iPhone 15&quot;,    // Key: &quot;model&quot;, Value: &quot;iPhone 15&quot;
  storage: 256,          // Key: &quot;storage&quot;, Value: 256
  hasCamera: true        // Key: &quot;hasCamera&quot;, Value: true
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;property-naming-rules&quot;&gt;Property Naming Rules&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Use camelCase for multi-word properties: &lt;code&gt;firstName&lt;/code&gt;, &lt;code&gt;phoneNumber&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Avoid spaces and special characters&lt;/li&gt;
&lt;li&gt;If you must use spaces, wrap in quotes: &lt;code&gt;&quot;phone number&quot;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const user = {
  firstName: &quot;Sarah&quot;,           // ✅ Good
  lastName: &quot;Johnson&quot;,          // ✅ Good
  &quot;phone number&quot;: &quot;123-456-7890&quot;, // ✅ Works but not recommended
  age: 28                       // ✅ Good
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;accessing-object-properties&quot;&gt;Accessing Object Properties&lt;/h3&gt;
&lt;h4 id=&quot;dot-notation-most-common-&quot;&gt;Dot Notation (Most Common)&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const book = {
  title: &quot;The Great Gatsby&quot;,
  author: &quot;F. Scott Fitzgerald&quot;,
  pages: 180,
  genre: &quot;Classic Literature&quot;
};

console.log(book.title);    // &quot;The Great Gatsby&quot;
console.log(book.author);   // &quot;F. Scott Fitzgerald&quot;
console.log(book.pages);    // 180
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;bracket-notation&quot;&gt;Bracket Notation&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log(book[&quot;title&quot;]);    // &quot;The Great Gatsby&quot;
console.log(book[&quot;author&quot;]);   // &quot;F. Scott Fitzgerald&quot;

// Useful when property names have spaces or special characters
const product = {
  &quot;product name&quot;: &quot;Laptop&quot;,
  &quot;price-usd&quot;: 999
};

console.log(product[&quot;product name&quot;]);  // &quot;Laptop&quot;
console.log(product[&quot;price-usd&quot;]);     // 999
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;dynamic-property-access&quot;&gt;Dynamic Property Access&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const student = {
  name: &quot;Emma&quot;,
  grade: &quot;A&quot;,
  subject: &quot;Mathematics&quot;
};

const propertyName = &quot;grade&quot;;
console.log(student[propertyName]); // &quot;A&quot;

// This won't work with dot notation
// console.log(student.propertyName); // undefined
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;modifying-object-properties&quot;&gt;Modifying Object Properties&lt;/h3&gt;
&lt;h4 id=&quot;adding-new-properties&quot;&gt;Adding New Properties&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const pet = {
  name: &quot;Buddy&quot;,
  type: &quot;dog&quot;
};

// Add new properties
pet.age = 3;
pet.breed = &quot;Golden Retriever&quot;;
pet[&quot;favorite toy&quot;] = &quot;Tennis ball&quot;;

console.log(pet);
// {
//   name: &quot;Buddy&quot;,
//   type: &quot;dog&quot;,
//   age: 3,
//   breed: &quot;Golden Retriever&quot;,
//   &quot;favorite toy&quot;: &quot;Tennis ball&quot;
// }
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;updating-existing-properties&quot;&gt;Updating Existing Properties&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const movie = {
  title: &quot;Inception&quot;,
  year: 2010,
  rating: 8.8
};

// Update properties
movie.rating = 9.0;
movie.director = &quot;Christopher Nolan&quot;;

console.log(movie.rating);   // 9.0
console.log(movie.director); // &quot;Christopher Nolan&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;deleting-properties&quot;&gt;Deleting Properties&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const account = {
  username: &quot;john_doe&quot;,
  password: &quot;secret123&quot;,
  email: &quot;*Emails are not allowed*&quot;,
  temporaryToken: &quot;abc123&quot;
};

// Delete the temporary token
delete account.temporaryToken;

console.log(account);
// {
//   username: &quot;john_doe&quot;,
//   password: &quot;secret123&quot;,
//   email: &quot;*Emails are not allowed*&quot;
// }
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;methods-in-objects&quot;&gt;Methods in Objects&lt;/h3&gt;
&lt;p&gt;Objects can also contain functions, which are called &lt;strong&gt;methods&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const calculator = {
  result: 0,
  
  add: function(number) {
    this.result += number;
    return this;
  },
  
  subtract: function(number) {
    this.result -= number;
    return this;
  },
  
  multiply: function(number) {
    this.result *= number;
    return this;
  },
  
  getResult: function() {
    return this.result;
  }
};

// Using the calculator
calculator.add(10).multiply(2).subtract(5);
console.log(calculator.getResult()); // 15
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;modern-method-syntax&quot;&gt;Modern Method Syntax&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const person = {
  name: &quot;Alice&quot;,
  age: 30,
  
  // Modern syntax (ES6+)
  greet() {
    return `Hello, I'm ${this.name}!`;
  },
  
  celebrateBirthday() {
    this.age++;
    return `Happy birthday! I'm now ${this.age} years old.`;
  }
};

console.log(person.greet()); // &quot;Hello, I'm Alice!&quot;
console.log(person.celebrateBirthday()); // &quot;Happy birthday! I'm now 31 years old.&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;understanding-this-in-objects&quot;&gt;Understanding 'this' in Objects&lt;/h3&gt;
&lt;p&gt;The keyword &lt;code&gt;this&lt;/code&gt; refers to the object that owns the method:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const restaurant = {
  name: &quot;Pizza Palace&quot;,
  location: &quot;Downtown&quot;,
  isOpen: true,
  
  getInfo() {
    return `${this.name} is located ${this.location} and is ${this.isOpen ? 'open' : 'closed'}`;
  },
  
  toggleStatus() {
    this.isOpen = !this.isOpen;
    return `${this.name} is now ${this.isOpen ? 'open' : 'closed'}`;
  }
};

console.log(restaurant.getInfo()); // &quot;Pizza Palace is located Downtown and is open&quot;
console.log(restaurant.toggleStatus()); // &quot;Pizza Palace is now closed&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;practical-examples&quot;&gt;Practical Examples&lt;/h3&gt;
&lt;h4 id=&quot;example-1-creating-a-user-profile&quot;&gt;Example 1: Creating a User Profile&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const userProfile = {
  firstName: &quot;Sarah&quot;,
  lastName: &quot;Johnson&quot;,
  email: &quot;*Emails are not allowed*&quot;,
  age: 28,
  preferences: {
    theme: &quot;dark&quot;,
    notifications: true,
    language: &quot;English&quot;
  },
  
  getFullName() {
    return `${this.firstName} ${this.lastName}`;
  },
  
  updateEmail(newEmail) {
    this.email = newEmail;
    return `Email updated to ${newEmail}`;
  },
  
  toggleNotifications() {
    this.preferences.notifications = !this.preferences.notifications;
    return `Notifications ${this.preferences.notifications ? 'enabled' : 'disabled'}`;
  }
};

console.log(userProfile.getFullName()); // &quot;Sarah Johnson&quot;
console.log(userProfile.updateEmail(&quot;*Emails are not allowed*&quot;)); // &quot;Email updated to *Emails are not allowed*&quot;
console.log(userProfile.toggleNotifications()); // &quot;Notifications disabled&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;example-2-building-a-shopping-cart&quot;&gt;Example 2: Building a Shopping Cart&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const shoppingCart = {
  items: [],
  total: 0,
  
  addItem(name, price, quantity = 1) {
    const item = {
      name: name,
      price: price,
      quantity: quantity,
      subtotal: price * quantity
    };
    
    this.items.push(item);
    this.calculateTotal();
    return `Added ${quantity} ${name}(s) to cart`;
  },
  
  removeItem(itemName) {
    this.items = this.items.filter(item =&amp;gt; item.name !== itemName);
    this.calculateTotal();
    return `Removed ${itemName} from cart`;
  },
  
  calculateTotal() {
    this.total = this.items.reduce((sum, item) =&amp;gt; sum + item.subtotal, 0);
  },
  
  getCartSummary() {
    return {
      itemCount: this.items.length,
      total: this.total,
      items: this.items
    };
  }
};

// Using the shopping cart
console.log(shoppingCart.addItem(&quot;Apple&quot;, 1.50, 3)); // &quot;Added 3 Apple(s) to cart&quot;
console.log(shoppingCart.addItem(&quot;Banana&quot;, 0.75, 2)); // &quot;Added 2 Banana(s) to cart&quot;
console.log(shoppingCart.getCartSummary());
// {
//   itemCount: 2,
//   total: 6,
//   items: [
//     { name: &quot;Apple&quot;, price: 1.50, quantity: 3, subtotal: 4.50 },
//     { name: &quot;Banana&quot;, price: 0.75, quantity: 2, subtotal: 1.50 }
//   ]
// }
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;nested-objects&quot;&gt;Nested Objects&lt;/h3&gt;
&lt;p&gt;Objects can contain other objects, creating powerful data structures:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const company = {
  name: &quot;Tech Solutions Inc.&quot;,
  founded: 2020,
  address: {
    street: &quot;123 Main St&quot;,
    city: &quot;San Francisco&quot;,
    state: &quot;CA&quot;,
    zipCode: &quot;94105&quot;
  },
  employees: [
    {
      name: &quot;Alice Johnson&quot;,
      position: &quot;Developer&quot;,
      salary: 75000,
      skills: [&quot;JavaScript&quot;, &quot;React&quot;, &quot;Node.js&quot;]
    },
    {
      name: &quot;Bob Smith&quot;,
      position: &quot;Designer&quot;,
      salary: 65000,
      skills: [&quot;Photoshop&quot;, &quot;Figma&quot;, &quot;CSS&quot;]
    }
  ],
  
  getCompanyInfo() {
    return `${this.name} was founded in ${this.founded} and is located in ${this.address.city}, ${this.address.state}`;
  },
  
  addEmployee(employee) {
    this.employees.push(employee);
    return `Added ${employee.name} as ${employee.position}`;
  }
};

console.log(company.getCompanyInfo()); // &quot;Tech Solutions Inc. was founded in 2020 and is located in San Francisco, CA&quot;
console.log(company.employees[0].skills[0]); // &quot;JavaScript&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;common-object-methods&quot;&gt;Common Object Methods&lt;/h3&gt;
&lt;h4 id=&quot;object-keys-&quot;&gt;Object.keys()&lt;/h4&gt;
&lt;p&gt;Get all property names:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const fruit = {
  name: &quot;Apple&quot;,
  color: &quot;red&quot;,
  taste: &quot;sweet&quot;,
  size: &quot;medium&quot;
};

const keys = Object.keys(fruit);
console.log(keys); // [&quot;name&quot;, &quot;color&quot;, &quot;taste&quot;, &quot;size&quot;]
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;object-values-&quot;&gt;Object.values()&lt;/h4&gt;
&lt;p&gt;Get all property values:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const values = Object.values(fruit);
console.log(values); // [&quot;Apple&quot;, &quot;red&quot;, &quot;sweet&quot;, &quot;medium&quot;]
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;object-entries-&quot;&gt;Object.entries()&lt;/h4&gt;
&lt;p&gt;Get key-value pairs:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const entries = Object.entries(fruit);
console.log(entries);
// [[&quot;name&quot;, &quot;Apple&quot;], [&quot;color&quot;, &quot;red&quot;], [&quot;taste&quot;, &quot;sweet&quot;], [&quot;size&quot;, &quot;medium&quot;]]
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;practical-use-case---iterating-through-an-object-&quot;&gt;Practical use case - iterating through an object:&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const scores = {
  Alice: 95,
  Bob: 87,
  Charlie: 92,
  Diana: 98
};

// Using Object.entries() to iterate
Object.entries(scores).forEach(([name, score]) =&amp;gt; {
  console.log(`${name}: ${score}%`);
});
// Alice: 95%
// Bob: 87%
// Charlie: 92%
// Diana: 98%
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;best-practices-for-working-with-objects&quot;&gt;Best Practices for Working with Objects&lt;/h3&gt;
&lt;h4 id=&quot;1-use-descriptive-property-names&quot;&gt;1. Use Descriptive Property Names&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// ❌ Not clear
const user = {
  n: &quot;John&quot;,
  a: 25,
  e: &quot;*Emails are not allowed*&quot;
};

// ✅ Clear and descriptive
const user = {
  name: &quot;John&quot;,
  age: 25,
  email: &quot;*Emails are not allowed*&quot;
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;2-group-related-properties&quot;&gt;2. Group Related Properties&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// ✅ Well-organized
const product = {
  id: 1,
  name: &quot;Laptop&quot;,
  
  // Group pricing information
  pricing: {
    cost: 800,
    retail: 1200,
    discount: 0.1
  },
  
  // Group specifications
  specs: {
    brand: &quot;Dell&quot;,
    processor: &quot;Intel i7&quot;,
    ram: &quot;16GB&quot;,
    storage: &quot;512GB SSD&quot;
  },
  
  // Group availability information
  availability: {
    inStock: true,
    quantity: 25,
    warehouse: &quot;West Coast&quot;
  }
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;3-use-methods-for-object-behavior&quot;&gt;3. Use Methods for Object Behavior&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const bankAccount = {
  balance: 1000,
  accountHolder: &quot;John Doe&quot;,
  
  // Methods for account operations
  deposit(amount) {
    if (amount &amp;gt; 0) {
      this.balance += amount;
      return `Deposited \({amount}. New balance: \){this.balance}`;
    }
    return &quot;Invalid deposit amount&quot;;
  },
  
  withdraw(amount) {
    if (amount &amp;gt; 0 &amp;amp;&amp;amp; amount &amp;lt;= this.balance) {
      this.balance -= amount;
      return `Withdrew \({amount}. New balance: \){this.balance}`;
    }
    return &quot;Invalid withdrawal amount or insufficient funds&quot;;
  },
  
  getBalance() {
    return `Current balance: $${this.balance}`;
  }
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;common-mistakes-to-avoid&quot;&gt;Common Mistakes to Avoid&lt;/h3&gt;
&lt;h4 id=&quot;1-confusing-dot-vs-bracket-notation&quot;&gt;1. Confusing Dot vs Bracket Notation&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const settings = {
  theme: &quot;dark&quot;,
  &quot;font-size&quot;: 16
};

// ❌ This won't work
// console.log(settings.font-size); // SyntaxError

// ✅ Use brackets for properties with hyphens
console.log(settings[&quot;font-size&quot;]); // 16
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;2-forgetting-this-in-methods&quot;&gt;2. Forgetting 'this' in Methods&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const counter = {
  count: 0,
  
  // ❌ Missing 'this'
  increment() {
    count++; // ReferenceError: count is not defined
  },
  
  // ✅ Using 'this' correctly
  increment() {
    this.count++;
  }
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;3-accidentally-creating-global-variables&quot;&gt;3. Accidentally Creating Global Variables&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// ❌ Missing 'const/let/var' creates global variable
person = {
  name: &quot;John&quot;
};

// ✅ Always declare objects properly
const person = {
  name: &quot;John&quot;
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;taking-your-skills-further&quot;&gt;Taking Your Skills Further&lt;/h3&gt;
&lt;p&gt;Now that you understand JavaScript objects, here are some next steps to continue your learning journey:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Practice Building Real Projects&lt;/strong&gt;: Create a todo list, address book, or simple game using objects&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Learn About Classes&lt;/strong&gt;: Objects and classes work together in modern JavaScript&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Explore JSON&lt;/strong&gt;: Learn how objects relate to JSON (JavaScript Object Notation)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Study Object-Oriented Programming&lt;/strong&gt;: Understand inheritance, encapsulation, and other OOP concepts&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dive into Advanced Topics&lt;/strong&gt;: Prototypes, destructuring, and spread operators&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Congratulations! You've just unlocked one of JavaScript's most powerful features. Objects are the foundation of modern web development, and mastering them opens doors to building amazing applications.&lt;/p&gt;
&lt;p&gt;Remember these key points:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Objects organize related data and functions together&lt;/li&gt;
&lt;li&gt;Use dot notation for simple property access&lt;/li&gt;
&lt;li&gt;Methods bring objects to life with behavior&lt;/li&gt;
&lt;li&gt;The 'this' keyword refers to the object itself&lt;/li&gt;
&lt;li&gt;Practice with real-world examples to solidify your understanding&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keep experimenting, keep coding, and most importantly, have fun with your newfound knowledge! JavaScript objects are your gateway to creating dynamic, interactive web applications.&lt;/p&gt;
&lt;h3 id=&quot;connect-amp-continue-learning&quot;&gt;Connect &amp;amp; Continue Learning&lt;/h3&gt;
&lt;p&gt;Ready to level up your JavaScript skills? I share more beginner-friendly tutorials and coding tips regularly:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Portfolio&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://abdelhakim-baalla.vercel.app/&quot;&gt;Visit My Portfolio&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LinkedIn&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.linkedin.com/in/abdelhakimbaalla/&quot;&gt;Connect with me&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Twitter&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://x.com/Abdelhakim99891&quot;&gt;Follow @Abdelhakim99891&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Instagram&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.instagram.com/abdelhakim.baalla/&quot;&gt;Follow @abdelhakim.baalla&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GitHub&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/Abdelhakim-Baalla&quot;&gt;Check out my code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dev.to&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://dev.to/abdelhakim_baalla&quot;&gt;Read more articles&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Medium&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://medium.com/@abdelhakimbaalla50&quot;&gt;Follow my blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;YouTube&lt;/strong&gt;: &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.youtube.com/@Dinocrafting&quot;&gt;Watch tutorials&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Found this helpful?&lt;/strong&gt; Give it a clap, share it with fellow developers, and let me know what you'd like to learn next in the comments!&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;em&gt;Happy coding!&lt;/em&gt;&lt;/p&gt;
</description>
<category>JavaScript</category>
<guid isPermaLink="true">https://coderlegion.com/4041/javascript-objects-your-first-step-to-data-magic</guid>
<pubDate>Tue, 15 Jul 2025 17:39:08 +0000</pubDate>
</item>
<item>
<title>The Unchaining: My Personal Journey Graduating from jQuery to Modern JavaScript</title>
<link>https://coderlegion.com/3104/the-unchaining-my-personal-journey-graduating-from-jquery-to-modern-javascript</link>
<description>&lt;p&gt;When I was building a quick frontend to the &lt;a rel=&quot;nofollow&quot; href=&quot;https://dev.to/jeffrey04/i-built-a-word-guessing-game-with-llm-357&quot;&gt;LLM game&lt;/a&gt;, I used &lt;a rel=&quot;nofollow&quot; href=&quot;https://jquery.com/&quot;&gt;jQuery&lt;/a&gt; to quickly whip out a prototype. Only after I was happy with it, I ported the code to the modern DOM API. As a result, I totally removed the dependency on jQuery. This whole experience makes me wonder, do people still use jQuery, in this age of frontend engineering? I took some time over the weekend to port one of &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/Jeffrey04/grid-maker&quot;&gt;my old jQuery plugins&lt;/a&gt;. This is not intended as a how-to (we have enough of them everywhere), but rather a reflection on how things has changed over time.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn-images-1.medium.com/max/1024/1*BIfHe74Cx5kZD5vAogeJww.png&quot; alt=&quot;&quot;&gt;&lt;br&gt;
&lt;em&gt;A cute illustration generated by Microsoft Copilot&lt;/em&gt;&lt;/p&gt;
&lt;h4 id=&quot;when-jquery-ruled-the-web-my-journey-begins&quot;&gt;When jQuery Ruled the Web: My Journey Begins&lt;/h4&gt;
&lt;p&gt;I joined web development in an era where people finally realized JavaScript can do more than just adding fun animations to a page. With the ability to send HTTP request, this opens up a whole new world of interactivity. This newly discovered magic extended the imagination boundary and inspired new dynamic designs.&lt;/p&gt;
&lt;p&gt;Previously, these kinds of interactivity could only be achieved with Macromedia/Adobe Flash/Shockwave, requiring a separate runtime installation.&lt;/p&gt;
&lt;p&gt;New design patterns emerged, and eventually led to the creation of new JavaScript framework and libraries. In my first job, I was introduced to &lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/YUI_Library&quot;&gt;Yahoo UI (YUI) library&lt;/a&gt; at work. jQuery came out slightly later, and it eventually became the most popular library at the time, likely due to the simplistic syntax and efficiency. Sizzle, the query engine powering the library, was touted the best and fastest at the time.&lt;/p&gt;
&lt;p&gt;Compared to the Java-like verbosity of YUI, jQuery was designed to get the same tasks done with less code. One thing I really like about the latter is the ability to chain function calls together.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$(element)
  .trigger(&quot;board:block:init:pre&quot;, [context, options])
  .addClass(&quot;block&quot;)
  .width(size)
  .height(size)
  .data(&quot;column&quot;, col_idx)
  .css({
    float: &quot;left&quot;,
    &quot;margin-right&quot;: options.margin_px + &quot;px&quot;,
    &quot;margin-bottom&quot;: options.margin_px + &quot;px&quot;,
    &quot;border-width&quot;: options.border_px + &quot;px&quot;,
    &quot;border-style&quot;: &quot;solid&quot;,
    &quot;border-color&quot;: options.border_color,
  })
  .trigger(&quot;board:block:init:post&quot;, [context, options]);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the snippet above, the chain does:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Trigger a custom event with type board:block:init:pre with additional arguments to be sent to the handler&lt;/li&gt;
&lt;li&gt;Add a class block to the selected element&lt;/li&gt;
&lt;li&gt;Set the width and height of the element&lt;/li&gt;
&lt;li&gt;Set a value to the data property named column&lt;/li&gt;
&lt;li&gt;Set the inline CSS style declaration&lt;/li&gt;
&lt;li&gt;Trigger another custom event with type board:block:init:post with additional arguments to be sent to the handler.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The compactness jQuery brings is still unrivalled, even with the much improved DOM API. We will briefly go through what jQuery does later, using my old jQuery plugin — grid-maker as an example.&lt;/p&gt;
&lt;p&gt;At one time, I was building a lot of mini web apps, and they all have one single common element — a grid. You might be wondering, why not Flexbox? It was new at the time, and it seemed to work well, but it also brought more complexity. Even now, I still don’t fully get it, though I completed &lt;a rel=&quot;nofollow&quot; href=&quot;https://flexboxfroggy.com/&quot;&gt;this cute gamified tutorial&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Anyway, after a few attempts, I revised the code, and decided to extract and reimplement the logic of building a responsive grid &lt;a rel=&quot;nofollow&quot; href=&quot;https://cslai.coolsilon.com/2016/06/10/generating-grids-in-javascript/&quot;&gt;as a jQuery plugin&lt;/a&gt;. And yes, this library is very well deprecated, so go use a proper CSS grid. You may read this article as a tribute to jQuery’s greatness.&lt;/p&gt;
&lt;h4 id=&quot;the-allure-of-chaining-how-jquery-simplified-my-development&quot;&gt;The Allure of Chaining: How jQuery Simplified My Development&lt;/h4&gt;
&lt;p&gt;Previously, we saw a snippet adapted from the grid-maker plugin. So let’s dive deeper into how do we start coding with jQuery. For DOM manipulation, we usually start by writing a query, following CSS selector rule. Say you want to look for the second div of class board in a div with class hello, you would need the selector div.hello div.board:nth-child(2). Pass the selector as string to the jQuery function (by default aliased to a dollar sign $) to get started, as shown below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$(&quot;div.hello div.board:nth-child(2)&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can do DOM manipulation to the returned jQuery object wrapping the matched element. It can now move around the tree of DOM elements or be populated with other elements. We also saw how the HTML attributes (data-column, class) and inline style could be modified. The one change per function call is likely following &lt;a rel=&quot;nofollow&quot; href=&quot;https://refactoring.guru/design-patterns/builder&quot;&gt;the builder pattern&lt;/a&gt;. Here comes my favourite feature — the chaining of function calls (many considered this a &lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/Fluent_interface&quot;&gt;Fluent Interface&lt;/a&gt;). Let’s say we want to adapt the creation of 10 rows from the grid-maker&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$(this)
  .append(
    $.map(_.range(options.row), function (row_idx) {
      return $(document.createElement(&quot;div&quot;))
    }),
  )
  .append(
    $(document.createElement(&quot;br&quot;)
      .css(&quot;clear&quot;, &quot;both&quot;)
  )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The last append() could be rewritten as follows to be more concise&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.append($.parseHTML('&amp;lt;br style=&quot;clear: both;&quot; /&amp;gt;'))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Just a side note, $.map functions similar to the new .map() function for arrays. In modern JavaScript the rough equivalent would be&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;_.range(10) // this underscore method returns a series of 10 numbers in array
  .map((row_idx) =&amp;gt; document.createElement(&quot;div&quot;));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Yes, the jQuery plugin was written in the pre-ES6 era.&lt;/p&gt;
&lt;p&gt;The grid-maker was made to be event-driven. Web applications were starting to get more complex (and hence the birth of more capable frameworks like React, Angular, Vue etc) at the time. As the complexity grew, keeping track of things became more difficult. And through the building of the mini web apps, I figured the best strategy would be delegating all the updates to the event listeners. Registering an event listener is easy, just tag a on() call to the chain. Let’s say we want to delegate the further initialization work of each created row to a function called row_init, we can do this&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.append(
    $.map(_.range(options.row), function (row_idx) {
      return $(document.createElement(&quot;div&quot;))
        .on(&quot;board:row:init&quot;, row_init)
    }),
  )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we could trigger the actual initialization with trigger(), additional data can be passed to the event handler in the second argument as a list:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.append(
    $.map(_.range(options.row), function (row_idx) {
      return $(document.createElement(&quot;div&quot;))
        .on(&quot;board:row:init&quot;, row_init)
        .trigger(&quot;board:row:init&quot;, [
          some,
          other,
          arguments
        ]);
    }),
  )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Though not present in the grid-maker, jQuery was also known for simplifying HTTP requests. It eventually introduced the Promise interface in later versions, similar to the one built into the modern JavaScript now. Yes, it is similar to the new FetchAPI.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$.ajax(&quot;https://example.com&quot;)
  .then(
    () =&amp;gt; { console.log(&quot;success&quot; },
    () =&amp;gt; { console.error(&quot;failure&quot; },
  )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next we will look at the porting process.&lt;/p&gt;
&lt;h4 id=&quot;side-by-side-unpacking-modern-js-vs-jquery-s-approach&quot;&gt;Side-by-Side: Unpacking Modern JS vs. jQuery’s Approach&lt;/h4&gt;
&lt;p&gt;For simpler web applications, I still prefer to keep it simple. Possibly due to my familiarity with jQuery, I still build my first iteration of work with it. Why is that the case? I hear you ask, but hopefully the reason becomes clear as we move along.&lt;/p&gt;
&lt;p&gt;We started by selecting an element, let’s emulate jQuery’s behavior, so the solution can be applied to not just 1, but possibly multiple matches. Let’s start with finding the list of elements using document.querySelectorAll, as shown below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const elems = document.querySelectorAll(&quot;div.hello div.board:nth-child(2)&quot;);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we need to loop through them, this is easy, just chuck everything into a loop:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;elems.forEach((board) =&amp;gt; { ... })
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For each matched board, we populate the rows&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;_.range(options.row).forEach((row_idx) =&amp;gt; {
  const elem = document.createElement(&quot;div&quot;);
  const br = document.createElement(&quot;br&quot;);
  br.style.clear = &quot;both&quot;;

  board.appendChild(elem);
  board.appendChild(br);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A reference of the row’s div element is kept for event registration purpose, to be shown later. The first major difference would be the losing of (subjectively) elegant chaining syntactic sugar. I also know I could use innerHTML property for the br tag, but I don’t like the idea of injecting raw HTML string into it.&lt;/p&gt;
&lt;p&gt;Now, let’s talk about custom events. I decided the library to be event-driven because I wanted to avoid tracing a long list of function calls when a change happens. This is particularly useful when you have multiple elements to be updated whenever something happens. Instead of chaining them together in the event handler, let each individual element subscribe to changes. As an additional benefit, you get a more testable code as the scope gets broken down into smaller units.&lt;/p&gt;
&lt;p&gt;Let’s start with event listeners, the equivalent of jQuery’s .on() method in JavaScript is .addEventListener().&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;elem.addEventListener(&quot;board:row:init&quot;, row_init);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In order to trigger the event manually, the equivalent of .trigger() would be the .dispatchEvent() method. However, this time, we would need to create a CustomEvent for the purpose. This is where you find the abstraction offered by jQuery valuable.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;elem.dispatchEvent(new CustomEvent(&quot;board:row:init&quot;));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We needed to pass additional argument just now, but unfortunately, it is done a little bit differently with vanilla JavaScript. Pass the additional data through event.detail by adding a second argument to the CustomEvent instantiation.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;elem.dispatchEvent(new CustomEvent(
    &quot;board:row:init&quot;,
    { detail: {
      some,
      other,
      arguments
    }}));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And event handler function would have to be declared differently, as shown below&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// jQuery version
function row_init(event, some, other, arguments) { ... }

// vanilla JavaScript version
function row_init(event) {
  const {some, other, arguments} = event.detail;
  ...
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The new syntactic sugar on object destructuring is used here to somewhat replicate the behaviour.&lt;/p&gt;
&lt;p&gt;The FetchAPI equivalent to $.ajax would be somewhat similar for people who are already familiar with jQuery’s Promise object.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fetch(&quot;https://example.com&quot;)
  .then(
    () =&amp;gt; { console.log(&quot;success&quot; },
    () =&amp;gt; { console.error(&quot;failure&quot; },
  )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Just for a visual comparison, the screenshot shows the difference of the two implementations.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn-images-1.medium.com/max/1024/1*gPLWwYgkur6pbw9hCMzMXA.png&quot; alt=&quot;&quot;&gt;&lt;br&gt;
&lt;em&gt;Code comparison&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Different formatters were used so the line count isn’t the best way to determine which is better. However, even with all the new API and syntactic sugar, the vanilla JavaScript version still feels a lot verbose. It is especially evident when it comes to event handling, with the explicit CustomEvent object instantiation. Also losing DOM manipulation methods like appendTo(), prepend(), prependTo() etc. will require developers to rethink the population sequence. Throughout the process of porting legacy code to modern JavaScript, I find this reference titled “&lt;a rel=&quot;nofollow&quot; href=&quot;https://youmightnotneedjquery.com/&quot;&gt;You might not need jQuery&lt;/a&gt;” useful, especially for people who are looking to “graduate” from jQuery and embrace the new APIs.&lt;/p&gt;
&lt;p&gt;There are certainly a lot more to cover when it comes to migrating away from jQuery, as shown in the linked website. The point of this exercise is more about the question we ask in the beginning — do people still code using jQuery now, with all these new toys brought by the recent updates to the API and language itself?&lt;/p&gt;
&lt;h4 id=&quot;a-coming-of-age-in-code-what-my-jquery-journey-taught-me&quot;&gt;A “Coming of Age” in Code: What My jQuery Journey Taught Me&lt;/h4&gt;
&lt;p&gt;As mentioned earlier, the idea of this article came from a realization of building prototype with jQuery by default. If I had kept the original revision done purely with jQuery, I wouldn’t have to port &lt;code&gt;grid-maker&lt;/code&gt; to prepare for this article. Having the original and ported code, and comparing them side-by-side is definitely a very interesting experience, considering the original code was written close to a decade ago.&lt;/p&gt;
&lt;p&gt;I am still in awe to see the original code still works fine in action today.&lt;/p&gt;
&lt;p&gt;Would this exercise change my habit of not starting with jQuery in the future? Likely not. Grabbing jQuery and start working almost feels like a muscle memory deeply ingrained in my body. Or, it could be just because it worked so well and rarely failed me. Heck the prototype application I built still works to this day, what more can I ask?&lt;/p&gt;
&lt;p&gt;On the other hand, I certainly see some footprints left by jQuery in the new APIs. They could all be purely coincidence, but it was indeed almost revolutionary at its prime.&lt;/p&gt;
&lt;p&gt;If I could make an analogy, I would see this article as a coming of age film, showing the process of us growing out of jQuery in the context of web development. It is still great, but it is likely time to move on. If you are new into the field, may this article serve as a decent introductory to the library.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;This article was shaped with the editorial assistance of Gemini. Rest assured, though, that the technical code and the voice telling this story are entirely my own. If my work resonates with you, I’d love to connect for job opportunities or project collaborations. You can reach me here on &lt;a rel=&quot;nofollow&quot; href=&quot;https://kitfucoda.medium.com&quot;&gt;Medium&lt;/a&gt; or find me on &lt;a rel=&quot;nofollow&quot; href=&quot;https://linkedin.com/in/jeffrey04/&quot;&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/3104/the-unchaining-my-personal-journey-graduating-from-jquery-to-modern-javascript</guid>
<pubDate>Tue, 20 May 2025 06:32:01 +0000</pubDate>
</item>
</channel>
</rss>