<?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>DEV Community: Joshua Ng'ang'a Raphael</title>
    <description>The latest articles on DEV Community by Joshua Ng'ang'a Raphael (@joshraphael).</description>
    <link>https://dev.to/joshraphael</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%2F1166405%2F5a1de7bb-c5a7-4202-bb4b-6188b3a8d9bf.jpg</url>
      <title>DEV Community: Joshua Ng'ang'a Raphael</title>
      <link>https://dev.to/joshraphael</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joshraphael"/>
    <language>en</language>
    <item>
      <title>A case of no value vs no object: Comparing 'null', 'undefined' and 'undeclared'.</title>
      <dc:creator>Joshua Ng'ang'a Raphael</dc:creator>
      <pubDate>Fri, 15 Mar 2024 13:48:37 +0000</pubDate>
      <link>https://dev.to/joshraphael/a-case-of-no-value-vs-no-object-comparing-null-undefined-and-undeclared-419g</link>
      <guid>https://dev.to/joshraphael/a-case-of-no-value-vs-no-object-comparing-null-undefined-and-undeclared-419g</guid>
      <description>&lt;p&gt;&lt;strong&gt;Weird JavaScript&lt;/strong&gt;&lt;br&gt;
Often times, programmers coming from other languages such as Java or Python which uses the &lt;strong&gt;'none'&lt;/strong&gt; keyword in place of &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt;, are often left bewildered with just how to interact with &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; in JavaScript, considering its confusing similarity but complete use case contrast with the &lt;strong&gt;&lt;code&gt;'undefined'&lt;/code&gt;&lt;/strong&gt; keyword. &lt;br&gt;
While in most languages, &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; is often used to refer to the absence of an &lt;strong&gt;assigned value&lt;/strong&gt;, JavaScript instead uses the keyword &lt;strong&gt;&lt;code&gt;'undefined'&lt;/code&gt;&lt;/strong&gt; to do so with &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; referring to the intentional &lt;strong&gt;absence&lt;/strong&gt; of any object instance. &lt;br&gt;
In this article, we discuss the two keywords as well as elaborate on the differences between them.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Null&lt;/strong&gt;&lt;br&gt;
Used to signify that a &lt;strong&gt;variable ** or **object&lt;/strong&gt; doesn't currently point to any &lt;strong&gt;valid&lt;/strong&gt; object instance, Brendan Eich in the development of object oriented JavaScript saw it valid to provide developers with a clear way to indicate the &lt;strong&gt;absence&lt;/strong&gt; of a relevant object instance.&lt;/p&gt;

&lt;p&gt;This allows &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; to be applied in the following use cases;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explicit initialization as well as object reference resetting -&lt;/strong&gt; allowing for the initializing of variables to &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt;, indicating &lt;strong&gt;no current&lt;/strong&gt; object reference which may be assigned later as well as erasing the memory associated with it when no longer needed allowing for garbage-collection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Initialize user to null&lt;/span&gt;
&lt;span class="c1"&gt;// Later in the code...&lt;/span&gt;
&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// Assign an object reference&lt;/span&gt;
&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//Release the memory associated with user&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;As a return value in functions-&lt;/strong&gt; depending on the scenario, &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; can be used in functions to indicate;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Error signaling:&lt;/strong&gt; indicating an error or inability to produce a valid result.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isValidUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Return null to indicate invalid URL&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// Otherwise, fetch data from the URL...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://example.com/api&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error fetching data.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Default Value:&lt;/strong&gt; functions may return &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; as a default value when given no valid result or object available.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Return element if found&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Return null if element is not found&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;findElement&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Element not found.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;From the previous examples, &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; can be seen to be of type &lt;strong&gt;&lt;em&gt;object&lt;/em&gt;&lt;/strong&gt; with its introduction to JavaScript providing a means to indicate that one is referring to a object instance that does not exist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checking for null&lt;/strong&gt;&lt;br&gt;
To check if an object or variable is pointing to an object instance that does not exist, we use strict equality to compare the variable to &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; as shown;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Variable is null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Undefined&lt;/strong&gt;&lt;br&gt;
Being one of the object oriented programming languages allowing for the declaring of variables without immediately assigning them values, there was a need to represent the absence of a defined value or property in JavaScript.&lt;br&gt;
As the keyword &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; already served the purpose of indicating the &lt;strong&gt;absence&lt;/strong&gt; of an &lt;strong&gt;object instance&lt;/strong&gt;, there arose the need of a different keyword which would instead represent the presence of a &lt;strong&gt;variable&lt;/strong&gt; that has not been assigned a value where one should be defined, in other words, the variable is &lt;strong&gt;&lt;code&gt;'undefined'&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checking for undefined&lt;/strong&gt; &lt;br&gt;
We check for &lt;strong&gt;&lt;code&gt;'undefined'&lt;/code&gt;&lt;/strong&gt; as follows;&lt;br&gt;
Checking if a variable is of type &lt;strong&gt;&lt;code&gt;'undefined'&lt;/code&gt;&lt;/strong&gt; by using strict equality as shown;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="c1"&gt;// resolved if variable is undefined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using &lt;strong&gt;'typeof'&lt;/strong&gt; operator:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="c1"&gt;// resolved if variable is undefined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Undeclared&lt;/strong&gt;&lt;br&gt;
A variable is said to be undeclared when an attempt at trying to access it is made yet the variable has not been created.&lt;br&gt;
We can check if a variable exists by;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checking its &lt;strong&gt;type&lt;/strong&gt;;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// foo could get resolved and it's defined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Logging the suspect variable in the console;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F46mbbq2tnbf9uyr99zch.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F46mbbq2tnbf9uyr99zch.png" alt="Console results for undeclared variable" width="800" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As observed, trying to access a variable that has not been initialized leads to the returning of a &lt;strong&gt;reference error&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mistaken Identity&lt;/strong&gt;&lt;br&gt;
As both &lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;null&lt;/code&gt;&lt;/strong&gt; are &lt;strong&gt;&lt;em&gt;falsy&lt;/em&gt;&lt;/strong&gt; values meaning they are both treated as &lt;strong&gt;'false'&lt;/strong&gt; in boolean context, there arises a need to distinguish the two as trying to access a variable that is not assigned a value and one that doesn't point to any valid object instance will return the same error; &lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt;.&lt;br&gt;
Inorder to confirm the specific scenario at hand, the use of strict equality &lt;code&gt;===&lt;/code&gt; is used and not &lt;code&gt;==&lt;/code&gt; as the loose equality double equal operator carries out &lt;strong&gt;&lt;em&gt;'type coercion'&lt;/em&gt;&lt;/strong&gt; which converts the type of the variables being compared into the same type.&lt;/p&gt;

&lt;p&gt;Comparing &lt;strong&gt;&lt;code&gt;'undefined'&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; using &lt;strong&gt;'=='&lt;/strong&gt;;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyvculadj3k9p02v55ks0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyvculadj3k9p02v55ks0.png" alt="Loose equality comparison" width="800" height="128"&gt;&lt;/a&gt;&lt;br&gt;
Comparing the two using &lt;strong&gt;'==='&lt;/strong&gt;;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg08f3i659qyz69u1i6rs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg08f3i659qyz69u1i6rs.png" alt="Strict equality comparison" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;We discuss &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;'undefined'&lt;/code&gt;&lt;/strong&gt; and outline their key differences and similarities and why there was a need to have both. We also provide use cases for both values as well as techniques on distinguishing one from another.&lt;br&gt;
Mechanisms for checking and dealing with &lt;strong&gt;&lt;code&gt;'null'&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;'undefined'&lt;/code&gt;&lt;/strong&gt; should be provided to avoid accessing invalid objects or variables working with invalid objects or variables.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>python</category>
    </item>
    <item>
      <title>Prototypal inheritance; what really happens under the hood.</title>
      <dc:creator>Joshua Ng'ang'a Raphael</dc:creator>
      <pubDate>Thu, 29 Feb 2024 11:43:06 +0000</pubDate>
      <link>https://dev.to/joshraphael/prototypal-inheritance-what-really-happens-under-the-hood-1a07</link>
      <guid>https://dev.to/joshraphael/prototypal-inheritance-what-really-happens-under-the-hood-1a07</guid>
      <description>&lt;p&gt;&lt;strong&gt;Everything behaves as an object&lt;/strong&gt;&lt;br&gt;
In JavaScript, arrays and functions and various datatypes are considered to be objects or rather behave like one.&lt;br&gt;
An &lt;strong&gt;&lt;u&gt;object&lt;/u&gt;&lt;/strong&gt; is defined as a composite data type representing a collection of related data and functionality .&lt;/p&gt;

&lt;p&gt;They mainly consist of;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Properties&lt;/strong&gt;: key-value pairs which define the object's characteristics&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Methods&lt;/strong&gt;: functions that are associated with the object and operate on its properties. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Functions have  &lt;em&gt;&lt;strong&gt;built in methods&lt;/strong&gt;&lt;/em&gt;  such as &lt;strong&gt;'apply'&lt;/strong&gt; that calls the function with a given &lt;strong&gt;'this'&lt;/strong&gt; value and also has properties such as &lt;strong&gt;&lt;em&gt;'length'&lt;/em&gt;&lt;/strong&gt; which would indicate the number of parameters expected by a given function. &lt;br&gt;
One can view in-built methods and properties of a function through the console by logging the following;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Log built-in methods of a function
console.log(Object.getOwnPropertyNames(Function.prototype));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frqv6vls8gd6m9bv90726.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frqv6vls8gd6m9bv90726.png" alt="Console logging of function methods" width="784" height="119"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Log built-in properties of a function
console.log(Object.getOwnPropertyNames(Function));

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwpgjps0bpjkvoaa71hnn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwpgjps0bpjkvoaa71hnn.png" alt="Console logging of function properties" width="770" height="69"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly arrays consist of in built methods such as &lt;strong&gt;'push()'&lt;/strong&gt; that adds elements to the end of an array.&lt;br&gt;
They also have a &lt;strong&gt;'length'&lt;/strong&gt; property that indicates the number of elements in an array.&lt;br&gt;
One can view an array's in built methods and properties by logging the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Log built-in methods of arrays
console.log(Object.getOwnPropertyNames(Array.prototype));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshg6na1gxulneo58pm8n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshg6na1gxulneo58pm8n.png" alt="Console logging array methods" width="770" height="217"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Log built-in properties of arrays
console.log(Object.getOwnPropertyNames(Array));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsjl4p7fcmp6rfmhp0wgc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsjl4p7fcmp6rfmhp0wgc.png" alt="Console logging array properties" width="771" height="109"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From our earlier definition, it is safe to consider functions and arrays to be objects considering they have in-built key-value pairs (properties) and methods.&lt;br&gt;
For any function or array that is initialized, it inherits this in-built methods from the &lt;strong&gt;'Function.prototype'&lt;/strong&gt; and the &lt;strong&gt;'Array.prototype'&lt;/strong&gt;.&lt;br&gt;
What is a &lt;strong&gt;prototype&lt;/strong&gt;?&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Prototypal inheritance&lt;/strong&gt; &lt;br&gt;
A &lt;strong&gt;'Prototype'&lt;/strong&gt; is an internal property of objects that &lt;em&gt;serves as a blueprint for creating new objects&lt;/em&gt;.&lt;br&gt;
&lt;em&gt;&lt;strong&gt;Prototypal inheritance&lt;/strong&gt;&lt;/em&gt; allows for the &lt;strong&gt;&lt;em&gt;transfer&lt;/em&gt;&lt;/strong&gt; of an objects in built methods and properties to &lt;em&gt;any instantiated object&lt;/em&gt;.&lt;br&gt;
When a property or method is accessed on an object, JavaScript will first check if the object has that property or method. &lt;br&gt;
If not it proceeds to move up and looks into the prototype of the object. If the object's prototype does not contain the accessed method or property, JavaScript proceeds to move up once more and check the prototype of our objects' prototype. &lt;br&gt;
This will go on until either the accessed property is found or the ultimate prototype (&lt;strong&gt;'Object.prototype'&lt;/strong&gt;) which contains all properties and methods inherited by all objects in JavaScript is reached.&lt;br&gt;
An attempt to access a property above the 'Object.prototype' will return the &lt;strong&gt;'null'&lt;/strong&gt; object which indicates the property or method an object is trying to access does not exist.&lt;br&gt;
The &lt;strong&gt;&lt;u&gt;linking&lt;/u&gt;&lt;/strong&gt; between an &lt;u&gt;&lt;em&gt;object&lt;/em&gt;&lt;/u&gt; to its &lt;u&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/u&gt; from which it inherits its properties and methods to another prototype of another object up until the &lt;strong&gt;ultimate prototype&lt;/strong&gt; 'Object.prototype' is referred to as a &lt;strong&gt;'prototypal chain'&lt;/strong&gt; and is the mechanism enabling &lt;strong&gt;'prototypal inheritance'&lt;/strong&gt; which can be defined as &lt;strong&gt;&lt;em&gt;the inheritance or the referencing of properties and methods from other objects.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fplbh3ef9t0a01bxy75qy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fplbh3ef9t0a01bxy75qy.png" alt="Prototypal chain" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Object creation methods and accessing their prototypes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Objects are created using;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Object literals&lt;/li&gt;
&lt;li&gt;Constructor functions&lt;/li&gt;
&lt;li&gt;'Object.create()' 
Each method takes up different syntax and provides varying benefits which we will outline.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;strong&gt;Using 'Object literals'&lt;/strong&gt; (declaring objects)&lt;/p&gt;

&lt;p&gt;For creation of simple, single instances of an object with a fixed set of properties and methods, object literals will be our preferred object creation method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Creating an object using object literal

var personLiteral = {
  firstName: "Mansa",
  lastName: "Musa",
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

console.log(personLiteral.fullName()); // Output: Mansa Musa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Getting and setting of an objects prototype&lt;/strong&gt; 
used to be carried out using the keyword 'proto' whih has however been deprecated. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In place of &lt;strong&gt;'proto'&lt;/strong&gt;, two new methods provide an explicit way to get and set the prototype of an object;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;'Object.setPrototypeOf(obj, prototype)'&lt;/strong&gt; - sets the prototype of the specified object, in this case &lt;strong&gt;'obj'&lt;/strong&gt; to the specified prototype &lt;strong&gt;'prototype'&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;'Object.getPrototypeOf(obj)'&lt;/strong&gt; - gets the prototype of our specified object &lt;strong&gt;'obj&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constructor functions&lt;/strong&gt;&lt;br&gt;
Constructor functions are used in the creation of objects with &lt;strong&gt;&lt;em&gt;multiple instances&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;prototypal inheritance&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;dynamic initialization&lt;/em&gt;&lt;/strong&gt; which allows constructor functions to accept parameters during instantiation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Creating objects using constructor function
function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;

  this.fullName = function() {
    return this.firstName + " " + this.lastName;
  };
}

// Creating instances of Person
var person1 = new Person("Lucky", "Says");
var person2 = new Person("Bob", "Johnson");

console.log(person1.fullName()); // Output: Lucky  Says
console.log(person2.fullName()); // Output: Bob Johnson

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

&lt;/div&gt;



&lt;p&gt;Invoking &lt;strong&gt;'new'&lt;/strong&gt; instantiates a object from our constructor which inherits the properties of the prototype through a special keyword &lt;strong&gt;prototype&lt;/strong&gt; which copies reference of the prototype object to the new object's internal &lt;strong&gt;[[Prototype]]&lt;/strong&gt; property of the new instance. This effectively shares properties found on the &lt;u&gt;'prototype object'&lt;/u&gt;.&lt;br&gt;
&lt;strong&gt;Mutability of prototypes&lt;/strong&gt;&lt;br&gt;
Objects instantiated using Constructor functions can have their behavior &lt;u&gt;dynamically extended&lt;/u&gt; by modifying our object prototypes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; // Define a constructor function
function Person(name, age) {
  this.name = name;
  this.age = age;
}

// Add a method to the prototype
Person.prototype.greet = function() {
  console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
};

// Create instances of Person
var person1 = new Person('Alice', 30);
var person2 = new Person('Bob', 25);

// Call the method added to the prototype
person1.greet(); // Output: Hello, my name is Alice and I am 30 years old.
person2.greet(); // Output: Hello, my name is Bob and I am 25 years old.

// Modifying the prototype to add a new property
Person.prototype.gender = 'Female';

// Accessing the new property from instances
console.log(person1.gender); // Output: Female
console.log(person2.gender); // Output: Female

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

&lt;/div&gt;



&lt;p&gt;In this example, We add a method &lt;u&gt;&lt;strong&gt;'greet'&lt;/strong&gt;&lt;/u&gt; to our &lt;strong&gt;Person&lt;/strong&gt; prototype and proceed to create two new instances of &lt;strong&gt;Person&lt;/strong&gt; and call the '&lt;u&gt;greet&lt;/u&gt;' method on them&lt;br&gt;
We then add a new property 'gender' to the &lt;strong&gt;Person&lt;/strong&gt; prototype, essentially adding them to all instances of the object before calling them on our two instances &lt;strong&gt;'person1'&lt;/strong&gt; and &lt;strong&gt;'person2'&lt;/strong&gt;. &lt;br&gt;
allowing you to even change properties.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntactical sugar&lt;/strong&gt;&lt;br&gt;
Objects in JavaScript can also be created with &lt;strong&gt;classes&lt;/strong&gt;&lt;br&gt;
**Unlike **other languages working with classes, we can add properties to the prototype and hence to every&lt;br&gt;
instantiated object of this constructor;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Rectangle {
  constructor(height, width) {
    this.name = "Rectangle";
    this.height = height;
    this.width = width;
  }
}

class FilledRectangle extends Rectangle {
  constructor(height, width, color) {
    super(height, width);
    this.name = "Filled rectangle";
    this.color = color;
  }
}

const filledRectangle = new FilledRectangle(5, 10, "blue");
// filledRectangle ---&amp;gt; FilledRectangle.prototype ---&amp;gt; Rectangle.prototype ---&amp;gt; Object.prototype ---&amp;gt; null


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

&lt;/div&gt;



&lt;p&gt;Classes are referred to as &lt;strong&gt;'syntactical sugar'&lt;/strong&gt; as they merely provide a more &lt;strong&gt;&lt;em&gt;declarative&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;readable&lt;/em&gt;&lt;/strong&gt; syntax for defining object prototypes.&lt;br&gt;
They also provide more understandable syntax for working with inheritance, with built-in keywords like 'extends' for subclassing and 'super' for calling superclass constructors and methods.&lt;/p&gt;

&lt;p&gt;Despite the syntactical difference, classes are eventually &lt;em&gt;&lt;strong&gt;compiled as constructor functions at runtime&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;'Object.create()'&lt;/strong&gt;&lt;br&gt;
 &lt;strong&gt;'Object.create()'&lt;/strong&gt; is a method in JavaScript allowing us to create new objects having &lt;strong&gt;&lt;em&gt;specified their prototype&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Creating a prototype object
var personPrototype = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

// Creating objects using Object.create()
var person1 = Object.create(personPrototype);
person1.firstName = "Emily";
person1.lastName = "Brown";

var person2 = Object.create(personPrototype);
person2.firstName = "David";
person2.lastName = "Lee";

console.log(person1.fullName()); // Output: Emily Brown
console.log(person2.fullName()); // Output: David Lee
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This explicit and direct assigning of prototypes to objects enables us to create objects without the use of constructor functions, thus eliminating any &lt;strong&gt;constructor overhead&lt;/strong&gt; leading to cleaner and more concise code that does not require instantiation-time initialization logic.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Performance concerns&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Prototypical inheritance&lt;/strong&gt; provides great&lt;br&gt;
dynamicity to code and reduces the need for repetitive codebases. It should however be undertaken with care so as to avoid accumulating &lt;strong&gt;overhead&lt;/strong&gt;.&lt;br&gt;
Every attempt to access properties high up on the prototype chain incurs a &lt;strong&gt;negative impact&lt;/strong&gt; on code performance.&lt;br&gt;
To avoid unnecessary performance drops, one may consider checking if the desired property exists on &lt;em&gt;itself&lt;/em&gt; using &lt;u&gt;&lt;strong&gt;'hasOwnProperty'&lt;/strong&gt;&lt;/u&gt; or &lt;u&gt;&lt;strong&gt;'Object.hasOwn'&lt;/strong&gt;&lt;/u&gt; before proceeding to traverse the prototype chain.&lt;br&gt;
It is also advisable to check if the desired property exists as trying to access nonexistent properties leads to full traversal of the full prototype chain.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>interview</category>
      <category>oop</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Event-driven architecture: navigating the single threaded nature of Node.js</title>
      <dc:creator>Joshua Ng'ang'a Raphael</dc:creator>
      <pubDate>Fri, 23 Feb 2024 21:35:14 +0000</pubDate>
      <link>https://dev.to/joshraphael/event-driven-architecture-navigating-the-single-threaded-nature-of-nodejs-1aj4</link>
      <guid>https://dev.to/joshraphael/event-driven-architecture-navigating-the-single-threaded-nature-of-nodejs-1aj4</guid>
      <description>&lt;p&gt;&lt;strong&gt;JavaScript's 'comeback story'&lt;/strong&gt;&lt;br&gt;
Despite its popularity nowadays, JavaScript had been once relegated to a simple scripting language primarily for client-side web development. &lt;br&gt;
Over the years, JavaScript has however experienced a resurgence, becoming a versatile and powerful language suitable for use across both the front and back-end in software development. &lt;br&gt;
One major factor to this resurgence is the development of &lt;strong&gt;Node.js&lt;/strong&gt; which allowed for the execution of JavaScript on the server-side, thus giving rise to the &lt;em&gt;full-stack JavaScript developer&lt;/em&gt;. &lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Lightweight and Efficient&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node.js&lt;/strong&gt; is a &lt;strong&gt;lightweight runtime environment&lt;/strong&gt; which is built on the JavaScript V8 engine which allows for the rendering of JavaScript server-side. It is a lightweight, efficient, high performing environment which has allowed for the development of scalable and high-performance software such as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;real-time applications&lt;/li&gt;
&lt;li&gt;web servers&lt;/li&gt;
&lt;li&gt;web applications&lt;/li&gt;
&lt;li&gt;Command Line tools &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Node.js was written to provide JavaScript functionality on the back and since JavaScript is a &lt;strong&gt;&lt;em&gt;single-threaded language&lt;/em&gt;&lt;/strong&gt;, Node.js was developed using a single-threaded model.&lt;br&gt;
This simply means that Node.js has a single event loop that executes only one task at a time, be it processing incoming requests, handling callbacks, and executing JavaScript code. &lt;br&gt;
Node.js however implements a &lt;strong&gt;Non-blocking I/O (input/output)&lt;/strong&gt; model to bypass whatever bottlenecks that may arise in single-threaded execution. &lt;br&gt;
This allows for &lt;em&gt;input/output operations&lt;/em&gt; to be executed asynchronously allowing for the handling of a large number of concurrent connections which allows for the development of scalable applications.&lt;br&gt;
The implementation of an &lt;strong&gt;event-driven non-blocking I/O model&lt;/strong&gt; helps to eliminate a few performance issues one may experience using single-threaded systems. However one may still encounter some limitations which we discuss as well as some workarounds to them.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Blocking Operations&lt;/strong&gt;:  Any synchronous or blocking operations can be encountered thus blocking an application's responsiveness and impacting performance &lt;br&gt;
In Node.js, synchronous methods may be found in either &lt;strong&gt;native modules&lt;/strong&gt;; which are modules written outside as well as in the Node.js standard library. For example in the File System module, an attempt to read from the file system may be written as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/file.md&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// blocks here until file is read. Synchronous methods end with the word Sync &lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;moreWork&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// will run after console.log&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use of such synchronous methods may exhibit poor performance.&lt;br&gt;
Node.js however provides alternative asynchronous methods to each synchronous blocking method.&lt;br&gt;
Our previous example can thus be rewritten to provide &lt;strong&gt;asynchronous&lt;/strong&gt;, non-blocking execution as follows;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/file.md&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes it is however not possible to use the non-blocking alternative of a synchronous method.&lt;br&gt;
One vital tool Node.js provides for this scenario is &lt;strong&gt;Libuv&lt;/strong&gt; which is an open-source library that is the core behind Node.js's event-driven, non-blocking model.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Concurrency Limitations&lt;/strong&gt; - Node.js is built on a single loop, non-blocking event-driven system that allows it to manage I/O-intensive operations. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F10o0v2omhkx1iamnkg9h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F10o0v2omhkx1iamnkg9h.png" alt="Event loop visual" width="300" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is however a single-threaded system that uses &lt;strong&gt;one event loop&lt;/strong&gt;. This causes serious issues when executing CPU-intensive tasks as applications are blocked from further execution. Node.js however provides an ingenious solution in &lt;strong&gt;'worker threads'&lt;/strong&gt;.&lt;br&gt;
 &lt;strong&gt;Worker threads&lt;/strong&gt;, introduced in &lt;strong&gt;v10.5&lt;/strong&gt; of Node.js provide for the offloading of CPU-intensive tasks out from the event loop allowing for the execution of threads in parallel in a non-blocking manner. They essentially provided a separate runtime for CPU-intensive tasks by offloading them from the main event loop and thus maintain application responsiveness. They are however used at a cost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Threading issues&lt;/strong&gt;&lt;br&gt;
  Whilst a step forward towards achieving true parallelism using Node.js, the use of threads is not without a few challenges;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increased complexity; one must now manage the synchronization between the primary and worker threads.&lt;/li&gt;
&lt;li&gt;Restricted APIs; worker threads are only granted access to a restricted set of APIs not including DOM and UI APIs.&lt;/li&gt;
&lt;li&gt;Significant resource allocation; each thread is assigned its own instance of the V8 JavaScript engine.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although solving the issue of limited concurrency, one must use worker threads conservatively and only when necessary.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Error handling&lt;/strong&gt;&lt;br&gt;
 The &lt;strong&gt;asynchronous nature&lt;/strong&gt; of Node.js model which makes use of &lt;strong&gt;&lt;u&gt;callback-based APIs&lt;/u&gt;&lt;/strong&gt; may lead to &lt;strong&gt;'callback hell'&lt;/strong&gt; as one may have to read through large amounts of code to identify the specific callback that is experiencing an error.&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Uncaught exceptions&lt;/u&gt;&lt;/strong&gt; might also terminate the Node.js process abruptly and can crash the application if not handled properly.&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Stack traces&lt;/u&gt;&lt;/strong&gt; which provide information on the sequence of function calls may not accurately provide a solution to finding the source of error as they are &lt;u&gt;asynchronous&lt;/u&gt; in nature&lt;br&gt;
Error handling and debugging in Node.js should be done using provided debugging tools and libraries.&lt;br&gt;
Consistent error handling mechanisms such as use of the &lt;strong&gt;'throw'&lt;/strong&gt; as well as the &lt;strong&gt;'try...catch'&lt;/strong&gt; &lt;br&gt;
also provide for easier debugging.&lt;/p&gt;

&lt;h2&gt;
  
  
  Still the preferred solution
&lt;/h2&gt;

&lt;p&gt;This article outlines a few factors one should consider when choosing to work with Node.js. Despite some limitations, workarounds have been provided by the brilliant contributors and developers that make Node.js a reality.&lt;br&gt;
For scalable, efficient, high performing web-apps and real-time applications, Node.js is still a strong contender for the preferred environment to work with.&lt;/p&gt;

</description>
      <category>node</category>
      <category>eventdriven</category>
      <category>javascript</category>
    </item>
    <item>
      <title>'this' and its ever changing context.</title>
      <dc:creator>Joshua Ng'ang'a Raphael</dc:creator>
      <pubDate>Wed, 21 Feb 2024 20:22:04 +0000</pubDate>
      <link>https://dev.to/joshraphael/this-and-its-ever-changing-context-2coe</link>
      <guid>https://dev.to/joshraphael/this-and-its-ever-changing-context-2coe</guid>
      <description>&lt;p&gt;The this keyword is a special keyword associated mainly with the languages C++, Java and JavaScript. It however varies in application between the three languages, with '&lt;strong&gt;this&lt;/strong&gt;' being used quite similarly in Java and C++ to refer to the current object within instance methods.  &lt;/p&gt;




&lt;p&gt;&lt;strong&gt;'this' in C++&lt;/strong&gt;&lt;br&gt;
When used in c++, the keyword 'this' is used as a pointer that points to the object whose member function is being executed. It holds the memory address of the current object and accesses members of the current object within member functions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;

class NewClass {
public:
 int num;

void printVal() {
std::cout &amp;lt;&amp;lt; "Value of val is: " &amp;lt;&amp;lt;this-&amp;gt;val &amp;lt;&amp;lt; std::end1;
}
};
 int main() {
NewClass obj;
obj.val = 36;
obj.printVal(); // Value of num is: 36
return 0;

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

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;'this' in Java&lt;/strong&gt;&lt;br&gt;
Just as in C++, &lt;strong&gt;'this'&lt;/strong&gt; is used in Java to refer to the current object instance to access members of the current object within the object instance methods&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class NewClass{
int x;
public void printX() {
  System.out.printIn("Value of x:  " + this.x);
}
public static void main(String[] args) {
   NewClass obj = new NewClass();
    obj.x = 46;
    obj.printX(); // Expected Output: Value of x: 46
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As seen in the examples, the &lt;strong&gt;this&lt;/strong&gt; keyword accesses the object members val(36) in C++ and x(46) in Java within member functions of the objects. &lt;br&gt;
The 'this' keyword in JavaScript however, is applied dynamically depending on the scope and context in which the function containing the 'this' keyword is invoked.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;'this' in JavaScript&lt;/strong&gt;&lt;br&gt;
As earlier discussed, &lt;em&gt;the &lt;strong&gt;this&lt;/strong&gt; keyword will refer to different objects depending on its current scope and context&lt;/em&gt;. As a result &lt;strong&gt;'this'&lt;/strong&gt; can be applied in various contexts to bring about different meaning as discussed below &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;'this' in Global context&lt;/strong&gt;- when not used within a regular function, object or constructor function, 'this' is placed in global scope. 
Simply put, in JavaScript we consider functions to be treated as objects; hence the keyword 'this' will be called in the global scope provided it is outside of any object and it will instead refer to the global object.
The global object in browsers is referred to as the window and can be output by logging 'this' in the console.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiiz1v6j659tv2nn1695r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiiz1v6j659tv2nn1695r.png" alt="result of logging 'this' in if-conditional in the global object" width="714" height="229"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;'this' in method invocation&lt;/strong&gt; - a function that is defined as a property of an object is called a method and when it is called, it is invoked as a method of an object. In this case, 'this' refers to the object in which the method is a property.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {
name: 'Unai',
greet: function() {
console.log('Good evening, ' + this.name);
}
};
obj.greet(); // Output: Good evening, Unai
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;'this' in constructor invocation&lt;/strong&gt; - we use constructor functions to create multiple instances of objects.
If the constructor function used to create an instance of an object is invoked with the &lt;strong&gt;'new'&lt;/strong&gt; keyword and the arguments to be used as the properties of the new object are passed   , &lt;strong&gt;'this'&lt;/strong&gt; is now used to refer to the newly created object.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Constructor function
function Vehicle(name) {
this.name = name;
}
let vehicle1 = new Vehicle('Audi');
console.log(vehicle1.name);  // Output: Audi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Constructor invocation and callback functions&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Special cases may be encountered when using callback functions within constructor functions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fud9g1db3yoffn56lo0lq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fud9g1db3yoffn56lo0lq.png" alt="code screenshot of the result of console logging 'this' inside a callback function within a constructor" width="486" height="342"&gt;&lt;/a&gt;&lt;br&gt;
As shown in this example, the expected output which is the new instantiated object is not logged. Rather, the window objected is returned indicating that the 'this' keyword is referring to the global object instead of the 'me' object. &lt;br&gt;
This confusion is brought about by the callback function which is executed in an entirely different context to the constructor function as it is not defined as a method of the constructor despite being written within it.&lt;br&gt;
The callback function can be however made to execute in the same context as the constructor by binding the 'this' object to the function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6jl7rdupk9v57ovxetpv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6jl7rdupk9v57ovxetpv.png" alt="code screenshot of console logging 'this' in a callback function with the 'this' object now binded to the function" width="486" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is an example of explicit binding and its benefits which we take a deeper look into as the next context in which 'this' is used.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;'this' in explicit binding&lt;/strong&gt; - explicit binding is the clear and direct process of specifying the value of &lt;strong&gt;'this'&lt;/strong&gt; within a function using functions such as &lt;strong&gt;'call'&lt;/strong&gt; or &lt;strong&gt;'bind'&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useName() {
console.log('My name is ' + this.name);
} //this is used within a function
let person2 = { name: 'Josh' };
useName.call(person2); // Output: My name is Josh

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

&lt;/div&gt;



&lt;p&gt;Here explicit binding using 'call()' allows us to specify the value of the keyword 'this' as the object &lt;strong&gt;&lt;em&gt;'person2'&lt;/em&gt;&lt;/strong&gt;. &lt;/p&gt;




&lt;p&gt;Despite the confusing variability of the 'this' keyword in JavaScript, a good tip to keep at the back of your mind is that the 'this' keyword will always refer to the object that is currently executing or invoking the function where &lt;strong&gt;'this'&lt;/strong&gt; is used.&lt;br&gt;
In this article we have tried to elaborate on the various use cases of the &lt;strong&gt;'this'&lt;/strong&gt; keyword. By reaching the end of this article, you should hopefully be able to interpret the context of 'this' the next time you encounter it.&lt;/p&gt;

</description>
      <category>interview</category>
      <category>oop</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Design patterns: Event delegation for cleaner, more concise code.</title>
      <dc:creator>Joshua Ng'ang'a Raphael</dc:creator>
      <pubDate>Wed, 14 Feb 2024 11:27:59 +0000</pubDate>
      <link>https://dev.to/joshraphael/design-patterns-event-delegation-for-cleaner-more-concise-code-4di0</link>
      <guid>https://dev.to/joshraphael/design-patterns-event-delegation-for-cleaner-more-concise-code-4di0</guid>
      <description>&lt;p&gt;Event delegation is a design pattern where an event listener is applied to a parent element container instead of applying one to each individual child element. Events that occur within the children elements are then handled by the parent element.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Day to day event delegation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In a given office,the manager may be placed in charge of several groups of workers. The number of workers may be too many for the manager to individually handle and supervise. Rather than individually micromanaging each worker, the office manager may assign team leaders to each group of workers. &lt;br&gt;
With the appointment of team leaders, the manager who now represents our parent element can then assign tasks to each team through the team leaders who represent event listeners attached to our parent element (manager) and receive feedback from the multiple workers who represent our children elements via the assigned team leaders. &lt;br&gt;
Similarly, in event delegation we attach an event listener to our parent container rather than to each individual child element. An event that occurs to any of the children elements  is bubbled up to the parent element and the parent's event listener handles it.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Use cases for event delegation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In this article we showcase an example of event delegation implemented using JavaScript. &lt;br&gt;
 We attach an event listener to the &lt;strong&gt;'Ul'&lt;/strong&gt; parent element using its id selector &lt;strong&gt;'Parent&lt;/strong&gt; on which upon the occurrence of a click event, our function checks if the element clicked on is a &lt;strong&gt;'li'&lt;/strong&gt; element. If the element is confirmed as a list item, it has its CSS class &lt;strong&gt;'highlight'&lt;/strong&gt; toggled to an either active or inactive state.&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/Joshua-Nganga/embed/gOEQLze?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why use event delegation?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The ability to manipulate the behavior of multiple elements using only one event listener &lt;strong&gt;(dynamic contextual handling)&lt;/strong&gt; is just but one of the benefits offered by event delegation. Event delegation is also advantageous by providing;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cleaner, more concise code&lt;/strong&gt; - providing an easily maintainable codebase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduced memory consumption&lt;/strong&gt; - arising as a result of fewer attached event listeners.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility and scalability&lt;/strong&gt; - as events in large scale applications are more easily handled. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article we discuss event delegation and showcase one of its use cases in handling  (dynamic contextual handling) using a single event listener attached to a parent element. &lt;/p&gt;

&lt;p&gt;Event delegation is definitely one of the more simpler design patterns programmers should look to implement in an attempt to improve the quality of their code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>designpatterns</category>
    </item>
  </channel>
</rss>
