<?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 Toddypet on Medium]]></title>
        <description><![CDATA[Stories by Toddypet on Medium]]></description>
        <link>https://medium.com/@toddypet?source=rss-b613cb093c84------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NHo3O7IWSnOuXZH59g4UtA.jpeg</url>
            <title>Stories by Toddypet on Medium</title>
            <link>https://medium.com/@toddypet?source=rss-b613cb093c84------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:13:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@toddypet/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 — Error handling]]></title>
            <link>https://toddypet.medium.com/javascript-error-handling-9bcfb785088?source=rss-b613cb093c84------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bcfb785088</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[error-handling]]></category>
            <dc:creator><![CDATA[Toddypet]]></dc:creator>
            <pubDate>Sat, 09 Oct 2021 23:35:02 GMT</pubDate>
            <atom:updated>2021-10-09T23:35:02.489Z</atom:updated>
            <content:encoded><![CDATA[<h3>Javascript — Error handling</h3><p>Why error handling? While we are executing code, we might encounter lots of errors including javascript error, system error or API returned error. Once the error occurs and we didn’t handle it properly, it will terminate code execution. In some scenarios we don’t want our code to terminate, we should use error handling to catch the error and make sure we continue executing our code.</p><p>without error handling, code terminates at line 1</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jbFLAMuhh3QD8J7EGu7h6w.png" /></figure><p>with error handling, code will execute till the end of the script</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sJ1piB2YoyrjZ63xjaFgdg.png" /></figure><p>In the previous article, I have explained the difference between callback, promise, async/await (<a href="https://toddypet.medium.com/javascript-asynchronous-programming-77f8899cd143">https://toddypet.medium.com/javascript-asynchronous-programming-77f8899cd143</a>). This article is created to handle a more complex scenario on asynchronous error handling.</p><p>The outline for this article:</p><ol><li>Basic error handling</li><li>Promise error handling</li><li>Async/await error handling</li></ol><blockquote><strong>Basic Error handling</strong></blockquote><p>For the basic error handling, if start with the try-catch statement:</p><p><strong>Try: </strong>allows you to define the code you want to execute. If at any line in this block’s execution have errors, it will be handle in catch</p><p><strong>Catch:</strong> allows you to define a block of code to be executed if any error in try block</p><pre>try {</pre><pre>} catch(error){</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C4EY0tE5H4voSZVWdZEE8g.png" /></figure><p><strong>Throw:</strong> You can also have better control of the program flow by manually throw errors in try block. You can throw a String, Number, Boolean or an Object</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xQ2IS4XCDqoUGLf93ZcaRg.png" /></figure><p><strong>Finally:</strong> finally statement is optional, if will execute code after try and catch regardless of the result</p><p>it runs when no errors in try block</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*thxBLzndboNB8uz5Nf67Rg.png" /></figure><p>it also runs if any error been caught in catch block</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_8iw47rIrLMMSADXZAjW6A.png" /></figure><blockquote><strong>Promise error handling</strong></blockquote><p>Now, let’s see how error handling works in promise.</p><p>Promise chain handles error in an elegant way. As long as at any step in the chain throws errors, it will jump to catch block to handle it.</p><p>When both taskOne, taskTwo pass:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ev0DFOra0O5M6EujrnMmxg.png" /></figure><p>When taskOne fail, it skip taskTwo chain and jump to catch:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ANpCP-xb2cGje4WKeGfAFw.png" /></figure><p>When taskTwo fail, it will jump to catch:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wkShwb6vfv2yH7mQz_3LAw.png" /></figure><p>You can also analyse and rethrow an error in next catch block</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kNSeN7Fb299U4cL-V8N6IQ.png" /></figure><blockquote><strong>Async/Await error handling</strong></blockquote><p>we often use try..catch block to handle error for async/await.</p><p>Once Task 1 fail, it jumps to catch block and will not run taskTwo</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tk_sBCYKiWjd9t5ZGtTs3Q.png" /></figure><p>After run taskOne, when taskTwo fail, it will jump to catch to catch the error</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WskLA1w7igRbhcuMYhyB5g.png" /></figure><p>If you don’t have try catch block in async function. Because Async function it self also return promise, you can use a .catch after async function to handle error</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y0pwHljoRdD9SHulzMLIsQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bcfb785088" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL — Select and Lookup]]></title>
            <link>https://toddypet.medium.com/sql-select-and-lookup-807535b29917?source=rss-b613cb093c84------2</link>
            <guid isPermaLink="false">https://medium.com/p/807535b29917</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Toddypet]]></dc:creator>
            <pubDate>Sat, 11 Sep 2021 06:58:56 GMT</pubDate>
            <atom:updated>2024-01-01T17:47:57.172Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL — Select and Lookup</h3><p>Follow up on the previous article about SQL CRUD <a href="https://toddypet.medium.com/sql-create-insert-f9afe47316b5">https://toddypet.medium.com/sql-create-insert-f9afe47316b5</a>. In this article, I will go through the command you need while selecting data with SQL. I will first introduce a strategy for data analysis and then go through the SQL command which maps with each step.</p><blockquote><strong>Data analysis strategy</strong></blockquote><p>Almost every single thing we do can be recorded as data, businesses can empower their business decision by analyzing these data. However, have you ever encounter difficulties that reading a big set of data but don’t know what to do? It didn&#39;t matter which programming language, tools you are using(SQL, Python, Excel). You can easily clean up your data by following below 4 steps.</p><ol><li>Select: Select the data you need. Either from one table or is a combination of multiple tables</li><li>Dimension: Decide how you are going to slice or group the data.</li><li>Metrics: Decide what you want to measure. What you are calculating. Remember, you can only do it on numeric data</li><li>Function: What function do you want to apply to the data</li></ol><p>Let’s see how this strategy applies with SQL</p><blockquote><strong>Select</strong></blockquote><p>First of all, you need to get the set of data you need. You can do it in below ways:</p><ol><li>Select from one table</li></ol><pre>SELECT [column_name] <br>FROM [table_name]</pre><p>2. Select from two tables — JOIN (column level)</p><p>we use this when you have a foreign key to other tables and join on the key</p><pre>SELECT [table_name1].[column_name], [table_name2].[column_name] ...<br>FROM [table_name1]<br>JOIN [table_name2]<br>ON [table_name1].[column_name] = [table_name2].[column_name]</pre><ul><li>Left join: Contain all information from the left-hand table</li><li>Right join: Contain all information from the right-hand table</li><li>Inner join: Contain information that exists in both tables</li><li>Self join: join with itself often used</li></ul><p>3. Select to filter out the data you need, below are some of the combition you can use on WHERE command</p><p>WHERE + Like + wildcard</p><pre>SELECT <em>column1, column2, ...</em><br>FROM <em>table_name</em><br>WHERE <em>columnN</em> LIKE <em>pattern</em>;</pre><p>WHERE + IN</p><pre>SELECT <em>column_name(s)</em><br>FROM <em>table_name</em><br>WHERE <em>column_name</em> IN (<em>value1</em>,<em> value2</em>, ...);</pre><pre>SELECT <em>column_name(s)</em><br>FROM <em>table_name</em><br>WHERE <em>column_name</em> IN (<em>SELECT STATEMENT</em>);</pre><p>WHERE + BETWEEN (often used on date)</p><pre>SELECT <em>column_name(s)</em><br>FROM <em>table_name</em><br>WHERE <em>column_name </em>BETWEEN <em>value1</em> AND <em>value2;</em></pre><p>WHERE [condition 1] AND [consition 2]. AND operator display the value that all condition is true</p><pre>SELECT <em>column1</em>,<em> column2, ...</em><br>FROM <em>table_name</em><br>WHERE <em>condition1</em> AND <em>condition2</em> AND <em>condition3 ...</em>;</pre><p>WHERE [condition 1] OR [consition 2]. OR operator display if any of the condition is TRUE</p><pre>SELECT <em>column1</em>,<em> column2, ...</em><br>FROM <em>table_name</em><br>WHERE <em>condition1</em> OR <em>condition2</em> OR <em>condition3 ...</em>;</pre><blockquote><strong>Dimension(optional)</strong></blockquote><p>This step is like pivot in excel, let’s say you have a table of drinks order, you would like to see that which</p><p>GROUP BY + Having (on the aggregate function)</p><p>NOTE: whatever is in group by need to show in select</p><blockquote><strong>Metrics</strong></blockquote><p>After Group by, what you want to measure. Metrics are quantitive measurements, are the thing can be count.</p><p>For example as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k5G04aMvukZs_2MwPvFDAA.png" /></figure><p>we first group by the product_id, for each product_id we try to find the sum for all quantity * unit price</p><pre>SELECT product_id, SUM(quantity * unit_price) AS total_sales<br>FROM sales_data<br>GROUP BY product_id;<br><br><br>//result<br><br>| product_id | total_sales |<br>|------------|-------------|<br>| 101        | 50.00       |<br>| 102        | 20.00       |<br>| 103        | 30.00       |</pre><blockquote><strong>Function</strong></blockquote><p>Once you have chosen the Measure column, then you can think about what you are going to do with that number, below are some common commands you will put on the column</p><ol><li>Create a new column derives from an existing record</li></ol><pre>SELECT [column_name] AS [new_column_name]<br>FROM [table_name]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*mnMiKNilaknwR4X0UaJyCg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u4jVxfakJLKXW39eljJoeg.png" /></figure><p>2. Common commands that has been used: MIN, MAX, COUNT, AVG, SUM</p><blockquote><strong>Practice</strong></blockquote><p>There are lots of websites provides some interview question for you to practice your SQL. Below is some recommend website:</p><ol><li><a href="https://www.hackerrank.com/domains/sql?filters%5Bstatus%5D%5B%5D=unsolved&amp;badge_type=sql">https://www.hackerrank.com/domains/sql?filters%5Bstatus%5D%5B%5D=unsolved&amp;badge_type=sql</a></li><li><a href="https://sqlzoo.net/wiki/SQL_Tutorial">https://sqlzoo.net/wiki/SQL_Tutorial</a></li></ol><blockquote><strong>Ref</strong></blockquote><ul><li><a href="https://www.w3schools.com/sql/default.asp">W3Schools.com</a></li><li><a href="https://analytics.info.yorku.ca/reports-metrics/whats-the-difference-between-dimensions-metrics/">What&#39;s the Difference between &#39;Dimensions&#39; &amp; &#39;Metrics&#39;</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=807535b29917" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL — Create & Insert]]></title>
            <link>https://toddypet.medium.com/sql-create-insert-f9afe47316b5?source=rss-b613cb093c84------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9afe47316b5</guid>
            <category><![CDATA[sql-server]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Toddypet]]></dc:creator>
            <pubDate>Sat, 04 Sep 2021 11:41:04 GMT</pubDate>
            <atom:updated>2021-09-12T15:56:56.352Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL — Create, Update, Delete</h3><p>In this article. I will first explain some glossary from SQL then guide readers step by step on how to create a database, table, and add a record.</p><blockquote><strong>Tools</strong></blockquote><p>In this article, I will use <a href="http://sqlfiddle.com/">SQLFiddle</a> to create a table</p><blockquote><strong>What is SQL</strong></blockquote><p>Structured Query Language (SQL) can be classified into two parts</p><ol><li>Data Definition Language (DDL): is responsible for defining database and table including field, data type, length etc.</li><li>Data Manipulation Language (DML): is responsible for adding, deleting, updating, looking up data. A more common way to say it is Create, Read, Update, Delete (CRUD)</li></ol><p>As you can see the name of SQL comes from Structured Query language. As long as you can understand the rules of writing it, you can use SQL as a powerful tool.</p><blockquote><strong>Styling</strong></blockquote><p>Commands and reserved words should be in caps</p><p>wrap up a string with ‘’</p><p>wrap up table_name and column_name with ``</p><p>Separate the command with a different line</p><blockquote><strong>Create a database</strong></blockquote><pre>CREATE DATABASE [databaseName]<br>CHARACTER SET [unicode]<br>COLLATE [sort rule];</pre><ul><li>unicode: Unicode is <strong>a universal character encoding standard that assigns a code to every character and symbol</strong> in every language in the world. You can look up on the unicode standard, according to your application’s need, set up with different unicode standard.</li><li>sort rule: COLLATE command followed by a sort rule, with different sort rules, words can be translated differently. It stores the unicode in a different order, when it gets translated, it will pick up the 1st result in that list of characters</li></ul><blockquote><strong>Create a table</strong></blockquote><pre>CREATE TABLE [table_name](<br>  [column1_name] [data_type] [not_null] [default],<br>  [column2_name] [data_type] [not_null] [default],<br>  [column3_name] [data_type] [not_null] [default],<br>  PRIMARY KEY ([column_name])<br>);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2eFj3ox1FICjMDKfiti76w.jpeg" /></figure><ol><li>Name:</li></ol><ul><li>Table name</li><li>Column name: if it&#39;s a foreign key then the naming convention should be tableName_columnName</li></ul><p>2. Data Type:</p><ul><li>Data type: varchar, text,bitint, smallint, tinyint, float, double, decimal(often used in financial), timestamp(often used in createTine, LastModifiedTime)</li><li>Length: coming along with type, you can put a length to specify the length on the data type to avoid memory waste</li><li>Auto Increment: it will increase automatically, you don’t need to assign value to it. We often use it on id</li><li>Zerofill: fill in the empty value with zero</li><li>NOTNULL: can’t be null value</li><li>UNSINGED: can’t be negative value</li></ul><p>3. Primary key: assign the primary key. If you don’t have data that can be assigned as primary key, you can create an id column and use it as primary key. Make for this primary key, every value is unique.</p><blockquote><strong>Insert one record, Insert multiple records</strong></blockquote><pre>INSERT INTO [table_name] ([column_name, column_name...])<br>VALUES(&#39;ABC&#39;, 12, 23)</pre><p>Command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*fBw0QBoP0RyO799JkHpiWw.jpeg" /></figure><p>Values</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*1jG2r3cPujXIGG7hALv0ww.jpeg" /></figure><p>Make sure the column name and value matches its order</p><p>Result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OAkFWrvYqfhnEhugdTgGOA.png" /></figure><blockquote><strong>Update existing record</strong></blockquote><p>Let’s say we want to change the price for Latte from 3 to 3.2</p><pre>UPDATE [table_name]<br>SET [value]<br>WHERE [which record]</pre><p>Command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*gC1dIvBV6teO2x3KpqiSkg.jpeg" /></figure><p>Result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-14kRnq825vstOWL1xZhCg.png" /></figure><blockquote><strong>Delete record</strong></blockquote><p>If we want to delete a record permanently</p><pre>DELETE FROM [table_name]<br>WHERE [which record]</pre><p>Command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/1*oGD2Ix69VYIIMpuxfkPUHg.jpeg" /></figure><p>Result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vG14RQTsq-3Xlxbim3IfsA.png" /></figure><p>Soft delete record</p><p>Sometimes you might just want to soft delete a record so you can recover easily, the common way to do that is to add a column called deleted_at</p><pre>ALTER TABLE [table_name]<br>ADD [column_name] [type]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*sAc9dSNqreG6B2P4GfvGvA.jpeg" /></figure><p>Result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I0HGtqV5ni4TxwjDw_jFAQ.png" /></figure><blockquote><strong>SQL Fiddle example</strong></blockquote><p><a href="http://sqlfiddle.com/#!9/4ddd49/1">http://sqlfiddle.com/#!9/4ddd49/1</a></p><blockquote><strong>Reference</strong></blockquote><ul><li><a href="https://www.w3schools.com/sql/default.asp">W3Schools.com</a></li><li><a href="https://www.sqlshack.com/the-collate-sql-command-overview/">Overview of the Collate SQL command</a></li></ul><p><a href="https://webfocusinfocenter.informationbuilders.com/wfappent/TL3s/TL_nls/source/01_nls_7720.htm">https://webfocusinfocenter.informationbuilders.com/wfappent/TL3s/TL_nls/source/01_nls_7720.htm</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9afe47316b5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript — Asynchronous programming]]></title>
            <link>https://toddypet.medium.com/javascript-asynchronous-programming-77f8899cd143?source=rss-b613cb093c84------2</link>
            <guid isPermaLink="false">https://medium.com/p/77f8899cd143</guid>
            <category><![CDATA[asyncawait]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[callback]]></category>
            <category><![CDATA[asynchronous]]></category>
            <dc:creator><![CDATA[Toddypet]]></dc:creator>
            <pubDate>Sat, 14 Aug 2021 14:37:38 GMT</pubDate>
            <atom:updated>2021-10-09T10:41:53.787Z</atom:updated>
            <content:encoded><![CDATA[<h3>Javascript — Asynchronous programming</h3><p>In this article, I will explain how to use Javascript for Asynchronous programming.</p><p>Before we start, let’s first think about why we need Asynchronous programming. Asynchronous programming exists to solve the problem of blocking and save your time for waiting for the previous step to complete.</p><p>In the below example, considering each task takes 10 sec to complete. Synchronous will take around 30 sec to complete three tasks. On the other hand, asynchronous will only take a little more than 10 sec</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*obbYZevTZrid8W1rNhJ8tw.png" /></figure><blockquote><strong>Callback v.s. Promise v.s. Asyn/await</strong></blockquote><p>When you google javascript asynchronous, it might come up with the above glossaries. In short, they are all tools you can use to make your code asynchronous. The time when it comes out: callback -&gt; Promise (ES5) -&gt; Async/await (ES6). Instead of considering as different tools, you should consider that each of them is an improvement of the previous one. We develop a new one for the purpose of making your code easier to read.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FGwKSYZlU6ReBDtph8_4-A.png" /></figure><blockquote><strong>Callback</strong></blockquote><p>callback function = pass on the function as a parameter to control the sequence. Once you pass on a callback function, it will be put into event loop, in this way, main thread can carry on to run the next step and we only handle the function in event loop when there is some free time in main thread. In this way, we can control the sequence of code execution and also make javascript asynchronous.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qCYyKZ7CcnvftfF3FieGaw.png" /></figure><p>For more detail on how event loop works, please check my article: <a href="https://toddypet.medium.com/javascript-event-loop-2c8623b02568">https://toddypet.medium.com/javascript-event-loop-2c8623b02568</a></p><p>To put it in code. In below example, I use call back function to present how to manage to print task in sequence: Task 1 -&gt; Taks 2 -&gt; Taks3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rXL3Md2imx0utQaDrqBuGw.jpeg" /></figure><p>SetTimeout is the most common callback feature we see in code. it accepts a callback function and a time. In below example, you can see how Iuse setTimeout to delay the taskDone to print at the end of all of the execution</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b7RwhTs54QxJnhBu5h-4ug.jpeg" /></figure><p>However, if you chain up with multiple callbacks to control the sequence as they have dependencies on the previous step. we call it <strong>callback hell</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*RxK5eVgdtnY2wtCE8Idu3Q.png" /></figure><blockquote>Promise</blockquote><p>Promise have been introduced to solve the issue we have below while using callback</p><ol><li>hard to read the code when the function can be anywhere in your index.js file</li><li>callback hell</li></ol><p>Promise is a constructor, to create your promise there are some steps you need to follow</p><p>1.create a promise instance and set up resolve reject</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SXqFgmcqS_NeDIdvCR3xyg.jpeg" /></figure><p>2.use .then/.catch handler to handle the flow</p><p>.then(): will listen to what has been returned in resolve</p><p>.catch(): will listen to what has been returned in reject</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*teGZxd85PvLHCWlwtJaeMg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oenOlAPDN1LvV05Sqpn0Hg.jpeg" /></figure><p>You can also chain multiple promises in handler. Taking the previous example we have, to print Task 1 -&gt; Task 2, can be chained as below, simply put your taskTwo function as a return. In this way, you can make your code easier to read. Instead of jumping to different lines, you can read it from top to bottom as the chain behavior clear up the function flow</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NuR7grOqCarg2HvxZkv6yA.jpeg" /></figure><blockquote><strong>Async/Await</strong></blockquote><p>However, the chaining is still not intuitive enough. To make the code more readable and make it similar to how we write synchronous code. Asyn/Await have been introduced to make your code easier to read and better way of error handling</p><ol><li>create a promise instance</li><li>add async tag in front of the function. (NOTE: async function will return a promise)</li><li>In async function, mark await as a waiting point</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fdmyzEJ0TbcADeZ3Gs8nAA.jpeg" /></figure><p>To handle the error, we combine it with try….catch…</p><p>Once it rejects with error, it will stop the flow and execute the catch block</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*84-liwxhr8kM9JpgUjMhLA.jpeg" /></figure><blockquote>Conclusion</blockquote><p>as you can see above, these technologies have been developed to do asynchronous programming. Also, JS keep making improvement (from callback -&gt; async/await) to make your code easy to read. In this article, I aim to explain the relationship between different tools. There are more APIs you can use on the top of these tools:)</p><blockquote>Ref</blockquote><p><a href="https://javascript.info/async">https://javascript.info/async</a></p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise</a></p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function</a></p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77f8899cd143" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Authentication — Cookie v.s. JWT]]></title>
            <link>https://toddypet.medium.com/authentication-cookie-v-s-jwt-ef8451ecacda?source=rss-b613cb093c84------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef8451ecacda</guid>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[cookies]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Toddypet]]></dc:creator>
            <pubDate>Sat, 07 Aug 2021 18:49:37 GMT</pubDate>
            <atom:updated>2021-08-07T18:49:37.753Z</atom:updated>
            <content:encoded><![CDATA[<h3>Authentication — Cookie v.s. JWT</h3><p>In this article, I will explain why we need authentication and compare the difference between session-based authentication v.s. token-based authentication.</p><blockquote>How Authentication works</blockquote><p>To start with, let’s first think about how authentication works.</p><ol><li>user login</li><li>username/password pass on to the server</li><li>server verified the information and grant the permission by sending back the authentication token</li><li>once we receive the authentication token, we will use it in every of our user request</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e43YeFfT0QsH0Xfj_OfhYg.png" /></figure><blockquote>HTTP stateless</blockquote><p>You might be wondering why we need to show our authentication token in every client request. This is because <strong>HTTP is stateless</strong>, which means HTTP will not remember the state of every request which means every request is unique and servers are not able to tell which client is sending the request.</p><p>To overcome this, we use one of the below technology to save the state:</p><ul><li>cookie</li><li>web storage(local storage, session storage)</li><li>external file</li></ul><blockquote>Session-based authentication: Cookies</blockquote><p>For session-based authentication, we use cookies as an authentication token.</p><p>Server will create a session once user logs in and respond with a <strong>Set-Cookie</strong> in the header with <strong>session-id</strong>. Every following request then carries <strong>Cookie:session-id</strong> and uses it as a token to let server know who is sending the request. Once server receives the request, it can get the user detail by checking <strong>session-id. </strong>The session will be destroyed once user logs out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jWOoh-FD-rOwbkMTtKfdYA.png" /></figure><p>In our browser, we save it in web storage after we received cookie detail. If you open dev tools in your chrome browser, you should be able to find your cookie detail as Application -&gt; Storage -&gt; cookie</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*R8Ho6Kiy4ysLmiaj7Ny6Cg.png" /></figure><blockquote>Token-based authentication: JWT</blockquote><p>For token-based authentication, we use JWT (JSON Web Tokens) as an authentication token.</p><p>After user logs in, server will create a JWT token and send it back to client. Every following request will have JWT in the header for server to identify who is sending the request. When user logs out, we will delete JWT from the client side(local storage)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0jT7ILkjfcEkoY-mVakqDQ.png" /></figure><p>In our browser, we save normally save it in Local Storage</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*WUvYK95Pu5M2LB_G6UMDug.png" /></figure><p>JSON Web Tokens Structure</p><ol><li>Header</li><li>Payload</li><li>Signature</li></ol><p>You can use this <a href="https://jwt.io/">tool</a> to decode your JWT token</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iaoBbAoxa513BIV8ahAsYg.png" /></figure><blockquote>Ref</blockquote><ul><li><a href="https://jwt.io/introduction/?ref=hackernoon.com">JWT.IO - JSON Web Tokens Introduction</a></li><li><a href="https://hackernoon.com/using-session-cookies-vs-jwt-for-authentication-sd2v3vci">Using Session Cookies Vs. JWT for Authentication | Hacker Noon</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies?ref=hackernoon.com">Using HTTP cookies - HTTP | MDN</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef8451ecacda" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript — Event loop]]></title>
            <link>https://toddypet.medium.com/javascript-event-loop-2c8623b02568?source=rss-b613cb093c84------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c8623b02568</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[event-loop]]></category>
            <category><![CDATA[javascript-event-loop]]></category>
            <dc:creator><![CDATA[Toddypet]]></dc:creator>
            <pubDate>Sun, 25 Jul 2021 15:27:03 GMT</pubDate>
            <atom:updated>2021-07-25T15:39:49.034Z</atom:updated>
            <content:encoded><![CDATA[<h3>Javascript — Event loop</h3><p>Have you ever seen the below code and wondering why people write something to wait for 0 sec?</p><pre>setTimeout( () =&gt; {console.log(&#39;wait for 0 sec&#39;)},0)</pre><p>It seems like a meaningless step as in real life you won&#39;t hold off someone and said wait for 0 seconds. However, this actually covers a very valuable theory that can transform your javascript code from single thread to multithread use.</p><p>I found a youtube clip that best explains event loop. In this article, I will share the note I took while watching this youtube clip: <a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ">https://www.youtube.com/watch?v=8aGhZQkoFbQ</a>.</p><blockquote>Javascript = single thread</blockquote><p>To answer this question, we need to first understand how javascript actually works. Javascript location its memory in two main sectors:</p><ol><li>Memory heap: Memory allocation. Where the memory is allocated for variables and functions</li><li>Call stack: Execution context. which is a place to keep track of the function</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*x_9t-J0PIOxzdVUpuQe2uQ.png" /></figure><p>JS is single thread which means that it can do one thing at one time. One thread. One call stack. There are two common errors you will see in call stack if you treat it as single thread</p><ol><li>Blowing the stack. System shows RangeError</li><li>Block by asynchronous function. when you run an async function you will need to wait till finish to move to the next step</li></ol><blockquote>Web api and Event loop</blockquote><p>To avoid the scenario that you always need to wait for the response from the server to execute another step, <strong>Web api</strong> and <strong>Event loop </strong>provide a feature here to help.</p><p>Imagine you have an organized to-do list that you use you organize your day. However, there is always some small things that comes up that you need to find time to do it</p><ol><li>Web api: comes in to help you store those random tasks</li><li>Event loop: come in to help squeeze your small tasks into your busy schedule</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aNC65JzX_G_zqw-hVRavyQ.png" /></figure><p>You can only do these things after you finish you meeting</p><blockquote>How it works in codes</blockquote><p>Below is a simple example which can show you how it works, the arrange of these key role:</p><ol><li>JS : run function1</li><li>Web APIs: setTimeout</li><li>Eventloop: arrange when to run function1(), callback1(), callback2()</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7kAVVTvkfntlwsAj_4dluA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RlyhkFzceK0rswrhMp_FJQ.png" /></figure><p>You can see with the above example, the line been called in function1 will first execute and callback1, callback2 will be placed in the queue and only run when the stack is cleared.</p><pre>setTimeout( () =&gt;{console.log(&#39;wait for 10 sec&#39;)},10)</pre><p>If I change the code into 10 ms then it will wait at the web APIs box for 10ms and then push it to callback queue. However, if after 10ms there are still function in stack, it need to wait in queue still, event loop can only put it in when the stack is cleared</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oVRalGV2iFHRHmAtvfH6UA.png" /></figure><p>So, to summarize the important point for setTimeout</p><ol><li>it is deferring the execution until the stack is clear</li><li>setTimeout’s time means the minimum wait till the execution</li></ol><blockquote>More…</blockquote><p>There are more different types of Web APIs and different type of queues. Such as: Promise, rendering queue. I will cover that in other articles.</p><blockquote>Refs</blockquote><p>This article is a note Imade after I watch below youtube clips. Please do go and check that video :)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F8aGhZQkoFbQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D8aGhZQkoFbQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F8aGhZQkoFbQ%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a661a28c8cc4ab11cdfc9f9487ebd139/href">https://medium.com/media/a661a28c8cc4ab11cdfc9f9487ebd139/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c8623b02568" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript — Object Destructuring]]></title>
            <link>https://toddypet.medium.com/javascript-object-destructuring-76f8dde9e273?source=rss-b613cb093c84------2</link>
            <guid isPermaLink="false">https://medium.com/p/76f8dde9e273</guid>
            <category><![CDATA[objects]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Toddypet]]></dc:creator>
            <pubDate>Sat, 10 Apr 2021 15:42:05 GMT</pubDate>
            <atom:updated>2021-04-13T17:24:20.107Z</atom:updated>
            <content:encoded><![CDATA[<h3>Javascript — Object Destructuring</h3><p>In this article, I will explain object destructuring using pure Javascript and provide some examples of how I use it in my react project</p><blockquote>How to use it with pure Javascript</blockquote><ol><li>basic extract</li></ol><p>The basic use of destructuring is to us {} to extract one or more properties from your object. The below example shows that you can extract and create a string variable with the same property name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*xQkwGIdaF4J2LewK8kOf8A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*gtARQ9b4EfIydQJ4WhSpig.png" /></figure><p>2. default value</p><p>If the destructed object don’t have the properties value, you can assign a default value, otherwise, it will return undefined. However, by assigning it with a value, it will not change your original object</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4RLZIuf3Se0zc1BeTZ95Ig.png" /></figure><p>3. Alias</p><p>If you want to extract property and give it a different name, you can use : to create an alias of it. Again, this will not change your original object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1B47pjA7taflvE9ECs9C4g.png" /></figure><p>4. Nested Object</p><p>Another popular way to use it is to extract a nested object, below example you can see that although currentRanking is in the nested layer, we can still extract is as a string</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rNyaOW-DvXyAk81blPx7yw.png" /></figure><p>5. Rest Object</p><p>Combine it with rest syntax, you can extract multiple properties as an object variable.</p><p>Rest parameter and spread syntax: <a href="https://javascript.info/rest-parameters-spread">https://javascript.info/rest-parameters-spread</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tzrrntFGPzCsuCBlinP7jw.png" /></figure><blockquote>How I use it in my project</blockquote><ol><li>Use in React Props</li></ol><p>one of the common uses I find out is using it to pass in react props.</p><p>Let’s use this codeSandbox to explain the behavior: <a href="https://codesandbox.io/s/infallible-bogdan-6rt4h">https://codesandbox.io/s/infallible-bogdan-6rt4h</a></p><p>In parent component ./WeatheraApp.js, with conditional rendering, I am passing some props down to ./WeatherCard.js and ./WeatherSetting.js.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*BEti1MZ31ZXV9wircNOChA.png" /></figure><p>Let&#39;s first check what happens in weather cards, when passing in props if you check it on console.log, you can see it’s passing in a props object with below properties</p><pre>props: {<br> weatherElement:<br> moment:<br> fetchData:<br> setCurrentPage:<br> cityName:<br>}</pre><p>Hence, in weatherCard we can extract these properties</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0lNbIrp-IP8iokdyQf4Q4Q.png" /></figure><p>In weatherSetting, we extract it when sending it over as a parameter, hence in weatherSetting function we can use cityName directly</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q8h3M-GXR0be7V0IGiBxbQ.png" /></figure><blockquote>Ref</blockquote><ul><li>How to Use Object Destructuring in JavaScript: <a href="https://dmitripavlutin.com/javascript-object-destructuring/">https://dmitripavlutin.com/javascript-object-destructuring/</a></li><li>weatherApp project:<a href="https://ithelp.ithome.com.tw/articles/10222662">https://ithelp.ithome.com.tw/articles/10222662</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76f8dde9e273" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript — DateTime]]></title>
            <link>https://toddypet.medium.com/javascript-datetime-57794b31df54?source=rss-b613cb093c84------2</link>
            <guid isPermaLink="false">https://medium.com/p/57794b31df54</guid>
            <category><![CDATA[datetime]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[date]]></category>
            <dc:creator><![CDATA[Toddypet]]></dc:creator>
            <pubDate>Sun, 24 Jan 2021 12:31:15 GMT</pubDate>
            <atom:updated>2021-07-25T10:39:55.073Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript — DateTime</h3><p>Recently I am working on an app that required me to compare the DateTime data from API between my local pc time. Below I have listed out some useful method that I always use to help me compare and present the right time</p><blockquote><strong>Get local time</strong></blockquote><p>By default, when you are setting up a new Date Object, it will return a date object with your browser&#39;s time</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wk1BbDxBrD3snF2L5-E6jw.png" /></figure><p>After creating a Date object, you can use below method to get the data you need</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cf-j-Rs4C9VHndUNQnD4Ew.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/312/1*yigLklyzNDf5T28Cfd1wTg.png" /><figcaption>Ref: <a href="https://www.w3schools.com/js/js_date_methods.asp">https://www.w3schools.com/js/js_date_methods.asp</a></figcaption></figure><p>However, when you are developing an application, it&#39;s a common procedure that you will save and send the date in UTC time to support users in any timezone. In this case, you just need to specify UTC in your method, for example, getUTCTime()</p><blockquote><strong>Compate time</strong></blockquote><p>By using Date object, you can only use operator &gt; , &lt; , ≥, ≤. It doesn’t accept ===, == operator</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XudgX_oBjxiOOr_Wrj2_6A.png" /></figure><p>A common approach we use in javascript is to convert it into million-second value and then compare two values. You can either use .getTime() or Date.parse(). It will return the same result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l0BCAZE1Ai_EiV1rko0vgg.png" /></figure><blockquote><strong>Convert date to local timestamp format</strong></blockquote><p>While reading in a Date object, you can use .tolocalString Intl.DateTimeFormat object to convert it into the datetime format in your locals.</p><ol><li>Date object api</li></ol><p>The easiest way is to call the api .Below provide some example to show different type of api you can use</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*nLf4E1D6VBQ4f0VsLlIWig.png" /></figure><p>2. Intl.DateTimeFormat</p><pre>//first create a new date object<br>const date = new Date();</pre><pre>//convert the date object into Intel.DateTimeFormat object. without  specifying any set up, it will format with local language. console.log(new Intl.DateTimeFormat.format(date))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*eD-C3eLln2K6fmP2DnPliw.png" /></figure><pre>//specify date format, you can format it with assgined language<br>console.log(&#39;format ir in US&#39;, new Intl.DateTimeFormat(&#39;en-US).format(date))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*DOgqwzkWHd_wq1nD5s3MHw.png" /></figure><pre>//add an option (object) to specify the format you want to present<br>const options = {<br>hour: &#39;numeric&#39;, minute:&#39;numeric&#39;, second:&#39;numeric&#39;,<br>timeZone: &#39;UTC&#39;,<br>timeZoneName: &#39;short&#39;<br>}<br>console.log(new Intl.DateTimeFormat(&#39;en-GB&#39;, options).format(date))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*OZgStrRPa6eOnnm5rhs3MQ.png" /></figure><blockquote><strong>Ref</strong></blockquote><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat">Intl.DateTimeFormat</a></li><li><a href="https://www.w3schools.com/js/js_dates.asp">JavaScript Date Objects</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57794b31df54" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript ES6 — Const and Let]]></title>
            <link>https://toddypet.medium.com/javascript-es6-const-and-let-4aab863701e?source=rss-b613cb093c84------2</link>
            <guid isPermaLink="false">https://medium.com/p/4aab863701e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Toddypet]]></dc:creator>
            <pubDate>Mon, 28 Dec 2020 14:35:04 GMT</pubDate>
            <atom:updated>2021-04-07T16:30:58.775Z</atom:updated>
            <content:encoded><![CDATA[<h3>Javascript — Const v.s Let</h3><p>Const and Let is a new feature introduced in ES6. This article will explain the difference and usage of const and let</p><blockquote><strong>Definition</strong></blockquote><p>First of all, let’s take a look at the definition in MDN</p><ul><li><strong>Const : </strong>block-scoped, much like variables declared using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a> keyword. The value of a constant can&#39;t be changed through reassignment, and it can&#39;t be redeclared.</li><li><strong>Let:</strong> statement declares a block-scoped local variable, optionally initializing it to a value.</li></ul><p>By reading the definition, we can see that the main difference is on reassignment. In the next section, I will get into more detail on the feature comparison and provide an example to show you the error message you will get when use it in the wrong way.</p><blockquote><strong>Feature Comparison</strong></blockquote><ul><li><strong>Reassignment:</strong> You can only reassign value with the variable declared by let. If you try to reassign a value declared by const, you will get below TypeError</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*ck3kUYtW732478MDB11FWw.png" /></figure><ul><li><strong>Redeclared: </strong>As both const and let are block scope varible, you awill get syntx error if you are trying to redeclare it in the same block({})</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*XggHfLKpsGIFxFoAHs2S5g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/373/1*kzBSTxIyxakkOzCN41uNOw.png" /></figure><ul><li><strong>Block-scoped:</strong> Follow up on the previous point, if you try to declare it in the global level first and redeclare it in block level, you will not get any error.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*JpdUeX0nmiIUnPWbhlUgrA.png" /></figure><ul><li><strong>Update call by reference property: </strong>This is one of the feature that people get confused easily. The definition of reassignment we are referring here is actually reassignment on reference. .Because both array and Object are calll be reference. This explain why you are able to push new value to an array that been declared with const and change the property of object that declare with const</li></ul><p>Array</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*76jzpdOsccPHEY2L-40_UA.png" /></figure><p>Object</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*utwL_PYvJkQ4PyAu-7iPeA.png" /></figure><p>NOTE: You are not able to reassign it in below 2 ways, because it means that you are changing the memory address</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*utwL_PYvJkQ4PyAu-7iPeA.png" /></figure><p>To make it easy to compare, I made below table so I can easily reference it whenever I am not sure which one to use</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Titk8c2emnnTlm28AQnSVw.png" /></figure><blockquote>Usage</blockquote><p>Still not sure how to use it properly? I have listed out some of the common scenarios we face which will give you an idea how to use it properly</p><ul><li>use Const when you declare a variable that doesn’t want anyone else to change it</li></ul><p>This is a good practice to follow when you are working on a project. By declaring it as a const, other engineers will know that this is a static varible that shouldn&#39;t be changed</p><ul><li>const functionName = () =&gt;{}</li></ul><p>When you are declaring an arrow function, we normally see people declare it with const</p><ul><li>use let in for loop</li></ul><p>When declaring a for loop, use let as your i value will change.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/1*ex3IUDASvfYaK05XQocvTw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4aab863701e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>