<?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[Experienced frontend dev &amp; web design consultant. Specializing in HTML, CSS, JS, React, Material UI, Tailwind &amp; Bootstrap for custom web dev]]></title><description><![CDATA[Need a professional frontend dev and web design consultant? Kiprotich Dominic specializes in HTML, CSS, JS, React, Material UI, Tailwind, &amp; Bootstrap for custom web dev. Contact him today!]]></description><link>https://kiprotichdominic.com</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Apr 2026 10:32:37 GMT</lastBuildDate><atom:link href="https://kiprotichdominic.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Am I Ready?]]></title><description><![CDATA[In the dimly lit room, I sat hunched over my cluttered desk, my weary eyes fixed on the glowing screen before me. "Am I ready?" The question echoed through my mind like a haunting refrain, a relentless whisper that never seemed to fade. As a seasoned...]]></description><link>https://kiprotichdominic.com/am-i-ready</link><guid isPermaLink="true">https://kiprotichdominic.com/am-i-ready</guid><category><![CDATA[software development]]></category><category><![CDATA[impostor syndrome]]></category><dc:creator><![CDATA[Kiprotich Dominic]]></dc:creator><pubDate>Thu, 14 Sep 2023 11:21:56 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1694678281066/b1e21891-fa75-4e4d-bc4e-f6796ec5e3fe.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the dimly lit room, I sat hunched over my cluttered desk, my weary eyes fixed on the glowing screen before me. <strong><em>"Am I ready?"</em></strong> The question echoed through my mind like a haunting refrain, a relentless whisper that never seemed to fade. As a seasoned software engineer, I had conquered complex code, navigated intricate algorithms, and crafted elegant solutions. Yet, no matter how many lines of code I wrote or how many projects I completed, the nagging uncertainty lingered like a shadow.</p>
<h3 id="heading-how-do-i-know-i-am-ready"><strong>How Do I Know I Am Ready?</strong></h3>
<p>How do I know <strong><em>I am ready?</em></strong> That was the question that had eluded me for years, hidden within the labyrinth of self-doubt that I navigated daily.</p>
<p>Amid this persistent self-doubt, my thoughts often wandered to the ever-evolving realm of front-end development. It was the field where my passion truly thrived, where creativity blended seamlessly with logic, and where user interfaces became digital canvases waiting to be brought to life. But even here, in the world, I loved so dearly, the question of readiness loomed like a spectral spectre.</p>
<p>I would glance at my screen, adorned with code that seemed both familiar and foreign at times. The JavaScript frameworks and libraries I had mastered had undergone updates and transformations. New methodologies and best practices emerged with each passing season. It felt like a marathon where the finish line kept moving further away, and I, the runner, struggled to keep pace.</p>
<p>And so, I began to seek answers. I scoured online forums and community discussions, looking for that elusive checklist that would declare, <strong>"You are ready."</strong> Yet, what I discovered was not a <strong>definitive list</strong>, but a realization that readiness in front-end development was a <strong>journey</strong>, not a <strong>destination</strong>.</p>
<p>It was about understanding that the learning never ceased. Each <strong>project</strong>, each new technology, and each interaction with fellow developers contributed to my growth. The uncertainty wasn't a sign of inadequacy; it was a testament to my <strong>commitment</strong> to staying on the cutting edge of front-end development.</p>
<p>With time, I learned that readiness was not a <strong>static state</strong> but a <strong>state of mind.</strong> It was the <strong>confidence</strong> to tackle new challenges head-on, armed with the knowledge that I had the skills to adapt and learn as I went. It was about embracing the constant evolution of technology and using it as a driving force to push my boundaries.</p>
<p>In that dimly lit room, as my eyes continued to scan the screen, I realized that perhaps the question of <strong>"Am I ready?"</strong> was not one with a single, definitive answer. Instead, it was a question I would carry with me throughout my career, a question that would motivate me to keep learning, growing, and innovating in the captivating world of front-end development.</p>
]]></content:encoded></item><item><title><![CDATA[Expression vs Statement]]></title><description><![CDATA[On day 6(link) we dived into the scope of javascript and how javascript variables are visible or accessible in javascript programs. Specifically, it determines where a variable or function can be accessed and how long it remains in memory.
Today we d...]]></description><link>https://kiprotichdominic.com/expression-vs-statement</link><guid isPermaLink="true">https://kiprotichdominic.com/expression-vs-statement</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[javascript framework]]></category><category><![CDATA[expressions]]></category><category><![CDATA[javascript expressions]]></category><category><![CDATA[statements]]></category><dc:creator><![CDATA[Kiprotich Dominic]]></dc:creator><pubDate>Wed, 08 Mar 2023 04:07:38 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1678223173740/35e4b674-934d-4ee9-a38a-733aff96c22d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>On day 6(<a target="_blank" href="https://hashnode.com/post/cles0vnuo000f09k2d2tlblgy">link</a>) we dived into the scope of javascript and how javascript variables are visible or accessible in javascript programs. Specifically, it determines where a variable or function can be accessed and how long it remains in memory.</p>
<p>Today we dive into expressions vs statements in Javascript so we will first have to know the dictionary definition of the two words.</p>
<p><strong>Expression(declaration) -</strong> The action of making known one's thoughts or feelings.</p>
<p><strong>Statement(assertion) -</strong> A definite or clear expression of something in speech or writing.</p>
<p>What are there in <strong>Javascript?</strong></p>
<h3 id="heading-javascript-expression">Javascript Expression</h3>
<p>In JavaScript, an expression is a <strong>piece of code</strong> that produces a <strong>value</strong>. It can be a simple value, like a number or string, or a more complex value, like an object or array.</p>
<p>Some examples of JavaScript expressions are:</p>
<ul>
<li><p><strong>Literal Values:</strong> <code>5</code>, <code>"Hello, world!"</code>, <code>true</code>, <code>null</code>, <code>undefined</code>.</p>
</li>
<li><p><strong>Variables:</strong> <code>x</code>, <code>y</code>, <code>firstName</code>, <code>lastName</code>.</p>
</li>
<li><p><strong>Arithmetic Expressions:</strong> <code>2 + 3</code>, <code>x * y</code>, <code>10 / z</code>.</p>
</li>
<li><p><strong>Comparison Expressions:</strong> <code>x &lt; y</code>, <code>firstName === lastName</code>.</p>
</li>
<li><p><strong>Logical Expressions:</strong> <code>x &amp;&amp; y</code>, <code>x || y</code>.</p>
</li>
<li><p><strong>Function Calls</strong>: <code>square(5)</code>, <code>console.log("Hello, world!")</code>.</p>
</li>
<li><p><strong>Array and Object Expressions:</strong> <code>[1, 2, 3]</code>, <code>{ name: "John", age: 30 }</code>.</p>
</li>
</ul>
<p><strong>JavaScript expressions</strong> can be used in a variety of ways, such as:</p>
<ul>
<li><p>Assigning values to variables: <code>let x = 2 + 3;</code></p>
</li>
<li><p>Passing arguments to functions: <code>console.log(x * y);</code></p>
</li>
<li><p>Comparing values: <code>if (x &lt; y) { /* do something */ }</code></p>
</li>
<li><p>Returning values from functions: <code>function square(x) { return x * x; }</code></p>
</li>
<li><p>Creating objects and arrays: <code>let person = { name: "John", age: 30 };</code></p>
</li>
</ul>
<p>Understanding expressions is fundamental to writing JavaScript code, as they form the basis of many programming concepts, such as variables, functions, and operators.</p>
<h3 id="heading-javascript-statements">Javascript Statements</h3>
<p>In JavaScript, a statement is a piece of code that performs a specific action. It does not produce a value like an expression does, but instead instructs the computer to do something. There are many types of statements in JavaScript, including:</p>
<ol>
<li><strong>Variable Declarations:</strong></li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> x = <span class="hljs-number">10</span>;
<span class="hljs-keyword">const</span> PI = <span class="hljs-number">3.14159</span>;
</code></pre>
<p><strong>Variable Declarations</strong> are considered a <strong>statement in JavaScript</strong> because they are an action that instructs the computer to <strong>create a variable</strong> and optionally assign a value to it. The process of declaring a variable reserves memory space for the variable in the computer's memory, which is an action that cannot be done through an expression.</p>
<ol>
<li><strong>Conditional Statements:</strong></li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span> (x &lt; y) {
  <span class="hljs-comment">// do something if x is less than y</span>
} <span class="hljs-keyword">else</span> {
  <span class="hljs-comment">// do something else if x is not less than y</span>
}
</code></pre>
<p><strong>Conditional Statements</strong> are considered statements in JavaScript because they are an <strong>action</strong> that instructs the <strong>computer</strong> to make a decision <strong>based on a condition.</strong> The condition is an expression that produces a Boolean value (true or false), and the decision determines which block of code to execute based on the value of the condition.</p>
<ol>
<li><strong>Loop Statements:</strong></li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
  <span class="hljs-comment">// do something 10 times</span>
}

<span class="hljs-keyword">while</span> (x &lt; y) {
  <span class="hljs-comment">// do something while x is less than y</span>
}
</code></pre>
<p><strong>Loop Statements</strong> are considered <strong>statements</strong> in JavaScript because they are an <strong>action</strong> that instructs the computer to <strong>repeat a block of code multiple times</strong>. The loop statement consists of a condition that determines whether to continue the loop or exit it, and a block of code to execute on each iteration of the loop.</p>
<ol>
<li><strong>Function Declarations:</strong></li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello, "</span> + name + <span class="hljs-string">"!"</span>);
}
</code></pre>
<p><strong>Function Declarations</strong> are considered <strong>statements</strong> in JavaScript because they are an <strong>action</strong> that instructs the computer to <strong>create a new function</strong> with a given name and body. The function declaration defines the function and makes it available for use in the program.</p>
<ol>
<li><strong>Class Declarations:</strong></li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
  <span class="hljs-keyword">constructor</span>(name, age) {
    <span class="hljs-built_in">this</span>.name = name;
    <span class="hljs-built_in">this</span>.age = age;
  }

  sayHello() {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello, my name is "</span> + <span class="hljs-built_in">this</span>.name + <span class="hljs-string">" and I am "</span> + <span class="hljs-built_in">this</span>.age + <span class="hljs-string">" years old."</span>);
  }
}
</code></pre>
<p><strong>Class Declarations</strong> are considered statements in JavaScript because they are an <strong>action</strong> that instructs the computer to <strong>create a new class</strong> with a given name and body. The class declaration defines the class and makes it available for use in the program.</p>
<ol>
<li><strong>Expression Statements:</strong></li>
</ol>
<pre><code class="lang-javascript">a + b;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello, world!"</span>);
</code></pre>
<p>In this example, you are telling the computer to evaluate the expression <code>"Hello, world!"</code> and pass it as an argument to the <code>console.log()</code> function. The <code>console.log()</code> function then prints the value of the expression to the console.</p>
<p><strong>Expression Statements</strong> are considered statements in JavaScript because they are an <strong>action</strong> that instructs the computer to evaluate an expression and possibly do something with the resulting value. The expression statement consists of an expression followed by a semicolon.</p>
<p>These are just a few examples of statements in JavaScript, but there are many more. <strong>Statements</strong> are <strong>fundamental</strong> to <strong>JavaScript programming</strong> and are used to control the flow of <strong>execution</strong> in a program. Understanding <strong>statements</strong> is essential for writing functional, efficient, and easy-to-read JavaScript code.</p>
<h3 id="heading-difference-between-javascript-expressionsandampstatements">Difference between Javascript expressions&amp;Statements</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>JavaScript Expressions</strong></td><td><strong>JavaScript Statements</strong></td></tr>
</thead>
<tbody>
<tr>
<td>Evaluates to a value</td><td>Performs an action</td></tr>
<tr>
<td>Can be used as part of a statement</td><td>Cannot be used as part of an expression</td></tr>
<tr>
<td><strong>Examples:</strong> 2 + 2, "Hello" + "World", x = 5</td><td><strong>Examples:</strong> if statement, for loop, the switch statement</td></tr>
<tr>
<td>Can be used as a function argument or returned value</td><td>Cannot be used as a function argument or returned value</td></tr>
<tr>
<td>Can be assigned to a variable</td><td>Cannot be assigned to a variable</td></tr>
<tr>
<td>Examples: x + y, 3 * z, "Hello" + name</td><td>Examples: if (condition) { code block }, while (condition) { code block }, for (initialization; condition; update) { code block }</td></tr>
<tr>
<td>Cannot contain control flow statements</td><td>Can contain control flow statements</td></tr>
</tbody>
</table>
</div><h3 id="heading-conclusion">Conclusion</h3>
<p>In summary, <strong>expressions</strong> in JavaScript evaluate to a value, can be used as part of a statement, can be used as a function argument or returned value, and can be assigned to a variable.</p>
<p><strong>Statements</strong>, on the other hand, perform an action, cannot be used as part of an expression, cannot be used as a function argument or returned value, and cannot be assigned to a variable. Control flow statements, such as break, continue, and return, can only be used in statements.</p>
<p>I hope you now understand the difference between expressions and statements in Javascript.</p>
<p>Please subscribe to our newsletter and we promise we won't spam your inbox. You will get the best resources about javascript and programming in general.</p>
<p>You can follow our 30in30 series using this <a target="_blank" href="https://kiprotichdominic.com/series/javascript-30-in-30">link</a> where we learn 30 concepts in 30 days.</p>
]]></content:encoded></item><item><title><![CDATA[Function Scope, Block Scope and Lexical Scope]]></title><description><![CDATA[For the last 5 days, we have covered 5 topics in JS that are important for every developer to know. If you are new here you follow this link JS30IN30 to be able to read through the series.
Scope
Today we are going in-depth about the scope in Javascri...]]></description><link>https://kiprotichdominic.com/function-scope-block-scope-and-lexical-scope</link><guid isPermaLink="true">https://kiprotichdominic.com/function-scope-block-scope-and-lexical-scope</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Scope in js]]></category><category><![CDATA[local scope]]></category><category><![CDATA[lexical scope]]></category><category><![CDATA[block scope]]></category><dc:creator><![CDATA[Kiprotich Dominic]]></dc:creator><pubDate>Fri, 03 Mar 2023 04:15:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1677792599685/cb6ef656-aa26-4d23-9dd6-8767c674192e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>For the last 5 days, we have covered 5 topics in JS that are important for every developer to know. If you are new here you follow this link <a target="_blank" href="https://kiprotichdominic.com/series/javascript-30-in-30">JS30IN30</a> to be able to read through the series.</p>
<h2 id="heading-scope">Scope</h2>
<p>Today we are going in-depth about the <strong>scope</strong> in Javascript programming.</p>
<p>In JavaScript, the term <code>scope</code> refers to the set of variables, functions, and objects that are accessible in a particular context. The <strong><em>context</em></strong> can be a function, a block of code, or the entire program.</p>
<p>JavaScript has two types of scope: <strong>global scope</strong> and <strong>local scope</strong>.</p>
<h3 id="heading-global-scope"><strong>Global Scope</strong></h3>
<p><strong>Global scope</strong> refers to <strong>variables</strong>, <strong>functions</strong>, and <strong>objects</strong> that are available throughout the entire program. They are accessible from any part of the code, including within functions and other blocks.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Global variable</span>
<span class="hljs-keyword">var</span> myGlobalVariable = <span class="hljs-string">"Hello, world!"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myFunction</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Access global variable</span>
  <span class="hljs-built_in">console</span>.log(myGlobalVariable);
}

<span class="hljs-comment">// Call function</span>
myFunction(); <span class="hljs-comment">// Output: "Hello, world!"</span>

<span class="hljs-comment">// Access global variable from outside function</span>
<span class="hljs-built_in">console</span>.log(myGlobalVariable); <span class="hljs-comment">// Output: "Hello, world!"</span>
</code></pre>
<h3 id="heading-local-scope">Local Scope</h3>
<p><strong>Local scope</strong>, on the other hand, refers to <strong><em>variables</em></strong>, <strong><em>functions</em></strong>, and <strong><em>objects</em></strong> that are only accessible within a specific <strong>block</strong> or <strong>function</strong>. They are not accessible outside of that context.</p>
<p>Local scope is comprised of two types; Function scope and block scope.</p>
<p><strong>Function Scope</strong></p>
<p>In <strong><em>JavaScript</em></strong>, variables defined with the "<strong>var</strong>" keyword have a <strong><em>function-level scope</em></strong>, meaning they are only accessible within the function they are defined in.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myFunction</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Local variable</span>
  <span class="hljs-keyword">var</span> myLocalVariable = <span class="hljs-string">"Hello, world!"</span>;

  <span class="hljs-comment">// Access local variable</span>
  <span class="hljs-built_in">console</span>.log(myLocalVariable);
}

<span class="hljs-comment">// Call function</span>
myFunction(); <span class="hljs-comment">// Output: "Hello, world!"</span>

<span class="hljs-comment">// Attempt to access local variable from outside function</span>
<span class="hljs-built_in">console</span>.log(myLocalVariable); <span class="hljs-comment">// Output: ReferenceError: myLocalVariable is not defined</span>
</code></pre>
<p>In this example, the variable <code>myLocalVariable</code> is declared inside the <code>myFunction</code> function, making it a local variable with function scope. It can only be accessed within the <code>myFunction</code> function.</p>
<p>The <code>myFunction</code> function logs the value of the local variable to the console using <code>console.log(myLocalVariable);</code>.</p>
<p>After defining the function, we call it using <code>myFunction()</code>, which logs the value of the local variable to the console.</p>
<p>Finally, we attempt to access the local variable from outside the function using <code>console.log(myLocalVariable);</code>, which results in a <code>ReferenceError</code> because the variable is not defined outside the function's scope.</p>
<p><strong>Block Scope</strong></p>
<p>Variables defined with the <code>let</code> and <code>const</code> keywords have a <strong>block-level scope</strong>, meaning they are only accessible within the block they are defined in.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myFunction</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Local variable with block scope</span>
  <span class="hljs-keyword">if</span> (<span class="hljs-literal">true</span>) {
    <span class="hljs-keyword">let</span> myLocalVariable = <span class="hljs-string">"Hello, world!"</span>;

    <span class="hljs-comment">// Access local variable</span>
    <span class="hljs-built_in">console</span>.log(myLocalVariable);
  }

  <span class="hljs-comment">// Attempt to access local variable from outside block</span>
  <span class="hljs-built_in">console</span>.log(myLocalVariable); <span class="hljs-comment">// Output: ReferenceError: myLocalVariable is not defined</span>
}

<span class="hljs-comment">// Call function</span>
myFunction();
</code></pre>
<p>In this example, the variable <code>myLocalVariable</code> is declared inside a block of code (an <code>if</code> statement) using the <code>let</code> keyword, which gives it block scope. It can only be accessed within the block where it is declared.</p>
<p>The <code>myFunction</code> function logs the value of the local variable to the console using <code>console.log(myLocalVariable);</code>.</p>
<p>After defining the function, we call it using <code>myFunction()</code>, which logs the value of the local variable to the console if the condition is true.</p>
<p>Finally, we attempt to access the local variable from outside the block using <code>console.log(myLocalVariable);</code>, which results in a <code>ReferenceError</code> because the variable is not defined outside the block's scope.</p>
<h3 id="heading-lexical-scope"><strong>Lexical Scope</strong></h3>
<p>The lexical scope can refer to both local and global scopes. In JavaScript, the scope of a variable or function is determined by its location within the code's nested hierarchy of functions. The variable or function can be defined in a local scope, which means it is only accessible within the function where it is defined, or it can be defined in a global scope, which means it is accessible from anywhere in the program.</p>
<p><strong>For example;</strong> If a <strong><em>variable</em></strong> is defined outside of any <strong><em>function</em></strong>, it has <em>a</em> <strong><em>global scope</em></strong> and can be accessed from any function or block within the program. On the other hand, if a <strong>variable</strong> is defined <strong><em>within a function</em></strong>, it has <strong>local scope</strong> and is only accessible within that function.</p>
<p>Here's an example of a variable with global scope and a variable with local scope:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Global variable with global scope</span>
<span class="hljs-keyword">var</span> globalVariable = <span class="hljs-string">"Hello, world!"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">myFunction</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Local variable with local scope</span>
  <span class="hljs-keyword">var</span> localVariable = <span class="hljs-string">"Hello, there!"</span>;
  <span class="hljs-built_in">console</span>.log(globalVariable); <span class="hljs-comment">// Output: "Hello, world!"</span>
  <span class="hljs-built_in">console</span>.log(localVariable); <span class="hljs-comment">// Output: "Hello, there!"</span>
}

myFunction();
<span class="hljs-built_in">console</span>.log(globalVariable); <span class="hljs-comment">// Output: "Hello, world!"</span>
<span class="hljs-built_in">console</span>.log(localVariable); <span class="hljs-comment">// Output: ReferenceError: localVariable is not defined</span>
</code></pre>
<p>In this example, <code>globalVariable</code> is defined outside of any function, giving it global scope. It can be accessed from within the <code>myFunction</code> function as well as from outside the function.</p>
<p>On the other hand, <code>localVariable</code> is defined within the <code>myFunction</code> function, giving it local scope. It can only be accessed from within the function and not from outside. When we attempt to log <code>localVariable</code> outside of the function, we get a <code>ReferenceError</code> because it is not defined in that scope.</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>Understanding scope is important in JavaScript because it can help prevent naming collisions and improve code organization. By using a local scope, you can limit the accessibility of variables and functions, which can help prevent unintended side effects and make your code easier to understand and maintain.</p>
<p>Please leave a comment if there is anything left out. I may not cover everything but hope this helps.</p>
]]></content:encoded></item><item><title><![CDATA[Thread Of Execution And Call Stack In Javascript]]></title><description><![CDATA[For the last 5 days, we covered important topics in javascript you should understand as a developer. This is also part of the series but it also adds to the call stack topic we did on day 1 of 30.
Here are the topics we covered with the links to acce...]]></description><link>https://kiprotichdominic.com/thread-of-execution-and-call-stack-in-javascript</link><guid isPermaLink="true">https://kiprotichdominic.com/thread-of-execution-and-call-stack-in-javascript</guid><category><![CDATA[callstack]]></category><category><![CDATA[30in30]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[threadofexecution]]></category><category><![CDATA[executioncontext]]></category><dc:creator><![CDATA[Kiprotich Dominic]]></dc:creator><pubDate>Wed, 01 Mar 2023 19:37:57 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1677699547784/6803bdb6-aed8-4052-889a-2096f261486a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>For the last 5 days, we covered important topics in javascript you should understand as a developer. This is also part of the series but it also adds to the call stack topic we did on day 1 of 30.</p>
<p>Here are the topics we covered with the links to access them:</p>
<ol>
<li><p><a target="_blank" href="https://kiprotichdominic.com/day-1-of-30-the-call-stack">The Call Stack</a></p>
</li>
<li><p><a target="_blank" href="https://kiprotichdominic.com/day-2-of-30-primitive-types">Primitive Types</a></p>
</li>
<li><p><a target="_blank" href="https://kiprotichdominic.com/day-3-of-30-value-types-and-reference-types">Value Types and Reference Types</a></p>
</li>
<li><p><a target="_blank" href="https://kiprotichdominic.com/day-4-of-30-implicit-explicit-nominal-structuring-and-duck-typing">implicit, Explicit, Nominal, Structuring and duck typing</a></p>
</li>
<li><p><a target="_blank" href="https://kiprotichdominic.com/day-5-of-30-vs-vs-typeof-in-javascript">\== vs === vs typeof</a></p>
</li>
</ol>
<p>The topics covered are really important as you grow or hone your skills as a Javascript developer.</p>
<h3 id="heading-execution-context">Execution Context</h3>
<p>Before we go into call-stack we should first understand what is <strong>Execution Context.</strong> This is one of the topics we missed in our <a target="_blank" href="https://kiprotichdominic.com/day-1-of-30-the-call-stack">first post</a> about the <strong>Call Stack</strong></p>
<p>Javascript is a single-threaded language because it runs on a single thread(single sequential flow of control) within a web browser or web server.</p>
<p>Javascript executes one instruction at a time, meaning that it can only run one task or process at a time.</p>
<p>The single-threaded nature of JavaScript means that it can only execute one task at a time, even if there are multiple tasks or events in-line for execution. For example, if a JavaScript function is running, any other functions or events that need to be processed must wait until the first function completes.</p>
<p>While JavaScript is <strong>single-threaded</strong> within a web browser, it does support <strong>asynchronous programming</strong> through the use of <strong><em>callbacks, promises</em>,</strong> and async/await functions.</p>
<p>Asynchronous programming allows code to be executed in a non-blocking way, which can improve the performance of web applications by allowing them to handle multiple tasks at once.</p>
<p>When a javascript program is run this is what happens:</p>
<ol>
<li><p>The JavaScript engine reads and interprets the code line by line, tokenizing and parsing it to create an abstract syntax tree (AST-data structure used by compilers and interpreters to represent the syntax of a programming language).</p>
</li>
<li><p>The engine creates a global execution context, which includes the global object (in a browser, this is the window object), any global variables, and any functions defined in the global scope.</p>
</li>
<li><p>As the engine executes each line of code, it creates a new execution context for each function that is called. This includes information about the function's local variables, any arguments that were passed to the function, and its scope chain.</p>
</li>
<li><p>Each new execution context is added to the call stack, which keeps track of the order in which functions were called. The top of the call stack is always the currently executing function.</p>
</li>
<li><p>When a function completes execution, its execution context is removed from the call stack, and the engine moves on to the next function in the call stack.</p>
</li>
<li><p>If an error occurs during execution, the engine stops executing the code and throws an error message.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1677698210051/6f0adb4e-ca60-4986-9b1d-d91c5f33ed71.png" alt class="image--center mx-auto" /></p>
<p>The <strong>execution contex</strong>t and <strong>call stack</strong> are critical to understanding how a JavaScript program runs. The <strong>execution context</strong> is the <strong>environment</strong> in which code is executed, and it contains all the necessary information and data structures that the code needs to run correctly.</p>
<p>The <strong>call stack</strong> keeps track of the order in which functions were called, allowing the engine to execute them in the correct order and return control to the previous function when each one completes.</p>
<p>Overall, the execution context and call stack work together to ensure that a JavaScript program is executed in the correct order, with each function executing in the proper context and returning control to the previous function when it is finished.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1677697266760/75f6e0a1-2834-422f-b1bb-647585f9368b.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-call-stack"><strong>Call Stack</strong></h3>
<p>What is Stack? Is it a pile of objects neatly arranged? Yes, it is, but that is the dictionary definition of the <strong>stack.</strong> Now, what is call stack? is it calling objects?</p>
<p>As it sounds that can be correct, but according to programming it is a bit different.</p>
<p><strong>Stack -</strong> In computer science is an abstract(theoretical) datatype that serves as a collection of elements with two main operations; <strong>Push</strong>, which adds an element to the collection, and <strong>Pop</strong>, which removes the <strong>most recently added element</strong> that was not yet removed from the stack.</p>
<p>Based on this argument <strong>stack</strong> is a place in memory where your <strong>local variables</strong> and your <strong>function arguments</strong> go.</p>
<p>Since the <strong>stack</strong> uses basic operations such as <strong>Push</strong> and <strong>Pop,</strong> we have to add more functionality to the stack so that it can be efficient as possible.</p>
<p>These operations will be used to check the status of the stack:</p>
<ul>
<li><p><strong>peek() -</strong> Used to get the top data element in the stack, without removing it.</p>
</li>
<li><p><strong>isEmpty() -</strong> Check if the stack is empty.</p>
</li>
<li><p><strong>isFull() -</strong> Check if the stack is full.</p>
</li>
</ul>
<p><img src="https://www.tutorialspoint.com/data_structures_algorithms/images/stack_representation.jpg" alt="Data Structure and Algorithms - Stack" class="image--center mx-auto" /></p>
<p>Let's now move to <strong>call stack,</strong> You might be wondering what is <strong>CALL?</strong></p>
<p>A <strong>call</strong> in the <strong>call stack</strong> refers to a function or subroutine that is currently being executed by a program.</p>
<p>When a program runs, each function call is added to the call stack, which keeps track of the order in which the functions are called. As each f<strong>unction completes</strong>, it is <strong>removed</strong> from the call stack.</p>
<p>The <strong>call stack</strong> is important because it allows the program to keep track of where it is in the execution of a particular task. This is particularly important when a function calls another function, as the program needs to remember where it was in the original function after the second function completes.</p>
<p><strong>Stack Overflow</strong></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/lvjT-ud6XfVQ5KYVWxZZWkKeVTgtJqFD0pWv" alt="The JavaScript Call Stack - What It Is and Why It's Necessary" /></p>
<p>Now that we understand <strong>call stack</strong> another thing that comes to mind is what happens when the stack is full. What happens to our program? Probably stops since there is no more memory left for execution.</p>
<p>In cases where too many <strong>functions</strong> are called, or if a <strong>function</strong> calls itself repeatedly (<strong>known as recursion</strong>), the <strong>call stack</strong> can become too large(overflow), which can cause a <strong>stack overflow error</strong> and <strong>crash the program</strong> or terminates unexpectedly, as it cannot continue to run without the necessary memory resources.</p>
<p>This can be avoided by writing efficient and well-structured code.</p>
<p>I hope this will help you understand how javascript is executed in-depth.</p>
<p>Please leave a comment if there is anything left out. I may not cover everything but hope this helps.</p>
]]></content:encoded></item><item><title><![CDATA[== vs === vs typeof in javascript]]></title><description><![CDATA[In yesterday's lesson, we covered the concepts of Implicit, Explicit, Nominal, Structuring, and Duck Typing in JavaScript. We delved deep into each of these terms and provided some useful references for further study. If you missed that article, you ...]]></description><link>https://kiprotichdominic.com/vs-vs-typeof-in-javascript</link><guid isPermaLink="true">https://kiprotichdominic.com/vs-vs-typeof-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[typeof]]></category><category><![CDATA[comparison]]></category><category><![CDATA[day5of30]]></category><category><![CDATA[30in30]]></category><dc:creator><![CDATA[Kiprotich Dominic]]></dc:creator><pubDate>Tue, 28 Feb 2023 17:58:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1677606981259/02e1c0e1-0ec0-48f2-be3c-d98d1f75fd53.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In yesterday's lesson, we covered the concepts of Implicit, Explicit, Nominal, Structuring, and Duck Typing in JavaScript. We delved deep into each of these terms and provided some useful references for further study. If you missed that article, you can find it <a target="_blank" href="https://kiprotichdominic.com/day-4-of-30-implicit-explicit-nominal-structuring-and-duck-typing">here</a>. Now, let's move on to today's post.</p>
<p>In yesterday's lesson, we covered <strong>implicit</strong> behavior in javascript. <strong>Implicit behavior</strong> refers to actions that occur automatically without being explicitly stated in the code.</p>
<p>Javascript has an <strong>implicit behavior</strong> where it automatically converts data types to a common type when performing operations. This is called <strong>type coercion.</strong></p>
<p><strong>For example</strong>: if you add a number to a string using the "+" operator, JavaScript will implicitly convert the number to a string and concatenate the two values.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//Type coercion</span>
<span class="hljs-keyword">let</span> result = <span class="hljs-string">"2"</span> + <span class="hljs-number">3</span>;
<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// Output: "23"</span>
</code></pre>
<p>Coming back to the topic of the day; when you see <code>`==` </code> , <code>===</code> and <code>typeof</code> what comes immediately to your mind is a comparison in Javascript but the latter is different as it is used to check the type of a datatype.</p>
<p>When used in combination with the equality operators <code>==</code> and <code>===</code>, it can help ensure that the values being compared are of the same type.<br />For example, consider the following code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> x = <span class="hljs-number">5</span>;
<span class="hljs-keyword">let</span> y = <span class="hljs-string">"5"</span>;

<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> x === <span class="hljs-keyword">typeof</span> y &amp;&amp; x == y) {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"x and y are equal"</span>);
}
<span class="hljs-comment">//Will log undefined since x and y are of different types.</span>
</code></pre>
<p>In this code above, the <code>typeof</code> <strong>operator</strong> is used to check if <code>x</code> and <code>y</code> are of the same <strong>type</strong> before performing the <strong>loose equality comparison</strong> with the <code>==</code> operator. Since <code>x</code> is a number and <code>y</code> is a string, JavaScript will convert <code>y</code> to a number before performing the comparison, and the code will output "x and y are equal".</p>
<p>If we were to use the strict equality comparison operator <code>===</code> instead, the code would output "x and y are not equal", because the two values are of different types.</p>
<p><strong>Note:</strong> it is recommended to use the <code>typeof</code> operator to check the data type of a value before using the <code>==</code> or <code>===</code> operators to perform equality comparisons, to ensure that the values being compared are of the same type.</p>
<p>I hope you have understood what all these operators do in javascript and how they can be used together can help you write more efficient and reliable JavaScript code.</p>
<p>Tomorrow we won't be handling any topic but we will summarize all 5 days in one post to improve our understanding of the concepts learned.</p>
<p>As always leave a comment and share this post if you found this post helpful:</p>
]]></content:encoded></item><item><title><![CDATA[Implicit, Explicit, Nominal, Structuring and Duck Typing]]></title><description><![CDATA[Today I was a bit confused by the topic chosen for the day. I kept wondering what is implicit, explicit and most of all Duck Typing. The more I pondered the more curious I get.
Join me as we demystify these topics in Javascript first we should have a...]]></description><link>https://kiprotichdominic.com/implicit-explicit-nominal-structuring-and-duck-typing</link><guid isPermaLink="true">https://kiprotichdominic.com/implicit-explicit-nominal-structuring-and-duck-typing</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Implicit]]></category><category><![CDATA[nominal]]></category><category><![CDATA[structuring]]></category><category><![CDATA[duck typing]]></category><dc:creator><![CDATA[Kiprotich Dominic]]></dc:creator><pubDate>Mon, 27 Feb 2023 20:21:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1677529201677/b3d77c6e-8d4f-4e83-9370-6235f66dd68f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Today I was a bit confused by the topic chosen for the day. I kept wondering what is implicit, explicit and most of all <strong>Duck Typing.</strong> The more I pondered the more curious I get.</p>
<p>Join me as we demystify these topics in Javascript first we should have a dictionary definition according to <a target="_blank" href="http://google.com">google</a>:</p>
<ul>
<li><p><strong>Implicit(inferred, understood, hinted) -</strong> Suggested though not directly expressed.</p>
<p>  <strong>In a Sentence:</strong> Mama Mboga's implicit pricing strategy was to give discounts to regular customers buying in bulk.</p>
</li>
<li><p><strong>Explicit(clear, direct, plain) -</strong> Stated clearly and in detail, leaving no room for confusion or doubt.</p>
<p>  <strong>In a Sentence:</strong> Through explicit communication, Mama Mboga made it clear that the prices of her vegetables were fixed and non-negotiable, even for her regular customers.</p>
</li>
<li><p><strong>Nominal(formal, official) -</strong> Existing in name only.</p>
</li>
<li><p><strong>Structuring -</strong> Construct or arrange according to a plan; give a pattern or organization.</p>
</li>
<li><p><strong>Duck Typing -</strong> According to <a target="_blank" href="https://en.wikipedia.org/wiki/Duck_typing">Wikipedia</a>, is an application of the duck test—"If it walks like a duck and it quacks like a duck, then it must be a duck"—to determine whether an object can be used for a particular purpose. With nominative typing, an object is of a given type if it is declared to be.</p>
</li>
</ul>
<p>Having understood the basic meanings of these terms, let's now have a look at what they mean in Javascript and how they are used.</p>
<h3 id="heading-implicit-in-javascript"><strong>Implicit In Javascript</strong></h3>
<p>In JavaScript, implicit behavior refers to actions that occur automatically without being explicitly stated in the code.</p>
<p>Here are some examples of implicit behavior in JavaScript:</p>
<ol>
<li><p><strong>Type coercion:</strong> JavaScript automatically converts data types to a common type when performing operations. For example, if you add a number to a string using the "+" operator, JavaScript will implicitly convert the number to a string and concatenate the two values.</p>
</li>
<li><p><strong>Default values:</strong> When a function is called with missing arguments, JavaScript will set the missing values to undefined by default.</p>
</li>
<li><p><strong>Global scope:</strong> If you use a variable without declaring it using the "var", "let", or "const" keywords, JavaScript will automatically create a global variable with the same name, which can lead to unexpected behavior.</p>
</li>
<li><p><strong>Truthy and falsy values:</strong> In JavaScript, some values are considered "truthy" (e.g., non-empty strings, numbers other than zero) and others are considered "falsy" (e.g., empty strings, zero, undefined). When evaluating expressions, JavaScript will implicitly convert values to their corresponding truthy or falsy values.</p>
</li>
<li><p><strong>Binding of "this":</strong> The value of the "this" keyword inside a function depends on how the function is called. If the function is called a method of an object, "this" will refer to the object. If the function is called without an explicit context, "this" will refer to the global object.</p>
</li>
</ol>
<p>When comparing values using the double equals (==) operator, JavaScript will perform implicit type conversion to compare values of different types. Similarly, when a function is called without a specified "this" context, JavaScript will use the global "this" context, which is implicit behavior. Understanding implicit behavior in JavaScript is important for writing efficient and bug-free code.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Type coercion</span>
<span class="hljs-keyword">let</span> result = <span class="hljs-string">"2"</span> + <span class="hljs-number">3</span>;
<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// Output: "23"</span>

<span class="hljs-comment">// Default values</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name</span>) </span>{
  name = name || <span class="hljs-string">"stranger"</span>;
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello, "</span> + name + <span class="hljs-string">"!"</span>);
}
greet(); <span class="hljs-comment">// Output: "Hello, stranger!"</span>

<span class="hljs-comment">// Global scope</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  bar = <span class="hljs-number">10</span>;
}
foo();
<span class="hljs-built_in">console</span>.log(bar); <span class="hljs-comment">// Output: 10</span>

<span class="hljs-comment">// Truthy and falsy values</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isTruthy</span>(<span class="hljs-params">value</span>) </span>{
  <span class="hljs-keyword">if</span> (value) {
    <span class="hljs-built_in">console</span>.log(value + <span class="hljs-string">" is truthy"</span>);
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">console</span>.log(value + <span class="hljs-string">" is falsy"</span>);
  }
}
isTruthy(<span class="hljs-number">1</span>); <span class="hljs-comment">// Output: "1 is truthy"</span>
isTruthy(<span class="hljs-string">""</span>); <span class="hljs-comment">// Output: " is falsy"</span>

<span class="hljs-comment">// Binding of "this"</span>
<span class="hljs-keyword">let</span> person = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">"John"</span>,
  <span class="hljs-attr">sayHello</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, "</span> + <span class="hljs-built_in">this</span>.name + <span class="hljs-string">"!"</span>);
  }
};
<span class="hljs-keyword">let</span> sayHello = person.sayHello;
sayHello(); <span class="hljs-comment">// Output: "Hello, undefined!"</span>
</code></pre>
<p>In the code above, you can see how implicit behavior occurs in different scenarios such as type coercion, default values, global scope, truthy and falsy values, and binding of "this". Understanding these behaviors is essential for writing effective and bug-free code in JavaScript.</p>
<h3 id="heading-explicit-in-javascript"><strong>Explicit In Javascript</strong></h3>
<p>In Javascript, explicit behavior refers to actions that are explicitly stated in the code and require specific instruction to occur.</p>
<p>Here are some examples of explicit behavior in JavaScript:</p>
<ol>
<li><p><strong>Type conversion:</strong> Unlike implicit type coercion, type conversion in JavaScript requires explicit instruction to convert a value from one type to another. For example, you can use the <strong>Number() function</strong> to convert a <strong>string</strong> to a <strong>number</strong>, or the <strong>String() function</strong> to convert a <strong>number</strong> to a <strong>string.</strong></p>
</li>
<li><p><strong>Method invocation:</strong> When a function is called a method of an object, you can explicitly set the value of "this" using the call() or apply() methods.</p>
</li>
<li><p><strong>Variable scoping:</strong> In JavaScript, you can use the "var", "let", or "const" keywords to explicitly declare variables and control their scope.</p>
</li>
<li><p><strong>Conditional statements:</strong> Using if-else statements, switch statements, and ternary operators allow you to explicitly control the flow of your code based on specific conditions.</p>
</li>
<li><p><strong>Iteration:</strong> JavaScript provides explicit ways to iterate over arrays, objects, and other collections using loops, while loops, and forEach() methods.</p>
</li>
</ol>
<p>Here's an example code that illustrates explicit behavior in JavaScript:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Type conversion</span>
<span class="hljs-keyword">let</span> numString = <span class="hljs-string">"10"</span>;
<span class="hljs-keyword">let</span> num = <span class="hljs-built_in">Number</span>(numString);
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> num); <span class="hljs-comment">// Output: "number"</span>

<span class="hljs-comment">// Method invocation</span>
<span class="hljs-keyword">let</span> person1 = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">"John"</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-number">30</span>
};
<span class="hljs-keyword">let</span> person2 = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">"Jane"</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-number">25</span>
};
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello, "</span> + <span class="hljs-built_in">this</span>.name + <span class="hljs-string">"!"</span>);
}
greet.call(person1); <span class="hljs-comment">// Output: "Hello, John!"</span>
greet.call(person2); <span class="hljs-comment">// Output: "Hello, Jane!"</span>
<span class="hljs-comment">// Variable scoping</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">var</span> x = <span class="hljs-number">1</span>;
  <span class="hljs-keyword">let</span> y = <span class="hljs-number">2</span>;
  <span class="hljs-keyword">const</span> z = <span class="hljs-number">3</span>;
  <span class="hljs-built_in">console</span>.log(x, y, z);
}
foo(); <span class="hljs-comment">// Output: 1 2 3</span>

<span class="hljs-comment">// Conditional statements</span>
<span class="hljs-keyword">let</span> num1 = <span class="hljs-number">10</span>;
<span class="hljs-keyword">let</span> num2 = <span class="hljs-number">20</span>;
<span class="hljs-keyword">if</span> (num1 &gt; num2) {
  <span class="hljs-built_in">console</span>.log(num1 + <span class="hljs-string">" is greater than "</span> + num2);
} <span class="hljs-keyword">else</span> {
  <span class="hljs-built_in">console</span>.log(num2 + <span class="hljs-string">" is greater than "</span> + num1);
}

<span class="hljs-comment">// Iteration</span>
<span class="hljs-keyword">let</span> nums = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; nums.length; i++) {
  <span class="hljs-built_in">console</span>.log(nums[i]);
}
nums.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">num</span>) </span>{
  <span class="hljs-built_in">console</span>.log(num);
});
</code></pre>
<p>The <strong>call</strong> is a method that is used to <strong>explicitly set the value</strong> of <strong>this</strong> in the greet function.</p>
<p>When a <strong>function</strong> is invoked as a method of an object, <strong>this</strong> keyword is set to the <strong>object</strong> that the <strong>method</strong> is called on. However, when a <strong>function</strong> is invoked directly, <strong>this</strong> keyword refers to the <strong>global object</strong> (or <strong>undefined</strong> in strict mode).</p>
<p>In the example code, the <strong><em>greet</em> <em>function</em></strong> does not have an object context to refer to, so this would normally be <strong><em>undefined</em></strong>. However, the <strong><em>call</em></strong> method is used to explicitly set the value of <strong><em>this</em></strong> to the <strong><em>person1</em></strong> and <strong><em>person2</em></strong> objects respectively. This allows the <strong><em>greet</em></strong> <strong><em>function</em></strong> to access the <strong><em>name</em></strong> <strong><em>property</em></strong> of each object and print out a personalized greeting for each person.</p>
<p>So, in short, the <strong><em>call</em></strong> <strong><em>method</em></strong> is used to explicitly set the value of <strong>this</strong> in a <strong>function call.</strong></p>
<p>In the code above, you can see how explicit behavior is used in different scenarios such as type conversion, method invocation, variable scoping, conditional statements, and iteration. These behaviors require explicit instructions to occur and provide greater control over the behavior of the code.</p>
<h3 id="heading-nominal-in-javascript"><strong>Nominal In Javascript</strong></h3>
<p>In programming languages, nominal typing is a type system in which <strong>variables</strong> and <strong>values</strong> are <strong>explicitly</strong> named and categorized according to their <strong>data type</strong> or <strong>class.</strong></p>
<p><strong>Nominal typing</strong> requires explicit declarations of <strong>data types</strong> and enforces strong typing, meaning that <strong>variables</strong> cannot be automatically coerced or converted to different types.</p>
<p>In <strong>JavaScript</strong>, nominal typing is not as strict as in some other languages, such as <strong>Java</strong> or <strong>C#</strong>. However, <strong>JavaScript</strong> does have some features that can be considered nominal typing:</p>
<ol>
<li><p><strong>Object constructors:</strong> JavaScript uses constructor functions to create objects with specific <strong>data types</strong>. For example, the <strong>Date</strong> constructor creates objects that represent dates and the <strong>RegExp</strong> constructor creates objects that represent regular expressions.</p>
</li>
<li><p><strong>Classes:</strong> Starting from <strong>ES6</strong>, JavaScript supports classes, which allow you to define blueprints for objects with specific data types. Classes can be seen as a more formalized way of using constructor functions.</p>
</li>
<li><p><strong>Type annotations:</strong> Although JavaScript does not enforce strong typing, you can use type annotations to explicitly declare the data type of a variable or function parameter. This can help catch type errors during development and make the code more self-documenting.</p>
</li>
</ol>
<h3 id="heading-structuring-in-javascript"><strong>Structuring In Javascript</strong></h3>
<p>Structuring in Javascript refers to the process of organizing code into logical and reusable parts. Structuring can help make code easier to understand, maintain, and debug and also promote code reuse and modularity.</p>
<p>Some common techniques for structuring JavaScript code include:</p>
<ol>
<li><p><strong>Functions:</strong> Functions are one of the fundamental building blocks of JavaScript code. They allow you to encapsulate logic and behavior into reusable units that can be called from other parts of your code.</p>
</li>
<li><p><strong>Objects:</strong> Objects in JavaScript provide a way to <strong>group related data</strong> and functionality into a <strong>single unit</strong>. Objects can be used to represent real-world entities or to encapsulate functionality that needs to be reused across different parts of your code.</p>
</li>
<li><p><strong>Modules:</strong> JavaScript modules are a way of encapsulating functionality into self-contained units of code that can be easily imported and used in other parts of your application. <strong>Modules</strong> provide a way to manage dependencies and reduce the risk of naming conflicts and other issues that can arise in larger codebases.</p>
</li>
<li><p><strong>Classes:</strong> JavaScript classes, introduced in <strong>ES6</strong>, provide a way to define <strong>blueprints for objects</strong> with shared behavior and properties. Classes can be seen as a way to encapsulate related functionality into reusable units and provide a more formalized way of using constructor functions.</p>
</li>
<li><p><strong>Promises:</strong> Promises in JavaScript provide a way to handle <strong>asynchronous operations</strong> in a <strong>structured</strong> and <strong>organized</strong> way. Promises allow you to <strong>chain</strong> together <strong>multiple</strong> <strong>asynchronous</strong> operations, handle errors, and avoid <strong>callback hell.</strong></p>
</li>
</ol>
<p>Here is an example of using some of these techniques for structuring JavaScript code:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Define a function for adding two numbers</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addNumbers</span>(<span class="hljs-params">num1, num2</span>) </span>{
  <span class="hljs-keyword">return</span> num1 + num2;
}

<span class="hljs-comment">// Define an object for representing a person</span>
<span class="hljs-keyword">let</span> person = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">"John Smith"</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-number">30</span>,
  <span class="hljs-attr">email</span>: <span class="hljs-string">"john.smith@example.com"</span>,
  <span class="hljs-attr">getAge</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.age;
  }
};

<span class="hljs-comment">// Define a module for performing math operations</span>
<span class="hljs-keyword">let</span> mathUtils = (<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyNumbers</span>(<span class="hljs-params">num1, num2</span>) </span>{
    <span class="hljs-keyword">return</span> num1 * num2;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">divideNumbers</span>(<span class="hljs-params">num1, num2</span>) </span>{
    <span class="hljs-keyword">return</span> num1 / num2;
  }

  <span class="hljs-keyword">return</span> {
    <span class="hljs-attr">multiplyNumbers</span>: multiplyNumbers,
    <span class="hljs-attr">divideNumbers</span>: divideNumbers
  };
})();

<span class="hljs-comment">// Define a class for representing a car</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span> </span>{
  <span class="hljs-keyword">constructor</span>(make, model, year) {
    <span class="hljs-built_in">this</span>.make = make;
    <span class="hljs-built_in">this</span>.model = model;
    <span class="hljs-built_in">this</span>.year = year;
  }

  getInfo() {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.year + <span class="hljs-string">" "</span> + <span class="hljs-built_in">this</span>.make + <span class="hljs-string">" "</span> + <span class="hljs-built_in">this</span>.model;
  }
}

<span class="hljs-comment">// Use a promise to fetch data from an API</span>
fetch(<span class="hljs-string">"https://jsonplaceholder.typicode.com/posts/1"</span>)
  .then(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> response.json())
  .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(data))
  .catch(<span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(error));
</code></pre>
<p>By using these techniques and others, you can structure your JavaScript code in a way that promotes readability, maintainability, and code reuse, and helps you avoid common pitfalls and issues that can arise in large and complex codebases.</p>
<h3 id="heading-duck-typing">Duck Typing</h3>
<p>Duck typing is a concept in programming that refers to the practice of determining the type or behavior of an object based on its properties and methods, rather than its explicit type. In other words, if an object looks like a duck, swims like a duck, and quacks like a duck, then it's a duck. This approach is often used in dynamically typed languages like JavaScript.</p>
<p>In JavaScript, duck typing allows you to write flexible and reusable code that can work with a wide range of objects, as long as they have the required properties and methods. For example, you might write a function that expects an object with a <strong><em>"name"</em></strong> property and a <strong><em>"sayHello"</em></strong> method:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">obj</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello, <span class="hljs-subst">${obj.name}</span>!`</span>);
  obj.sayHello();
}

<span class="hljs-keyword">const</span> person = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">'Alice'</span>,
  sayHello() {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Hi there!'</span>);
  }
};

<span class="hljs-keyword">const</span> animal = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">'Duck'</span>,
  sayHello() {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Quack!'</span>);
  }
};

greet(person); <span class="hljs-comment">// logs "Hello, Alice!" and "Hi there!"</span>
greet(animal); <span class="hljs-comment">// logs "Hello, Duck!" and "</span>
</code></pre>
<p>In the above example, the <code>greet</code> function takes an object <code>obj</code> as its parameter, and assumes that the object has a <code>name</code> property and a <code>sayHello</code> method. The <code>person</code> and <code>animal</code> objects are created with those properties and methods, so they can be passed to the <code>greet</code> function.</p>
<p>When <code>greet(person)</code> is called, the output is <code>"Hello, Alice!"</code> and <code>"Hi there!"</code> because <code>person</code> has a <code>name</code> property with the value <code>'Alice'</code> and a <code>sayHello</code> method that logs <code>"Hi there!"</code>.</p>
<p>Similarly, when <code>greet(animal)</code> is called, the output is <code>"Hello, Duck!"</code> and <code>"Quack!"</code> because <code>animal</code> has a <code>name</code> property with the value <code>'Duck'</code> and a <code>sayHello</code> method that logs <code>"Quack!"</code>.</p>
<p>Note that neither <code>person</code> nor <code>animal</code> are explicitly declared as a certain type (e.g. class or interface) - the <code>greet</code> function just assumes that they have the required properties and methods based on their structure. This is an example of duck typing in JavaScript.</p>
<p>This was a lot to takein for the day and I hope you have now understood these different and important terminologies in javascript and how you use the daily.</p>
<p>Please leave a comment if you found this post helpful:</p>
<p>For <strong>Day 5</strong> we will be dealing with <code>== vs === vs typeof</code> in javascript. See you then:</p>
]]></content:encoded></item><item><title><![CDATA[Value Types and Reference Types]]></title><description><![CDATA[Value Types
In JavaScript, a value type is also referred to as a primitive type. These are the most basic data types in the language and include:

String

Number

Boolean

Null

Undefined

Symbol (added in ES6)


When a primitive value is assigned to...]]></description><link>https://kiprotichdominic.com/value-types-and-reference-types</link><guid isPermaLink="true">https://kiprotichdominic.com/value-types-and-reference-types</guid><category><![CDATA[primitive ]]></category><category><![CDATA[Non-Primitive datatypes]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[value types]]></category><category><![CDATA[reference types]]></category><dc:creator><![CDATA[Kiprotich Dominic]]></dc:creator><pubDate>Sun, 26 Feb 2023 18:21:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1677433771252/98ea8809-1499-4f74-a472-8e552ccf5f3c.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-value-types">Value Types</h3>
<p>In JavaScript, <strong>a value type</strong> is also referred to as a primitive type. These are the most basic data types in the language and include:</p>
<ul>
<li><p>String</p>
</li>
<li><p>Number</p>
</li>
<li><p>Boolean</p>
</li>
<li><p>Null</p>
</li>
<li><p>Undefined</p>
</li>
<li><p>Symbol (added in ES6)</p>
</li>
</ul>
<p>When a primitive value is assigned to a variable or passed as a function argument, a copy of the value is created. This means that changes made to the variable or argument in one part of the code do not affect the value of the original variable or argument in another part of the code.</p>
<p>For example, consider the following code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> x = <span class="hljs-number">10</span>;
<span class="hljs-keyword">let</span> y = x;
y = <span class="hljs-number">20</span>;
<span class="hljs-built_in">console</span>.log(x); <span class="hljs-comment">// Output: 10</span>
<span class="hljs-built_in">console</span>.log(y); <span class="hljs-comment">// Output: 20</span>
</code></pre>
<p>In this code, <code>x</code> is assigned the value <code>10</code>, and then <code>y</code> is assigned the value of <code>x</code>. When <code>y</code> is changed to <code>20</code>, it does not affect the value of <code>x</code>. When <code>x</code> is logged to the console, it still has the value <code>10</code>.</p>
<p>Primitive types are immutable, meaning their values cannot be changed. Any operation that appears to modify a primitive value actually creates a new value.</p>
<p>For example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> str = <span class="hljs-string">"hello"</span>;
str.toUpperCase(); <span class="hljs-comment">// Returns "HELLO"</span>
<span class="hljs-built_in">console</span>.log(str); <span class="hljs-comment">// Output: "hello"</span>
</code></pre>
<p>In this code, the <code>toUpperCase()</code> method does not actually modify the value of <code>str</code>. Instead, it returns a new string with all characters in uppercase. The original value of <code>str</code> remains unchanged.</p>
<h3 id="heading-reference-types">Reference Types</h3>
<p>In JavaScript, reference types are more complex data types than value types. Reference types include:</p>
<ul>
<li><p>Objects</p>
</li>
<li><p>Arrays</p>
</li>
<li><p>Functions.</p>
</li>
</ul>
<p>When you assign a reference type to a variable or pass it as an argument to a function, a reference to the object is made, not a copy of the object itself. This means that any changes made to the object through one reference will be visible through all other references to the same object.</p>
<p>For example, if you have an array <code>arr1</code> and you create a second reference <code>arr2</code> that points to the same array:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> arr1 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];
<span class="hljs-keyword">let</span> arr2 = arr1;
</code></pre>
<p>If you change the contents of the array through the <code>arr2</code> reference:</p>
<pre><code class="lang-javascript">arr2.push(<span class="hljs-number">4</span>);
</code></pre>
<p>The contents of the array are changed for both the <code>arr1</code> and <code>arr2</code> references:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(arr1); <span class="hljs-comment">// Output: [1, 2, 3, 4]</span>
<span class="hljs-built_in">console</span>.log(arr2); <span class="hljs-comment">// Output: [1, 2, 3, 4]</span>
</code></pre>
<p>This is because <code>arr1</code> and <code>arr2</code> both references the same array in memory.</p>
<h3 id="heading-note">Note</h3>
<p>Reference types work this way in Javascript because they are designed to be more efficient when working with large or complex data structures.</p>
<p>When you assign a <strong>value type</strong> to a <strong>variable</strong>, a copy of the value is made, which takes up <strong>memory space</strong>. This means that if you have a large <strong>array</strong> or <strong>object</strong>, making a copy of it every time you pass it to a function or assign it to a variable could be very inefficient and use up a lot of memory.</p>
<p>In contrast, when you assign a re<strong>ference type</strong> to a <strong>variable</strong>, only a reference or pointer to the original data structure is stored in memory. This means that you can work with the data without duplicating it, which is much more efficient when dealing with large or complex data structures.</p>
<p>Additionally, <strong>reference types</strong> allow for more flexibility and dynamic behavior in your code, since you can modify the data structure and have those changes be reflected in all references to that structure. This is particularly useful when working with shared data structures in <strong>concurrent programming</strong>, as it ensures that all changes to the shared data are visible to all threads or processes accessing it.</p>
<p><strong>Youtube Resources To Learn More:</strong></p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=fD0t_DKREbE">Mosh Hamedani</a></p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=-hBJz2PPIVE&amp;t=32s">Web Dev Simplified</a></p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=9ooYYRLdg_g&amp;t=514s">Academind</a></p>
]]></content:encoded></item><item><title><![CDATA[Primitive Types]]></title><description><![CDATA[According to google datatype is a particular kind of data item, as defined by the values it can take, the programming language used, or the operations that can be performed on it.
A data type, in programming, is a classification or categorization of ...]]></description><link>https://kiprotichdominic.com/primitive-types</link><guid isPermaLink="true">https://kiprotichdominic.com/primitive-types</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[data types]]></category><category><![CDATA[primitive types]]></category><category><![CDATA[non-primitive-types]]></category><dc:creator><![CDATA[Kiprotich Dominic]]></dc:creator><pubDate>Sat, 25 Feb 2023 19:30:55 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1677353446024/95a3b295-5818-4083-b6eb-22f4a0e2add5.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>According to <a target="_blank" href="https://www.google.com/search?q=datatype+meaning&amp;sxsrf=AJOqlzXgM2uQzYDV7CT7-LmcXN9u30P3cQ%3A1677349194034&amp;ei=SlH6Y_3cAYqqkdUPw-ecaA&amp;ved=0ahUKEwj9qf_WpLH9AhUKVaQEHcMzBw0Q4dUDCA8&amp;uact=5&amp;oq=datatype+meaning&amp;gs_lcp=Cgxnd3Mtd2l6LXNlcnAQAzIFCAAQgAQyBQgAEIAEMgUIABCABDILCAAQFhAeEPEEEAoyCwgAEBYQHhDxBBAKMgkIABAWEB4Q8QQyCQgAEBYQHhDxBDIJCAAQFhAeEPEEMgkIABAWEB4Q8QQyCQgAEBYQHhDxBDoKCAAQRxDWBBCwAzoHCAAQsAMQQzoECAAQQzoKCAAQgAQQFBCHAjoGCAAQChBDOgUIABCRAjoHCAAQgAQQCjoICAAQFhAeEAo6CggAEBYQHhAPEApKBAhBGABQ5g9YvRtgih5oA3ABeACAAY4CiAG3D5IBAzItOJgBAKABAcgBCsABAQ&amp;sclient=gws-wiz-serp">google</a> <strong>datatype</strong> is a particular kind of data item, as defined by the values it can take, the programming language used, or the operations that can be performed on it.</p>
<p>A <strong>data type</strong>, in programming, is a <strong>classification</strong> or <strong>categorization</strong> of data that specifies the <strong>type of values</strong> that a <strong>variable</strong> or <strong>expression</strong> can take on in a computer program. It tells the computer how to interpret the data and how to operate it.</p>
<p>In JavaScript, data types can be categorized into two main categories: <strong>Primitive types</strong> and <strong>Non-Primitive(object types).</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1677352035255/cc0ef78e-af29-40f4-9af4-9431e84dffc8.jpeg" alt class="image--center mx-auto" /></p>
<ul>
<li><p><strong>Primitive Types:</strong> In programming refer to data types that are not objects and have no methods or properties of their own and refer to a single value. In JavaScript, there are six primitive types:</p>
<ul>
<li><p><strong>Number:</strong> used to represent numeric values, both integers and floating-point numbers. For example, 1, 2.5, -10, etc.</p>
</li>
<li><p><strong>String:</strong> used to represent textual data as a sequence of characters. For example, "hello", "world", "123", etc.</p>
</li>
<li><p><strong>Boolean:</strong> used to represent logical values of true or false. For example, true or false.</p>
</li>
<li><p><strong>Undefined:</strong> used to represent a variable that has been declared but has not been assigned a value. For example, let a;</p>
</li>
<li><p><strong>Null:</strong> used to represent a deliberate non-value or absence of an object. For example, let a = null;</p>
</li>
<li><p><strong>Symbol:</strong> introduced in ECMAScript 6, used to create unique identifiers for object properties. For example, let a = Symbol('my symbol');</p>
</li>
</ul>
</li>
<li><p><strong>Non-Primitive Types(Object Types):</strong> also referred to as object types, they are complex <strong>data types</strong> that can store multiple values and have methods and properties. Here are some examples of non-primitive types in JavaScript:</p>
<ul>
<li><p><strong>Object:</strong> a collection of key-value pairs, where each value can be of any data type.</p>
</li>
<li><p><strong>Array:</strong> a special type of object used to store ordered collections of data.</p>
</li>
<li><p><strong>Function:</strong> a type of object that can be invoked with arguments to perform a specific task.</p>
</li>
<li><p><strong>Date:</strong> used to represent dates and times.</p>
</li>
<li><p><strong>RegExp:</strong> used to represent regular expressions for pattern matching.</p>
</li>
<li><p><strong>Error:</strong> used to represent errors that can occur during program execution.</p>
</li>
</ul>
</li>
</ul>
<p>Difference between <strong>Primitive Types</strong> and <strong>Non-primitive types:</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>Primitive Types</td><td>Non-primitive Types</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Stored as</strong></td><td>Actual Value</td><td>Reference to value</td></tr>
<tr>
<td><strong>Type of value</strong></td><td>Simple</td><td>Complex</td></tr>
<tr>
<td><strong>Examples</strong></td><td>Number, String, Boolean, null, undefined</td><td>Object, Array, Function</td></tr>
<tr>
<td><strong>Comparison</strong></td><td>Value equality using == and === operators</td><td>Reference equality using == and === operators</td></tr>
<tr>
<td><strong>Mutation</strong></td><td>Immutable; that is, <strong>they cannot be altered</strong>.</td><td>Mutable data types because <strong>we can change the value after creation</strong>.</td></tr>
<tr>
<td><strong>Pass-by-value</strong></td><td>Yes</td><td>No (pass-by-reference)</td></tr>
<tr>
<td><strong>Size in memory</strong></td><td>Fixed (usually 8 bytes)</td><td>Variable (depending on the size of the object and its properties)</td></tr>
<tr>
<td><strong>Copy behavior</strong></td><td>Creates a new copy of the value when assigned to a new variable or passed as an argument to a function</td><td>Creates a new reference to the same object when assigned to a new variable or passed as an argument to a function</td></tr>
</tbody>
</table>
</div><p>Keep in mind that the exact behavior of these data types may vary depending on the programming language or environment being used.</p>
<p>Understanding data types is essential for effective programming. By mastering the characteristics and behaviors of different data types, you can write more efficient, robust, and maintainable code, and avoid common pitfalls and errors.</p>
]]></content:encoded></item><item><title><![CDATA[The Call-Stack]]></title><description><![CDATA[Explaining Call Stack To A 5 Year Old
For example, let's say you're playing a game on your computer.
The computer needs to keep track of all the things happening in the game, like where your character is and what the enemies are doing. The computer d...]]></description><link>https://kiprotichdominic.com/the-call-stack</link><guid isPermaLink="true">https://kiprotichdominic.com/the-call-stack</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[callstack]]></category><category><![CDATA[functions]]></category><dc:creator><![CDATA[Kiprotich Dominic]]></dc:creator><pubDate>Fri, 24 Feb 2023 11:38:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1676308058011/a114918c-42a9-4dee-9cbc-e0c9ef8e1e36.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Explaining Call Stack To A 5 Year Old</strong></p>
<p>For example, let's say you're playing a game on your computer.</p>
<p>The computer needs to keep track of all the things happening in the game, like where your character is and what the enemies are doing. The computer does this by using a call-stack.<br />When you start the game, the computer puts the first instruction on the call-stack. This might be something like "Load the game". Then, the computer starts following that instruction. As it does, it adds more instructions to the call-stack.<br />For example, if you move your character, the computer might add an instruction to the call-stack that says "Move the character". If an enemy attacks, the computer might add an instruction that says "Handle the enemy attack".<br />The computer keeps following the instructions on the call-stack until it reaches the end of the program or until there's an error. When it's done with an instruction, it removes it from the call-stack and goes back to the previous instruction.<br />So, just like you can add or remove blocks from the top of a stack, the computer can add or remove instructions from the call-stack. This helps the computer keep track of what it's doing and makes sure everything happens in the right order.</p>
<p><strong>Now! What is Call-stack?</strong></p>
<p>The call stack is a data structure that keeps track of the order in which functions are called in a program. It's called a "stack" because it operates on the <strong>"last-in, first-out" (LIFO) principle</strong>, meaning that the last function called is the first one to be completed.</p>
<p><img src="https://d585tldpucybw.cloudfront.net/sfimages/default-source/blogs/2020/2020-11/lifo.png" alt="Stack and Queue in JavaScript" class="image--center mx-auto" /></p>
<p>Every time a function is called, it's added to the top of the call stack. When the function returns, it's removed from the top of the stack. The next function on the stack then resumes execution. If a function calls another function, the new function is added to the top of the stack, and the previous function is paused until the new function returns.</p>
<p>The call stack is used to keep track of the execution context of a program, including the values of variables, the current line of code being executed, and the location to return to when the function finishes executing. The call stack is crucial for managing the flow of control in a program and makes sure that functions are executed in the correct order and that they return to the correct location when they're finished.</p>
<p>In the case of an error, the call stack can be useful for debugging. For example, if a program crashes with an error message, the call stack can show you the sequence of functions that were called leading up to the error, helping you to understand what caused the problem and how to fix it.</p>
<p><strong>Example In Javascript</strong></p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello, <span class="hljs-subst">${name}</span>!`</span>);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">introduce</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"My name is Jane."</span>);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">start</span>(<span class="hljs-params"></span>) </span>{
  greet(<span class="hljs-string">"John"</span>);
  introduce();
}

start();
</code></pre>
<p>In this example, we have three functions: <code>greet</code>, <code>introduce</code>, and <code>start</code>. <code>greet</code> takes one argument (a name) and logs a greeting to the console, <code>introduce</code> logs a self-introduction to the console, and <code>start</code> calls <code>greet</code> with the argument "John" and then calls <code>introduce</code>.</p>
<p>When we call <code>start()</code>, it gets added to the call stack. Inside <code>start</code>, <code>greet("John")</code> is called and added to the top of the call stack. <code>greet</code> logs "Hello, John!" to the console and returns, so it is removed from the call stack. Next, <code>introduce()</code> is called and added to the top of the call stack. <code>introduce</code> logs "My name is Jane." to the console and returns, so it is removed from the stack. Finally, <code>start()</code> returns and is removed from the call stack, leaving an empty call stack.</p>
<p>So the call stack for this example would look something like this:</p>
<p><strong>N/B:</strong> Remember <strong>Call-Stack</strong> uses the principle <strong>of Last In First Out(LIFO)</strong></p>
<pre><code class="lang-javascript">start()  - #First <span class="hljs-built_in">Function</span>
greet(<span class="hljs-string">"John"</span>) - #Second <span class="hljs-built_in">Function</span>
introduce() - #Last <span class="hljs-built_in">Function</span>
</code></pre>
<p>and then the last function(<strong>introduce()</strong>) added is removed from stack:</p>
<pre><code class="lang-javascript">start()
greet(<span class="hljs-string">"John"</span>)
</code></pre>
<p>and then the function(<strong>greet("John")</strong>) is called and removed from the stack;</p>
<pre><code class="lang-javascript">start()
</code></pre>
<p>and finally, the call stack is empty when the last function(<strong>start()</strong>) is called.</p>
]]></content:encoded></item><item><title><![CDATA[Important concepts in React that you should be familiar with]]></title><description><![CDATA[Components:
A component is a piece of code that returns a JSX element, which can be rendered to the DOM. Components are the building blocks of a React application and can be reused throughout the app to create a modular and maintainable codebase.
Com...]]></description><link>https://kiprotichdominic.com/important-concepts-in-react-that-you-should-be-familiar-with</link><guid isPermaLink="true">https://kiprotichdominic.com/important-concepts-in-react-that-you-should-be-familiar-with</guid><category><![CDATA[React]]></category><category><![CDATA[javascript framework]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Kiprotich Dominic]]></dc:creator><pubDate>Mon, 09 Jan 2023 19:43:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673291612104/0662d3d5-b80f-4de5-89e6-517b30d6c2d1.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Components</strong>:</p>
<p>A component is a piece of code that returns a JSX element, which can be rendered to the DOM. Components are the building blocks of a React application and can be reused throughout the app to create a modular and maintainable codebase.</p>
<p>Components can be either functional or class-based. A functional component is a plain JavaScript function that takes props as an argument and returns a JSX element. A class-based component is a class that extends the <code>React.Component</code> base class and has a render method that returns a JSX element.</p>
<p>Here's an example of a functional component:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello {props.name}!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> MyComponent;
</code></pre>
<p>And here's an example of a class-based component:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello {this.props.name}!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> MyComponent;
</code></pre>
<p><strong>Props</strong>:</p>
<p>Props (short for "properties") are values that are passed to a component from its parent component. They can be used to pass data into a component and customize its behavior.</p>
<p>Props are passed to a component as an object, and the component can access the props using the <code>props</code> parameter. For example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello {props.name}!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> MyComponent;
</code></pre>
<p><strong>State</strong>:</p>
<p>State is an object that stores data specific to a component. It can be used to track and manage the data that a component needs to function. Unlike props, state can be modified within a component.</p>
<p>When the state of a component changes, React will automatically re-render the component, updating the view to reflect the new state. This allows you to build interactive and dynamic user interfaces.</p>
<p>You can define the initial state of a component inside the constructor method of a class-based component or by using the useState hook in a functional component.</p>
<p>Here is an example of a class-based component that stores a count in its state:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Example</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = { <span class="hljs-attr">count</span>: <span class="hljs-number">0</span> };
  }
  render() {
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>{this.state.count}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
  }
}
</code></pre>
<p>Here is an example of a functional component that stores a count in its state:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Example</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>{count}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
}
</code></pre>
<p><strong>Lifecycle methods</strong>:</p>
<p>In React, lifecycle methods are methods that are called at different stages of a component's life cycle, such as when it is rendered to the DOM or when it is about to be removed from the DOM. They are a way for a component to perform certain actions at specific points in its life cycle, such as fetching data or updating the DOM.</p>
<ol>
<li><p>Lifecycle methods are only available in class-based components, as they are methods of the <code>React.Component</code> base class. Some common lifecycle methods include:</p>
<ul>
<li><p><code>constructor(props)</code>: This method is called before the component is mounted (added to the DOM). It is a good place to initialize the state and bind event handlers.</p>
</li>
<li><p><code>componentDidMount()</code>: This method is called after the component is mounted (added to the DOM). It is a good place to perform API calls or other side effects that need to happen after the component is rendered.</p>
</li>
<li><p><code>shouldComponentUpdate(nextProps, nextState)</code>: This method is called before the component is updated (when the component's props or state change). It should return a boolean value indicating whether the component should be updated or not. If it returns <code>true</code>, the component will be updated; if it returns <code>false</code>, the update will be cancelled.</p>
</li>
<li><p><code>componentDidUpdate(prevProps, prevState)</code>: This method is called after the component is updated. It is a good place to perform side effects that need to happen after the component has been updated.</p>
</li>
<li><p><code>componentWillUnmount()</code>: This method is called before the component is removed from the DOM. It is a good place to perform clean-up tasks, such as cancelling network requests or removing event listeners.</p>
</li>
</ul>
</li>
</ol>
<pre><code>Here<span class="hljs-string">'s an example of a class-based component that uses several lifecycle methods:</span>
</code></pre><pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  <span class="hljs-keyword">constructor</span>(props) {
    <span class="hljs-built_in">super</span>(props);
    <span class="hljs-built_in">this</span>.state = {
      <span class="hljs-attr">data</span>: <span class="hljs-literal">null</span>
    };
  }

  componentDidMount() {
    <span class="hljs-comment">// Perform API call or other side effect</span>
    fetch(<span class="hljs-string">'https://my-api.com/data'</span>)
      .then(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> response.json())
      .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">this</span>.setState({ data }));
  }

  shouldComponentUpdate(nextProps, nextState) {
    <span class="hljs-comment">// Only update the component if the data has changed</span>
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.state.data !== nextState.data;
  }

  componentDidUpdate(prevProps, prevState) {
    <span class="hljs-comment">// Perform side effect after the component has been updated</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Component updated'</span>);
  }

  componentWillUnmount() {
    <span class="hljs-comment">// Perform clean-up tasks before the component is removed from the DOM</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Component unmounting'</span>);
  }

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        {this.state.data ? <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{this.state.data.title}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span> : <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Loading...<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> MyComponent;
</code></pre>
<p>In a functional component, you cannot use lifecycle methods because they are only available in class-based components. However, you can use the <code>useEffect</code> hook to perform side effects in a functional component.</p>
<p>The <code>useEffect</code> hook is a function that takes a callback function as an argument and is called after the component is rendered. It is a way to perform side effects, such as fetching data or adding event listeners, in a functional component.</p>
<p>Here's an example of how to use the <code>useEffect</code> hook in a functional component:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [data, setData] = useState(<span class="hljs-literal">null</span>);

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-comment">// Perform API call or other side effect</span>
    fetch(<span class="hljs-string">'https://my-api.com/data'</span>)
      .then(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> response.json())
      .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> setData(data));
  }, []);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {data ? <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{data.title}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span> : <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Loading...<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> MyComponent;
</code></pre>
<p>In this example, the <code>useEffect</code> hook is called after the component is rendered, and it performs an API call to fetch data. The data is then stored in the component's state using the <code>setData</code> function. The component renders an <code>h1</code> element with the data's title if the data is available, or a loading message if it is not.</p>
<p>The <code>useEffect</code> hook takes an optional second argument, which is an array of values that the hook should watch for changes. If any of the values in the array change, the hook will be called again. This is useful if you only want to perform the side effect when certain values change.</p>
<p>For example, you can use the <code>useEffect</code> hook to add an event listener only when the component is rendered for the first time by passing an empty array as the second argument:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params"></span>) </span>{
  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-comment">// Add event listener</span>
    <span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Clicked'</span>));

    <span class="hljs-comment">// Remove event listener on unmount</span>
    <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> {
      <span class="hljs-built_in">window</span>.removeEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Clicked'</span>));
    };
  },
</code></pre>
<p><strong>Hooks</strong>:</p>
<p>A hook is a function that allows a functional component to use state and other React features. Hooks were introduced in React 16.8 and are a way to add state and other functionality to functional components, which are components that are written as a JavaScript function and cannot use state or lifecycle methods.</p>
<ol>
<li><p>There are several built-in hooks in React, such as:</p>
<ul>
<li><p><code>useState</code>: This hook allows a functional component to use state. It returns an array with the current state value and a function to update it.</p>
</li>
<li><p><code>useEffect</code>: This hook allows a functional component to perform side effects, such as making an API call or adding an event listener. It is called after the component is rendered.</p>
</li>
<li><p><code>useContext</code>: This hook allows a functional component to consume a context value. It takes a context object as an argument and returns the current context value for that context.</p>
</li>
</ul>
</li>
</ol>
<pre><code>Here<span class="hljs-string">'s an example of a functional component that uses the `useState` and `useEffect` hooks:</span>
</code></pre><pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-comment">// Update the document title after the count changes</span>
    <span class="hljs-built_in">document</span>.title = <span class="hljs-string">`You clicked <span class="hljs-subst">${count}</span> times`</span>;
  }, [count]); <span class="hljs-comment">// Only re-run the effect if count changes</span>

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>You clicked {count} times<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setCount(count + 1)}&gt;Click me<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> MyComponent;
</code></pre>
<p>In this example, the <code>MyComponent</code> component uses the <code>useState</code> hook to add a state value called <code>count</code>, which is initialized to <code>0</code>. The component also uses the <code>useEffect</code> hook to update the document title every time the <code>count</code> value changes. The component renders an <code>h1</code> element displaying the current <code>count</code> value and a button that, when clicked, calls the <code>setCount</code> function to update the <code>count</code> value.</p>
<p><strong>Context</strong>:</p>
<p>In React, context is a way to pass data through the component tree without having to pass props down manually at every level. This is useful for data that is needed by many components within an application but does not fit the parent-child hierarchy of the component tree.</p>
<p>Here is an example of how you might use context in a React application:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Create a context for the theme</span>
<span class="hljs-keyword">const</span> ThemeContext = React.createContext(<span class="hljs-string">'light'</span>);

<span class="hljs-comment">// Create a component that consumes the theme context</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ThemedButton</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ThemeContext.Consumer</span>&gt;</span>
      {theme =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`${<span class="hljs-attr">theme</span>}<span class="hljs-attr">-theme</span>`}&gt;</span>{theme}<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      )}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ThemeContext.Consumer</span>&gt;</span></span>
  );
}

<span class="hljs-comment">// Create a component that provides the theme context</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-comment">// Provide the theme context value to the tree below</span>
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ThemeContext.Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"dark"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ThemedButton</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">ThemeContext.Provider</span>&gt;</span></span>
    );
  }
}
</code></pre>
<p>In this example, the <code>App</code> component provides the <code>ThemeContext</code> value to the component tree below it, and the <code>ThemedButton</code> component consumes this context value by using the <code>ThemeContext.Consumer</code> component.</p>
<p><strong>Styling</strong>:</p>
<ol>
<li><p>There are several ways to style components in React:</p>
<ol>
<li><p>Inline styling: You can use the <code>style</code> attribute to apply inline styles to a single component. This is useful for small, one-off styles, but can become cumbersome for larger projects.</p>
<pre><code class="lang-javascript"> render() {
   <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">color:</span> '<span class="hljs-attr">red</span>', <span class="hljs-attr">fontSize:</span> '<span class="hljs-attr">16px</span>' }}&gt;</span>Hello, world!<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
 }
</code></pre>
</li>
<li><p>CSS classes: You can define your styles in a separate CSS file and apply them to components using the <code>className</code> attribute. This is the traditional method of styling web applications, and is a good choice for larger projects with a more complex set of styles.</p>
<pre><code class="lang-javascript"> render() {
   <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"red-text large-font"</span>&gt;</span>Hello, world!<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
 }
</code></pre>
</li>
<li><p>CSS-in-JS: There are also several libraries that allow you to define your styles in JavaScript, and apply them directly to your components. This can be a good choice for projects that want to keep all their styling logic in a single place, but it can add additional complexity to your project.</p>
<pre><code class="lang-javascript"> <span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

 <span class="hljs-keyword">const</span> RedText = styled.div<span class="hljs-string">`
   color: red;
   font-size: 16px;
 `</span>;

 render() {
   <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">RedText</span>&gt;</span>Hello, world!<span class="hljs-tag">&lt;/<span class="hljs-name">RedText</span>&gt;</span></span>;
 }
</code></pre>
</li>
</ol>
</li>
</ol>
<p>These are some of the important concepts you should understand in React.</p>
<p>#React #ReactNation #ReaFrica</p>
]]></content:encoded></item></channel></rss>