<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: vamstroy</title>
    <description>The latest articles on Forem by vamstroy (@vamstroy).</description>
    <link>https://forem.com/vamstroy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2287888%2F964546f3-360e-4068-bdea-105f759d0fa7.jpg</url>
      <title>Forem: vamstroy</title>
      <link>https://forem.com/vamstroy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vamstroy"/>
    <language>en</language>
    <item>
      <title>Top 5 Game-Changing JavaScript Features in ECMAScript 2024: Boost Your Code Like Never Before!</title>
      <dc:creator>vamstroy</dc:creator>
      <pubDate>Mon, 04 Nov 2024 18:37:20 +0000</pubDate>
      <link>https://forem.com/vamstroy/top-5-game-changing-javascript-features-in-ecmascript-2024-boost-your-code-like-never-before-ok6</link>
      <guid>https://forem.com/vamstroy/top-5-game-changing-javascript-features-in-ecmascript-2024-boost-your-code-like-never-before-ok6</guid>
      <description>&lt;h2&gt;
  
  
  1. Top-Level await – Async Simplified!
&lt;/h2&gt;

&lt;p&gt;Gone are the days when async code required wrapping everything in functions. With top-level await, we can directly use await in modules without needing an async function wrapper. This feature is particularly handy for simplifying code and reducing boilerplate&lt;/p&gt;

&lt;p&gt;Before top-level await, fetching data required an async function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function fetchData() {
  const response = await fetch('https://api.example.com/data');
  const data = await response.json();
  return data;
}
fetchData();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, with top-level await, we can call await at the root level:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const response = await fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes async code in modules more straightforward and readable, ideal for quick setup scripts or loading data at the start of an application.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Pipeline Operator (|&amp;gt;) – Cleaner Function Chaining
&lt;/h2&gt;

&lt;p&gt;The pipeline operator introduces a new way to chain functions in a readable format. It’s like visually “piping” data through a sequence of functions, making code flow naturally from left to right.&lt;/p&gt;

&lt;p&gt;Consider transforming a value through multiple functions without the pipeline operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const result = capitalize(square(double(value)));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the pipeline operator, the transformations are clearer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const result = value |&amp;gt; double |&amp;gt; square |&amp;gt; capitalize;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This change doesn’t just look cleaner; it’s also easier to follow and maintain, especially for functions with multiple transformation steps​&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Promise.withResolvers – Promises Made Easy
&lt;/h2&gt;

&lt;p&gt;Handling promises often requires creating a promise and then separately defining resolve and reject functions. Promise.withResolvers streamlines this by bundling the promise with resolve and reject handlers, making asynchronous workflows even cleaner.&lt;/p&gt;

&lt;p&gt;Before&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let resolve, reject;
const promise = new Promise((res, rej) =&amp;gt; {
  resolve = res;
  reject = rej;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Promise.withResolvers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { promise, resolve, reject } = Promise.withResolvers();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Records and Tuples – Immutable Data Structures
&lt;/h2&gt;

&lt;p&gt;Records and tuples bring immutability directly to JavaScript, letting you create data structures that can’t be changed after they’re created. Records work like immutable objects, while tuples function as fixed-length, immutable arrays. They make data integrity easier to maintain, reducing accidental changes&lt;/p&gt;

&lt;p&gt;Creating a record and a tuple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = #{ name: 'Alice', age: 30 };  // Record
const colors = #['red', 'green', 'blue'];    // Tuple

// Any attempt to modify throws an error
person.name = 'Bob'; // Error
colors.push('yellow'); // Error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This immutability makes records and tuples excellent for representing fixed data, like configurations or constants&lt;/p&gt;

&lt;h2&gt;
  
  
  5. String.toWellFormed and String.isWellFormed – Better Unicode Handling
&lt;/h2&gt;

&lt;p&gt;Unicode errors can crop up unexpectedly, especially when handling international text. String.toWellFormed and String.isWellFormed allow developers to check and convert strings for Unicode correctness. They help avoid rendering issues and data corruption in multilingual applications&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = 'Hello, \uD800'; // Lone surrogate
console.log(text.isWellFormed()); // false

const wellFormedText = text.toWellFormed();
console.log(wellFormedText); // Corrected Unicode text

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

&lt;/div&gt;



&lt;p&gt;These methods are essential for applications that require robust, global text handling, ensuring that strings are valid and well-formed for Unicode representation&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: Modern JavaScript, Now Even Better
&lt;/h2&gt;

&lt;p&gt;ECMAScript 2024 is packed with tools that bring ease, clarity, and reliability to JavaScript coding. From async simplification to immutability and better Unicode handling, these updates ensure that JavaScript continues to meet the demands of modern development. So, dive in, experiment with these features, and see how they transform your coding experience!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>learning</category>
      <category>career</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Why JavaScript Never Sleeps: An Easy Guide to the Event Loop</title>
      <dc:creator>vamstroy</dc:creator>
      <pubDate>Mon, 28 Oct 2024 19:02:24 +0000</pubDate>
      <link>https://forem.com/vamstroy/why-javascript-never-sleeps-an-easy-guide-to-the-event-loop-9hm</link>
      <guid>https://forem.com/vamstroy/why-javascript-never-sleeps-an-easy-guide-to-the-event-loop-9hm</guid>
      <description>&lt;p&gt;&lt;strong&gt;JavaScript as a Single-Threaded Language:&lt;/strong&gt; Briefly mention how JavaScript handles only one task at a time, but the Event Loop makes it seem otherwise&lt;/p&gt;

&lt;p&gt;. &lt;strong&gt;How the Event Loop Works:&lt;/strong&gt; The key steps in the Event Loop process &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stack:&lt;/strong&gt; The call stack manages synchronous code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Queue:&lt;/strong&gt; The callback queue holds tasks waiting to be executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Loop:&lt;/strong&gt; The Event Loop checks if the stack is empty before &lt;br&gt;
moving tasks from the queue to the stack.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7tpckidvcb299gqkcxn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7tpckidvcb299gqkcxn.png" alt="Image description" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Breakdown of the Workflow :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Initial Code Execution&lt;/strong&gt;:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('Hi');
$.get('url', function cb(data) {
  console.log(data);
});
console.log('JSConfEU');

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When this code starts executing, console.log('Hi') is added to the Call Stack, and "Hi" is printed in the Console.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Asynchronous Function Call ($.get):&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Next, $.get('url', function cb(data) { ... }) is encountered. This function initiates an HTTP request to retrieve data from 'url'.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instead of blocking the main thread, the request is sent to the Web APIs environment (often part of the browser).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The callback function (cb) is registered to execute once the HTTP request completes, but it's not executed immediately. The HTTP request continues to load in the Web APIs section, marked as XHR (XMLHttpRequest), which is typical for handling network requests.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Moving to the Next Synchronous Line:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;javaScript moves on to the next line, console.log('JSConfEU'), which is added to the Call Stack. "JSConfEU" is then printed to the Console.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Event Loop and Task Queue:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;After finishing all synchronous code, the Call Stack becomes empty, and JavaScript waits for any asynchronous tasks to complete.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the HTTP request completes, the callback function (cb) is moved from the Web APIs to the Task Queue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Event Loop continuously checks if the Call Stack is empty, and when it is, the Event Loop moves tasks from the Task Queue to the Call Stack&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Callback Execution:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the Event Loop places the callback function on the Call Stack, it executes console.log(data); within the callback function, printing the data received from the HTTP request to the Console.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;This flow demonstrates how the Event Loop handles asynchronous code &lt;br&gt;
 by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Processing synchronous code immediately on the Call Stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Handling asynchronous tasks in the Web APIs and, once completed, moving them to the Task Queue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Allowing the Event Loop to move tasks to the Call Stack only when it's empty, ensuring non-blocking code execution.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
