<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: nagwan sami</title>
    <description>The latest articles on Forem by nagwan sami (@nagwan).</description>
    <link>https://forem.com/nagwan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F106499%2F9e5d07ba-ff3c-43eb-b5ad-801136255d87.jpeg</url>
      <title>Forem: nagwan sami</title>
      <link>https://forem.com/nagwan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nagwan"/>
    <language>en</language>
    <item>
      <title>What is JavaScript?!!</title>
      <dc:creator>nagwan sami</dc:creator>
      <pubDate>Sun, 05 May 2024 14:13:53 +0000</pubDate>
      <link>https://forem.com/smileycode/what-is-javascript-4mac</link>
      <guid>https://forem.com/smileycode/what-is-javascript-4mac</guid>
      <description>&lt;p&gt;If you decided one day to go into a discussion about Javascript, you will probably hear words like &lt;strong&gt;high-level&lt;/strong&gt;, &lt;strong&gt;garbage-collected&lt;/strong&gt;, &lt;strong&gt;interpreted&lt;/strong&gt;, &lt;strong&gt;multi-paradigm&lt;/strong&gt;, &lt;strong&gt;dynamic&lt;/strong&gt;, &lt;strong&gt;prototype-based&lt;/strong&gt;, and &lt;strong&gt;single-threaded&lt;/strong&gt;, which may turn you into 🥱, 😵‍💫  or 🤨.&lt;/p&gt;

&lt;p&gt;Each of these words explains an aspect of javascript, and putting them all together will help draw a mental image of javascript, the main goal of this writing, is to build this mental image 🤝.&lt;/p&gt;

&lt;p&gt;Let’s start 🏁&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;High-level&lt;/strong&gt; 👀: It simply means, we do not have to worry about the hardware staff, that’s it 🤷‍♀️. &lt;/p&gt;

&lt;p&gt;Each program that runs on a computer needs some hardware resources, to do its work. Some programming languages are designed to be more developer-friendly, which means enabling the developers to focus on the functionality of the program rather than the low-level details of machine instructions, such as Python and Javascript these are called high-level programming languages. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Garbage collection&lt;/strong&gt; 🧹: This is an algorithm inside the javascript engine that automatically removes old unused variables from the computer memory, to not block it with unnecessary stuff.&lt;/p&gt;

&lt;p&gt;Oh, and for the weird word &lt;em&gt;engine&lt;/em&gt;, do not worry we are going to discuss it in detail in another writing 🤞, temporarily consider it as a Javascript lab, it is where the javascript code is executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interpreted&lt;/strong&gt; 👩‍💻: Saying that Javascript is an interpreted language, is not 100% true, let’s break this down…&lt;/p&gt;

&lt;p&gt;There are two main ways for a programming language to be translated into machine language 0s &amp;amp; 1s, &lt;strong&gt;compiling&lt;/strong&gt; and &lt;strong&gt;interpreting&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Javascript uses a combination of the two techniques, so it is not purely an interpreted language.&lt;/p&gt;

&lt;p&gt;You may now wonder, so why it is famed as an interpreted language?!! &lt;/p&gt;

&lt;p&gt;There are many reasons for this that we may discuss later but for now, let’s say it is a kind of label javascript earned in its earliest days as browsers’ engines were mainly dependent on interpreting for code execution.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Multi-paradigm&lt;/strong&gt; 🤓: a programming paradigm is a mindset and style in code structure flow and techniques.&lt;/p&gt;

&lt;p&gt;The point is that some programming languages are limited to a single paradigm unlike Javascript, you choose your favourite paradigm and Javascript will stay as powerful as it is✌️, this is a kind of flexibility, don't you agree🤔?!&lt;/p&gt;

&lt;p&gt;With the mention of flexibility, let’s discuss… &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dynamic&lt;/strong&gt; 🕶: While using a dynamically typed language we do not need to explicitly set a variable data type, in other words, in dynamically typed languages data types are associated with values, not with the variables this explains why when it comes to initialising a variable in javascript we do not need to set a specific data type to it, it is just set when the code is executed, and we are still able to change a variable datatype by reassigning it, the debate around if this considered a pro or a con of JavaScript is there, but it is out of our scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prototype-base&lt;/strong&gt; 🧬: In JavaScript, almost everything has a template from which we can simply make a copy, this copy we can modify as much as we can but it still INHERITS the features of the main template we cloned it from. The template is what is called a prototype.&lt;/p&gt;

&lt;p&gt;Now let's move to our last term which is…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Single-threaded&lt;/strong&gt; 🫡: We can think of the word thread as it means task, so when we say single-threaded it means managing one task at a time, let’s try to understand this a little bit better.&lt;/p&gt;

&lt;p&gt;There is a tight relationship between the core design of a programming language and the environment this language runs on.&lt;/p&gt;

&lt;p&gt;Some programming languages are designed on their core to support multiple threads, like Python for example, while others are not like Javascript.&lt;/p&gt;

&lt;p&gt;There should be a kind of harmony between a programming language and the environment it runs on, so if a language supports multiple threading the environment it runs on has to support it, otherwise, the language’s multithreading capabilities would be disabled which will cause many errors and unexpected behaviour, depending on the core code of the program. &lt;/p&gt;

&lt;p&gt;Javascript was originally designed to make web pages more vivid, and browsers are single-threaded by nature, so even if the Javascript core is designed to handle multiple threading it wouldn't be useful in the traditional sense of web pages.&lt;/p&gt;

&lt;p&gt;In light of the previously mentioned points, you may wonder, since JS code execution is done sequentially if any code takes a long time to execute, it will block anything that needs to be executed after that, but as we are interacting with web pages this does not seem to be the case!! so how 🤔?!! &lt;/p&gt;

&lt;p&gt;The answer to this question requires going through the process of JavaScript code execution which we may cover in another writing, but simply, there is an execution mechanism in browsers that takes asynchronous actions, actions with delays e.g. API calls, to be handled away from the main thread, then these actions get back again to the main thread once they are done, this is not so nuanced, just to cut a long story short.&lt;/p&gt;

&lt;p&gt;Hope this helps and thanks for reading. If you have any questions or topics you want me to write about I will be happy to help❤️.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>frontend</category>
    </item>
    <item>
      <title>why should we use React hooks?!</title>
      <dc:creator>nagwan sami</dc:creator>
      <pubDate>Tue, 01 Dec 2020 17:43:37 +0000</pubDate>
      <link>https://forem.com/nagwan/why-should-we-use-react-hooks-1aa8</link>
      <guid>https://forem.com/nagwan/why-should-we-use-react-hooks-1aa8</guid>
      <description>&lt;p&gt;Hooks 🤔 Hooks 👀 Hooks 🤨 &lt;br&gt;
What Hooks are 🙄&lt;/p&gt;

&lt;p&gt;Personally cannot see better answer than the one mentioned in the official react docs 🤷&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Mmm, Not convinced yet 😏, why we should even seek to replace classes 🙄&lt;/p&gt;

&lt;p&gt;It is not a replacement rather than a recommendation and a new way of writing more elegant and neat code.&lt;/p&gt;

&lt;p&gt;let's discuss one of the core and maybe the main difference between function and class component, and pointing out how hooks are gonna help us writing effortless code.&lt;/p&gt;

&lt;p&gt;I LOVE this part😉&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Class components do not capture the rendered values, while functional components DO&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now it may be a 🙃, 🤔 or 🤨 moment, but wait ✋&lt;/p&gt;

&lt;p&gt;An application UI is the representative of the current application state, with every change in the application state, or props we got a fresh new render for our UI, with everything included within, EVERYTHING here means everything even the event handlers used. Everything in the UI belongs to an EXACT particular render with EXACT particular data, &lt;em&gt;(either state or props)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOW, WHAT 🙄?!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/function-component-pq68t" rel="noopener noreferrer"&gt;check this snippet&lt;/a&gt; and let's do the following...&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;select a friend then press &lt;code&gt;send msg&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;let's say you choose &lt;code&gt;Adam&lt;/code&gt; you will see &lt;code&gt;you choose Adam&lt;/code&gt; and after 3 seconds a message &lt;code&gt;Hi Adam&lt;/code&gt; is printed, fair enough!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: &lt;em&gt;the 3 seconds because we use &lt;code&gt;setTimeOut()&lt;/code&gt; to wait for a while before printing the message&lt;/em&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;select a friend then press &lt;code&gt;send msg&lt;/code&gt;, and before the 3 seconds pass, select a different friend and press &lt;code&gt;send msg&lt;/code&gt; again 👩‍💻&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;let's say in the first time you selected &lt;code&gt;Sarah&lt;/code&gt; and then &lt;code&gt;Adam&lt;/code&gt;, &lt;br&gt;
what happened is, with your first click you see &lt;code&gt;you choose Sarah&lt;/code&gt; and once you press again it changes to be &lt;code&gt;you choose Adam&lt;/code&gt;,&lt;br&gt;
&lt;strong&gt;BUT&lt;/strong&gt; after 3 seconds the message printed is &lt;code&gt;Hi sarah&lt;/code&gt;, &lt;em&gt;your first choice&lt;/em&gt;, and after other 3 seconds the message changes to be &lt;code&gt;Hi adam&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;we agree this is the right behaviour and nothing abnormal or creepy is happening here, BUT again we need to emphasis this is happening because the function component &lt;strong&gt;&lt;em&gt;capture&lt;/em&gt;&lt;/strong&gt; the values rendered. &lt;/p&gt;

&lt;p&gt;We may think of &lt;em&gt;capture&lt;/em&gt; as saving or preserving the data and knows what to do with, which is not the case in a class component, let's check &lt;a href="https://codesandbox.io/s/class-components-eud1p" rel="noopener noreferrer"&gt;this snippet&lt;/a&gt; and repeat the exact same steps we have made above...&lt;/p&gt;

&lt;p&gt;When we select a friend, let's say &lt;code&gt;Adam&lt;/code&gt;, and before 3 seconds reselect another friend, &lt;code&gt;Sarah&lt;/code&gt;, after 3 seconds we will not see our first choice, instead we are gonna see our last choice &lt;code&gt;Hi sarah&lt;/code&gt;😵&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;let's take it step by step:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;both function and class components access the selected value from their &lt;code&gt;props&lt;/code&gt;, which is &lt;strong&gt;immutable&lt;/strong&gt; CANNOT BE CHANGED, but in the class component, the &lt;code&gt;props&lt;/code&gt; are accessible via &lt;code&gt;this&lt;/code&gt;, which is &lt;strong&gt;mutable&lt;/strong&gt; CAN BE CHANGED and it is meant to be.&lt;/p&gt;

&lt;p&gt;so the props value gonna printed in the class component is taken from the new &lt;code&gt;this&lt;/code&gt;, actually the very new &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;to solve 🔧 this, and make a class component &lt;strong&gt;capture&lt;/strong&gt; its values during different renders we used to follow many approaches:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;saving the props value in a variable😏
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;friend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;friend&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which is not the choice recommended, what if we have a fair number of props or we needed even to access functions and state, it is gonna be a real mess🤦.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tying up everything we need to a specific render, &lt;a href="https://codesandbox.io/s/class-components-capture-values-rzt27" rel="noopener noreferrer"&gt;check this snippet&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;using this way everything needed is connected to a specific render.&lt;/p&gt;

&lt;p&gt;this one of the core difference between class and functional components, and this difference actually used to cause a lot of problems and most of us stuck there for a while one day, but we had to use class components as we needed state, and lifecycle hooks and there was no alternative so we had to, but now with hooks, we do not have to any more😉.&lt;/p&gt;

&lt;p&gt;Hope this helps, thanks for reading and if you have any questions or any topic you want me to write about I will be happy to help, and your comments and constructive notes are more than welcome ❤️&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>components</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React synthetic events</title>
      <dc:creator>nagwan sami</dc:creator>
      <pubDate>Tue, 28 Jul 2020 21:27:02 +0000</pubDate>
      <link>https://forem.com/smileycode/react-synthetic-events-34e5</link>
      <guid>https://forem.com/smileycode/react-synthetic-events-34e5</guid>
      <description>&lt;p&gt;I would like a fresh start 😇&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt;&lt;br&gt;
   A JavaScript library used to build vivid shiny user interfaces🥰.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event&lt;/strong&gt;&lt;br&gt;
   The different ways the user uses to interact with an application, e.g. click, mouseover, input… etc&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Synthetic&lt;/strong&gt; &lt;br&gt;
    It is a ...🤔 Let's understand how React deals with events first.&lt;/p&gt;

&lt;p&gt;React listens to every event at the document level, after receiving an event from the browser, React wraps this event with a wrapper that has the same interface as the native browser event, which means we still can use methods like &lt;code&gt;preventDefault()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So what is the need for this wrapper?!!😏&lt;/p&gt;

&lt;p&gt;think of a situation where the exact same event has different names across different browsers.&lt;/p&gt;

&lt;p&gt;imagine an event that fires when the user winks😉, this event in chrome called &lt;strong&gt;A&lt;/strong&gt; in Safari called &lt;strong&gt;B&lt;/strong&gt;, in such case, we will need to make different implementations for each browser😵.&lt;/p&gt;

&lt;p&gt;What this wrapper does is registering all the different names for the same event effect, &lt;em&gt;&lt;strong&gt;winking&lt;/strong&gt; in our case&lt;/em&gt;, with only one name, so in a case when we want to listen to our winking effect instead of being listening to &lt;strong&gt;A&lt;/strong&gt; for chrome and &lt;strong&gt;B&lt;/strong&gt; for Safari we just use &lt;strong&gt;onWink&lt;/strong&gt;, which is the wrapper react creates around the real event.&lt;/p&gt;

&lt;p&gt;So whenever we are triggering an event in a React component, we are not actually dealing with the real DOM event, instead, we are dealing with React's custom event type, &lt;strong&gt;a synthetic event&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now close your eyes😴, but not this kind of closing😅, and in your memory remember all the onClick(s), onBlur(s), onChange(s) you have ever used in your react components, these are not the real ones, these are react's synthetic events😇.&lt;/p&gt;

&lt;p&gt;So we do not have to think anymore about different browsers implementations, react makes creating cross-browsers applications much less painful, which means we can concentrate more on adding browsers prefixes to our CSS properties😅.&lt;/p&gt;

&lt;p&gt;This is not the end, as this is not the only bright side of React's synthetic events😃. Another catchy benefit of synthetic events is that React reuses these events objects, by pooling them, which results in performance gains.&lt;/p&gt;

&lt;p&gt;Once the event handler is invoked, &lt;em&gt;an event handler is a method executed once an event is triggered&lt;/em&gt;, all the properties on this event object will be nullified, &lt;em&gt;set to their empty/default states&lt;/em&gt;, to be ready to be reused again.&lt;/p&gt;

&lt;p&gt;Till this point, everything looks amazing, and you might feel🧐, but you might also go through some 🤨, 🙄, or even 😤 moments once you see &lt;strong&gt;Warning: This synthetic event is reused for performance reasons in the browser&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What makes most of us go through 🤨, 🙄, and 😤 moments, is not that despite it is warning it is written in red, but it is actually accessing any event properties in the event handler function fails&lt;/p&gt;

&lt;p&gt;Imagine the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ExampleComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handelArrowBtn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//down arrow button&lt;/span&gt;
      &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;38&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// up arrow button&lt;/span&gt;
      &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
        &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onKeyDown&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handelArrowBtn&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ExampleComponent&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This counter will neither be increased nor decreased. And our lovely red warning will be printed in the browser console.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's see what is happening here...
&lt;/h3&gt;

&lt;p&gt;After the event handler function, &lt;em&gt;&lt;code&gt;handelArrowBtn()&lt;/code&gt; in our case&lt;/em&gt;, is invoked the object of our synthetic event, &lt;code&gt;onKeyDown&lt;/code&gt; in our case, gets nullified, the old values to the keys within this object are no longer exist, the event object returned to its original state to be ready to be reused, and as this is an object, so our &lt;code&gt;handelArrowBtn()&lt;/code&gt; has access to it by reference which means our function now has access to the event object with its original state(nullified version).&lt;/p&gt;

&lt;h4&gt;
  
  
  So, how can we solve this?!😯
&lt;/h4&gt;

&lt;p&gt;Actually, this can be solved in many ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Store the event property we need
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handelArrowBtn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;keyCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;38&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or we may also pass the properties we want as arguments to the event handler function instead of directly accessing it from the function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
        &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onKeyDown&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handelArrowBtn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using &lt;code&gt;event.persist()&lt;/code&gt; which will remove the synthetic event from the pool, which enables us accessing the event object properties in our code
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handelArrowBtn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;persist&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;38&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this helps and thanks for reading. If you have any questions or topics you want me to write about I will be happy to help❤️.&lt;/p&gt;

</description>
      <category>react</category>
      <category>syntheticevents</category>
      <category>web</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Javascript var, let and const</title>
      <dc:creator>nagwan sami</dc:creator>
      <pubDate>Sat, 18 Jul 2020 20:15:59 +0000</pubDate>
      <link>https://forem.com/smileycode/var-let-and-const-in-js-597n</link>
      <guid>https://forem.com/smileycode/var-let-and-const-in-js-597n</guid>
      <description>&lt;p&gt;I hate intros but we must have one 🤷&lt;/p&gt;

&lt;p&gt;Before 2015 javascript developers used to use &lt;code&gt;var&lt;/code&gt; as the keyword to declare variables, and life was easy, but not calm 😅&lt;/p&gt;

&lt;p&gt;With using &lt;code&gt;var&lt;/code&gt; to declare variables developers had to fight in many fields…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Redeclaration&lt;/strong&gt; 🤦‍♀️&lt;br&gt;
Believe it or not, with &lt;code&gt;var&lt;/code&gt; you can use the same variable name as many times as you can without facing any errors, but you have to be ready for the unexpected results😬.&lt;/p&gt;

&lt;p&gt;imagine the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Our New User&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sarah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Hi Sarah&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing that comes to your mind now is, so what?!,&lt;br&gt;
I redefine the variable, what the problem with that?!🤨 It is not a problem at all, as long as you know that is exactly what you want to do, not just that you forget that this variable has already declared before🧐.&lt;/p&gt;

&lt;p&gt;The real problem with redeclaration comes with large apps, and once you forget that you have used the same variable name before.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;DISCLAIMER&lt;/em&gt;✋: this won`t be a problem in case you have solid memory that helps you remember all the variables you have declared within a specific scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scope&lt;/strong&gt; 😵&lt;br&gt;
The line above ends with the word &lt;em&gt;SCOPE&lt;/em&gt;, before digging deeper, let`s first understand what scope is, think of scop as a &lt;em&gt;box&lt;/em&gt; within which some functions and variables are accessible.&lt;/p&gt;

&lt;p&gt;Chances are variables declared using &lt;code&gt;var&lt;/code&gt; keyword are limitless, except if they are declared within a function.&lt;/p&gt;

&lt;p&gt;This means if a variable is not inside a function it will be accessible in the whole app😨.&lt;/p&gt;

&lt;p&gt;Now try to connect this point with the one before, &lt;em&gt;redeclaration&lt;/em&gt;, &lt;br&gt;
now developers have to remember all the variables they have declared in the global/function scope else they find themselves trapped with results they never expect.&lt;/p&gt;

&lt;p&gt;Imagine the following…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Our New User&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sarah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Sarah&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Sarah&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The log inside the if block makes sense, as we are logging the variable defined within this block, but the log outside the if block is the one highlights the problem, it supposed to prints &lt;em&gt;“Our New User”&lt;/em&gt; the value of the variable declared outside the if block, but what happens here is the variable &lt;code&gt;name&lt;/code&gt; declared within the if block totally replaced the one declared outside the if block and here we have to mention hoisting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hoisting&lt;/strong&gt; 😧&lt;br&gt;
Hoisting is the process of lifting variables and functions declarations to the top of their scope.&lt;/p&gt;

&lt;p&gt;Variables declared with the keyword &lt;code&gt;var&lt;/code&gt; hoisted to the top of the global/function scope and initialized with the value undefined.&lt;/p&gt;

&lt;p&gt;Connecting this with the previous point,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Our New User&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sarah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Sarah&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Sarah&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;we now can find out what is happening here, the new variable declared within the if block is hoisted to the top of the function and of course replaced the original one, which justifies why the two console logs prints the same result.&lt;/p&gt;

&lt;p&gt;Now as we went through the problems js developers spent a long time fighting with, now let`s move forward how ES2015 saved our day 😉. &lt;/p&gt;

&lt;p&gt;Regarding redeclaration, variables declared using either &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; cannot be redeclared within the same scope.&lt;/p&gt;

&lt;p&gt;Mentioning the scope, both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are block-scoped, &lt;em&gt;a block of code is any set of code within {}&lt;/em&gt;, which means if a variable declared using either &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; within {} it won`t be accessible outside these {}, despite they are hoisted to the top of their scop, which is the {}.&lt;/p&gt;

&lt;p&gt;Now let's check out our sayHi function...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Our New User&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="c1"&gt;// out of our scope&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sarah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Sarah&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Our New User&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it works as expected the new variable declared within the if block stays within the if block and it does not affect the one outside the if block &lt;/p&gt;

&lt;p&gt;But now the question is, which one to use &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; 🤔?!&lt;br&gt;
The answer is, whatever you want😇, just remember that variables declared with &lt;code&gt;let&lt;/code&gt; can be updated while those created with &lt;code&gt;const&lt;/code&gt; cannot.&lt;/p&gt;

&lt;p&gt;Hope this helps and thanks for reading. If you have any questions or topics you want me to write about I will be happy to help❤️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
