<?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 Kevin Lai on Medium]]></title>
        <description><![CDATA[Stories by Kevin Lai on Medium]]></description>
        <link>https://medium.com/@kevinlai76?source=rss-72aed5d6c5ff------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*CJG1SBMDklnvts86.jpg</url>
            <title>Stories by Kevin Lai on Medium</title>
            <link>https://medium.com/@kevinlai76?source=rss-72aed5d6c5ff------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 05:33:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kevinlai76/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[Arrow Functions and “this”]]></title>
            <link>https://medium.com/@kevinlai76/arrow-functions-and-this-3768bc929ed9?source=rss-72aed5d6c5ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/3768bc929ed9</guid>
            <dc:creator><![CDATA[Kevin Lai]]></dc:creator>
            <pubDate>Mon, 04 May 2020 03:32:02 GMT</pubDate>
            <atom:updated>2020-05-04T03:32:02.642Z</atom:updated>
            <content:encoded><![CDATA[<p>Lets jump right into an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AFZ3aKjn54WHBuHn4Q5bvw.png" /></figure><p>In our code we’ve created a class Person. When creating a new person it takes in one parameter in the constructor ‘name’. In the class we have two functions ‘sayNameArrow’ and sayNameFunction. In ‘sayNameArrow’ we have a setTimeout with an anonymous arrow function that logs out ‘Arrow:’ and this.name immediately. In sayNameFunction we have the exact same code but instead of an anonymous arrow function we have a regular anonymous function and this logs ‘Function’ and this.name.</p><p>The only difference is whether or not the function was called with an arrow function or not but the logs are completely different:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WnSOrkM93EwTxwCF2Icl0w.png" /></figure><p>We create a new person object with ‘Fred’ passed in as our argument for the constructor. Within the class function this.name == ‘Fred’. Our arrow function maintains this initial bind. However when the function is called with a regular function nothing is logged for this.name. Why?</p><p>The reason is because ‘this’ is different inside an arrow function and inside a regular function. In a regular function <strong>‘this’ is defined on where the function is called</strong>. If we look at our code, sayNameFunction is called in the global scope, in the global scope this.name is not defined in fact if we console.log(this.name) in the global scale we get this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-fIWJcEAfBq3XmHtf3bISw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*29koUQEo2YvNhrihZbUYog.png" /></figure><p>Our console log in the global scope returns a blank and in our regular function it also returns a blank because this.name is not defined and ‘this’ is globally scoped because of where it’s function is called.</p><p>In our arrow function ‘this’ is not redefined by where the function is called. ‘this’ in an arrow function maintains it’s value regardless of where the function is called instead <strong>‘this’ in an arrow function is defined by where the function is defined</strong>.</p><p>So to Wrap things up:</p><p><strong>‘this’ in a standard function is defined by where the function is called.</strong></p><p><strong>‘this’ in an arrow function is defined by where the function is defined.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3768bc929ed9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Arrow Functions Basics]]></title>
            <link>https://medium.com/@kevinlai76/javascript-arrow-functions-basics-9f0b226cd30a?source=rss-72aed5d6c5ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f0b226cd30a</guid>
            <dc:creator><![CDATA[Kevin Lai]]></dc:creator>
            <pubDate>Mon, 27 Apr 2020 02:03:23 GMT</pubDate>
            <atom:updated>2020-04-27T02:03:23.813Z</atom:updated>
            <content:encoded><![CDATA[<p>With the advent of es6 came the arrow function, the arrow function condensed the syntax of functions into more concise lines of code. Instead of talking about them lets take a quick and basic look at them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L2xoVNfN6QXh7XWlOu5Sdg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzl-xFeENvMrqQrsyML9-g.png" /></figure><p>This simple function ‘example0’ takes in one argument and returns a string ‘hi…’ with whatever name is passed in. This function written syntactically takes up three lines, with arrow functions this function can be rewritten like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MYSLksJrfGWypBFEqcGYGQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Px-fu2mvimE-FfIefWzh5g.png" /></figure><p>We condensed everything into one line. Please note that we saved our function to a variable named example1, the arrow function portion is</p><p>(name) =&gt; {return `hi ${name}`}</p><p>For myself I consider the above the default way of writing an arrow function however here are some caveats about the arrow function you may or may not need to know.</p><ol><li>If your function has exactly <strong>one</strong> parameter you <strong>may</strong> choose to exclude the parentheses. name =&gt; {return `hi ${name}`}</li><li>If the only statement in your function body is a ‘return’ statement you <strong>may</strong> remove ‘return’ and the curly brackets. name =&gt; `hi ${name}`</li></ol><p>note that these syntactical rules <strong>may be</strong> applied so you can also write your functions however you feel comfortable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XWsLlJNNIsSg2UkzlkDBVQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c5jsxc0bs--XmKTfgHFA1w.png" /><figcaption>All variations produce the same results.</figcaption></figure><p>Thats it for now, in the next blog we’ll see how ‘this’ is affected by arrow functions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f0b226cd30a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Algorithms: Bubble Sort]]></title>
            <link>https://medium.com/@kevinlai76/javascript-algorithms-bubble-sort-ee1bffe15bee?source=rss-72aed5d6c5ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee1bffe15bee</guid>
            <dc:creator><![CDATA[Kevin Lai]]></dc:creator>
            <pubDate>Mon, 20 Apr 2020 01:27:48 GMT</pubDate>
            <atom:updated>2020-04-20T01:27:48.147Z</atom:updated>
            <content:encoded><![CDATA[<p>Bubble sort is a sorting algorithm that can be use on relatively small data sets. In terms of big O notation, the best case scenario is O(n), in most cases however the run time will be O(n²). What bubble sort does is it compares every adjacent value to one another, if the current value is greater than the next value the two values swap positions. This continues on through multiple passes through the array until the larger values “bubble” to the end of the array.</p><p>To give a visual representation of this, lets say we have an array [3, 1, 2, 5, 4]</p><p>[<strong>3</strong>, <strong>1</strong>, 2, 5, 4] → [1, <strong>3</strong>, <strong>2</strong>, 5, 4] → [1, 2, <strong>3</strong>, <strong>5</strong>, 4] → [1, 2, 3, <strong>5</strong>, <strong>4</strong>] → [1, 2, 3, 4, 5]</p><p>The bolded numbers represent the current pair of numbers being compared. In the first example 3 is greater than 1 so 3 and 1 swap places, the process continues through the fist pass and we find that we live in an ideal world our array is sorted. However lets look at another example with some code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Gt78uGnKNVw5YUmCMhEKg.png" /></figure><p>Our example here is [6, 2, 4, 3, 1], we want our end result to be [1, 2, 3, 4, 6]. We declare a variable length and set it equal to the length of the array. We open a for loop and nest another for loop inside, by doing this we are ensuring multiple passes over the array. In terms of efficiency, this method will loop over an already sorted array because we didn’t set conditions to stop the loop if the array is already sorted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gBtLaYfDxf6IujV6HMsXVA.png" /></figure><p>In this log above, we can see that by the fourth parent loop the array is sorted but the loop continues for a fifth and final iteration because our condition to stop is the length of the array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oJWPkX_lYn_z7KhX4oYXaw.png" /></figure><p>In the code above we’ve refactored our code to be more efficient by adding in the additions of lines 7, 14, and 18. Before we enter our inner loop we declare a boolean named swap and set it equal to false, if a swap happens in the inner loop then we set swap to true, when we exit our inner loop we wrote a conditional that checks whether or not a swap has occurred if no swap has occurred then the loop breaks and we return our array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZKUrPLTPcqKltQcyKo1ATA.png" /></figure><p>You can see in the log above the iteration stops when the array has been sorted instead of continuing until the conditional for the loop has been met.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee1bffe15bee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript: Recursion Basics]]></title>
            <link>https://medium.com/@kevinlai76/javascript-recursion-basics-2ca4aba992e0?source=rss-72aed5d6c5ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ca4aba992e0</guid>
            <dc:creator><![CDATA[Kevin Lai]]></dc:creator>
            <pubDate>Mon, 13 Apr 2020 03:34:02 GMT</pubDate>
            <atom:updated>2020-04-13T03:34:02.575Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is recursion?</strong></p><p>Recursion is when a function calls itself.</p><p>Recursion could look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*odb3iGONYImJ9SP-AlR6HQ.png" /></figure><p>BUT…we run into an issue:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sUAHB35EMOF_64rziYK87w.png" /></figure><p>The function is calling itself over and over again but we haven’t written a way to stop the function from calling itself. In a recursive function there are three main components that when put together stop the function from looping infinitely:</p><ol><li>The Terminal condition</li></ol><p>The terminal condition is the fail safe. This condition is usually there to catch any bad inputs. Our example now does not take in any arguments but in a later example we will see an example of a terminal condition used to stop bad inputs.</p><p>2. The Base Case</p><p>The base case is a conditional usually written within an if statement that tells the function has completed it’s goal. It is similar to the terminal case in that both conditions stop the function from running any further, however the purpose of the terminal case is to catch any bad inputs whereas a base case tells us when the function has completed it’s intended purpose. In our example above, we want our recursion to stop when we’ve looped 5 times. We can do this by creating a count variable and incrementing this variable by 1 each loop, we can also write an if statement within our function to check the count variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KzLX-7zQXuNf-BbzMco5qA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DRZfkIH9KalLIQU_fIjSnw.png" /></figure><p>3. The Recursion</p><p>This is the recursion itself, in our example it is the return statement that returns the function invocation.</p><p>Now with this in mind lets look at a common recursion problem, factorial. We want to write a function that takes in an argument, an integer. Our function will find the factorial of that argument. The factorial of a number can be found by multiplying the number by itself -1 until it reaches the number 1.</p><p>Example 1: The factorial of <strong>4</strong> (4!) is <em>4*3*2*1 = </em><strong><em>24</em></strong></p><p>Example 2: The factorial of <strong>10 </strong>(10!) is <em>10*9*8*7*6*5*4*3*2*1 = </em><strong>3628800</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i4FlHvEvRc8QDNx7On9beQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WBWZzOKBPEK7GGSm-RVFtw.png" /></figure><p>We see here that our code works lets break down whats happening.</p><ol><li>We invoke our function on line 12 with the argument of 4.</li><li>4 is greater than 0 and 4 !== 0 so it fails both our terminal and base case.</li><li>Our return looks like this: <em>return</em><strong> 4 </strong>* <em>factorial(</em><strong><em>4</em></strong><em>-</em><strong><em>1</em></strong><em>)</em></li><li>There is no value assigned yet to <em>factorial</em>(<strong>3</strong>) so we go through the function again</li><li>3 is greater than 0 and 3 !== 0 so it fails both conditions again.</li><li>Our return looks like this: <em>return</em> <strong>3</strong> * <em>factorial</em>(<strong>3</strong>-<strong>1</strong> ) so we go through the function again…</li><li>2 is greater than 0 and 2 !== 0 so it fails both conditions</li><li>Our return looks like this: <em>return</em> <strong>2</strong> * <em>factorial</em>(<strong>2</strong>-<strong>1</strong>), we can’t evaluate this so we have to invoke <em>function</em>(<strong>1</strong>)</li><li>1 is greater than 0 and<strong> </strong>1 !== 0 so it fails our cases again</li><li>Our return looks like this: return <strong>1</strong> * <em>factorial</em>(<strong>1</strong>–<strong>1</strong>), we invoke factorial(0)</li><li>Now our base condition is finally hit! 0 === 0 so we immediately return 1</li><li>Since our function has return a value that does not need to be evaluated, the recursion ‘unwinds’, since recursion is just a group of nested function calls the inner most functions are returned first:</li></ol><p>factorial(0) returns 1</p><p>factorial(1) returns 1 * factorial(0), or 1 * 1</p><p>factorial(2) return 2 * factorial(1), or 2 * 1 * 1</p><p>factorial(3) returns 3 * factorial(2), or 3*2*1*1</p><p>factorial(4) returns 4 * factorial(3), or 4*3*2*1*1 // 24</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ca4aba992e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript: Currying]]></title>
            <link>https://medium.com/@kevinlai76/javascript-currying-5b680a2de92f?source=rss-72aed5d6c5ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b680a2de92f</guid>
            <dc:creator><![CDATA[Kevin Lai]]></dc:creator>
            <pubDate>Mon, 06 Apr 2020 03:01:39 GMT</pubDate>
            <atom:updated>2020-04-06T03:01:39.143Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is currying?</strong></p><p>In order to speak about currying we have to talk about functional programming. Function programming is becoming somewhat of a buzz word in terms of a paradigm shift in programming. What functional programming is, is the process of programming with pure functions, avoiding any side effects and shared state. Out of this paradigm concepts like currying are born. So what exactly is currying? Currying is a way in which we can transform a function that takes in multiple arguments into a series of partials. In other words a function which is usually called in this manner: <em>f</em>(a, b) can be transformed into be called like this <em>f</em>(a)(b). Lets look at the example below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HZXeDEUnsEvfXrTGaBEkZw.png" /></figure><p>In our example we have a simple function multiply that takes in two arguments a and b and returns a * b. When we invoke our function with the arguments 8 and 20 we are returned an integer of 120. Simple.</p><p>In our curryMultiply function we see that it takes in one parameter a and it returns another function that takes in one parameter b, within this callback function it returns a * b. When the arguments 8 and 20 <em>(invoked: </em><strong><em>curryMultiply(8)(20)</em></strong><em>) </em>we still get 160 as our answer but what is the point of writing functions out in this way?</p><p>Currying separates our functions into partials, partials are “functions that return functions with some already predefined arguments and need some arguments to be completed.” This allows us to save the partials as separate variables giving us the same functionality without the repetition. Lets look at this example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WGkKzBot7Osv7AM8LTEJRg.png" /></figure><p>In our example above we want our function to multiply the second argument passed in by 3, so we want our function to multiply the second parameter b by three. What we can do is create a partial by invoking our function with one argument (3) and saving it to a variable multiplyByThree. This saves the function definition <em>function(b){ return a * b } (a = 3) </em>to the variable multiplyByThree so when we invoke multiplyByThree with an argument it will multiply that argument by three. In our example we invoke multiplyByThree with the argument 30 passed in and it returns 90. With currying we allow our function to be clearly stated. We can create multiple partials with different functionality and invoke them in a way that is cleaner and more legible.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b680a2de92f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximum Subarray: Kadane’s Algorithm]]></title>
            <link>https://medium.com/@kevinlai76/maximum-subarray-kadanes-algorithm-18b7d8f84d1b?source=rss-72aed5d6c5ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/18b7d8f84d1b</guid>
            <dc:creator><![CDATA[Kevin Lai]]></dc:creator>
            <pubDate>Mon, 30 Mar 2020 03:23:49 GMT</pubDate>
            <atom:updated>2020-03-30T03:23:49.075Z</atom:updated>
            <content:encoded><![CDATA[<p>For this algorithm problem, it asks you, given an array of integers find the contiguous subarray which has the largest sum and return its sum.</p><p>example:</p><p>array = [-2, 1, 3, -2, 1, 4, -5, 1]</p><p>sum = 7</p><p>explanation: [1, 3, -2, 1, 4] is the largest sub array within the given array which adds up to 7.</p><p>The naive solution to this problem would be to brute force it. This just means that starting with the element at the first index in the array, you would form all subarrays available, as you iterate through the array. In our example the first iteration would look like:</p><p>[-2] (sum = -2)</p><p>[-2, 1] (sum = -1)</p><p>[-2, 1, 3] (sum = 2)</p><p>[-2, 1, 3, -2] (sum = 0)</p><p>[-2, 1, 3, -2, 1] (sum = 1)</p><p>[-2, 1, 3, -2, 1, 4] (sum = 5)</p><p>[-2, 1, 3, -2, 1, 4, -5] (sum = 0)</p><p>[-2, 1, 3, -2, 1, 4, -5, 1] (sum = 1)</p><p>This would continue with each element of the array until the end. The time complexity for this solution is O(n²), meaning this solution although functional is not optimal.</p><p><strong>Kadane’s Algorithm:</strong></p><p>Kadane’s algorithm is a solution that can solve our maximum subarray problem in linear timeO(n). To understand Kadane’s algorithm lets look at it in code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lSPs1rLNxMCpc6r8SFSzTw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5eGc_pEa3lNbzPthW3JW2w.png" /><figcaption>Here is a simple table that tracks the changes in value for the Current Sum and Max Sum variables.</figcaption></figure><p>We define two variables to keep track of two separate but similar things. Max sum in this case is the variable in which we will save our highest sum and ultimately return as our final result. Current sum is a temporary storage that stores the current highest sum, it’s value is determined by this logic:</p><p><em>If the previous sum of elements plus the current element is greater than the current element itself, then current sum’s value is set to the previous sum of elements plus the current element.</em></p><p><em>If previous sum of elements plus the current element is less than the current element then the current sum is equal to the current element.</em></p><p>After we determine the value of current sum we make a similar comparison between current sum and max sum:</p><p><em>If the max sum is greater than the current sum then max sum’s value does not change.</em></p><p><em>If the current sum is greater than the max sum then max sum’s value is set to the current sum.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18b7d8f84d1b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Call Stack]]></title>
            <link>https://medium.com/@kevinlai76/the-call-stack-5e9f2cd24705?source=rss-72aed5d6c5ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e9f2cd24705</guid>
            <dc:creator><![CDATA[Kevin Lai]]></dc:creator>
            <pubDate>Mon, 23 Mar 2020 03:11:37 GMT</pubDate>
            <atom:updated>2020-03-23T03:11:37.940Z</atom:updated>
            <content:encoded><![CDATA[<p>The call stack in the most basic sense is a data structure that functions on a <strong>last in first out</strong> principal to temporarily store and manage functions when they are invoked(called). Another thing to keep in mind about the call stack is, it is <strong>single threaded. </strong>Being single threaded means it only processes one command at a time. In this case if our call stack contains more than one function invocation, the last function that was invoked would resolve first. A simple example of this is:</p><p><strong>Call Stack:</strong></p><ul><li>first function invocation</li><li>second function invocation</li><li>third function invocation</li></ul><p>In this example the third function invocation would resolve first since it was the last function invocation to be pushed into the stack, when the third function invocation is finished resolving it is popped off the list and the next function invocation on the call stack is the second function invocation. This process continues until we pop everything off the call stack and we return to our global execution context.</p><p><em>*as an aside the global execution context is the environment in which our code lives. In a way this is our global scope.*</em></p><p>A simple example in which the call stack is utilized is when a function invokes another function. When the parent function is invoked, it is pushed into the call stack, as we are resolving the parent function we hit the child function within, the child function is pushed on to the call stack and it is now the current function being resolved, after it is done running, it is popped off the call stack and the parent function resumes after the child function was invoked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5fnMgdxhUs5wN4_BVu1D5g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KH6xfoVltv6u9iciYVMyMg.png" /></figure><p>Our code is executed in this order:</p><ol><li>Functions are defined and saved to memory.</li><li>conversation function is invoked and pushed onto the call stack.</li></ol><blockquote>Call stack:</blockquote><blockquote>- conversation</blockquote><p>3. console.log(‘Hello’) within conversation is executed.</p><p>4. body function is invoked and pushed onto the call stack.</p><blockquote>Call Stack:</blockquote><blockquote>- converstation</blockquote><blockquote>- body</blockquote><p>5. console.log(‘What nice weather we’re having’, ‘Yes, what a lovely day!’) within body is executed.</p><p>6. body function is resolved and popped off the call stack.</p><blockquote>Call Stack:</blockquote><blockquote>- conversation</blockquote><p>6. console.log(‘GoodBye!’) is executed.</p><p>7. conversation function is resolve and popped off the call stack.</p><blockquote>Call Stack:</blockquote><p>Call Stack is empty so whenever a function is invoked it will be pushed into the call stack and the process will repeat.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e9f2cd24705" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Immediately Invoked Function Expression (IIFE)]]></title>
            <link>https://medium.com/@kevinlai76/immediately-invoked-function-expression-iife-521b5a5bf417?source=rss-72aed5d6c5ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/521b5a5bf417</guid>
            <dc:creator><![CDATA[Kevin Lai]]></dc:creator>
            <pubDate>Mon, 16 Mar 2020 03:01:43 GMT</pubDate>
            <atom:updated>2020-03-16T03:01:43.999Z</atom:updated>
            <content:encoded><![CDATA[<p>An immediately invoked function expression or IIFE, is a function written in javascript that is… invoke immediately as soon as it is defined.</p><p>In javascript we are use to writing functions definitions that serve a specific purpose, we can later choose to invoke these functions when we require it’s functionality. A basic example of this is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*04Iz_KK8soF1mKzSHaLJiA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6qg6Klepk1Sw0BL7Lbm7Iw.png" /></figure><p>In lines 1 through 3 we define a function named hello, and in line 5 we invoke said function and we get “Hello World!” in our log.</p><p>SO what is an iife? What does that look like? it looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vmOW8sAnkH_lbdze0FO9wQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IE3DSGXh0br68F7yzLgqeA.png" /></figure><p>You may notice that encapsulating our function definition are parentheses, you may also notice that immediately outside of the parentheses is another set of parentheses invoking our function. The first set of parentheses are necessary because they turn our function definition into a function expression. If you try to invoke a function definition immediately after it is defined it will not work. The concept is aptly named immediately invoked function <strong>EXPRESSION</strong> for a reason.</p><p>Just as an aside. While in the process of writing this blog and writing out the examples you see I ran into a tiny bug that I though was interesting:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5j9rW6BGQWoCHTcY3F7glg.png" /></figure><p>Here are my examples. I have the function definition hello written on lines 1–3, on line 5 I invoke hello, and on lines 7–9 I have my iffe written. What do you think this prints out? Well, it prints out this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PHYHiOc7Zu-nOQOJmloZTg.png" /></figure><p>After a quick search on the internet, the reason why this is throwing up an error is because javascript is interpreting this code to read:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*delx32eYVCJfh3cbsblcrw.png" /></figure><p>The solution? Slap a semicolon after the hello invocation on line 5 and it’ll be good to go:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zv7BQG_4yh2Ql1aBW3gY6Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GsZR7xJleL4CdOb584_EzA.png" /></figure><p>Nice! Back to iffe’s.</p><p>IFFE’s are great for creating private variables and functions that don’t clutter the global scope:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-xdpaFL2EZDWabnGY-mUEQ.png" /></figure><p>In this example our variables secret and anotherSecret are private to our iffe shhhh, we have a function scramble that is also private to our iffe. The benefits of this are you can keep all your variables and functions private to an iffe where they can not be accessed and confused with variables in the global scope while still maintaining functionality in your code, neat!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=521b5a5bf417" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript: Hoisting (and a brief summary of Execution Context).]]></title>
            <link>https://medium.com/@kevinlai76/javascript-hoisting-and-a-brief-summary-of-execution-context-1b621d20f609?source=rss-72aed5d6c5ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b621d20f609</guid>
            <dc:creator><![CDATA[Kevin Lai]]></dc:creator>
            <pubDate>Mon, 09 Mar 2020 02:07:20 GMT</pubDate>
            <atom:updated>2020-03-09T02:07:20.129Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Defining execution context:</strong></h4><p>An execution context can be defined as the environment in which your code…executes in…</p><p>To clarify there are only two instances in which an execution context is created:</p><ol><li>When a javascript engine is started.</li><li>When a function is invoked.</li></ol><p>We’ll circle back to this later.</p><p>An execution context has two phases a “creation” &amp; “execution”. In the creation phase all variables are declared and a space is reserved for them in memory. <strong><em>In this phase variables are not yet assigned their given value</em></strong>. (This is related to hoisting so we’ll hold off with the explanation for a second) Function definitions declared and are saved in memory.</p><p><strong><em>In the execution phase your code is run line by line in order, variables are assigned their given value and invocation of functions and methods are executed.</em></strong></p><p>Now knowing this information lets look at this example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J3CVNgzsBaeRI331QI17FA.png" /></figure><p>When we run this code what do you think the console logs will output?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o4ktKCo8qzlxDraB0QGzxQ.png" /></figure><p>The console logs spit out undefined. The reason for this is during the creation phase javascript “moves” all declarations to the top of the scope. Variables are assigned a temporary value of undefined and saved to memory, function declarations are directly saved to memory. Then in the execution phase javascript goes through the code in order it is written and either invokes any functions it encounters or assigns the written value to any variables. So in our example this would look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*18AYwXXB1CLSxsrkAl8Clw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pWFpUteHZNQCpBc5r74EfA.png" /></figure><p>What happened during our creation phase is hoisting. Javascript “moves” all variable declarations to the top of their scope and assigns them “undefined” they are then are saved to memory. (Moves is in quotes because no code is physically moved at all!) The reason why our console logs are displaying undefined is because the console logs come before our variables are assigned their given value during the execution phase. They retain their undefined assignment out of the creation phase because javascript runs the code in order in the execution phase so the console logs are invoked first before the variable have the chance to be assigned their given value.</p><p>Circling way back to when execution context is created, the first instance that an execution context is created is when a javascript engine is started, a <strong>global execution context</strong> is created. This is where all your code lives, it encompasses all the code that you write. When a function is invoked in the global execution context, it creates a baby execution context within the global execution context called a <strong>function execution context</strong>. In this execution context is where all the code in the invoked function is executed.</p><p>And thats it! Kind of an abrupt end but I want to dive deeper into this in future blogs! BYE!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b621d20f609" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript: Callbacks and Higher Order Functions]]></title>
            <link>https://medium.com/@kevinlai76/javascript-callbacks-and-higher-order-functions-3e5a81b4ec9d?source=rss-72aed5d6c5ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e5a81b4ec9d</guid>
            <dc:creator><![CDATA[Kevin Lai]]></dc:creator>
            <pubDate>Mon, 02 Mar 2020 02:52:53 GMT</pubDate>
            <atom:updated>2020-03-02T02:52:53.049Z</atom:updated>
            <content:encoded><![CDATA[<p>What is a callback in javascript?</p><p><strong>A callback is a function that is passed as an argument to another function.</strong></p><p>what is a higher order function?</p><p><strong>A higher order function is a function that takes at least one function as an input and/or returns a function as an output.</strong></p><p>So…what does this mean and what is the purpose?</p><p>Lets look at some code first!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GvNNvNVuNVnl_wnfPrZVMQ.png" /></figure><p>In our example here we have a function manipulate, it takes a parameter callback. Within manipulate we are looping through our array and executing the function we pass in as an argument for callback on each element in the array, pushing it into result and returning it.</p><p>In this example our function manipulate is a higher order function, it takes a function as an input and executes it within it’s body. Which means the function we pass in as an argument is a callback. So…why is this important?</p><p>Lets look at a new example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-DxpM-JRsF--Zj2gWBLVLw.png" /></figure><p>in this bit of code we have a function add10 in our function we have a for loop that iterates through an array, in the loop’s body we add 10 to the current element and push it in to our result array, lastly we console log our result array and we get [11, 12, 13]. Now what if we want to create a function add100 what would that look like? It would look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KioX8KUyzQzkdNpmmo9cwg.png" /></figure><p>Hmmm…all we’ve done really is change the function’s name and within the for loop change the amount we’re adding. Thats a lot of repeated code for a small change. Lets refactor this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iX8Q8VtGTBRk-o7Yh6OcvA.png" /></figure><p>cool this gets rid of our DRY issue, and this function works great if all we had to do was add a number to our elements in an array, but what if we wanted our function to have flexible functionality lets look back to our first example with the words:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GvNNvNVuNVnl_wnfPrZVMQ.png" /></figure><p>What callbacks do is abstract our code so that it can function in multiple ways. It generalizes our code so that it can perform many functions instead of having just one.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e5a81b4ec9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>