<?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 Kenneth Kwakye-Gyamfi on Medium]]></title>
        <description><![CDATA[Stories by Kenneth Kwakye-Gyamfi on Medium]]></description>
        <link>https://medium.com/@kenneth.cross95?source=rss-31e16387286a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*OCFKAj-0ylvMo0urCqhEiA.jpeg</url>
            <title>Stories by Kenneth Kwakye-Gyamfi on Medium</title>
            <link>https://medium.com/@kenneth.cross95?source=rss-31e16387286a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:16:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kenneth.cross95/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[JavaScript Gotchas (Primitives & Objects)]]></title>
            <link>https://medium.com/@kenneth.cross95/javascript-gotchas-primitives-objects-eee868bc80b?source=rss-31e16387286a------2</link>
            <guid isPermaLink="false">https://medium.com/p/eee868bc80b</guid>
            <dc:creator><![CDATA[Kenneth Kwakye-Gyamfi]]></dc:creator>
            <pubDate>Fri, 08 Mar 2019 14:12:03 GMT</pubDate>
            <atom:updated>2019-03-08T15:00:17.826Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*vVEZhiRhMUX2EfXxX5IptA.png" /></figure><p>We’ve seen memes like this pointing out quirky “flaws” in the JavaScript programming language. We’ve also seen terms such as only <a href="https://developer.mozilla.org/en-US/docs/Glossary/Mutable">objects and arrays are mutable in JavaScript</a>. This article seeks to point out a few under-the-hood functionalities of JavaScript. In order to make it as short as possible, multiple topics will be combined.</p><h3>Only objects and arrays are mutable in JavaScript</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c091AMTzzN4RS7Egq7_tUw.png" /></figure><p>This may seem fairly straightforward, until it’s not. A simple look in a dictionary will tell us that when something is “mutable”, it is liable to change (mutated). That’s cool, some constructs in JavaScript change and some do not. This is no big deal until something like this happens in your code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13b484dd7c5a34af236681f2374a7a71/href">https://medium.com/media/13b484dd7c5a34af236681f2374a7a71/href</a></iframe><p>To better understand this in JavaScript, assume the following:</p><ul><li>All data types in JavaScript are objects. So a string is an object, a boolean is an object, and an object is, well, an object.</li><li>The mutability of a data type affects how it is copied onto another variable.</li></ul><p>Let’s look at this piece of code to understand it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57201408089cfea68ae6420c22367cab/href">https://medium.com/media/57201408089cfea68ae6420c22367cab/href</a></iframe><p>This makes mutable objects (where object = data type) a bit tricky to work with. Getting new variables will require that we explicitly clone them. And there are quite a number of ways of cloning mutable objects. <a href="https://developer.mozilla.org/en-US/docs/Glossary/Mutable">This great article on the MDN docs</a> might shed some more light with examples.</p><h3>Understanding the plus (+) and minus (-) operators in JavaScript</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZdPJ8Me9DbvWrdA_HNa7yg.png" /></figure><p>To understand how the plus and minus (by the way, it is not limited to these two operators… they are the most troublesome, however) operators work, it will help to understand the following:</p><ul><li>There are six primitive data types in JavaScript: boolean, number, string, null, undefined and Symbol (introduced in ECMAScript 2015). There are non-primitives such as arrays, <em>Date object</em>, regular JavaScript objects, etc.</li><li>Each data type (primitive or non-primitive) when used in conjunction with any of these operators, tries to perform a data type-to-primitive conversion, converting itself to a primitive value.</li><li>The plus and minus operator work by either concatenating strings or adding/subtracting numbers. This is to say that they work on just numbers and strings.</li><li>Even the primitive values convert themselves to numbers or strings to allow the operator to process them properly.</li></ul><p>The operators themselves also play a key role when it comes to the type conversion. The plus operator is used for both string concatenation and number addition, and favors string conversions over number conversions. The minus operator is used for number subtraction and favors number conversions over string ones. Let’s try to explain how these work in a couple of gists</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a26fecd56447c88f447ba6df4234ebc/href">https://medium.com/media/4a26fecd56447c88f447ba6df4234ebc/href</a></iframe><p>Seems simple, we are just doing simple + and -. Let’s look at a more complex interaction:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e583c590d57308d0563789961a4df92d/href">https://medium.com/media/e583c590d57308d0563789961a4df92d/href</a></iframe><p>There is a great tutorial on <a href="https://javascript.info/type-conversions">Type conversions on JavaScript.info</a> but essentially the takeaway from that is:</p><ul><li>In order to perform the better-fit operation, the following methods are called, in order: toPrimitive() which takes care of converting it to any of the primitive methods</li><li>valueOf which is a legacy version of the toPrimitive method, i.e. used before ECMAScript 2015</li><li>toString trying to convert it to string</li><li>Final attempt to converting it to numbers, which may result in a number, or NaN</li></ul><p>In my opinion, all these quirks and gotchas come about because JavaScript is loosely typed and attempts to satisfy everyone and everything. But, great power comes with great responsibility (dedication to understanding these gotchas).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eee868bc80b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>