<?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 Shameera Carrim on Medium]]></title>
        <description><![CDATA[Stories by Shameera Carrim on Medium]]></description>
        <link>https://medium.com/@shameeracarrim?source=rss-a6523f370868------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RQJQV3CLgmlPwXEa7gdN8w.png</url>
            <title>Stories by Shameera Carrim on Medium</title>
            <link>https://medium.com/@shameeracarrim?source=rss-a6523f370868------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 19:22:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shameeracarrim/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[Sorting in JS]]></title>
            <link>https://shameeracarrim.medium.com/sorting-in-js-cd270fe5be16?source=rss-a6523f370868------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd270fe5be16</guid>
            <dc:creator><![CDATA[Shameera Carrim]]></dc:creator>
            <pubDate>Thu, 12 Jun 2025 02:00:40 GMT</pubDate>
            <atom:updated>2025-06-12T02:00:40.996Z</atom:updated>
            <content:encoded><![CDATA[<h4>Sort?? What is sorting? 😳</h4><p>Sorting is simply <strong>arranging meaningful information or even useless data into a specific order</strong>.</p><p>But what does “arranging meaningful or useless data” actually mean?</p><p>Well, a silly-sounding question maybe — but a good one for curious devs! 😄</p><h4>🧹 Useless Data</h4><p>Think of various items on a desk: stapler, eraser, pen, pencil.<br> You may not gain much from sorting them — but you <em>can</em> arrange them by <strong>size</strong> just for fun or tidiness.</p><h4>📊 Meaningful Data</h4><p>Now take something more practical: bakery orders from a frequent customer.<br> You have their purchase history. As a seller, you might want to <strong>sort the receipts by item name or quantity</strong> to easily spot which items were bought the most.</p><h3>🧠 How to Sort Information</h3><ul><li>For <strong>meaningless/random data</strong>, the first step is to <strong>categorize</strong> it.<br> For example, from a mess of items, group similar ones like color pencils, books, or colorful pens.</li><li>Then, <strong>sort within the category</strong> — for example, arrange the color pencils in a rainbow color order.</li></ul><h3>🍰 Back to the Bakery Example</h3><p>In a bakery, you may have different types of records:</p><ul><li>Raw materials (e.g., flour, sugar, eggs)</li><li>Utility expenses (e.g., electricity, water)</li><li>Sales receipts (payments from customers)</li></ul><p>To calculate <strong>gross profit</strong>, the seller must:</p><ol><li>Add up sales records</li><li>Add up expenses (after identifying and categorizing them)</li><li>Sort both by date to understand profit trends over time</li></ol><p>If the seller wants to find out which items are selling the most, they should <strong>sort items by sales quantity</strong>.</p><p>⚙️ Now Let’s Talk Sorting in JavaScript</p><ol><li>The bit by step mode</li></ol><p>Now stop with baking 🙂. Let’s come to usual boring examples. What do we learn in nursery. To count 😂 1,2,3,4,5,6,7,8,9,10. Okay back to dev mode. How to retain this in memory. We assign them to a variable. But to what variable type? Array data type dummy 😃</p><pre>const numberList = [1,2,3,4,5,6,7,8,9,10]</pre><p>So we can either keep this as it is since it’s organized into an order, small to big. But how do we make it big to small? Just a random example.</p><pre>let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];<br>let sortedArr = [];<br>let used = new Array(arr.length).fill(false); // Track which elements have been used<br><br>while (sortedArr.length &lt; arr.length) {<br>  let max = -Infinity; // Start with a very small number<br>  let maxIndex = -1;<br>  <br>  // Find the largest unused element<br>  for (let i = 0; i &lt; arr.length; i++) {<br>    if (!used[i] &amp;&amp; arr[i] &gt; max) {<br>      max = arr[i];<br>      maxIndex = i;<br>    }<br>  }<br>  <br>  // Add the largest value to the sorted array<br>  sortedArr.push(max);<br>  used[maxIndex] = true; // Mark this element as used<br>}<br><br>console.log(sortedArr);</pre><p>2. The easy peasy lemon squeezy way</p><p>Look up and you can see a lot of code lines. Thankfully even as a beginner you can use Array.prototype.sort() and escape from writing multiple lines of code. Phew 😅</p><p>Ways to sort list of numbers🔢</p><pre>const numberList = [7, 8, 1, 2, 5, 6, 9, 3, 4, 10];<br><br>// ⚠️ Default sort treats numbers as strings<br>numberList.sort(); // [1, 10, 2, 3, 4, 5, ...]<br>console.log(&quot;Default:&quot;, numberList);<br><br>// 🔽 Descending<br>numberList.sort((a, b) =&gt; b - a);<br>console.log(&quot;Descending:&quot;, numberList);<br><br>// 🔼 Ascending<br>numberList.sort((a, b) =&gt; a - b);<br>console.log(&quot;Ascending:&quot;, numberList);</pre><p>Ways to sort list of terms 🔠🔡</p><pre>const fruits = [&quot;Banana&quot;, &quot;Orange&quot;, &quot;Apple&quot;, &quot;Mango&quot;];<br><br>// 🔼 Ascending (default)<br>fruits.sort();<br>console.log(&quot;Default (Ascending):&quot;, fruits);<br><br>// 🔽 Descending<br>fruits.sort((a, b) =&gt; b.localeCompare(a));<br>console.log(&quot;Descending:&quot;, fruits);<br><br>// 🔼 Ascending with localeCompare (explicit)<br>fruits.sort((a, b) =&gt; a.localeCompare(b));<br>console.log(&quot;Ascending:&quot;, fruits);   </pre><p>Sorting may seem like a simple concept, but it’s a <strong>powerful tool</strong> that helps bring order to chaos — whether you’re organizing desk items, analyzing bakery sales, or handling data in code.</p><p>JavaScript makes sorting easy with built-in methods like .sort(), but it&#39;s still valuable to understand what&#39;s happening under the hood. Knowing both manual and shortcut approaches gives you a better grip on how sorting works — and when to use custom logic.</p><p>So next time you’re faced with a pile of unordered data (or cupcakes 🍰), remember: a little sorting can go a long way in making sense of it all. Happy coding and keep things sorted — both in code and in life! 😄</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd270fe5be16" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NPM]]></title>
            <link>https://shameeracarrim.medium.com/npm-6e0d7a156afa?source=rss-a6523f370868------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e0d7a156afa</guid>
            <category><![CDATA[node]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shameera Carrim]]></dc:creator>
            <pubDate>Sat, 09 Sep 2023 17:14:34 GMT</pubDate>
            <atom:updated>2023-09-09T17:15:36.721Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever wondered what NPM is and why it’s a must-know tool for JavaScript developers? 😐</p><p>NPM, short for Node Package Manager. NPM is the go-to solution for managing packages and dependencies in your Node.js projects. In this article, we’ll dive into the importance of NPM and guide you through the process of installing and managing packages.</p><p><strong>Importance of NPM</strong></p><ol><li>Package management — In software development, projects often rely on external libraries and code modules. NPM makes it easy to discover, install, and keep track of these dependencies. Devs do not have to waste time manually downloading and organizing libraries.</li><li>Version Control and Dependency Resolution — It ensures project uses the correct versions of packages, it can specify the exact version or a version range for each dependency, preventing compatibility issues. When you run npm install, NPM automatically resolves and installs the appropriate versions, saving you from potential obstacles down the road.</li></ol><p><strong>Installing and Managing Packages</strong></p><p>Prerequisite*</p><p>You have to install Node.js. NPM is bundled with it. Once installed we can access NPM.</p><p>To check node is installed we can execute below command in terminal</p><pre>npm -v</pre><p>This command should display the installed NPM version. If it’s not installed, you can download and install Node.js from the official website (<a href="https://nodejs.org/">https://nodejs.org/</a>), which includes NPM.</p><p><strong>Creating a node project</strong></p><p>To start using NPM in your project, you first need to initialize a package.json file. This file serves as a manifest for your project, listing its metadata and dependencies.</p><p>Navigate to your project directory/folder and open up the terminal and type npm init. This will create a package.json file.</p><p><strong>Adding a dependency</strong></p><p>Below command will install the relevant package to the node project.</p><pre>npm install package-name --save</pre><p>If you’re installing a package for development purposes, such as a testing library or build tool, you can use the --save-dev flag:</p><pre>npm install package-name --save-dev</pre><p>This ensures that the package is listed under the dependencies or devDependencies section of your package.json, depending on the flag you used.</p><p><strong>Updating Packages</strong></p><p>To keep your project up to date with the latest package versions, you can use the npm update command:</p><pre>npm update</pre><p>Read more about NPM from official website (<a href="https://docs.npmjs.com/cli/v6/commands/npm-install">https://docs.npmjs.com/cli/v6/commands/npm-install</a>)</p><p><strong>There are alternatives to NPM.</strong></p><p>Few of the package managers are;</p><p><strong>Yarn</strong>:</p><ul><li>Developed by Facebook.</li><li>Faster and more secure than NPM.</li></ul><p><strong>pnpm</strong>:</p><ul><li>Space-efficient package manager.</li><li>Uses a shared global store.</li><li>Faster installations and removes unused packages.</li></ul><p><strong>Bower</strong>:</p><ul><li>Front-end package manager.</li><li>Designed for managing web assets.</li><li>Useful for front-end libraries and assets.</li></ul><p>Even though there are alternatives, devs still use NPM .With NPM you’ll find that building and maintaining Node.js applications becomes very efficient. So, go ahead and build node projects using the power of NPM. 🔥</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e0d7a156afa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Framer motion part 1]]></title>
            <link>https://shameeracarrim.medium.com/framer-motion-part-1-1cf6c377d118?source=rss-a6523f370868------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cf6c377d118</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[framer-motion]]></category>
            <dc:creator><![CDATA[Shameera Carrim]]></dc:creator>
            <pubDate>Sat, 09 Sep 2023 16:22:28 GMT</pubDate>
            <atom:updated>2023-09-09T16:22:28.474Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is Framer motion?</strong> 🤔</p><p>It is a powerful motion library for react.</p><p>This library allows devs 🧑🏻‍💻👩🏻‍💻 to add animations to React components with less code.</p><p><strong>How to add Framer motion to react app?</strong></p><p>After setting up the react app, install this dependency using below command.</p><p><strong>npm :</strong> npm i framer-motion</p><p><strong>yarn :</strong> yarn add framer-motion</p><p><strong>How to create a animated component using Framer Motion?</strong></p><p>Step 1 : Create a new JSX file</p><p>Framer motion API provide motion component.</p><p>Step 2 : Import motion element from Framer motion</p><pre>import {motion} from &quot;framer-motion&quot;;</pre><p>Step 3 : Write the component within the JSX</p><pre>import {motion} from &quot;framer-motion&quot;;<br><br>const MotionComponent = () =&gt; {<br> return (<br>   &lt;motion.div/&gt;<br>  )<br>}<br><br>export default MotionComponent;</pre><p>To perform basic animation we can use motion component with animate prop, which is used frequently to animate a component.</p><pre>// Simple animation to move in the X axis<br><br>&lt;motion.div animate={{ x: 100 }} /&gt;</pre><pre>// Simple animation to move an element in the X and Y axis<br><br> &lt;motion.div<br>          animate={{<br>            y: 50,<br>            x: 10<br>          }}<br>        &gt;<br>          &lt;InnerBox /&gt;<br> &lt;/motion.div&gt;</pre><p><strong>These below props can be used along with animate prop to create smooth animations</strong></p><ol><li>transition - handle movement of element during animation</li><li>initial - animation at start or entrance</li><li>exit - animation at end or exit</li></ol><p><strong>Let’s add an animation to an element when onClick event is triggered</strong></p><p>Suppose we want to move or rotate an React component when a button is clicked, but how to do it smoothly?</p><p>We can keep a state to manage track of button click and intention.</p><p>Lets name the intention rotate and configure useState.</p><pre>const [rotate, setRotate] = useState(false);</pre><p>So whenever we want the button to rotate we will pass value to setRotate.</p><pre>&lt;motion.div<br>          animate={{<br>            rotate: rotate ? 360 : 0<br>          }}<br>          initial={{ scale: 1 }}<br>        &gt;<br>          &lt;InnerBox<br>            color=&quot;lightblue&quot;<br>            onClick={() =&gt; {<br>            // this action will pass true when rotate is false that will rotate element<br>            // 360 degrees<br><br>            // this action will pass false when rotate is true that will rotate element<br>            // 0 degrees (back to orginal)<br>              setRotate(!rotate);<br>            }}<br>          /&gt;<br>&lt;/motion.div&gt;</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Flearning-framer-motion-y94tnp%3Ffontsize%3D14%26hidenavigation%3D1%26theme%3Ddark&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fy94tnp&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fy94tnp%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/69e90cbd243f78b089c0255c3348c672/href">https://medium.com/media/69e90cbd243f78b089c0255c3348c672/href</a></iframe><p>In the above preview there is another element below the rotating element.</p><p><strong>Transitions</strong></p><p>When can we use transitions?</p><p>Suppose we need to slow down the movement of the animation or speed up. We can control that by the duration. The higher the duration, the slower the animation will be completed. The lesser the duration the faster the animation will be completed. We can also control start of animation movement by delay property.</p><p>When you click on that it will move in the x- axis in a slower speed. We can achieve this transition by using below keys.</p><ol><li>type:&#39;tween&#39; — denotes duration-based animation</li><li>duration — exact duration for the animation to continue</li><li>delay —delay the start of the movement</li></ol><pre>&lt;motion.div<br>          animate={{<br>            x: move ? 30 : -10<br>          }}<br>          transition={{<br>            /* delay the start of the movement*/<br>            delay: 1,<br>            /* manage movement duration */<br>            type: &quot;tween&quot;,<br>            duration: 1<br>          }}<br>        &gt;<br>          &lt;InnerBox<br>            color=&quot;lightgray&quot;<br>            onClick={() =&gt; {<br>              setMove(!move);<br>            }}<br>          /&gt;<br> &lt;/motion.div&gt;</pre><p>When referring to above code snippet, we can see there is a move animation planned to happen when InnerBox element is clicked. When transitions are set, the movement of element in X is delayed by 1 second and it will take 1 second to move the InnerBox.</p><p>So this is for the part 1 😋. I will write more about how to bounce an element when we click in the upcoming article. 😺</p><p>Adios 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1cf6c377d118" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing good code]]></title>
            <link>https://shameeracarrim.medium.com/writing-good-code-64101b5a80ac?source=rss-a6523f370868------2</link>
            <guid isPermaLink="false">https://medium.com/p/64101b5a80ac</guid>
            <category><![CDATA[good-code]]></category>
            <category><![CDATA[practice]]></category>
            <dc:creator><![CDATA[Shameera Carrim]]></dc:creator>
            <pubDate>Sat, 25 Feb 2023 14:47:53 GMT</pubDate>
            <atom:updated>2023-02-25T14:47:53.303Z</atom:updated>
            <content:encoded><![CDATA[<p>So we developers write code to solve day to day problems. But can we just go ahead and write code and automate a task and be satisfied it anyhow worked?</p><p><strong>Nope!</strong></p><p>There are 3 pillars developers should keep in mind :)</p><ol><li>Write readable code</li><li>Speed (Code efficiency) — The code should have big O time complexity that is efficient</li><li>Memory — Memory usage of code. Again big O is used to measure space complexity.</li></ol><p>Code solutions usually have a tradeoff between space and memory.</p><ul><li>If code written to execute faster, then have to sacrifice memory.</li><li>If code written to utilize less memory, then have to sacrifice increased speed.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64101b5a80ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data types in JS]]></title>
            <link>https://shameeracarrim.medium.com/data-types-in-js-e0d4f23dfec5?source=rss-a6523f370868------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0d4f23dfec5</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[datatypes-in-javascript]]></category>
            <dc:creator><![CDATA[Shameera Carrim]]></dc:creator>
            <pubDate>Sun, 23 Oct 2022 09:34:37 GMT</pubDate>
            <atom:updated>2022-10-23T14:30:31.633Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is a data type?</strong></p><blockquote>“A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error.” (techtarget, <a href="https://www.techtarget.com/searchapparchitecture/definition/data-type">data type</a>, November 2016)</blockquote><p>From the above referenced quote, we can understand that a variable defined with a specific data type will describe what specific type of value it has and also only allows to assign values to the variable that describes the data type.</p><p>For example, a variable that is defined with string data type, will tell us that variable holds a value of string or variable only allows to be assigned a value of string.</p><p><strong>How to declare a variable with a data type in JS?</strong></p><p>In JavaScript, programmers cannot specify data types when declaring variables. Because it is a weakly typed programming language and will make assumptions of the data type based on the value the programmer assign.</p><blockquote>JavaScript is considered a “weakly typed” or “untyped” language. The type in question here are the data types — nothing to do with typing from your keyboard. For programmers coming from C++ or Java, two strongly typed languages, this means that JavaScript will figure out what type of data you have and make the necessary adjustments so that you don’t have to redefine your different types of data. (oreilly, <a href="https://www.oreilly.com/library/view/javascript-design/0735711674/0735711674_ch02lev1sec3.html">JavaScript Design</a> ,William B. Sanders)</blockquote><p>For more information about JavaScript being weekly typed programming language, read an article by Charlie Levine, <a href="https://javascript.plainenglish.io/javascript-is-drunk-ec4ca3a84608">JavaScript Is Drunk</a>.</p><p><strong>So how is variable declared in JS?</strong></p><p>Variables in JS are declared in below syntax.</p><pre>[variable type] [variable name] = [value]</pre><pre>var name = &quot;John Doe&quot;;</pre><p>JavaScript will read and figure the variable holds a value of string.</p><p>Also we can check the data type of a variable by using typeof operator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*wRsGuMSmKBy3A1vZazoxWw.png" /></figure><p><strong>So what are the data types of JS?</strong></p><p><strong>string</strong></p><p>string variable is a collection of alphanumeric characters and symbols.</p><pre>var name = &quot;John Doe&quot;<br>var heightDesc = &quot;John doe is 5 feet tall! &quot;;</pre><p><strong>number</strong></p><p>number variables allows integers and decimals.</p><pre>var age = 10;<br>var weight = 45.2;</pre><p><strong>boolean</strong></p><p>boolean variables can only contain either true orfalse .</p><pre>var isVisible = true</pre><p><strong>undefined</strong></p><p>undefined variable is when a variable is declared but not given a value.</p><pre>var name;</pre><p><strong>null</strong></p><p>null is similar to undefined, but it has to be set intentionally. Null variable means it’s empty or nothing and it is because the developer set it be like that.</p><pre>var username = null</pre><p><strong>objects</strong></p><p>It is an unordered collection of key/value pairs. The collection can consist of variables, functions and data structures.</p><pre>//object</pre><pre>var person = {<br>  name: &quot;John Doe&quot;,<br>  age: 10,<br>  interest: [&quot;gaming&quot;,&quot;reading&quot;],<br>  getTotalScore: function(prevScore, currentScore){<br>      return prevScore + currentScore;<br>  }<br>}</pre><p><strong>arrays</strong></p><p>Array allows to store multiple collections of data in a single variable.</p><pre>// array</pre><pre>var colourList = [&quot;red&quot;,&quot;blue&quot;,&quot;orange&quot;];</pre><pre>var products = [ {id:0, name: &quot;john&quot;}, {id:1, name: &quot;Doe&quot;}];</pre><p>These data types are categorized into two types.</p><ol><li>Primitive Types</li><li>Reference types</li></ol><p><strong>Primitive types</strong></p><p>Data types fall under this type are;</p><p>string, number, boolean, null, undefined.</p><p>These data types are fall under primitive data type because the variables defined with these data type can only be assigned with one value at a time. Due to that the variable is able to hold the value.</p><blockquote>“In <a href="https://developer.mozilla.org/en-US/docs/Glossary/JavaScript">JavaScript</a>, a <strong>primitive</strong> (primitive value, primitive data type) is data that is not an <a href="https://developer.mozilla.org/en-US/docs/Glossary/Object">object</a> and has no <a href="https://developer.mozilla.org/en-US/docs/Glossary/Method">methods</a> or <a href="https://developer.mozilla.org/en-US/docs/Glossary/property/JavaScript">properties</a>.” (MDN Web Docs, Primitive)</blockquote><blockquote>“A primitive type has a fixed size in memory. For example, a number occupies eight bytes of memory, and a boolean value can be represented with only one bit. The number type is the largest of the primitive types. If each JavaScript variable reserves eight bytes of memory, the variable can directly hold any primitive value.” (oreilly, <a href="https://www.oreilly.com/library/view/javascript-the-definitive/0596000480/ch04s04.html#ftn.ch04-FTNOTE-3">Primitive Types and Reference Types</a>)</blockquote><p><strong>Reference types</strong></p><p>Data types fall under this type are;</p><p>objects, arrays, function</p><p>These data types are fall under reference data type because the variables defined with these data type can be assigned with more than one value at a time and due to that it does not have a fixed size. Therefore, the value of the variable is stored on the memory and its memory address is used to reference the value to the specific variable.</p><blockquote>“Reference types are another matter, however. Objects, for example, can be of any length — they do not have a fixed size. The same is true of arrays: an array can have any number of elements. Similarly, a function can contain any amount of JavaScript code. Since these types do not have a fixed size, their values cannot be stored directly in the eight bytes of memory associated with each variable. Instead, the variable stores a <em>reference</em> to the value. Typically, this reference is some form of pointer or memory address. It is not the data value itself, but it tells the variable where to look to find the value.” (oreilly, <a href="https://www.oreilly.com/library/view/javascript-the-definitive/0596000480/ch04s04.html#ftn.ch04-FTNOTE-3">Primitive Types and Reference Types</a>)</blockquote><p>The difference can between primitive type and reference type can be identified from below examples.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/447/1*em-YmfkcjBlOJi5fbclOCQ.png" /><figcaption>Code</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*tTE97bywdAN3dFjY8lUV5w.png" /><figcaption>Result printed</figcaption></figure><p>What I’m trying to imply from this example is, variable “name” is assigned to “nameP1”, this will result in passing <strong>value </strong>directly to “nameP1” and not a reference that points to the memory location that stores “name” variable’s value.</p><p>Therefore, if variable “nameP1” as a new value assigned, it does no change to value stored in “name” variable but only stores a new value in memory allocated for “nameP1”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*aMzRciNZyhut73IV6gC_2Q.png" /><figcaption>code</figcaption></figure><p>What do you think the value of “nameP1” here ?</p><p>It will be “Leonard Hofstadter” because “nameP1” does not hold a reference to memory address of “name”, but a value. So if value of “name” is changed after line 3, there wont be any change to “nameP1”.</p><p>That’s why they are called primitive types. 🌟</p><p>Let’s see the behavior of reference types 👀</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*MfhhOKnno48qjU6QqmtwxQ.png" /><figcaption>code</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UW7uVWgQlamlu4nQfBULrw.png" /><figcaption>Printed result</figcaption></figure><p>😲 why is the result printed like this ❓</p><p>Shouldn’t the result be as the primitive example and print as “Leonard Hofstadter” ? Unfortunately, no.</p><p>Because when a variable “personInfo” is created and assigned a value, its value is stored in a memory space and its memory address is used as a reference and attached to the the variable. And when the variable “personInfo” is assigned to another variable called “actor”, it is passing a reference not a value directly. This action allows variable “actor” to take control over the value originally stored when declaring “personInfo”. So at line 5, variable accesses the property named “personName” and updates its value. This is why it’s called reference type.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0d4f23dfec5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Function scope in JS]]></title>
            <link>https://shameeracarrim.medium.com/function-scope-in-js-17dd2442a58f?source=rss-a6523f370868------2</link>
            <guid isPermaLink="false">https://medium.com/p/17dd2442a58f</guid>
            <category><![CDATA[function-scope]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shameera Carrim]]></dc:creator>
            <pubDate>Sun, 16 Oct 2022 05:17:11 GMT</pubDate>
            <atom:updated>2022-10-16T05:17:11.624Z</atom:updated>
            <content:encoded><![CDATA[<p>This is also called as local scope.</p><p>What is the local/function scope ❓</p><p>The possible accessibility to variables and functions within the particular function block environment is called local/function scope.</p><pre>function random(){ // random function&#39;s scope start here<br>   //instructions<br>}// random function&#39;s scope ends here</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A-18Pn7oMhang0xWI3DFDg.png" /></figure><p>So what exactly are allowed to access or use within this block?</p><ol><li>Variables and functions defined within the function.</li><li>function’s arguments</li><li>variables and functions declared in its parent function’s scope</li><li>variables and functions declared in global scope.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cTdtdsuilcPUXc5yvWfZTQ.png" /><figcaption>Access to global variables within the function scope</figcaption></figure><p>There are few limitations for variables and functions declared within the function.</p><p>That is, they are unable to access outside the scope.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NiFhY5BoYIgnNqW7JzBfew.png" /></figure><p>Variables and functions declared in parent/global scopes can be used within the local scope but, variables and functions declared in the local scope cannot be used outside the local scope.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17dd2442a58f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Block Scope in JS]]></title>
            <link>https://shameeracarrim.medium.com/block-scope-in-js-c728ced65699?source=rss-a6523f370868------2</link>
            <guid isPermaLink="false">https://medium.com/p/c728ced65699</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[block-scope]]></category>
            <dc:creator><![CDATA[Shameera Carrim]]></dc:creator>
            <pubDate>Sat, 15 Oct 2022 11:45:44 GMT</pubDate>
            <atom:updated>2022-10-15T14:01:43.794Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Firstly, what is a block</strong> ❓</p><p>It is a wrapper that allows a programmer to write one or more instructions within it.</p><p>How does a block look like in JS ❓</p><pre>{} -&gt; a block</pre><pre>{<br>  // code is written within the block<br>}</pre><p>What is the use of a block ❓</p><p>First let’s look into a instruction written in only one line</p><pre>let color = &quot;red&quot;;<br>if(color === &quot;red&quot;) console.log(&quot;it is red&quot;);</pre><p>the statement above will print “it is red” if variable color has a value of “red”. What if we want more statements to be executed if color is red ?</p><p>First we have to write the condition and then we have to add a wrapper to it { } . Then add the instructions within the wrapper.</p><pre>let color = &quot;red&quot;;</pre><pre>if (color === &quot;red&quot;) {<br>   console.log(&quot;it is red&quot;);<br>   console.log(&quot;Therefore, background color should be red!&quot;);<br>}</pre><p>The block allows programmers to write multiple lines of statements in instances where js expects a single statement.</p><p><strong>Secondly, what is a scope ❓</strong></p><p><em>“Scope refers to the availability of variables and functions in certain parts of the code</em>” (programiz)</p><p>Let’s create a block add some variables within it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wxPso8GOxLJ-h1aaoia8pw.png" /><figcaption>Scope within the start of the block before variable initialization</figcaption></figure><p>So block is created at line 1 and closed at line 6. At line 2 lets see the scope before declaration.</p><p>In the scope we can see, variables are hoisted but variables declared using let and const are located in block scope, and variable declared using var in global scope.</p><p>So what is the difference ❓ 😕</p><p>let and const are block scoped variables, and they will be hoisted after the start of the block. And at the end of the block the variables are removed from the scope. Therefore, they are available for access within the specific block after variables are initialized, because before initialization they are in temporal dead zone(TDZ). <em>(</em><a href="https://shameeracarrim.medium.com/temporal-dead-zone-tdz-in-javascript-f57316df7c7c"><em>Read more about TDZ</em></a><em>)</em></p><p>But var is a global scoped variable. Even though it is written within the block, it will be hoisted and made available in the global scope prior to the block. Therefore, it is accessible before the block, within the block and after the block.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5pLyFBIwttez1G3Gs7IXqA.png" /><figcaption>Scope at line 1 before the block</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CAzt12WiClNdUYAnudRBZA.png" /><figcaption>Scope at line 5 within the block after variable initialization</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lvDAuw1WyEZOfa54plrLAA.png" /><figcaption>Scope at line 6 after the block</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c728ced65699" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Functions in JS]]></title>
            <link>https://shameeracarrim.medium.com/functions-in-js-5182cdce3fad?source=rss-a6523f370868------2</link>
            <guid isPermaLink="false">https://medium.com/p/5182cdce3fad</guid>
            <category><![CDATA[function]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shameera Carrim]]></dc:creator>
            <pubDate>Sat, 15 Oct 2022 01:34:32 GMT</pubDate>
            <atom:updated>2022-10-22T04:39:36.126Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is a function?</strong></p><p>A function is simply an activity, that is done to achieve particular outcome.</p><p>For example, we can think <strong>baking </strong>a cake in oven as a function. By a mix of correct amount of butter, flour, sugar, vanilla, eggs and baking powder put in an oven at right temperature for specific hours you can expect a baked cake. 🌟</p><p>So what is a function technically mean in programming context❓</p><p>“<em>A </em><strong><em>function</em></strong><em> or </em><strong><em>subroutine</em></strong><em> (when it doesn’t return a value) is a sequence of </em><a href="https://en.wikipedia.org/wiki/Instruction_(computer_science)"><em>program instructions</em></a><em> that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed</em>” (Wikipedia)</p><p>“<em>A function is a block of code that performs a task. It can be called and </em><a href="https://www.makeuseof.com/recursion-explained/"><em>reused multiple times</em></a><em>. You can pass information to a function and it can send information back. Many programming languages have built-in functions that you can access in their library, but you can also create your own functions” (</em>makeuseof, Jennifer Seaton, 2020)</p><p><strong>How to create your own function in JS?</strong></p><p>There are several ways</p><p>&gt; Regular functions</p><pre>/* <br>* syntax -&gt;<br>* function keyword -&gt; denotes it is a function<br>* name -&gt; particular task is uniquely identified by the name<br>* arguments space -&gt; incoming arguments mentioned there<br>* block -&gt; contains the step by step instruction<br>* extra info -&gt; function is hoisted at the top of the scope<br>*/</pre><pre>function [name](arguments)<br>{ //&lt;- block start here<br>    // instructions are written within braces/block<br>} //&lt;- block endshere</pre><pre>function addNumbers(num1,num2){<br>      const total = num1 + num2;<br>      return total;<br>}</pre><pre>//calling a function<br>addNumbers(2,4)</pre><p>&gt; Function Expressions</p><pre>/*<br>* syntax -&gt; function block is assigned to a variable<br>* extra info -&gt; the variable is hoisted at the top of the scope<br>*/</pre><pre>[variable keyword] [variable name] = function [name](arguments){ <br>    // instructions are written within braces/block<br>}</pre><pre>var addTwoNumbers = function (num1, num2) {<br>  var sum = num1 + num2;<br>  console.log(`sum of two numbers : ${sum}`)<br>};</pre><pre>//function invocation<br>addTwoNumbers(3, 5)</pre><p>&gt; Arrow Functions</p><pre>/* <br>* syntax -&gt; <br>* function block is assigned to a variable <br>* function keyword is removed<br>* &quot; =&gt; &quot; added after the argument parentheses<br>* extra info -&gt; the variable is hoisted at the top of the scope<br>*/</pre><pre>[variable keyword] [variable name] = (arguments) =&gt; { <br>  // instructions are written within braces/block<br>}</pre><pre>const sum2 = (num1, num2) =&gt; {<br>   return num1 + num2;<br>};<br>sum(1,2);</pre><pre>/*<br>* if the function has only one line within block,<br>* curly braces can be removed<br>* return keyword can be removed<br>*/</pre><pre>const sum2 = (num1, num2) =&gt; num1 + num2;<br>sum(1,2);</pre><pre>/* <br>* if the arrow function has only one argument,<br>* argument parentheses can be removed <br>* but if no argument is passed the parentheses should be written<br>*/</pre><pre>const squared = num1 =&gt;  num1 * num1;<br>squared(5);</pre><pre>const randomNumber2 = () =&gt; Math.random();<br>randomNumber2();</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5182cdce3fad" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Syntax Error vs TypeError vs ReferenceError in JS (short note)]]></title>
            <link>https://shameeracarrim.medium.com/syntax-error-vs-typeerror-vs-referenceerror-in-js-short-note-2e22d9d5012?source=rss-a6523f370868------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e22d9d5012</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[error]]></category>
            <dc:creator><![CDATA[Shameera Carrim]]></dc:creator>
            <pubDate>Wed, 12 Oct 2022 01:46:41 GMT</pubDate>
            <atom:updated>2022-10-12T01:57:46.605Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>SyntaxError</strong></p><pre>code : const a;</pre><pre>Error : SyntaxError: missing = in const declaration</pre><pre>Reason : when creating a variable using const keyword, variable   should be declared and assigned a value on the same line. Therefore, this error is fired because it is violating its syntax.</pre><p>TypeError</p><pre>code : const a = 1;<br>       a = 2;<br>       <br>Error : TypeError: invalid assignment to const &#39;a&#39;.</pre><pre>Reason : keyword type const does not allow reassign value. Due to this violation a type error is fired.</pre><p>ReferenceError</p><pre>code : console.log(x)</pre><pre>Error : ReferenceError: x is not defined.</pre><pre>Reason : x is not created in the memory,and cannot be referred because it was not defined. Due to this violation a reference error is fired.</pre><pre>code : console.log(a)<br>       const a = 1<br>Error : ReferenceError: a is not defined.</pre><pre>Reason : a is created in the memory, but cannot be referred to because it is in temporal dead zone. Due to this violation a reference error is fired.</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e22d9d5012" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Temporal Dead Zone (TDZ) in JavaScript]]></title>
            <link>https://shameeracarrim.medium.com/temporal-dead-zone-tdz-in-javascript-f57316df7c7c?source=rss-a6523f370868------2</link>
            <guid isPermaLink="false">https://medium.com/p/f57316df7c7c</guid>
            <category><![CDATA[tdz]]></category>
            <category><![CDATA[temporal-dead-zone]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shameera Carrim]]></dc:creator>
            <pubDate>Mon, 10 Oct 2022 12:28:24 GMT</pubDate>
            <atom:updated>2022-10-16T07:57:28.875Z</atom:updated>
            <content:encoded><![CDATA[<p>In JavaScript, the variables and functions are hoisted and are able to access in top of their scope.</p><p>For example,</p><pre>console.log(studentName);<br>printAge(12);</pre><pre>var studentName = &quot;Windy&quot;;<br>function printAge(age){<br>  console.log(&quot;Age is: &quot;,age);<br>} </pre><p>There are two keywords that was introduced by JavaScript ES2015 version to create variables and constants called let and const . Lets see how they behave when attempted access before initialization.</p><p><strong>var</strong></p><pre>console.log(studentName) //undefined</pre><pre>var studentName = &quot;Peter&quot;</pre><p><strong>const</strong></p><pre>console.log(studentAge) // Error</pre><pre>const studentAge = 12</pre><p><strong>let</strong></p><pre>console.log(studentClass) // Error</pre><pre>let studentClass = 7</pre><p>Let’s see if they are hoisted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HSnKMU2U99OYLXoIEZvs_w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CeSKAp9K3FMdGobVnSzOug.png" /></figure><p>At line number 1, even before code has started to execute, JavaScript has allocated memory to studentAge, studentIndex and studentClass and available in scope. Variable declared with var keyword attached to global space and the variable declared with let and const is attached to script space.</p><p>If const and let are also hoisted, why are the variables and constants unable to access prior to the initialization ❓</p><p>That is because of temporal dead zone (TDZ).</p><p>But what is meant by TDZ ❓ 😕</p><p>It is the time from let or const variable is hoisted and until it is initialized with some value.</p><p>If we try to access a const or let variable TDZ phase, output will be an referenceError.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vi7M5FZZS09VL4jXOeLHoA.png" /><figcaption>Attempting to access let variable during TDZ</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EW_DR2qeFC28esT5043cew.png" /><figcaption>Attempting to access const variable during TDZ</figcaption></figure><p>A referenceError will be fired because variable cannot be accessed during TDZ.</p><p>The best way to avoid above errors and avoid chances of accessing TDZ, is to declare variables within the top of the specific scope.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*Yiu8ogB3gVIgp4K2sgjvOw.png" /></figure><p>Want to know more about errors? <a href="https://shameeracarrim.medium.com/syntax-error-vs-typeerror-vs-referenceerror-in-js-short-note-2e22d9d5012"><em>(Read about errors in js)</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f57316df7c7c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>