<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abdul Rehan on Medium]]></title>
        <description><![CDATA[Stories by Abdul Rehan on Medium]]></description>
        <link>https://medium.com/@rehannajam2?source=rss-234f3bf7921e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*FhD2aCyoNI5BYNXtMk6smA.png</url>
            <title>Stories by Abdul Rehan on Medium</title>
            <link>https://medium.com/@rehannajam2?source=rss-234f3bf7921e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 16:46:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rehannajam2/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mastering Functions — JS]]></title>
            <link>https://medium.com/@rehannajam2/mastering-functions-js-c0db98bf06e3?source=rss-234f3bf7921e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0db98bf06e3</guid>
            <category><![CDATA[web]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Abdul Rehan]]></dc:creator>
            <pubDate>Sun, 06 Oct 2024 17:52:35 GMT</pubDate>
            <atom:updated>2024-10-06T17:52:35.791Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mastering Functions — JS🚀</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/979/1*js02-Z5eECNryd1Ds5Vvlw.png" /><figcaption>Figure: Article cover</figcaption></figure><p>Functions are the core of JavaScript, enabling you to modularize your code for greater simplicity and reusability. In this blog, we’ll explore the various types of functions and how they can enhance your coding practice 🙌</p><ol><li><strong>Function Statement</strong>: A function declared with the function keyword. It is sometimes referred to as a function declaration</li></ol><pre>function name(name) {<br>console.log(&#39;Name: &#39;, name);<br>}<br><br>name(&#39;Rehan&#39;);</pre><p>2. <strong>Function Expression</strong>: A function that is assigned to a variable. The function acts as a value, effectively initializing the variable with the function itself</p><pre>const name = function (value) {<br>console.log(&#39;Name: &#39;, value);<br>}<br><br>name(&#39;Rehan&#39;);</pre><p>The major difference between a function expression and a function statement is hoisting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*82UjRkmW_1DOV4csjYh1KA.png" /><figcaption>Figure: Output — i</figcaption></figure><p>If a function statement is invoked before its declaration, the entire function body is stored in memory, allowing it to produce an output. However, for a function expression, since the function is assigned to a variable, invoking it before the variable’s initialization will result in a reference error.</p><p>3. <strong>Anonymous Function</strong>: An anonymous function is a function without a name, lacking its own identity. It is similar to a function statement but does not have a designated name. These functions are often used where functions are treated as values. For example, you can assign an anonymous function to a variable</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Cjiv3E1ZRDFaSyE9lj3Ow.png" /><figcaption>Figure: Output — ii</figcaption></figure><p>4. <strong>Named Function Expression</strong>: It is similar to a function expression, but instead of an anonymous function, it uses a function with a designated name</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4o5KE5deP0xde7mT5kfjUA.png" /><figcaption>Figure: Output — iii</figcaption></figure><p>5. <strong>First-Class Functions</strong>: The ability of functions to be treated as values, allowing them to be passed as arguments to other functions and returned from functions. They are also known as first-class citizens</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q6gdMt147EJQraygFx5iSw.png" /><figcaption>Figure: Output — iv</figcaption></figure><h3>One like = one motivation for me &lt;/3</h3><p>If you enjoyed reading this, please hit the like button. It really motivates me to work harder! 😊</p><h3>Connect with me</h3><p>If you have any questions, feel free to reach out to me on <a href="https://www.linkedin.com/in/abrehan/">LinkedIn</a>. You can also find me at <a href="https://abrehan.dev/">abrehan.dev</a> 🍕</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rRVgzKIJsvCtsVbxdJQnpA.png" /><figcaption>Figure: LinkedIn</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0db98bf06e3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Closure Insights — JavaScript]]></title>
            <link>https://medium.com/@rehannajam2/closure-insights-javascript-8f398a691129?source=rss-234f3bf7921e------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f398a691129</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <dc:creator><![CDATA[Abdul Rehan]]></dc:creator>
            <pubDate>Sun, 22 Sep 2024 12:39:35 GMT</pubDate>
            <atom:updated>2024-09-22T12:39:35.833Z</atom:updated>
            <content:encoded><![CDATA[<h3>Closure Insights — JavaScript</h3><p>Let’s begin with a fundamental example of closures 👇</p><pre>function x() {<br>    var a = 7;<br>    function y() {<br>        console.log(&quot;VARIABLE A: &quot;, a);<br>    }<br><br>    y();<br>}<br><br>x();</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7GYRnG-DJVI2bCAyFWMuPA.png" /><figcaption>Figure: Output — i</figcaption></figure><p>Now, we can dive deeper using a debugger 💡</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k64OHS4IGK1oWfmQh2vIKQ.png" /><figcaption>Figure: Debugger</figcaption></figure><p>Closure basically means that a function is bundled with its lexical environment. Inside the function y, it forms a closure with the variable that was part of function x’s lexical scope.</p><p>You can also return a function. It might seem crazy to some programming languages, but that’s the beauty of JavaScript, where closures come into play and become more complex.</p><pre>function x() {<br>    var a = 7;<br>    function y() {<br>        console.log(&quot;VARIABLE A: &quot;, a);<br>    }<br><br>   return y;<br>}<br><br>var z = x();<br><br>console.log(&quot;Function Z: &quot;, z);</pre><p>What do you think will happen when I print z in the console? 🤔</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*zpThbxqh0VOC3PxTujbmwA.png" /><figcaption>Figure: Output — ii</figcaption></figure><p>Functions are so beautiful that when they are returned, they still maintain their lexical scope. To put it simply, when you return the function y, you’re not just returning that function; you’re returning a closure.</p><pre>function x() {<br>    var a = 7;<br>    function y() {<br>        console.log(&quot;VARIABLE A: &quot;, a);<br>    }<br><br>    a = 200;<br>   return y;<br>}<br><br>var z = x();<br><br>console.log(&quot;Function Z: &quot;, z);<br>z();</pre><p>If I change the value of the variable before returning the function y, will the execution of z show 100 or 200?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*YGkdrnxeKWUOODyiHhvC-Q.png" /><figcaption>Figure: Output — iii</figcaption></figure><p>When you return the function y, it comes along with its lexical scope. In this case, the variable a does not refer to the value; rather, it&#39;s the variable&#39;s reference that is returned. The function carries the reference to those variables, and that reference persists.</p><h3>One like = one motivation for me &lt;/3</h3><p>If you enjoyed reading this, please hit the like button. It really motivates me to work harder! 😊</p><h3>Connect with me</h3><p>If you have any questions, feel free to reach out to me on <a href="https://www.linkedin.com/in/abrehan/">LinkedIn</a>. You can also find me at <a href="https://abrehan.dev/">abrehan.dev</a> 🍕</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2ZymGfgrgtkWnT5JI-Bghg.png" /><figcaption>Figure: LinkedIn</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f398a691129" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Inside JavaScript: Scope and Lexical Environment ]]></title>
            <link>https://medium.com/@rehannajam2/inside-javascript-scope-and-lexical-environment-ef61169de5f3?source=rss-234f3bf7921e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef61169de5f3</guid>
            <category><![CDATA[web]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Abdul Rehan]]></dc:creator>
            <pubDate>Sun, 18 Aug 2024 22:16:40 GMT</pubDate>
            <atom:updated>2024-08-18T22:21:25.694Z</atom:updated>
            <content:encoded><![CDATA[<p>The scope in JavaScript is directly related to the lexical environment. If you understand the lexical environment, it will be very easy to grasp scope and the scope chain. 🤔</p><p>Let’s take a function and a variable below 👇</p><pre>function a() {<br>    console.log(&quot;THIS IS A FUNCTION&quot;);<br>}<br>var x = 10;<br>a();</pre><p>Can we access the variable x inside the function? Let&#39;s check it out.</p><pre>function a() {<br>    console.log(&quot;VALUE OF X: &quot;, x);<br>}<br>var x = 10;<br>a();</pre><p>When I do console.log inside the function, what do you think will be the output?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*TmSI-J0xgYI3o9FXXiR4zg.png" /><figcaption>Figure: Debugger</figcaption></figure><p>When the JavaScript engine reaches this line of code and tries to execute console.log for the variable x, it will first check if x exists in the local memory space of the function&#39;s execution context. Since x was never created inside the function, it won&#39;t be found there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*nJvwvphZMQzKpCsqX-JvgA.png" /><figcaption>Figure: Local memory — i</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*81SZByvxFsDujYXZ97i_QA.png" /><figcaption>Figure: Global memory</figcaption></figure><p>But in the console, it prints the value of x inside the function since x belongs to the global memory space.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Og0IHJ8ZyTCjFN4NxN_fxg.png" /><figcaption>Figure: Console — i</figcaption></figure><p>This was pretty easy. Let’s dive into something more complex 🔪</p><p>If I put another function inside function a, and try to access the value from the new function, what will happen now? 🤔</p><pre>function a() {<br>    b();<br>    function b() {<br>        console.log(&quot;VALUE OF X: &quot;, x);<br>    }<br>}<br>var x = 10;<br>a();</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lp1lPHbymRUBWU9ziKl1bg.png" /><figcaption>Figure: Console — ii</figcaption></figure><p>It has accessed the variable inside the function again, which is only possible because the variable is declared in the global execution context’s memory space.</p><p>What if I declare the variable inside one of the functions? 🤔</p><pre>function a() {<br>    var x = 10;<br>    b();<br>    function b() {<br>        console.log(&quot;VALUE OF X: &quot;, x);<br>    }<br>}<br><br>a();</pre><p>Can I still access the variable x in the child function? Of course, yes! 🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lp1lPHbymRUBWU9ziKl1bg.png" /><figcaption>Figure: Console — iii</figcaption></figure><p>But if I try to access the declared variable outside the function, will it still return a value in the console? 🤔</p><pre>function a() {<br>    var x = 10;<br>    b();<br>    function b() {<br>        // VARIABLE X IS NOT DEFINED HERE<br>    }<br>}<br><br>a();<br>console.log(&quot;VALUE OF X: &quot;, x);</pre><p>What do you think will be the output? 🤔</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*74N6-Um17_j-vPH8NWH0Dg.png" /><figcaption>Figure: Console — iv</figcaption></figure><p>Wonk, wonk! A ReferenceError. 😢</p><p>Here’s where scope comes into action. Scope determines where you can access a specific function or variable in the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*g4pE85Wj83XQ1bkduXgWzA.png" /><figcaption>Figure: Local memory — ii</figcaption></figure><p>The variable is declared inside the scope of function a, and the scope directly depends on the lexical environment.</p><h3>Visual Representation 🖊️</h3><p>When we execute this program, a global execution context is created and added to the call stack.</p><p>As you already know, the global execution context has memory and code as its components. When the program is executed, it will assign values to global variables and functions. Since function a is in the global memory, it will assign a value to it and then invoke it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*6A9Pss05Vx6WZKhQG_IJ7A.png" /><figcaption>Figure: Program execution</figcaption></figure><p>When function a is invoked, its execution context is also created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IsxgZhboj9x61zeYSlD2Yw.png" /><figcaption>Figure: Function’s execution context — i</figcaption></figure><p>Initially, the variable will be set to undefined, and the function will be assigned its body. Once the function starts executing line by line, undefined is replaced with the actual value of the variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e5HHXx_k02Iz9bh5XWNCVw.png" /><figcaption>Figure: Function’s execution context — ii</figcaption></figure><p>Once the code reaches the line where another function, b, is invoked, it will create a new execution context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SorO-PNvHoinn4YYxDcbzw.png" /><figcaption>Figure: Function’s execution context — iii</figcaption></figure><p>Whenever an execution context is created, a lexical environment is also created. The lexical environment is the local memory, along with the lexical environment of its parent.</p><p>The term lexical refers to a hierarchy or a sequence. We can say that function b is lexically inside function a, and function a is lexically inside the global scope.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H0Brm9FuWgszgqqY-TexHg.png" /><figcaption>Figure: Function’s execution context — iv</figcaption></figure><p>Whenever an execution context is created, you also get a reference to the lexical environment of its parent. The lexical parent of function b is function a. The lexical environment of a function includes its memory space plus the lexical environment of its parent, so function a&#39;s memory space plus the lexical environment of its parent, which is the global memory space. The global memory space also has a reference to its lexical parent, and at the outer level, it points to null as it does not have any parent.</p><p>Suppose you do a console.log of variable x inside function b. When the JavaScript engine encounters this line, it tries to find x in the local memory space of function b. When it fails to find x, it goes to the reference and visits the lexical environment of its parent, which is function a, where x is declared. Once it finds the variable x, it goes back to function b and prints it. Otherwise, it will move to the lexical environment of function a, and if it fails to find x there, it will move to its parent, which is the global lexical environment. If it does not find x there as well, it will move to the global lexical environment&#39;s parent, which is null, and the program will end with a ReferenceError.</p><p>This whole chain of lexical environments is called the scope chain. 😅</p><h3>One like = one motivation for me &lt;/3</h3><p>If you enjoyed reading this, please hit the like button. It really motivates me to work harder! 😊</p><h3>Connect with me</h3><p>If you have any questions, feel free to reach out to me on <a href="https://www.linkedin.com/in/abrehan/">LinkedIn</a>. You can also find me at <a href="https://abrehan.dev/">abrehan.dev</a> 🍕</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*An6VjAZkeZ8xY47M-95QzA.png" /><figcaption>Figure: LinkedIn</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef61169de5f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Functions Work in JavaScript ]]></title>
            <link>https://medium.com/@rehannajam2/how-functions-work-in-javascript-3122273e5f4a?source=rss-234f3bf7921e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3122273e5f4a</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[functions-in-javascript]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <dc:creator><![CDATA[Abdul Rehan]]></dc:creator>
            <pubDate>Sun, 11 Aug 2024 20:54:56 GMT</pubDate>
            <atom:updated>2024-08-11T20:54:56.479Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W2afM0MOGIeBGpVzb4MpSA.png" /><figcaption>Figure: Article cover</figcaption></figure><p>Have you ever wondered what happens when a function is executed in JavaScript? Let’s take two functions and invoke them.</p><pre>var x = 1;<br>one();<br>two();<br><br>console.log(&#39;GLOBAL VARIABLE: &#39; + x);<br><br>function one() {<br>    var x = 26;<br>    console.log(&#39;FUNCTION ONE: &#39; + x);<br>}<br><br>function two() {<br>    var x = 39;<br>    console.log(&#39;FUNCTION TWO: &#39; + x);<br>}</pre><p>Whenever JavaScript runs any program, a global execution context is created. As you may remember from my previous articles, it consists of two components: the memory (variable environment) and the code (thread of execution).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PXmp62GcwfcJiqf5kduiSg.png" /><figcaption>Figure: Global execution context</figcaption></figure><p>Even before executing a single line of code, memory is allocated during the first phase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*VA8sQS6jAddpdw9IckEMYQ.png" /><figcaption>Figure: Memory phase — i</figcaption></figure><p>We have a call stack where all these global execution contexts are stored. As soon as the code is executed, the global execution context is pushed onto the call stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*Y3EWmwgAG83bWgkiygepFw.png" /><figcaption>Figure: Call stack — i</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k2acLCIRcCGbnl3ObvfwDw.png" /><figcaption>Figure: Call stack — ii</figcaption></figure><p>When the first line is executed, the undefined is replaced with the actual value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*n2DYia0n4n4qRRNTRqw5Vg.png" /><figcaption>Figure: Code phase — i</figcaption></figure><p>Now, the code moves to the next line where the function is invoked! 🥳</p><p>When function one is invoked, an execution context is created, repeating both phases — memory and code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*3pfZ3BH0yBREZbO4BbYrpw.png" /><figcaption>Figure: Memory phase — ii</figcaption></figure><p>The variable x in the function will have its own memory in the variable environment. The execution context is independent of everything else. As soon as this function is invoked, it is pushed onto the call stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Iuub-uA-NExVcrGV9BuU2w.png" /><figcaption>Figure: Call stack — iii</figcaption></figure><p>Once the code reaches line 8, the undefined placeholder is replaced with an actual value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-_Ojv8a8FSe5MSKopax1rw.png" /><figcaption>Figure: Call stack — iv</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*BrnyDY_TCpje26Qgs7x-Dw.png" /><figcaption>Figure: Code phase — ii</figcaption></figure><p>On line 9, there is a console.log of x. The JavaScript engine will look for the value of x in the local memory space, which is part of the function&#39;s execution context. Once it finds x, it will print it to the console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*X8TzSumiDBoorakhyFAi-Q.png" /><figcaption>Figure: Console</figcaption></figure><p>After the function finishes executing, its execution context is removed from the call stack. However, the global execution context remains since the entire code has not been executed yet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/1*Tfl6AeShfls0f9fdJo8YGA.png" /><figcaption>Figure: Call stack — v</figcaption></figure><p>The process repeats for function two as well. Once it is executed and removed from the call stack, the JavaScript engine will move to line 5 and look for the value of x in the local space of the execution context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*P5VGtFFHEeEdp6Rp97499w.png" /><figcaption>Figure: Code phase — iii</figcaption></figure><p>When the JavaScript engine sees that there’s nothing more to execute, it will pop the global execution context from the call stack and end the program.</p><h3>One like = one motivation for me &lt;/3</h3><p>If you enjoyed reading this, please hit the like button. It really motivates me to work harder! 😊</p><h3>Connect with me</h3><p>If you have any questions, feel free to reach out to me on <a href="https://www.linkedin.com/in/abrehan/">LinkedIn</a>. You can also find me at <a href="https://abrehan.dev/">abrehan.dev</a> 🍕</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*Xge1kW8OZSB2caEmECKCKQ.png" /><figcaption>Figure: LinkedIn</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3122273e5f4a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Hoisting 101]]></title>
            <link>https://medium.com/@rehannajam2/javascript-hoisting-101-ea8936df8ee4?source=rss-234f3bf7921e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea8936df8ee4</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Abdul Rehan]]></dc:creator>
            <pubDate>Sat, 03 Aug 2024 12:04:39 GMT</pubDate>
            <atom:updated>2024-09-23T13:17:54.961Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JlVLLnQeMa6nl26Uca2FuA.png" /><figcaption>Figure: Article cover</figcaption></figure><p>Hoisting is a phenomenon in JavaScript where you can access variables and functions even before initializing them.</p><p>Let’s look at an example of a code block below 👇</p><pre>getName();    <br>console.log(age);<br><br>var age = 22;<br><br>function getName() {<br>    console.log(&quot;Name: &quot;, &quot;Abdul Rehan&quot;);<br>}</pre><p>Before I execute this code, what do you expect to see? 🤔</p><p>We’re attempting to access the getName() function and the age variable before they&#39;ve been initialized.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/831/1*prpMTzQ9nQtTcFmz5dunHA.png" /><figcaption>Figure: Output to review — i</figcaption></figure><p>In the case of variables, it shows undefined, but for functions, it displays the results, whether it’s a function statement or when invoked. It’s quite strange how it works since we don’t see what’s happening behind the code.</p><p>Whenever you execute code, an execution context is created in two phases: memory creation and code execution. As soon as the code starts executing, memory is allocated to each variable and function. Even before the code starts executing, memory is allocated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*o-YUFDLMXBpLX5qtOpNo7Q.png" /><figcaption>Figure: Global scope</figcaption></figure><p>If we dig into the global scope, we’ll see that the variable x has already been allocated memory even before the code is executed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*yJxTrIKsVaNee-AXLGKHKA.png" /><figcaption>Figure: Memory allocation — i</figcaption></figure><p>In the case of functions, we get the actual copy of the function stored in memory instead of an undefined placeholder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*VDFUI_UNUZ6NmeDXYIYjjA.png" /><figcaption>Figure: Function copy — i</figcaption></figure><p>Until now, we have seen function invocation and variable access after the function statement and variable declaration. Let’s see what happens when we move the function invocation and variable access to the top.</p><p>Again, we have put the debugger on the first line of the code. Even before this first line’s execution, the variables and functions are already in the memory space.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*_-FfiznACDmUr4hC2E5tYw.png" /><figcaption>Figure: Memory allocation — ii</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*TB0RPPx1A2K4qreOpWJBag.png" /><figcaption>Figure: Function copy — ii</figcaption></figure><p>Now, if everything is executed line by line, you can observe the output shown in the console 👇</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*fHTY_TspSlcsMN1IvZbAAA.png" /><figcaption>Figure: Output to review — ii</figcaption></figure><p>Moving forward, we have seen enough about undefined, but have you ever thought about the difference between undefined and not defined? By observing the code block below, if I comment out the variable declaration and then execute the code, what will happen?</p><pre>getName();<br>console.log(&quot;VARIABLE: &quot;, x);<br>console.log(&quot;FUNCTION STATEMENT: &quot;, getName);<br><br>// var x = 22;<br><br>function getName() {<br>    console.log(&quot;Name: &quot;, &quot;Abdul Rehan&quot;);<br>}</pre><p>We have not reserved memory for the variable x in the global memory space. So, when it tries to access x and it is not found in the global object, JavaScript will throw a reference error saying x is not defined.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*UDay0VCvg2YpwSAfvynnDg.png" /><figcaption>Figure: Output to review — iii</figcaption></figure><h3>One like = one motivation for me &lt;/3</h3><p>If you enjoyed reading this, please hit the like button. It really motivates me to work harder! 😊</p><h3>Connect with me</h3><p>If you have any questions, feel free to reach out to me on <a href="https://www.linkedin.com/in/abrehan/">LinkedIn</a>. You can also find me at <a href="https://abrehan.dev/">abrehan.dev</a> 🍕</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*IRLVQkzYKvG1lLSOA0Ug_w.png" /><figcaption>Figure: LinkedIn</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea8936df8ee4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Script to Execution]]></title>
            <link>https://medium.com/@rehannajam2/from-script-to-execution-59afe2c26075?source=rss-234f3bf7921e------2</link>
            <guid isPermaLink="false">https://medium.com/p/59afe2c26075</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[architecture]]></category>
            <dc:creator><![CDATA[Abdul Rehan]]></dc:creator>
            <pubDate>Sat, 27 Jul 2024 12:22:39 GMT</pubDate>
            <atom:updated>2024-07-27T12:22:39.502Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TLgtilyZTvHLchk-8SO3sQ.png" /><figcaption>Figure: Article cover</figcaption></figure><p>Everything in JavaScript happens inside an execution context. You can think of this execution context as a wrapper that contains all the code to be executed.</p><h4>What does an execution context look like?</h4><p>The execution context is like a wrapper containing two components, memory and code.</p><ul><li>The memory component is where all functions and variables are stored as key-value pairs. It is also known as variable environment</li><li>The code component is where code is executed one line at a time. It is also known as the thread of execution</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*vz1LntE5Lpbna84b0Yy7Rw.png" /><figcaption>Figure: Execution context</figcaption></figure><p>As we know, JavaScript is a synchronous, single-threaded language. By single-threaded, it means JavaScript can only execute one command at a time. When we say JavaScript is synchronous and single-threaded, it means it executes one command at a time, but in a specific order.</p><h4>What happens when you run JavaScript code?</h4><p>Let’s take the following code block, which calculates the square of a number, to understand what happens when it is executed.</p><pre>var n = 26;<br><br>function squareRoot(num) {<br>    var result = num * num; <br>    return result;<br>}<br><br>var squareRootOfN = squareRoot(n);</pre><p>When you run this code, a global execution context is created. The execution context is created in two phases, the memory creation phase and the code execution phase.</p><p>In the first phase, JavaScript allocates memory to all the variables and functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*VghgChWM6N3Czjy_iCy8zQ.png" /><figcaption>Figure: Memory allocation</figcaption></figure><p>When it allocates memory to n, it stores a special value known as undefined. In the case of functions, it stores the entire code inside this memory space.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*hGypzGSEDkmEUAmoaRppgg.png" /><figcaption>Figure: Memory allocation with values</figcaption></figure><p>In the second phase, JavaScript runs through the code line by line and executes it. As soon as it encounters n, it assigns the value 26 to it. But when it comes to squareRootOfN, where the function is invoked, a new execution context is created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*OChe5_49T3LlF7EyyPhVQA.png" /><figcaption>Figure: New execution context on invocation</figcaption></figure><p>Now, the entire process will be repeated, with memory allocated to variables and functions inside the squareRootOfN function, and then each line of the function is executed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*vLG1QKC9IDoe99UPIx4aeA.png" /><figcaption>Figure: Memory allocation with values</figcaption></figure><p>When this function is invoked, the value of n, which is 26, is passed as an argument, and num, being a parameter, is assigned this value. The code will then move to the next line, where the calculated value is stored in result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*KO3mSEJ5ztLDWqh05futYQ.png" /><figcaption>Figure: Invoked function calculation</figcaption></figure><p>Now, the control goes to the return statement, which tells the function to return control back to the execution context where the function was invoked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*_ogTyzK26cVxH9FVCABWXw.png" /><figcaption>Figure: Control returned</figcaption></figure><p>When the function completes execution, its entire execution context will be deleted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*oNI51WK-alapiLDEDGehRg.png" /><figcaption>Figure: Function execution context deleted</figcaption></figure><h3>One like = one motivation for me &lt;/3</h3><p>If you enjoyed reading this, please hit the like button. It really motivates me to work harder! 😊</p><h3>Connect with me</h3><p>If you have any questions, feel free to reach out to me on <a href="https://www.linkedin.com/in/abrehan/">LinkedIn</a>. You can also find me at <a href="https://abrehan.dev/">abrehan.dev</a> 🍕</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*Rfy5FxC6hMjq_bnRNBKYkA.png" /><figcaption>Figure: LinkedIn</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59afe2c26075" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Beginner’s Guide to Node.js Event Loop]]></title>
            <link>https://medium.com/@rehannajam2/a-beginners-guide-to-node-js-event-loop-e70fbc5fb30c?source=rss-234f3bf7921e------2</link>
            <guid isPermaLink="false">https://medium.com/p/e70fbc5fb30c</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javscript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Abdul Rehan]]></dc:creator>
            <pubDate>Thu, 27 Jun 2024 07:40:13 GMT</pubDate>
            <atom:updated>2024-06-27T07:40:13.621Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aoUFU7hx-b4nB_NhaFVWTQ.png" /><figcaption>Figure: Article cover</figcaption></figure><p>Have you ever wondered what happens when you, as a user, request a server? Is it a machine dealing with multiple requests, or is it a machine choosing which requests it wants to handle? Don’t worry! I’ve got you covered. This concise article will explain how Node.js behaves when it receives requests from users and the responses it generates 🚀</p><p>Imagine you have a server for your Ecommerce application running on Node.js, and there’s a confused user making multiple requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*f__TnczfVhfgOYI40GBvLw.png" /><figcaption>Figure: Interaction between user and server</figcaption></figure><h3>What happens when a Node.js server receives a request?</h3><p>Assume the parent wrapper as Node.js containing two elements: an event queue and an event loop. When a user requests a server, their request is pushed into the event queue. The event loop captures each queued request based on a FIFO (first in, first out) principle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pUvqA9rQxikNajRIeIGAcg.png" /><figcaption>Figure: Architecture assumption</figcaption></figure><p>Now, once the event loop captures the request, it can be categorized into two types: a blocking request and a non-blocking request. A blocking request is synchronous, while a non-blocking request is asynchronous.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/837/1*0VOdAZCldWShOUd8iG3wtQ.png" /><figcaption>Figure: Captured request</figcaption></figure><p>If it’s an asynchronous operation, the event loop captures and processes it, then returns the desired requested resource to the user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pUvqA9rQxikNajRIeIGAcg.png" /><figcaption>Figure: Non-blocking operations</figcaption></figure><p>If it’s a synchronous operation, the event loop captures it and sends it to a thread pool. A thread pool typically consists of 4 workers by default, each responsible for executing its assigned request. Once a worker handles the request, it returns the result and becomes available again in the thread pool. However, if all workers are busy and a new request arrives, it must wait for an available worker. This situation can potentially lead to scalability issues and longer server response times when multiple users make simultaneous requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h0zapl_Uq1RFPxkvmKoeug.png" /><figcaption>Figure: Blocking operations</figcaption></figure><h3>One like = one motivation for me &lt;/3</h3><p>If you enjoyed reading this, please hit the like button. It really motivates me to work harder! 😊</p><h3>Connect with me</h3><p>If you have any questions, feel free to reach to me on <a href="https://www.linkedin.com/in/abrehan/">LinkedIn</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*Ft72IWRo4qH4eZ-ZwhBpog.png" /><figcaption>Figure: LinkedIn</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e70fbc5fb30c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Model-View-Controller (MVC)]]></title>
            <link>https://medium.com/@rehannajam2/model-view-controller-mvc-1426a8804c9a?source=rss-234f3bf7921e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1426a8804c9a</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[model-view-controller]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Abdul Rehan]]></dc:creator>
            <pubDate>Thu, 20 Jun 2024 06:30:54 GMT</pubDate>
            <atom:updated>2024-08-03T17:44:02.112Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*YqYxKn7Sn4s41v8ViYtTUg.png" /><figcaption>HR Management with the MVC Architecture</figcaption></figure><p>Hi 👋,</p><p>I hope this finds you well. I was earlier assigned a project proposal in a course of <strong>Professional Practices</strong> on the topic <strong>“Software Development and Society,”</strong> particularly within the realm of <strong>Human Resource (HR)</strong> management systems. Long story short, the mentioned <strong>architecture</strong> was chosen as an assumption to derive a possible <strong>conclusion,</strong> and this article aims to cover <strong>MVC,</strong> aligning with the topic.</p><h3>Architecture Synopsis:</h3><p>One common architecture pattern that can be followed for this project is the <strong>Model-View-Controller (MVC)</strong> architecture. For a web-based HR management system, several architecture patterns can be considered based on factors such as <strong>scalability, maintainability,</strong> and <strong>security.</strong></p><h3>Model:</h3><p>The Model represents the <strong>data </strong>and <strong>business logic</strong> of the HR management system. It <strong>encapsulates </strong>the<strong> data access layer,</strong> which interacts with the <strong>database </strong>to perform <strong>CRUD (Create, Read, Update, Delete)</strong> operations on employee information, payroll data, performance evaluations, etc.</p><p>In the context of this project, the Model would include <strong>classes </strong>or <strong>entities </strong>representing employees, departments, positions, and other relevant <strong>data entities.</strong> It would also include <strong>business logic</strong> for processing HR-related tasks such as employee onboarding, leave management, and performance reviews.</p><h3>View:</h3><p>The View represents the <strong>user interface (UI)</strong> of the HR management system. It presents data to users in a <strong>human-readable</strong> format and <strong>captures </strong>user input through forms, buttons, and other UI elements.</p><p>In the <strong>MVC architecture,</strong> the View is responsible for <strong>displaying </strong>HR-related information to HR managers, employees, and other stakeholders. It should provide intuitive navigation, interactive features, and responsive design to enhance user experience.</p><h3>Controller:</h3><p>The Controller acts as an <strong>intermediary </strong>between the <strong>Model </strong>and the <strong>View.</strong> It receives user requests from the View, processes them by <strong>invoking </strong>appropriate <strong>methods </strong>in the <strong>Model,</strong> and <strong>updates </strong>the <strong>View </strong>with the <strong>results.</strong></p><p>In the context of this project, the <strong>Controller </strong>would handle <strong>HTTP </strong>requests from <strong>web browsers</strong> or <strong>client </strong>applications, <strong>route </strong>them to the appropriate <strong>components </strong>in the <strong>Model,</strong> and <strong>render </strong>the corresponding <strong>views </strong>based on the <strong>requested </strong>actions.</p><h3>Benefits of MVC Architecture:</h3><h3>Separation of concerns:</h3><p>MVC <strong>separates </strong>the <strong>presentation layer</strong> <strong>(View)</strong> from the <strong>business logic (Model)</strong> and<strong> request handling (Controller),</strong> making the system easier to <strong>understand, maintain,</strong> and <strong>test.</strong></p><h3>Reusability:</h3><p>Components in the <strong>MVC architecture</strong> can be <strong>reused </strong>across different <strong>modules </strong>or <strong>projects,</strong> reducing development <strong>time </strong>and <strong>effort.</strong></p><h3>Scalability:</h3><p>MVC supports <strong>modular development,</strong> allowing developers to <strong>scale </strong>individual <strong>components </strong>(e.g., adding new features or integrating third-party services) without <strong>affecting </strong>other parts of the system.</p><h3>Flexibility:</h3><p>MVC provides <strong>flexibility </strong>in choosing <strong>technologies </strong>and <strong>frameworks </strong>for each<strong> layer,</strong> enabling <strong>developers </strong>to use the most suitable <strong>tools </strong>for their specific requirements.</p><h3>One clap = one motivation for me &lt;/3</h3><p>If you enjoyed reading this, please hit the clap button. It really motivates me to work harder! 😊</p><h3>Connect with me</h3><p>If you have any questions, feel free to reach to me on <a href="https://www.linkedin.com/in/abrehan/">LinkedIn</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*8rE_klm5mnQ23sjttWXj-w.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1426a8804c9a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Choosing the Perfect CSS Unit]]></title>
            <link>https://medium.com/@rehannajam2/choosing-the-perfect-css-unit-547ff651654c?source=rss-234f3bf7921e------2</link>
            <guid isPermaLink="false">https://medium.com/p/547ff651654c</guid>
            <category><![CDATA[css-units]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Abdul Rehan]]></dc:creator>
            <pubDate>Sun, 14 Apr 2024 18:33:13 GMT</pubDate>
            <atom:updated>2024-04-14T20:43:57.617Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever wondered which CSS unit to use? We often find ourselves perplexed depending on the scenario, making it difficult to decide whether to choose pixels, em, rem, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5_m38HOw-mGZLJbAiVWE0Q.png" /></figure><p>To understand units, we need to immerse ourselves in the concepts of absolute and relative.</p><h3>Absolute Units</h3><p>An absolute unit has the same size regardless of its parent element or window size. This means that any property using an absolute unit, which adjusts the size of an element, will remain the same when viewed on different devices. They are useful when responsiveness is not a concern. For example, pixels (px) are typically the most popular absolute unit for screens, while centimeters, millimeters, and inches are more common for print.</p><h3>Relative Units</h3><p>A relative unit is useful for styling responsive web applications because they scale relative to their parent elements or window size, depending on the unit used. For example, percentage (%), em, or rem.</p><p>Here are a few use cases where specific units are commonly utilized:</p><p>1) Pixels (px):</p><ul><li>It sets fixed dimensions for elements like borders, margins, paddings, and font-sizes</li></ul><pre>p {<br><br>border: 2px solid red;<br>margin: 10px;<br>padding: 10px;<br><br>}</pre><p>2) Percentage (%):</p><ul><li>It is relative to the parent element’s value for that property</li></ul><pre>.child {<br><br>margin: 10%;<br><br>}</pre><p>You want a child element to have 10% of the parent’s width as a margin so it never fills the entire parent element. If the parent’s size changes, the margin will update accordingly.</p><p>3) rem:</p><ul><li>It is relative to the font-size of the root (e.g. the &lt;html&gt; element)</li></ul><pre>.header {<br><br>font-size: 2rem;<br><br>}</pre><p>The font-size should be twice the size of the root element’s font. This could be how you size your headers because they should all be the same size regardless of the parent container.</p><p>4) em:</p><ul><li>It is relative to the current font-size of the element</li></ul><pre>.child {<br><br>  font-size: 2em;<br><br>}</pre><p>The font-size of a child element, such as a paragraph under a section’s title, will be half the size of its parent’s font-size.</p><h3>One clap = one motivation for me &lt;/3</h3><p>If you enjoyed reading this, please hit the clap button. It really motivates me to work harder! 😊</p><h3>Connect with me</h3><p>If you have any questions, feel free to reach to me on <a href="https://www.linkedin.com/in/abrehan/">LinkedIn</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mYhZew7_6WsKV6dNEM5vBA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=547ff651654c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hooks Handbook: useRef()]]></title>
            <link>https://medium.com/@rehannajam2/hooks-handbook-useref-8037b13b1335?source=rss-234f3bf7921e------2</link>
            <guid isPermaLink="false">https://medium.com/p/8037b13b1335</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[frontend]]></category>
            <dc:creator><![CDATA[Abdul Rehan]]></dc:creator>
            <pubDate>Thu, 22 Feb 2024 12:19:36 GMT</pubDate>
            <atom:updated>2024-02-22T12:19:36.985Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*YRmvVw_AcNsp4iyKrFfLyQ.png" /></figure><blockquote>useRef, an essential hook in React.js, allows you to reference and manipulate DOM elements.</blockquote><p>The useRef hook has a few key takeaways that need to be understood before diving into the examples:</p><ul><li>It creates a mutable variable that won’t trigger component re-renders</li><li>It preserves values and functions much like useState()</li><li>Call useRef at the top level of your component to declare a ref</li><li>The useRef hook does not notify you when its content changes</li><li>Mutating the .current property does not cause a re-render</li></ul><p>Let’s take an example of declaring a useRef hook.</p><pre>// IMPORTS -<br>import { useRef } from &quot;react&quot;;<br><br>const initialValue = {};<br><br>const UseRef = () =&gt; {<br>  const refContainer = useRef(initialValue);<br><br>  return &lt;div&gt;UseRef&lt;/div&gt;;<br>};<br><br>export default UseRef;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*d1A0jZxGMotFMnY4HMfDJA.png" /><figcaption>console.log(refContainer)</figcaption></figure><p>The hook returns a mutable ref object with its .current property initialized to the provided argument, initialValue.</p><p>Let’s consider an input field, where we will capture the entered value using a reference.</p><pre>// IMPORTS -<br>import { useState } from &quot;react&quot;;<br>import { useRef } from &quot;react&quot;;<br><br>const UseRef = () =&gt; {<br>  const refContainer = useRef();<br>  const [value, setValue] = useState(&quot;&quot;);<br><br>  const submitHandler = () =&gt; {<br>    console.log(refContainer.current);<br>  };<br><br>  return (<br>    &lt;div className=&quot;ref__container&quot;&gt;<br>      &lt;input type=&quot;text&quot; ref={refContainer} placeholder=&quot;Enter your name&quot;/&gt;<br>      {value &amp;&amp; &lt;p&gt;My name is: {value}&lt;/p&gt;}<br><br>      &lt;div className=&quot;ref__container__btn&quot;&gt;<br>        &lt;button type=&quot;submit&quot; onClick={() =&gt; submitHandler()}&gt;<br>          Submit<br>        &lt;/button&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default UseRef;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vMyce9K3s1rJc4ckoyTmZw.png" /><figcaption>The current user interface state</figcaption></figure><p>As soon as the user clicks submit, the entire input field is obtained as a reference, as the submit handler is accessing the .current property.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*tuw-NwX_j_gzQBOqbktqvw.png" /><figcaption>console.log(refContainer.current)</figcaption></figure><p>But what if we need to access the entered value? Don’t worry! I’ve got you covered. Just use the .value property.</p><pre>const submitHandler = () =&gt; {<br>    console.log(refContainer.current.value);<br>  };</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*hSZsFld4-63_uvqcaUoywg.png" /><figcaption>console.log(refContainer.current.value)</figcaption></figure><p>Now, we’ll preserve the entered value and render it in our component.</p><pre>// IMPORTS -<br>import { useState } from &quot;react&quot;;<br>import { useRef } from &quot;react&quot;;<br><br>const UseRef = () =&gt; {<br>  const refContainer = useRef();<br>  const [value, setValue] = useState(&quot;&quot;);<br><br>  const submitHandler = () =&gt; {<br>    setValue(refContainer.current.value);<br>  };<br><br>  return (<br>    &lt;div className=&quot;ref__container&quot;&gt;<br>      &lt;input type=&quot;text&quot; ref={refContainer} placeholder=&quot;Enter your name&quot; /&gt;<br>      {value &amp;&amp; &lt;p&gt;My name is: {value}&lt;/p&gt;}<br><br>      &lt;div className=&quot;ref__container__btn&quot;&gt;<br>        &lt;button type=&quot;submit&quot; onClick={() =&gt; submitHandler()}&gt;<br>          Submit<br>        &lt;/button&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default UseRef;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VdVRcGuqWrZ1ZJVSdaR3yw.png" /><figcaption>Updated state</figcaption></figure><p>Everything seems fine at the moment, but how about adding some flair by styling the result in red? We’ll declare a mutable reference to the DOM element I need to style and assign it as the element’s reference.</p><pre>const styleValue = useRef(); // Mutable reference to the DOM element</pre><p>We’ll use useEffect() to apply styling to the DOM element after the component has rendered and the value has been updated.</p><pre> useEffect(() =&gt; {<br>    if (styleValue.current) {<br>      styleValue.current.style.color = &quot;red&quot;; // Applying style to the DOM element<br>    }<br>  }, [value]);</pre><p>Finally, we’ll assign the reference to the element we want to style.</p><pre>// IMPORTS -<br>import { useEffect, useRef, useState } from &quot;react&quot;;<br><br>const UseRef = () =&gt; {<br>  const refContainer = useRef();<br>  const styleValue = useRef();<br>  const [value, setValue] = useState(&quot;&quot;);<br><br>  const submitHandler = () =&gt; {<br>    setValue(refContainer.current.value);<br>  };<br><br>  useEffect(() =&gt; {<br>    if (styleValue.current) {<br>      styleValue.current.style.color = &quot;red&quot;;<br>    }<br>  }, [value]);<br><br>  return (<br>    &lt;div className=&quot;ref__container&quot;&gt;<br>      &lt;input type=&quot;text&quot; ref={refContainer} placeholder=&quot;Enter your name&quot; /&gt;<br>      {value &amp;&amp; &lt;p ref={styleValue}&gt;My name is: {value}&lt;/p&gt;} {/* Giving styleValue as a reference */}<br>      <br>      &lt;div className=&quot;ref__container__btn&quot;&gt;<br>        &lt;button type=&quot;submit&quot; onClick={() =&gt; submitHandler()}&gt;<br>          Submit<br>        &lt;/button&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default UseRef;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKzhU1nzA4P4YACQFjDa_g.png" /><figcaption>Rendered result</figcaption></figure><p>👉 If you like the fifth phase of the Handbook, make sure to leave a review</p><p>👉 The code is available on <a href="https://github.com/abrehan2/Hooks-Handbook">GitHub</a></p><p>👉 You can find me on <a href="https://www.linkedin.com/in/abrehan/">LinkedIn</a></p><blockquote>Thank you for reading Hooks Handbook — useRef()</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8037b13b1335" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>