<?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 Harrison Grant-Favor on Medium]]></title>
        <description><![CDATA[Stories by Harrison Grant-Favor on Medium]]></description>
        <link>https://medium.com/@hyper_debugger?source=rss-6999e9c77c99------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hRXQR7ijzoQqh9sCpVp9Zg.png</url>
            <title>Stories by Harrison Grant-Favor on Medium</title>
            <link>https://medium.com/@hyper_debugger?source=rss-6999e9c77c99------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 00:32:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hyper_debugger/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 Functions — Part 1]]></title>
            <link>https://medium.com/swlh/javascript-functions-part-1-444d5ef5e21d?source=rss-6999e9c77c99------2</link>
            <guid isPermaLink="false">https://medium.com/p/444d5ef5e21d</guid>
            <category><![CDATA[functions-in-javascript]]></category>
            <category><![CDATA[objects-in-javascript]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Harrison Grant-Favor]]></dc:creator>
            <pubDate>Tue, 02 Jun 2020 10:33:05 GMT</pubDate>
            <atom:updated>2020-06-03T10:13:18.170Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript Functions — Part 1</h3><figure><img alt="Image of a JavaScript Function" src="https://cdn-images-1.medium.com/max/1024/1*bG9-bVQ5Z0m17yRNtAuRSQ.jpeg" /><figcaption>Image of a JavaScript Function</figcaption></figure><blockquote>The first time that I encountered functions in JavaScript, I was confused because coming from a background in Java which had methods that were always attached to classes (objects); seeing JavaScript functions that could exist on their own without being attached to objects took some getting used to. I could easily see advantages in this after a while though because the benefits of being able to abstract functionalities for reuse into functions without the extra overhead of creating objects cannot be overstated.</blockquote><p><strong>Functions</strong> in Mathematics are defined as a relation or expression involving one or more variables. A function is also defined as a process that associates to each element of a set X, a single element of a set Y. This technical jargon mostly means that a function maps inputs to outputs.</p><p><strong>Functions</strong> are one of the fundamental building blocks in JavaScript and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions">MDN</a> defines a function as <em>“</em>a JavaScript procedure — a set of statements that perform a task or calculates a value<em>”</em>.</p><p>The concept of functions in mathematics still applies to some extent in programming.</p><p>To use a function in JavaScript, the function must have been created at some point which is accessible within the scope it is to be called. JavaScript provides various mechanisms for creating functions:</p><ul><li><strong>Function Declarations</strong></li><li><strong>Anonymous Functions</strong></li><li><strong>Function Expressions</strong></li><li><strong>Arrow Functions</strong></li><li><strong>Function Object Constructors</strong></li></ul><h3>Function Declarations</h3><p>A function declaration consists of the “<strong><em>function</em></strong><em>” </em>keyword followed by the name of the function and zero or more parameters separated by commas and enclosed in parentheses. The body of the function is enclosed in curly braces immediately after the parameter list.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b757b3ca49f58561cf4b8289815b3d29/href">https://medium.com/media/b757b3ca49f58561cf4b8289815b3d29/href</a></iframe><p>JavaScript’s <a href="https://blog.usejournal.com/var-let-and-const-hoisting-and-scope-8860540031d1#35d0"><strong><em>hoisting</em></strong></a><strong><em> </em></strong>is especially useful in function declarations because it makes the function available for use even before it is declared in the code as long as the calling line of code is within the same scope as the declaration. Calling the function involves using the function name followed by the corresponding values (<strong><em>arguments</em></strong>) for parameters still enclosed in parentheses.</p><pre>add (1, 2)</pre><p>When this function is called, the current lexical context (<strong><em>this</em></strong>) would refer to the global object (<strong><em>window</em></strong> in the browser and <strong><em>global</em></strong> in node.js). If an object instance is created from a function using the new keyword, the current lexical context would refer to the function environment.</p><p>Functions create a <strong>function</strong> <strong>scope</strong> (lexical environment) and variables declared within this function using either <strong><em>var, let </em></strong>or <strong><em>const</em></strong> will be local to the function and cannot be accessed outside the function. This can be seen in the snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77c43fdc577629834bcd6edd2722e81f/href">https://medium.com/media/77c43fdc577629834bcd6edd2722e81f/href</a></iframe><h3>Anonymous Functions</h3><p>Anonymous functions as the name implies are unnamed function expressions. They hold no meaning on their own and have to be used within any of the following scenarios:</p><ul><li>The anonymous function can be assigned to variables to create Function Expressions.</li><li>Passed as arguments to other functions.</li><li>Returned from other functions or</li><li>Used in Immediately Invoked Function Expressions.</li></ul><h3>Function Expressions</h3><p>The difference between a function expression and a function declaration is that the function name is optional for function expressions and the expression is assigned to a valid identifier. Function expressions are possible because functions being first-class citizens in JavaScript are just like any other value types and can be assigned to variables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d984b07f3a2382996ca451e4758dfe11/href">https://medium.com/media/d984b07f3a2382996ca451e4758dfe11/href</a></iframe><p>For <strong>Function expressions</strong>, Hoisting involves moving the assigned identifier (variable) into memory. The same principles of hoisting for variables (defined with <strong><em>var, let </em></strong>or <strong><em>const</em></strong>) applies here. Check my article on <a href="https://blog.usejournal.com/var-let-and-const-hoisting-and-scope-8860540031d1#35d0"><em>VAR, LET and CONST -Hoisting, and Scope</em></a> to learn more about hoisting.</p><h3>Arrow Function Expressions</h3><p>Arrow function expressions can be seen as a short form for defining anonymous functions that do not create their own internal context.</p><p>According to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">MDN</a>; <em>“</em>An <strong>arrow function expression</strong> is syntactically a compact alternative to a regular function expression, although without its own bindings to the <strong><em>this</em></strong>, <strong><em>arguments</em></strong>, <strong><em>super</em></strong>, or <strong><em>new.target</em></strong> keywords<em>”</em>. Arrow function expressions are ill-suited as methods, and they cannot be used as constructors.</p><p>The syntax for defining arrow functions consist of zero or more parameters enclosed within parentheses followed by the symbol; <strong>=&gt;</strong> and then the body of the function enclosed withing curly braces immediately after the parameter list. The enclosing braces are optional if the function body is a single line and the last expression is implicitly returned from the arrow function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e79290e5e8000a3ad5cac2297afae36b/href">https://medium.com/media/e79290e5e8000a3ad5cac2297afae36b/href</a></iframe><p>Arrow functions do not create their own bindings for the keywords; <strong><em>this</em></strong>, <strong><em>arguments</em></strong>, <strong><em>super</em></strong>, or <strong><em>new.target</em></strong>. This implies that the bindings for these keywords created by the parent context of the arrow function will always be available within the arrow function. <strong><em>It also means that object instances cannot be created from arrow functions by calling the new keyword</em></strong>.</p><h3>Function Constructors</h3><p>JavaScript provides a Function object (<em>however rarely used</em>) from which <strong>functions</strong> can be constructed. The <strong>functions</strong> are created by instantiating the <strong>Function</strong> object and passing the function body (a string) as the last argument to the Function Constructor. All arguments before the function body argument would be taken as the parameter list to the function. The syntax for this can be seen below;</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbb9e444a04ce81edbf75b8a969e636d/href">https://medium.com/media/cbb9e444a04ce81edbf75b8a969e636d/href</a></iframe><h3>Immediately Invoked Function Expressions (IIFE)</h3><p>The term <em>Immediately Invoked Function Expressions (IIFE) </em>leaves no room for ambiguity and according to <a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE">MDN</a>; <em>“</em>an IIFE is a JavaScript function that runs as soon as it is defined<em>”</em>. It’s also known as a Self-executing anonymous function. They are useful in scenarios where you want to protect the global scope and restrict accessing of variables to just within the IIFE.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a822a63051720e497d3889d8394d8f8/href">https://medium.com/media/1a822a63051720e497d3889d8394d8f8/href</a></iframe><p>You would find that the function is defined and called at the same time with parameters that the function may expect being passed into it.</p><h3>Function Arguments Object (arguments)</h3><p>Every function scope has an <strong><em>arguments</em></strong> variable which is an array-like object that contains the arguments passed into that function. Its values can be accessed just like a regular array using zero-based indexing. The <strong><em>arguments</em></strong> object is sorted in the order that the parameters are passed to the function. Arrow functions do not create their own bindings for the <strong><em>arguments</em></strong> object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e56678fc098ef99b2ae34e39cb30b90c/href">https://medium.com/media/e56678fc098ef99b2ae34e39cb30b90c/href</a></iframe><p>JavaScript has no strict sense of function signatures and an arbitrary number of arguments can be passed when calling a function as shown by the example above.</p><h3>Rest Parameters</h3><p>Rest parameters (not to be confused with the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax"><em>spread syntax</em></a>) allow an indefinite parameter list to be represented as an array. They allow accessing an arbitrary number of arguments being passed to a function. The rest parameter can only be the last parameter in a function&#39;s definition and it is defined as the variable name prefixed with triple dots (<strong>…</strong>). A key difference between rest parameters and the <strong><em>arguments</em></strong> object is that while the <strong><em>arguments</em></strong> object contains all the arguments passed into a function, rest parameters is an array of only the arguments that haven’t been given a separate name.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05ebdd8499526bcf38693d1e400e5a35/href">https://medium.com/media/05ebdd8499526bcf38693d1e400e5a35/href</a></iframe><h3>Function bindings</h3><p>Functions have a context (<strong><em>this</em></strong>) which refers to the object that the function belongs to. It might be the global object or a custom object that the function is attached to. Arrow functions do not provide their own bindings to the “<strong><em>this</em></strong>” keyword. JavaScript provides built-in methods to explicitly set the context that a function would use. The methods are:</p><ul><li>Call</li><li>Apply</li><li>Bind</li></ul><h4>Function.call</h4><p>The <strong><em>call</em></strong> method on functions immediately calls a function with the given context supplied as the first argument to the “<strong><em>call</em></strong>” method and the regular arguments to the initial function passed as the rest arguments after passing the context.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19f5db096bba687368035a64e96b8aa4/href">https://medium.com/media/19f5db096bba687368035a64e96b8aa4/href</a></iframe><h4>Function.apply</h4><p>The only difference between the “<strong><em>call</em></strong>” and the “<strong><em>apply</em></strong>”<strong><em> </em></strong>methods is in the way the function arguments are being passed. Both of the methods have the context as the first argument. For “<strong><em>apply</em></strong>”<strong><em> </em></strong>the regular function arguments are an array passed as the second parameter to the function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05a622a40d0276d3fd1755daaa8fb569/href">https://medium.com/media/05a622a40d0276d3fd1755daaa8fb569/href</a></iframe><h4><strong>Function.bind</strong></h4><p>The “<strong><em>bind</em></strong>” method attached to functions creates a new function with a new context passed in as the first argument to “<strong><em>bind</em></strong>”. According to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind">MDN</a>, <em>“</em>the <strong>bind</strong>() method creates a new function that, when called, has its <strong><em>this</em></strong> keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called<em>”</em>. What this means is that both the “<strong><em>this</em></strong>” keyword and preceding regular arguments can be bound to a function using the <strong>bind()</strong> method. This makes it especially useful in partial functions because you can defer passing of some arguments to a later time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ec019b8648e1d0ab1e6e0d3f7131b89/href">https://medium.com/media/0ec019b8648e1d0ab1e6e0d3f7131b89/href</a></iframe><p>This is part one in a two-part series going in-depth into the definitions of functions in JavaScript. Concepts related to functional programming, recursion, parameter behaviour would also be discussed in the second part.</p><p>So yeah this article would be updated sometime in the near future with the link to the second part. Cheers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=444d5ef5e21d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/javascript-functions-part-1-444d5ef5e21d">JavaScript Functions — Part 1</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[VAR, LET and CONST -Hoisting and Scope]]></title>
            <link>https://medium.com/@hyper_debugger/var-let-and-const-hoisting-and-scope-8860540031d1?source=rss-6999e9c77c99------2</link>
            <guid isPermaLink="false">https://medium.com/p/8860540031d1</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Harrison Grant-Favor]]></dc:creator>
            <pubDate>Mon, 11 Mar 2019 18:33:06 GMT</pubDate>
            <atom:updated>2019-05-06T20:16:37.723Z</atom:updated>
            <content:encoded><![CDATA[<h3>VAR, LET and CONST -Hoisting, and Scope</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k3cDk06MO3SFjgCUrC3ttg.jpeg" /><figcaption>nothing to see here, just an image of a nucleus.</figcaption></figure><p>JavaScript, like a lot of more modern languages, provides a host of different methods for variable declaration. JavaScript has the <strong><em>var</em></strong>, <strong><em>let</em> </strong>and <strong><em>const</em> </strong>keywords for variable declarations and with each comes a different use case. This article aims to dive in-depth into the caveats and nuances which you might encounter when using either of these variable declaration methods.</p><p>For a bit more context, see the definitions of each of the keywords as defined in the <a href="https://www.ecma-international.org/ecma-262/6.0">official ECMAScript specifications</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript">MDN</a>.</p><blockquote><strong>Var</strong> keyword declares a variable which is scoped to its current execution context, optionally initializing to a value.</blockquote><blockquote><strong>Let</strong> keyword declares a block scoped variable, optionally initializing it to a value.</blockquote><blockquote><strong>Const</strong> keyword declares constants which are block scoped much like variables defined using <strong>let</strong> but the value of a constant cannot change. The <strong>const</strong> declaration creates a read-only reference to a value.</blockquote><h3>HOISTING</h3><p>Conceptually, hoisting suggests that variables and function declarations are physically moved to the top of your code. Technically, what happens is that the variable and function declarations are put into memory during the compilation phase but stay exactly where you typed them in your code. The primary importance of hoisting is that it allows you to use functions before you declare them in your code.</p><p>The key things to take out of the definition for hoisting are</p><ul><li><strong><em>What gets moved around is variable and function declarations. Variable assignments or initialization are never moved around</em>.</strong></li><li><strong><em>The declarations are not exactly moved to the top of your code; instead, they are put into memory</em>.</strong></li></ul><p>In JavaScript, all variables defined with the <strong><em>var</em></strong> keyword have an initial value of <strong><em>undefined</em></strong>. This is due to hoisting which puts the variable declarations in memory and initializes them with the value of <strong><em>undefined</em></strong>. This behavior can be shown with the following example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*LYZsYfSi_pNpUCHDo0z9lg.png" /><figcaption>line 2 throws a ReferenceError: y is not defined</figcaption></figure><p>However, variables defined with <strong><em>let</em></strong> or <strong><em>const</em></strong> keywords when hoisted are not initialized with a value of undefined. Rather, they are in a state called the <strong>Temporal Dead Zone</strong><em> </em>and are not initialized until their definitions are evaluated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*6_UuTjFttH5H_iuKttyeOg.png" /><figcaption>line 1 throws ReferenceError because it is still in Temporal Dead Zone</figcaption></figure><p>The next code snippet demonstrates the hoisting of <strong><em>let</em></strong> and <strong><em>const</em></strong> variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*DpEAcywKXt-x_9rnueuwwQ.png" /><figcaption>line 3 throws a ReferenceError because x in line 4 has been hoisted within the block</figcaption></figure><p>The <strong><em>x</em></strong> variable defined with <strong><em>let </em></strong>keyword in the block is hoisted and takes precedence over the <strong><em>x</em></strong> variable defined with <strong><em>var.</em></strong> However, it is still in the temporal dead zone when it’s referenced in <strong><em>console.log(x)</em></strong> and so throws a reference error.</p><h3>SCOPE</h3><p>Variables defined with the <strong><em>var</em></strong> keyword have a scope that is its current execution context. They are not block scoped and so can be accessed from outside the block they were defined, provided it’s still within the scope of its execution context. <strong><em>Let</em></strong> and <strong><em>const</em></strong> variables, however, are block scoped and cannot be accessed from outside the block. This concept is shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*8BZU5l9Nq5Gs4DIKi0LFsw.png" /><figcaption>some variables are referenced outside of their scope</figcaption></figure><p>Also, when you declare a global variable with <strong><em>var</em></strong> keyword, the variable gets attached to the global context (<strong>window</strong> in the browser and <strong>global</strong> in node). This is not the case with global variables declared with <strong><em>let</em></strong> and <strong><em>const</em></strong>.</p><h3>GOTCHAS</h3><ul><li>When you don’t declare a variable but assign a value to the variable, the variable gets created and is attached to the global execution context (<strong><em>window</em></strong> in the browser and <strong><em>global</em></strong> in node). This, however, is strongly advised against as it makes debugging very difficult.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*0QTwonrSzclzRSU8J5W7YQ.png" /><figcaption>the variables get attached to the global context</figcaption></figure><ul><li>Variables declared with <strong><em>var</em></strong> keyword can be redeclared at any point in the code even within the same execution context. This is not the case for variables defined with <strong><em>let</em></strong> and <strong><em>const</em></strong> keywords as they can only be declared once within their lexical scope.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*OAzGqNuoFE6aO12c8LpKsw.png" /><figcaption>cannot redeclare let and const variables.</figcaption></figure><p>This can especially cause a problem if you use <strong><em>let</em></strong> or <strong><em>const</em></strong> to declare a variable in multiple switch cases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*BS3MFChsa1IvKtX_0AQ4fQ.png" /><figcaption>foo in both cases reside in the same block scope</figcaption></figure><p>Of course, it can be avoided by using curly braces around the cases to define different blocks, but this should probably be refactored.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*srw2q2qH8ir8iwgOJy1YAw.png" /><figcaption>foo in both cases reside in different block scopes</figcaption></figure><ul><li>Another gotcha to remember about constants is that even though its value cannot be reassigned to, it is still mutable. The behavior can be seen in the fact that if the value is an object, the object’s properties can be modified.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*5GcP_6Bf3fzBBGcOcS9hxQ.png" /><figcaption>variables defined with const are still mutable.</figcaption></figure><p>So yeah that pretty much sums up the definitions and gotchas with using either of the variable declaration methods.</p><figure><a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_image"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PYxBP_WV7LI6zc-zdUkNqQ.png" /></a></figure><p>📝 Read this story later in <a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_read_later_text">Journal</a>.</p><p>👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. <a href="https://usejournal.com/newsletter/noteworthy-in-tech/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_text">Read the Noteworthy in Tech newsletter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8860540031d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>