<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[A software architect and developer specializing in building exceptional digital experiences.]]></description><link>https://blog.luismarques.io</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1672684148109/PAEId0CmT.png</url><title>Untitled Publication</title><link>https://blog.luismarques.io</link></image><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 19:42:59 GMT</lastBuildDate><atom:link href="https://blog.luismarques.io/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Why 0.1 + 0.2 != 0.3 in JavaScript (And How to Fix It)]]></title><description><![CDATA[The IEEE 754 Standard
IEEE 754 is a standard for representing floating point numbers in computers. It is widely used in programming languages, including JavaScript, to represent decimal values with a finite precision.
One disadvantage with using floa...]]></description><link>https://blog.luismarques.io/why-01-02-03-in-javascript-and-how-to-fix-it</link><guid isPermaLink="true">https://blog.luismarques.io/why-01-02-03-in-javascript-and-how-to-fix-it</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[IEEE 754 ]]></category><dc:creator><![CDATA[Luís Marques]]></dc:creator><pubDate>Tue, 03 Jan 2023 19:12:57 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/hecib2an4T4/upload/6aee6d01d774d23768e30198e703a593.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-the-ieee-754-standard">The IEEE 754 Standard</h2>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/IEEE_754">IEEE 754</a> is a standard for representing floating point numbers in computers. It is widely used in programming languages, including JavaScript, to represent decimal values with a finite precision.</p>
<p>One disadvantage with using floating point numbers is that they cannot accurately represent all decimal values. For example, in JavaScript, the result of 0.1 + 0.2 is not equal to 0.3:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">0.1</span> + <span class="hljs-number">0.2</span>); <span class="hljs-comment">// Output: 0.30000000000000004</span>
</code></pre>
<p>This is because 0.1 and 0.2 cannot be accurately represented in the binary floating point format used by <a target="_blank" href="https://en.wikipedia.org/wiki/IEEE_754">IEEE 754</a>. When these numbers are converted to binary, they are rounded to the nearest approximation, which leads to the small error we see above.</p>
<p>This issue is not unique to JavaScript, but can occur in any programming language that uses the <a target="_blank" href="https://en.wikipedia.org/wiki/IEEE_754">IEEE 754 standard</a> for floating point numbers.</p>
<h2 id="heading-the-solutions-in-javascript">The Solutions in JavaScript</h2>
<p>The best solution for adding decimal values in JavaScript depends on your specific requirements. Here are a few options that may be suitable for different scenarios:</p>
<ol>
<li><p>If you require high precision and don't mind adding an external library to your project, you could use a library like <a target="_blank" href="https://www.npmjs.com/package/decimal.js/">decimal.js</a> or <a target="_blank" href="https://www.npmjs.com/package/bignumber.js/">bignumber.js</a>. These libraries provide data types that can represent decimal values with higher precision than the native floating point data type.</p>
</li>
<li><p>If you need a quick and simple solution and don't mind losing some precision, you could use the <code>toFixed</code> method to round the result to a specific number of decimal places.</p>
</li>
<li><p>If you need a solution that is both fast and accurate and don't mind doing extra calculations, you could use fixed-point arithmetic. Representing decimal values as integers, by scaling them up by a certain factor, can help to avoid precision errors.</p>
</li>
</ol>
<h3 id="heading-using-decimaljshttpswwwnpmjscompackagedecimaljs">Using <a target="_blank" href="https://www.npmjs.com/package/decimal.js/">decimal.js</a></h3>
<p>If you require great precision and don't mind using an external library in your project, this is a viable option.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> Decimal <span class="hljs-keyword">from</span> <span class="hljs-string">'decimal.js'</span>;

<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">new</span> Decimal(<span class="hljs-number">0.1</span>).plus(<span class="hljs-keyword">new</span> Decimal(<span class="hljs-number">0.2</span>)).toNumber()); <span class="hljs-comment">// Output: 0.3</span>
</code></pre>
<h3 id="heading-using-the-tofixed-or-mathround-method">Using the <code>toFixed</code> or <code>Math.round</code> method</h3>
<p>This is a quick and easy method that is adequate for many applications, but it may not be ideal if great accuracy is required.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log((<span class="hljs-number">0.1</span> + <span class="hljs-number">0.2</span>).toFixed(<span class="hljs-number">2</span>)); <span class="hljs-comment">// Output: 0.30</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Math</span>.round(<span class="hljs-number">0.1</span> + <span class="hljs-number">0.2</span>)) <span class="hljs-comment">// Output: 0</span>
</code></pre>
<h3 id="heading-using-fixed-point-arithmetic">Using fixed-point arithmetic</h3>
<p>This is appropriate for circumstances requiring both speed and accuracy. However, if you need to work with very large numbers, the scaling factor may cause the numbers to become too enormous to represent effectively.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> precision = <span class="hljs-number">1000</span> 
<span class="hljs-built_in">console</span>.log((<span class="hljs-number">0.1</span> * precision + <span class="hljs-number">0.2</span> * precision) / precision) <span class="hljs-comment">// Output: 0.3</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Finally, the ideal solution will be determined by your specific needs and the trade-offs you are ready to make.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding the Unique Features of JavaScript]]></title><description><![CDATA[Automatic Type Coercion
In JavaScript, type coercion refers to the process of changing a value from one data type to another. This can happen automatically in some instances, such as when using the == operator to compare two numbers. As an example:
c...]]></description><link>https://blog.luismarques.io/understanding-the-unique-features-of-javascript</link><guid isPermaLink="true">https://blog.luismarques.io/understanding-the-unique-features-of-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[javascript fundamentals]]></category><dc:creator><![CDATA[Luís Marques]]></dc:creator><pubDate>Tue, 27 Dec 2022 11:01:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/02b75abf03e367f9da79f3f65ca4b313.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-automatic-type-coercion">Automatic Type Coercion</h2>
<p>In JavaScript, type coercion refers to the process of changing a value from one data type to another. This can happen automatically in some instances, such as when using the <code>==</code> operator to compare two numbers. As an example:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span> == <span class="hljs-string">'1'</span>); <span class="hljs-comment">// Outputs: true</span>
</code></pre>
<p>Before the comparison, the integer <code>1</code> is automatically transformed to the string <code>'1'</code>. This might lead to unexpected results, like in the following example:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span> + <span class="hljs-string">'2'</span> + <span class="hljs-number">3</span>); <span class="hljs-comment">// Outputs: '123'</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span> + <span class="hljs-number">2</span> + <span class="hljs-string">'3'</span>); <span class="hljs-comment">// Outputs: '33'</span>
</code></pre>
<p>The number <code>1</code> is converted to a string in the first line before being concatenated with the string <code>'2'</code> and the number <code>3</code>. The numbers <code>1</code> and <code>2</code> are combined together in the second line before being converted to a string and concatenated with the string <code>'3'</code>.</p>
<p>It's a good practice to utilize the <code>===</code> operator, which does not apply type coercion, or explicitly convert values to the correct data type using methods such as <code>parseInt()</code> or <code>Number()</code>.</p>
<h2 id="heading-the-difference-between-null-and-undefined">The Difference Between <code>null</code> and <code>undefined</code></h2>
<p><code>null</code> indicates a null or empty value in JavaScript, whereas <code>undefined</code> describes the absence of a value. These two terms are frequently used interchangeably, however, they are not identical.</p>
<p>If you declare a variable but don't give it a value, it will be undefined:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> x;
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// Outputs: undefined</span>
</code></pre>
<p>If, on the other hand, you give the value <code>null</code> to a variable, it indicates the explicit lack of a value:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> y = <span class="hljs-literal">null</span>;
<span class="hljs-built_in">console</span>.log(y); <span class="hljs-comment">// Outputs: null</span>
</code></pre>
<p>It's essential to grasp the difference between <code>null</code> and <code>undefined</code>, since they might act differently in different scenarios.</p>
<h2 id="heading-the-difference-between-and">The Difference Between <code>==</code> and <code>===</code></h2>
<p>The <code>==</code> operator is known as the loose equality operator in JavaScript, whereas the <code>===</code> operator is known as the strict equality operator. The fundamental difference is that the loose equality operator conducts type coercion before comparison, while the strict equality operator does not.</p>
<p>For example:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span> == <span class="hljs-string">'1'</span>); <span class="hljs-comment">// Outputs: true</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span> === <span class="hljs-string">'1'</span>); <span class="hljs-comment">// Outputs: false</span>
</code></pre>
<p>The number <code>1</code> is automatically changed to the string <code>'1'</code> before the comparison in the first line, therefore the result is <code>true</code>. Since the strict equality operator is used in the second line, the comparison fails because the operands are of different types.</p>
<p>It's recommended to use the strict equality operator whenever possible to avoid unexpected results due to type coercion. However, there may be situations where you need to use the loose equality operator, such as when you want to compare the values of variables that may contain different data types.</p>
<h2 id="heading-function-hoisting">Function Hoisting</h2>
<p>Functions in JavaScript are "hoisted" to the top of the current scope, which means you can call them before they are declared. For developers who are unfamiliar with this behavior, this might be confusing.</p>
<p>For example:</p>
<pre><code class="lang-javascript">foo(); <span class="hljs-comment">// Outputs: 'foo'</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'foo'</span>);
}
</code></pre>
<p>The method <code>foo()</code> is defined after it is called in this scenario, yet it still executes appropriately due to function hoisting. The JavaScript interpreter essentially "hoists" the function definition to the top of the current scope, allowing it to be called from anywhere inside that scope.</p>
<p>Function hoisting can be confusing since it defies the conventional sequence of execution in most programming languages.</p>
<h2 id="heading-variable-scoping">Variable Scoping</h2>
<p>In JavaScript, variables are only accessible within the function in which they are defined, or within the global scope if they are not defined within a function. This is referred to as function-level scoping.</p>
<p>For example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> x = <span class="hljs-number">1</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> y = <span class="hljs-number">2</span>;
  <span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// Outputs: 1</span>
  <span class="hljs-built_in">console</span>.log(y); <span class="hljs-comment">// Outputs: 2</span>
}

<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// Outputs: 1</span>
<span class="hljs-built_in">console</span>.log(y); <span class="hljs-comment">// Outputs: Uncaught ReferenceError: y is not defined</span>
</code></pre>
<p>In this example, the variable <code>x</code> is defined in the global scope and is accessible both inside and outside the function <code>foo()</code>. On the other hand, the variable <code>y</code> is defined within the function <code>foo()</code> and is only accessible within that function.</p>
<p>Another example: If you define a variable with the same name as a global variable within a function, the global variable will be overwritten within the function, but will retain its original value outside the function.</p>
<h2 id="heading-the-global-object">The Global Object</h2>
<p>The global object is the <code>window</code> object in a web browser or the global object in Node.js and represents the global scope of your JavaScript code.</p>
<p>For example:</p>
<pre><code class="lang-javascript">x = <span class="hljs-number">1</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">window</span>.x); <span class="hljs-comment">// Outputs: 1</span>
</code></pre>
<p>In this example, the global object is the <code>window</code> object and the variable <code>x</code> is a property of the <code>window</code> object. This means that existing global variables and functions can be accessed without being declared first.</p>
<p>Modifying the global object is generally not recommended, as declaring a variable with the same name as a global object property will overwrite the property, potentially causing issues in your code.</p>
<h2 id="heading-the-this-keyword">The "this" Keyword</h2>
<p>In JavaScript, the "this" keyword represents the object that is executing the current function. However, the value of "this" can vary based on how the function is called, which can be confusing for developers.</p>
<p>For example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> obj = {
  <span class="hljs-attr">x</span>: <span class="hljs-number">1</span>,
  <span class="hljs-attr">y</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.x);
  }
};

obj.y(); <span class="hljs-comment">// Outputs: 1</span>

<span class="hljs-keyword">const</span> y = obj.y;
y(); <span class="hljs-comment">// Outputs: undefined</span>
</code></pre>
<p>In the first example, the function <code>y()</code> is called as a method of the obj object, so "this" refers to <code>obj</code> within the function. In the second example, the function is stored in a separate variable and called directly, resulting in "this" referring to the global object.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> obj = {
  <span class="hljs-attr">x</span>: <span class="hljs-number">1</span>,
  <span class="hljs-attr">y</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.x);
  }
};

obj.y(); <span class="hljs-comment">// Outputs: 1</span>

<span class="hljs-keyword">const</span> y = obj.y;
y(); <span class="hljs-comment">// Outputs: undefined</span>

<span class="hljs-keyword">const</span> z = {
  <span class="hljs-attr">x</span>: <span class="hljs-number">2</span>,
  <span class="hljs-attr">y</span>: y
};

z.y(); <span class="hljs-comment">// Outputs: 2</span>
</code></pre>
<p>In this example, the function <code>y()</code> is defined as a method of the <code>obj</code> object, and it logs the value of the x property of <code>obj</code>. When <code>y()</code> is called as a method of <code>obj</code>, "this" refers to <code>obj</code>, so the output is <code>1</code>.</p>
<p>However, when <code>y()</code> is stored in the variable <code>y</code> and called directly, "this" refers to the global object (or the window object in a web browser), which does not have an <code>x</code> property. As a result, the output is <code>undefined</code>.</p>
<p>Finally, when <code>y()</code> is called as a method of the <code>z</code> object, "this" refers to <code>z</code>, which has an <code>x</code> property with a value of <code>2</code>. As a result, the output is <code>2</code>.</p>
<p>One way to avoid issues with "this" is to use the <code>bind()</code> method to explicitly set the value of "this" within a function. As an example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> obj = {
  <span class="hljs-attr">x</span>: <span class="hljs-number">1</span>,
  <span class="hljs-attr">y</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.x);
  }
};

obj.y(); <span class="hljs-comment">// Outputs: 1</span>

<span class="hljs-keyword">const</span> y = obj.y.bind(obj);
y(); <span class="hljs-comment">// Outputs: 1</span>
</code></pre>
<h2 id="heading-asynchronous-programming">Asynchronous Programming</h2>
<p>In JavaScript, asynchronous programming allows for the execution of multiple tasks concurrently. This is achieved by using callback functions, which execute after a specific event has occurred.</p>
<p>For example:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Hello, world!'</span>);
}, <span class="hljs-number">1000</span>);
</code></pre>
<p>As an example, the <code>setTimeout()</code> function can be utilized to execute a callback function after a delay of 1000 milliseconds (1 second). This enables the JavaScript interpreter to continue executing other code during the delay period.</p>
<p>Asynchronous programming can be powerful, but can also be complex as it requires considering the flow of code differently. For instance, callback functions may be needed to ensure certain tasks are finished before others can start.</p>
<p>Promises, objects that represent the eventual completion or failure of an asynchronous operation, can be used to manage asynchronous actions in a more predictable and organized manner.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>JavaScript has a number of quirks and peculiarities that make it a distinct and often difficult language to deal with. However, learning how to traverse these intricacies will help you use JavaScript to develop powerful and engaging web apps.</p>
]]></content:encoded></item><item><title><![CDATA[How to Generate a Global Unique Identifier (UUID) with JavaScript]]></title><description><![CDATA[A UUID (Universal Unique Identifier) is a string of characters that is guaranteed to be unique across all devices and time. UUIDs are widely used to uniquely identify resources such as database entries or files.
What exactly is a UUID?
A UUID is a st...]]></description><link>https://blog.luismarques.io/how-to-generate-a-global-unique-identifier-uuid-with-javascript</link><guid isPermaLink="true">https://blog.luismarques.io/how-to-generate-a-global-unique-identifier-uuid-with-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[uuid]]></category><category><![CDATA[algorithm]]></category><dc:creator><![CDATA[Luís Marques]]></dc:creator><pubDate>Wed, 21 Dec 2022 23:49:40 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/74e5278a94e4941c80fcdd5cf105cd13.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A UUID (Universal Unique Identifier) is a string of characters that is guaranteed to be unique across all devices and time. UUIDs are widely used to uniquely identify resources such as database entries or files.</p>
<h2 id="heading-what-exactly-is-a-uuid">What exactly is a UUID?</h2>
<p>A UUID is a string of characters that follows a predefined standard. A UUID is written in the following format:</p>
<p><code>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</code></p>
<p>Each <code>x</code> above represents a hexadecimal digit (0-9, a-f). The UUID is composed of 32 hexadecimal digits divided by hyphens into five parts.</p>
<p>There are several versions of UUIDs, but the most common is version 4.</p>
<h2 id="heading-generating-a-uuid-in-javascript">Generating a UUID in JavaScript</h2>
<p>In JavaScript, there are numerous ways to generate a UUID. The built-in <code>Math.random()</code> function may be used to produce random hexadecimal digits, which can then be concatenated to form the UUID string.</p>
<h3 id="heading-generating-a-uuid-using-just-mathrandom">Generating a UUID using just <code>Math.random()</code></h3>
<p>The UUID is formed by concatenating 32 random hexadecimal digits generated by this function. The hyphens are then added to split the UUID into groups, as required by the UUID format.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">generateUUID</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Generate random hexadecimal digits</span>
  <span class="hljs-keyword">var</span> digits = <span class="hljs-string">"0123456789abcdef"</span>;
  <span class="hljs-keyword">var</span> n = digits.length;

  <span class="hljs-comment">// Generate random hexadecimal digits and concatenate them to form the UUID</span>
  <span class="hljs-keyword">var</span> uuid = <span class="hljs-string">""</span>;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">32</span>; i++) {
    uuid += digits[<span class="hljs-built_in">Math</span>.floor(<span class="hljs-built_in">Math</span>.random() * n)];
  }

  <span class="hljs-comment">// Add hyphens to the UUID to separate it into groups</span>
  uuid = uuid.substr(<span class="hljs-number">0</span>, <span class="hljs-number">8</span>) + <span class="hljs-string">"-"</span> + uuid.substr(<span class="hljs-number">8</span>, <span class="hljs-number">4</span>) + <span class="hljs-string">"-"</span> + uuid.substr(<span class="hljs-number">12</span>, <span class="hljs-number">4</span>) + <span class="hljs-string">"-"</span> + uuid.substr(<span class="hljs-number">16</span>, <span class="hljs-number">4</span>) + <span class="hljs-string">"-"</span> + uuid.substr(<span class="hljs-number">20</span>, <span class="hljs-number">12</span>);

  <span class="hljs-keyword">return</span> uuid;
}
</code></pre>
<h3 id="heading-generating-a-uuid-using-the-current-time-and-mathrandom">Generating a UUID using the Current Time and <code>Math.random()</code></h3>
<p>This function creates a new JavaScript Date object and gets the current time in milliseconds since 1970/01/01. It then defines a string template with x's and y's as placeholders, which will be replaced with random hexadecimal digits created using <code>Math.random()</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">generateUUID</span>(<span class="hljs-params"></span>) </span>{
   <span class="hljs-comment">// Get current time in milliseconds</span>
   <span class="hljs-keyword">var</span> d = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getTime();

   <span class="hljs-comment">// Define the UUID template with placeholder characters</span>
   <span class="hljs-keyword">var</span> uuid = <span class="hljs-string">"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"</span>;

   <span class="hljs-comment">// Replace the placeholders with random hexadecimal digits</span>
   uuid = uuid.replace(<span class="hljs-regexp">/[xy]/g</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">c</span>) </span>{
       <span class="hljs-comment">// Generate a random number between 0 and 15</span>
       <span class="hljs-keyword">var</span> r = (d + <span class="hljs-built_in">Math</span>.random()*<span class="hljs-number">16</span>)%<span class="hljs-number">16</span> | <span class="hljs-number">0</span>;

       <span class="hljs-comment">// Update value of d for the next placeholder</span>
       d = <span class="hljs-built_in">Math</span>.floor(d/<span class="hljs-number">16</span>);

       <span class="hljs-comment">// Convert the number to a hexadecimal digit and return it</span>
       <span class="hljs-keyword">return</span> (c==<span class="hljs-string">"x"</span> ? r : (r&amp;<span class="hljs-number">0x3</span>|<span class="hljs-number">0x8</span>)).toString(<span class="hljs-number">16</span>);
   });

   <span class="hljs-keyword">return</span> uuid;
}
</code></pre>
<h3 id="heading-generating-a-uuid-using-cryptogetrandomvalues">Generating a UUID using <code>crypto.getRandomValues()</code></h3>
<p>Another way to generate a UUID in JavaScript is to use the <code>crypto</code> module, which is available in modern browsers and Node.js. The crypto <code>module</code> provides a <code>crypto.getRandomValues()</code> function that generates cryptographically secure random values.</p>
<p>Here is an example of how to use the <code>crypto</code> module to generate a version 4 UUID:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">generateUUID</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Use the crypto module to generate a random array of bytes</span>
  <span class="hljs-keyword">var</span> array = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>(<span class="hljs-number">16</span>);
  crypto.getRandomValues(array);

  <span class="hljs-comment">// Convert the array of bytes to a hexadecimal string</span>
  <span class="hljs-keyword">var</span> uuid = <span class="hljs-string">""</span>;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; array.length; i++) {
    uuid += array[i].toString(<span class="hljs-number">16</span>).padStart(<span class="hljs-number">2</span>, <span class="hljs-string">"0"</span>);
  }

  <span class="hljs-comment">// Add hyphens to the UUID to separate it into groups</span>
  uuid = uuid.substr(<span class="hljs-number">0</span>, <span class="hljs-number">8</span>) + <span class="hljs-string">"-"</span> + uuid.substr(<span class="hljs-number">8</span>, <span class="hljs-number">4</span>) + <span class="hljs-string">"-"</span> + uuid.substr(<span class="hljs-number">12</span>, <span class="hljs-number">4</span>) + <span class="hljs-string">"-"</span> + uuid.substr(<span class="hljs-number">16</span>, <span class="hljs-number">4</span>) + <span class="hljs-string">"-"</span> + uuid.substr(<span class="hljs-number">20</span>, <span class="hljs-number">12</span>);

  <span class="hljs-keyword">return</span> uuid;
}
</code></pre>
<h3 id="heading-generating-a-uuid-using-cryptorandomuuid">Generating a UUID using <code>crypto.randomUUID()</code></h3>
<p>The <code>crypto</code> module also includes the <code>crypto.randomUUID()</code> method, which uses a cryptographically safe random number generator to produce a v4 UUID.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> uuid = crypto.randomUUID();
</code></pre>
<h3 id="heading-generating-a-uuid-using-the-uuid-npm-packagehttpswwwnpmjscompackageuuid">Generating a UUID using the <a target="_blank" href="https://www.npmjs.com/package/uuid">uuid npm package</a></h3>
<p>One of the preferred approaches is using a library called <code>uuid</code> to generate the v4 UUID:</p>
<pre><code class="lang-bash">npm install uuid
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { v4 <span class="hljs-keyword">as</span> uuidv4 } <span class="hljs-keyword">from</span> <span class="hljs-string">'uuid'</span>;
uuidv4(); <span class="hljs-comment">// ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>We looked at how to construct a Universal Unique Identifier (UUID) in JavaScript. We examined many approaches for generating UUIDs, including utilizing the built-in <code>Math.random()</code> function, the <code>crypto</code> module, and the existing <code>uuid</code> npm package.</p>
<p>Using <code>Math.random()</code> to produce a UUID is a simple and quick method, however the unpredictability of the UUID may not be adequate for some use cases, in which case you may add the current time to boost the uniqueness. Using the <code>crypto</code> module, on the other hand, provides a more safe and trustworthy approach to generate a UUID, but it is only accessible in modern browsers and Node.js.</p>
<p>Whichever approach you use, generating a UUID in JavaScript is a handy tool for uniquely identifying resources in your application.</p>
]]></content:encoded></item><item><title><![CDATA[Meteor.js vs React: Pros and Cons]]></title><description><![CDATA[When building modern web applications, developers have a multitude of libraries and frameworks to choose from. Two of the most widely used are Meteor.js and React. Both are powerful tools that can help you create fast, scalable, and efficient applica...]]></description><link>https://blog.luismarques.io/meteorjs-vs-react-pros-and-cons</link><guid isPermaLink="true">https://blog.luismarques.io/meteorjs-vs-react-pros-and-cons</guid><category><![CDATA[Meteor]]></category><category><![CDATA[React]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Luís Marques]]></dc:creator><pubDate>Tue, 13 Dec 2022 23:40:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/01e7b4d6f6173ac48d24916b19cbcf82.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When building modern web applications, developers have a multitude of libraries and frameworks to choose from. Two of the most widely used are Meteor.js and React. Both are powerful tools that can help you create fast, scalable, and efficient applications, however, they serve different purposes and have some key differences.</p>
<p>In this article, we'll look at the key differences between Meteor.js and React and clarify when to use each.</p>
<h2 id="heading-what-is-meteorjs"><strong>What is Meteor.js?</strong></h2>
<p>Meteor.js is a full-stack JavaScript framework for creating real-time web applications from start to finish. It offers a range of tools and libraries for front and back-end development, including a reactive data layer, build tool, and testing framework.</p>
<p>Meteor.js was designed to make it easy for developers to build real-time web applications with speed and efficiency. Its reactive data model ensures that the application's user interface is continuously updated anytime the database data changes, resulting in responsive and efficient applications that are always up-to-date with their data.</p>
<h2 id="heading-what-is-react"><strong>What is React?</strong></h2>
<p>React is a JavaScript library for building user interfaces that originated from Facebook. It enables the building of reusable UI components, simplifying the creation of complex and scalable applications.</p>
<p>React utilizes a virtual DOM (Document Object Model) to update the user interface efficiently. When data changes, React compares the new virtual DOM with the previous one and only updates the necessary parts of the user interface. This process is faster and more efficient than completely redrawing the entire user interface every time data changes.</p>
<h2 id="heading-key-differences-between-meteorjs-and-react"><strong>Key differences between Meteor.js and React</strong></h2>
<p>Now that we've covered the fundamentals of Meteor.js and React, let's look at some of their significant differences:</p>
<ol>
<li><p><strong>Full-stack vs. front-end:</strong> Meteor.js is a full-stack framework that includes everything needed to create a fully functional web app, including a server-side runtime, database, and a front-end library. Because React is a front-end framework for developing user interfaces, it must be used in conjunction with other tools and libraries to create a fully functional application.</p>
</li>
<li><p><strong>Reactive data model vs. virtual DOM:</strong> As previously stated, Meteor.js utilizes a reactive data model, which updates the user interface instantly as data changes. React, on the other hand, makes effective use of a virtual DOM to refresh the user interface. Both systems have advantages and limitations, but the reactive data model could be easier to use there is no need to manually update the interface.</p>
</li>
<li><p><strong>Ecosystem:</strong> Meteor.js has a smaller ecosystem, since it is a full-stack framework that includes everything needed to build a real-time web app. This can be seen as an advantage and disadvantage, as it offers less choice in terms of tools and libraries, but includes everything required in the framework. Because React is only a front-end framework, it has a broader ecosystem, but this implies that additional tools and libraries must be used and integrated to build a complete application.</p>
</li>
</ol>
<h2 id="heading-when-to-use-meteorjs"><strong>When to use Meteor.js</strong></h2>
<p>Meteor.js is a good choice if you're building a real-time web application, and want a full-stack framework that includes everything you need to get started. Its reactive data model makes it easy to build responsive and efficient applications, and its build tool and testing framework make development and deployment simple.</p>
<p>Meteor.js is a wonderful solution for quickly building a prototype or MVP because of its full-stack nature, which allows for the development of an application from start to finish without the need to integrate several tools and libraries.</p>
<p>On the other hand, Meteor.js may not be the best answer for larger and more complicated apps with many components. Its smaller ecosystem may restrict your options for libraries and tools, also, you may need to use workarounds to accomplish certain functionalities.</p>
<h2 id="heading-when-to-use-react"><strong>When to use React</strong></h2>
<p>If you're creating a user interface for a web app and want a framework that makes it simple to create reusable components, React is a great option. Its virtual DOM makes it efficient and speedy, and its big ecosystem provides a diverse set of tools and libraries.</p>
<p>Also, the vast ecosystem provides you with a plethora of alternatives when it comes to picking the best tools for the job.</p>
<p>On the other hand, React may not be the best choice for building real-time web apps, or when a full-stack framework that includes everything is preferred.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>To summarize, Meteor.js and React are both powerful tools for building web applications, but they serve different purposes and have some key differences. Meteor.js is a full-stack framework that includes everything you need to build a real-time web application, while React is a front-end library that makes it easy to build reusable UI components.</p>
<p>When deciding which one to use, consider the type of application you're building, the size and complexity of the application, and your preferences and experience. Both Meteor.js and React can help you build fast, scalable, and efficient applications, but they each have their strengths and limitations.</p>
]]></content:encoded></item></channel></rss>