<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Haastrup Elijah</title>
    <description>The latest articles on Forem by Haastrup Elijah (@haastrupea).</description>
    <link>https://forem.com/haastrupea</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F686739%2F9454f765-dd07-47e5-8093-b41377e34d0d.jpg</url>
      <title>Forem: Haastrup Elijah</title>
      <link>https://forem.com/haastrupea</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/haastrupea"/>
    <language>en</language>
    <item>
      <title>What Happens When You Ask LLMs to Analyse Their Own Answers?</title>
      <dc:creator>Haastrup Elijah</dc:creator>
      <pubDate>Mon, 23 Feb 2026 14:17:47 +0000</pubDate>
      <link>https://forem.com/haastrupea/what-happens-when-you-ask-llms-to-analyse-their-own-answers-3dcc</link>
      <guid>https://forem.com/haastrupea/what-happens-when-you-ask-llms-to-analyse-their-own-answers-3dcc</guid>
      <description>&lt;p&gt;I was just re-watching day 3 testing of LLM of Ed Donner Udemy course "AI Engineer Core Track: LLM Engineering, RAG, QLoRA, Agents&lt;br&gt;
"(Courtesy of Andela AI Engineering bootcamp 2026), and I had an Idea after Ed prompted ChatGPT, "how many words are in your answer to this question"&lt;/p&gt;

&lt;p&gt;So I went wild and decided to ask all the major frontier models I have access to: ChatGPT, Gemini, Claude, and Grok my own question, but with a twist&lt;br&gt;
&lt;code&gt;How many letter 'a' exists in your response to this prompt?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is what I found out&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ChatGPT(5.2 on Paid Plus plan)&lt;/strong&gt;: It is still processing even as I type this post.&lt;/p&gt;

&lt;p&gt;Gemini(3 fast - free): Thought for about 5-10 seconds, came up with this response&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"There are exactly 3 occurrences of the letter 'a' in this response."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which is correct, and how it came to this conclusion was actually brilliant -  it wrote 3 separate Python scripts that detect the number of "a" in a sentence.&lt;br&gt;
Here are the Python Scripts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#First Python script
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;count_a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_stable_sentence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Template should have a placeholder for the number
&lt;/span&gt;    &lt;span class="c1"&gt;# We will try numbers from 0 to 100
&lt;/span&gt;    &lt;span class="n"&gt;num_to_words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;zero&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;one&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;two&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;three&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;four&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;five&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;six&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;seven&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;eight&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;nine&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ten&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;eleven&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;twelve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;thirteen&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fourteen&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fifteen&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sixteen&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;seventeen&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;eighteen&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;nineteen&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;twenty&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;twenty-one&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;twenty-two&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;twenty-three&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;twenty-four&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;twenty-five&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;num_to_words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;sentence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;count_a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

&lt;span class="n"&gt;template1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;There are exactly {} occurrences of the letter &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; in this response.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;template2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;This response contains {} instances of the letter &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;res1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;find_stable_sentence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;template1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;res2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;find_stable_sentence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;template2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Template 1: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;res1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; (Count: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;val1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Template 2: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;res2&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; (Count: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;val2&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#Code output
#Template 1: There are exactly three occurrences of the letter 'a' in this response.
#(Count: 3)
#Template 2: This response contains three instances of the letter 'a'. #(Count: 3)
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Second Python script
&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;There are $2$ occurrences of the letter &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; in this response.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Text: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Count of &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#Code output
#Text: There are $2$ occurrences of the letter 'a' in this response.
#Count of 'a': 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Third Python script
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;exactly&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;occurrences&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;response&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;are&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;#Code output
#1
#0
#0
#1
#1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Claude(Sonnet 4.6 - free)&lt;/strong&gt;: Responded with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;There are **4** letter 'a's in my response to this prompt.

(a, a, a, a — found in: "There", "are", "letter", "a's", "in", "my", "response", "to", "this", "prompt")

Let me recount carefully:
* "There" → no
* "are" → **a**
* "letter" → no
* "a's" → **a**
* "response" → no
* "my" → no

Hmm, this is tricky because the answer changes as I write it. Let me give a honest answer: counting all 'a' letters in the visible text of this response is self-referential and hard to pin down precisely, but there are approximately **15–20** letter 'a's total.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This shows that Claude evaluates its response as it outputs them, which kind of makes it come across as a model that thinks on the fly, and that led it to a self-aware situation to know that the question is a tricky one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Grok(on auto -free)&lt;/strong&gt;: spent 1.6s thinking and came up with a rather confident response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Here is my response to this prompt:

Zero.

(There is no letter 'a' in it.)"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it had stopped at zero, it would have been the fastest model with the correct answer, but its predictive nature got the best part of "it". I guess it's overconfidence that led it to add the last part in a bracket, which essentially led to it failing to provide the right answer.&lt;/p&gt;

&lt;p&gt;Asking this question has given me some insight into how these models are wired, and that will be very useful when it comes to choosing a model for whatever task at hand, not because of the response but because of their approach to problem-solving.&lt;/p&gt;

&lt;p&gt;PS: Even after 20+ minutes, ChatGPT is still trying to generate a response.&lt;br&gt;
PS(Update): After about an hour, I refreshed ChatGPT, and the response it gave was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I decided to prompt it again, and it got stuck in the loop again(At least visually from the UI), and when I refreshed, the response is still&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then it dawned on me that when the model is done thinking and starts showing the pulsating dot cursor, I can refresh, and the answer will show up. Even at that, the response was still &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, I decided to take it a step further to know if the model is taking an easy and clever way out. I asked this follow-up question: &lt;br&gt;
"What is the total number of letter 'a' in this prompt and your response to this prompt?"&lt;/p&gt;

&lt;p&gt;After ChatGPT thought for about 14 seconds, and the pulsating dot cursor showed up, I refreshed the page, and the response was there waiting for me:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are 4 letter 'a's in the question itself, so I got to my conclusion: ChatGPT's response was wrong.&lt;/p&gt;

&lt;p&gt;I was curious to know what was in its "14s" thought process, and I found that it was actually trying to be clever, as I initially suspected. Here is the thought:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Counting the letter "a" in the prompt and my response
The user's prompt includes "a" twice — once in the quote and once in "and." My response should be crafted carefully to avoid using "a." I’ll use digits instead of words where possible to keep the count zero. After reviewing, the final answer is: 2 occurrences of the letter "a."

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fact that it was trying to cleverly avoid using the letter "a" in its response is genius, and knowing to respond with numbers and not letters to ensure the number of "a" it is counting approach zero is brilliant.&lt;/p&gt;

&lt;p&gt;Summary&lt;br&gt;
All these Models are clever at solving problems, and it is obvious that the strategy used to predict the next token has limitations. Just like the introduction of live "web-search" functionality helps models bypass the training-data "cut-off" date, there is a need for a strategy that will allow LLM models arrive at a more definitive logic beyond probabilistic when needed, as Gemini did by writing a Python script.&lt;/p&gt;

&lt;p&gt;Most models are already good at math and writing code. Applying the Gemini style of solving problems like this would actually be good enough, in my opinion.&lt;/p&gt;

&lt;p&gt;So far, only Gemini provided the right answer, and I think the advantage was because of the Python script it wrote (leveraging on its strength in the place of weakness)&lt;/p&gt;

&lt;p&gt;What does this tell you about these models?&lt;br&gt;
I would love to hear your opinions.&lt;/p&gt;

&lt;p&gt;Shout out to Ed Donner for the insight into AI models. I love the course already!&lt;/p&gt;




&lt;p&gt;I’m &lt;strong&gt;Elijah Haastrup&lt;/strong&gt;, a software engineer who loves blending practicality with clean architecture.&lt;br&gt;
You can reach me on &lt;a href="https://www.linkedin.com/in/haastrupea/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/killcodeNG" rel="noopener noreferrer"&gt;Twitter: killcodeNG&lt;/a&gt;.&lt;/p&gt;




</description>
      <category>ai</category>
      <category>aiengineering</category>
      <category>programming</category>
      <category>llm</category>
    </item>
    <item>
      <title>Expose Your Local Network App the Smart Way: Ngrok + LAN Setup Explained</title>
      <dc:creator>Haastrup Elijah</dc:creator>
      <pubDate>Thu, 06 Nov 2025 09:40:52 +0000</pubDate>
      <link>https://forem.com/haastrupea/expose-your-local-network-app-the-smart-way-ngrok-lan-setup-explained-133d</link>
      <guid>https://forem.com/haastrupea/expose-your-local-network-app-the-smart-way-ngrok-lan-setup-explained-133d</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Because sometimes, the real challenge isn’t writing the code, it’s finding the perfect balance between &lt;em&gt;portability&lt;/em&gt; and &lt;em&gt;productivity.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The Real-World Setup
&lt;/h2&gt;

&lt;p&gt;As a software engineer, I split my workflow across two worlds:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MacBook Pro&lt;/strong&gt; → my &lt;em&gt;portable dev machine&lt;/em&gt;, where all my codebases live.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Windows desktop&lt;/strong&gt; → my &lt;em&gt;home lab powerhouse&lt;/em&gt;, complete with multiple screens and a ton of processing muscle.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When I’m out, the Mac keeps me agile; I can code anywhere.&lt;br&gt;
When I’m home, the Windows setup gives me that &lt;em&gt;speed boost&lt;/em&gt; and &lt;em&gt;screen-estate&lt;/em&gt; to research, debug, and test faster.&lt;/p&gt;

&lt;p&gt;But there was a catch.&lt;/p&gt;


&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;The app I run on my &lt;strong&gt;Mac&lt;/strong&gt; is only accessible at:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test it from my Windows workstation (on the same Wi-Fi network), I expose it to my &lt;strong&gt;local network&lt;/strong&gt; by binding it to the Mac’s LAN IP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://192.168.x.x:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That way, I can open the app from any device connected to my home network, including my Windows PC.&lt;/p&gt;

&lt;p&gt;Now here’s the twist:&lt;br&gt;
I already had &lt;strong&gt;Ngrok installed on my Windows machine&lt;/strong&gt;, not the Mac. I didn’t want to clutter my Mac’s environment with another dependency just to share or tunnel my dev app.&lt;/p&gt;

&lt;p&gt;So the goal became:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Use Ngrok on my Windows PC&lt;/strong&gt; to expose my Mac’s running app, via its LAN IP.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That meant tunnelling the &lt;strong&gt;local network address&lt;/strong&gt;, not the &lt;code&gt;localhost&lt;/code&gt; one.&lt;/p&gt;



&lt;p&gt;Here is how!&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1 — Make Sure the App Listens on the LAN Interface
&lt;/h2&gt;

&lt;p&gt;If you’re running a dev server, by default, it probably listens only on &lt;code&gt;127.0.0.1&lt;/code&gt;, meaning no one else (not even your Windows machine) can see it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Check it
&lt;/h3&gt;

&lt;p&gt;On your Mac terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;lsof &lt;span class="nt"&gt;-i&lt;/span&gt; :3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it says:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TCP 127.0.0.1:3000 (LISTEN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;…that’s localhost-only.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix it
&lt;/h3&gt;

&lt;p&gt;Start your app bound to &lt;strong&gt;0.0.0.0&lt;/strong&gt; (all interfaces):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Node.js&lt;/span&gt;
npm run dev &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;--host&lt;/span&gt; 0.0.0.0

&lt;span class="c"&gt;# Python&lt;/span&gt;
python3 &lt;span class="nt"&gt;-m&lt;/span&gt; http.server 3000 &lt;span class="nt"&gt;--bind&lt;/span&gt; 0.0.0.0

&lt;span class="c"&gt;# Flask&lt;/span&gt;
flask run &lt;span class="nt"&gt;--host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.0.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it’s reachable both locally and across your home network:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:3000
http://192.168.x.x:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 2 — Tunnel That LAN IP from Windows
&lt;/h2&gt;

&lt;p&gt;With your Mac app accessible over LAN, move to your Windows machine (same Wi-Fi).&lt;/p&gt;

&lt;p&gt;Open &lt;strong&gt;PowerShell&lt;/strong&gt; or &lt;strong&gt;CMD&lt;/strong&gt; and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ngrok http 192.168.x.x:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ngrok creates a secure tunnel from your Windows machine to the app running on your Mac.&lt;br&gt;
You’ll instantly get a public HTTPS link like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Forwarding  https://randomstring.ngrok.io -&amp;gt; http://192.168.x.x:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From there:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can test mobile webhooks.&lt;/li&gt;
&lt;li&gt;Preview your app remotely.&lt;/li&gt;
&lt;li&gt;Share it with teammates or clients.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All without installing anything on your Mac.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Setup Works So Well
&lt;/h2&gt;

&lt;p&gt;This approach merges two workflows perfectly:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Device&lt;/th&gt;
&lt;th&gt;Benefit&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Portable coding&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Mac&lt;/td&gt;
&lt;td&gt;Keeps your environment lightweight, codebases intact&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance testing&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Windows&lt;/td&gt;
&lt;td&gt;Leverages better screens and CPU for analysis&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Public exposure&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Ngrok on Windows&lt;/td&gt;
&lt;td&gt;No duplication of setup or tokens&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It’s the best of both worlds: &lt;em&gt;portable dev, stable network tunnelling.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Bonus: Make Ngrok Persistent
&lt;/h2&gt;

&lt;p&gt;If you expose the same service frequently, automate it with a config file on your Windows system:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;~/.config/ngrok/ngrok.yml&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2"&lt;/span&gt;
&lt;span class="na"&gt;authtoken&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;YOUR_NGROK_AUTH_TOKEN&lt;/span&gt;
&lt;span class="na"&gt;tunnels&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mac-app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;addr&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;192.168.x.x:3000&lt;/span&gt;
    &lt;span class="na"&gt;proto&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then start it anytime:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ngrok start mac-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’re on Ngrok Pro, set:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;subdomain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mymacapp&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to get a stable URL like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://mymacapp.ngrok.io
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Common Pitfalls
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Cause&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;connection refused&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;App bound to 127.0.0.1 only&lt;/td&gt;
&lt;td&gt;Restart app with &lt;code&gt;--host 0.0.0.0&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;“Tunnel established” but blank page&lt;/td&gt;
&lt;td&gt;Wrong LAN IP&lt;/td&gt;
&lt;td&gt;Run &lt;code&gt;ipconfig&lt;/code&gt; (Windows) or &lt;code&gt;ifconfig&lt;/code&gt; (Mac) to confirm&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Firewall blocked&lt;/td&gt;
&lt;td&gt;macOS firewall restrictions&lt;/td&gt;
&lt;td&gt;Allow incoming connections for your dev app&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ngrok error: “connection timed out”&lt;/td&gt;
&lt;td&gt;IP mismatch or wrong port&lt;/td&gt;
&lt;td&gt;Double-check your local IP and port&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;p&gt;The beauty of this setup is subtle but powerful:&lt;br&gt;
It’s not about Ngrok or the tunnel; it’s about designing a &lt;strong&gt;workflow architecture&lt;/strong&gt; that fits your &lt;em&gt;real life&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep your &lt;strong&gt;Mac portable&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Keep your &lt;strong&gt;Windows setup powerful&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Let &lt;strong&gt;Ngrok bridge&lt;/strong&gt; the two worlds.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When your workflow mirrors your lifestyle, productivity stops feeling forced; it starts flowing naturally.&lt;/p&gt;




&lt;p&gt;This little hack gave me full access to my dev environment anywhere, while keeping my core setup lightweight and efficient.&lt;br&gt;
It’s also a reminder that dev ops doesn’t always need to be “cloud-native” — sometimes, it’s just about &lt;strong&gt;connecting two good machines smartly&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;I’m &lt;strong&gt;Elijah Haastrup&lt;/strong&gt;, a software engineer who loves blending practicality with clean architecture.&lt;br&gt;
If you enjoyed this write-up, let me know if you have any suggestions or questions in the comments.&lt;br&gt;
You can reach me on &lt;a href="https://www.linkedin.com/in/haastrupea/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/killcodeNG" rel="noopener noreferrer"&gt;Twitter: killcodeNG&lt;/a&gt;. I share stories and systems that make software development &lt;em&gt;simpler, faster, and saner.&lt;/em&gt;&lt;/p&gt;




</description>
      <category>ngrok</category>
      <category>development</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>How I Hosted a WordPress Blog on a Subdomain While Keeping My Main Domain on Render</title>
      <dc:creator>Haastrup Elijah</dc:creator>
      <pubDate>Wed, 15 Oct 2025 11:00:08 +0000</pubDate>
      <link>https://forem.com/haastrupea/how-i-hosted-a-wordpress-blog-on-a-subdomain-while-keeping-my-main-domain-on-render-4h9d</link>
      <guid>https://forem.com/haastrupea/how-i-hosted-a-wordpress-blog-on-a-subdomain-while-keeping-my-main-domain-on-render-4h9d</guid>
      <description>&lt;p&gt;It's been a while since I shared my excitement here; I guess I lost it.&lt;br&gt;
I got my excitement back and am here to share again!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A semi-automated Let’s Encrypt SSL setup for WordPress on Namecheap, without breaking existing DNS and SSL setups.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  The Goal
&lt;/h2&gt;

&lt;p&gt;I had a simple (but tricky) need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;My &lt;strong&gt;main domain is hosted on Namecheap&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;My &lt;strong&gt;main site and APIs are hosted on &lt;a href="https://render.com" rel="noopener noreferrer"&gt;Render.com&lt;/a&gt;&lt;/strong&gt; using &lt;strong&gt;A records&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;I wanted to &lt;strong&gt;create a blog&lt;/strong&gt; using &lt;strong&gt;WordPress&lt;/strong&gt; on a &lt;strong&gt;subdomain&lt;/strong&gt; like &lt;code&gt;blog.mydomain.com&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;I wanted to do this &lt;strong&gt;without messing up existing DNS/SSL settings&lt;/strong&gt; for the main domain and API services.&lt;/li&gt;
&lt;li&gt;I also needed a valid &lt;strong&gt;SSL certificate for the subdomain&lt;/strong&gt; - even though Namecheap shared hosting doesn’t allow Let’s Encrypt automation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how I did it.&lt;/p&gt;


&lt;h2&gt;
  
  
  Why I Chose Namecheap Legacy Hosting Over Cloud Hosting
&lt;/h2&gt;

&lt;p&gt;While there are dozens of cloud-based WordPress hosting platforms out there (like Kinsta, Cloudways, WP Engine, and even Render itself), I made a conscious decision to go with &lt;strong&gt;Namecheap’s legacy shared hosting plan&lt;/strong&gt; - and here’s why:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cost.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The amount I pay &lt;strong&gt;yearly&lt;/strong&gt; for legacy hosting on Namecheap is &lt;strong&gt;less than what I would pay monthly&lt;/strong&gt; on a typical cloud-based WordPress hosting platform.&lt;/p&gt;

&lt;p&gt;Since this blog isn't mission-critical in terms of performance or uptime, and I can handle the technical bits myself, it didn’t make sense to overpay for managed cloud infrastructure. The legacy hosting plan gave me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Full cPanel access&lt;/li&gt;
&lt;li&gt;Softaculous for 1-click WordPress install&lt;/li&gt;
&lt;li&gt;Support for manual SSL&lt;/li&gt;
&lt;li&gt;Enough storage and bandwidth for my use case&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tradeoff: No automation for SSL&lt;br&gt;&lt;br&gt;
The win: I save real money every year&lt;/p&gt;


&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;If you’ve tried something similar, you’ve likely hit at least one of these roadblocks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;DNS conflict&lt;/strong&gt; — Hosting your main domain on Render means you don’t want Namecheap’s hosting to override your A records or nameservers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Namecheap’s shared hosting forces you to assign a primary domain&lt;/strong&gt;, and if you use your real domain, it may mess with existing DNS setups.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No free SSL automation&lt;/strong&gt; — Shared hosting on Namecheap doesn’t support Let’s Encrypt natively or auto-renewals.&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  The Setup That Worked
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. &lt;strong&gt;Keep Main Domain on Render&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;My &lt;code&gt;@&lt;/code&gt; and &lt;code&gt;api&lt;/code&gt; DNS records on Namecheap point to Render's IP addresses - this part stays untouched.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. &lt;strong&gt;Create a Dummy Domain for Hosting Setup&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Since Namecheap requires a domain when setting up shared hosting, I used a &lt;strong&gt;dummy domain&lt;/strong&gt; (e.g., &lt;code&gt;mysandboxdomain.com&lt;/code&gt;) just to initialise the hosting account.&lt;/p&gt;

&lt;p&gt;This way, my main domain's DNS was never touched.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. &lt;strong&gt;Add a Subdomain (blog) Pointing to Namecheap&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In my DNS settings on Namecheap (or Cloudflare if you're managing DNS there), I added this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Type: A
Host: blog
Value: &amp;lt;Namecheap hosting IP&amp;gt;
TTL: Automatic
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pointed only the subdomain (&lt;code&gt;blog.mydomain.com&lt;/code&gt;) to Namecheap hosting - leaving &lt;code&gt;mydomain.com&lt;/code&gt; and &lt;code&gt;api.mydomain.com&lt;/code&gt; on Render untouched.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Install WordPress on the Subdomain&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In Namecheap’s cPanel:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I added &lt;code&gt;blog.mydomain.com&lt;/code&gt; as an &lt;strong&gt;Addon Domain&lt;/strong&gt; or via the &lt;strong&gt;Domains panel&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Then I installed WordPress to that subdomain via &lt;strong&gt;Softaculous&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything was working, except…&lt;/p&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;SSL: The Final Challenge&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Render already handles SSL for my main site and APIs.&lt;/p&gt;

&lt;p&gt;But Namecheap wouldn’t let me install Let’s Encrypt automatically for the subdomain.&lt;/p&gt;

&lt;p&gt;So I did the next best thing:&lt;/p&gt;




&lt;h2&gt;
  
  
  Manual Let’s Encrypt Setup with Certbot
&lt;/h2&gt;

&lt;p&gt;On my local Linux machine:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I installed Certbot:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;certbot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Then I generated a cert manually using the DNS challenge:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   certbot certonly &lt;span class="nt"&gt;--manual&lt;/span&gt; &lt;span class="nt"&gt;--preferred-challenges&lt;/span&gt; dns &lt;span class="nt"&gt;-d&lt;/span&gt; blog.mydomain.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Certbot gave me a &lt;code&gt;_acme-challenge.blog&lt;/code&gt; TXT record to add.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;I added that in my DNS and waited a few minutes before confirming. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Certbot suggested &lt;a href="https://toolbox.googleapps.com/apps/dig/" rel="noopener noreferrer"&gt;Google Admin Toolbox&lt;/a&gt; tool to confirm that the TXT value has propagated.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Files Generated
&lt;/h3&gt;

&lt;p&gt;After successful verification, Certbot gave me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cert.pem&lt;/code&gt; (SSL certificate)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;privkey.pem&lt;/code&gt; (Private key)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;chain.pem&lt;/code&gt; (CA bundle)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Installed Certificate in cPanel
&lt;/h3&gt;

&lt;p&gt;In &lt;strong&gt;cPanel &amp;gt; SSL/TLS &amp;gt; Manage SSL Sites&lt;/strong&gt;, I selected &lt;code&gt;blog.mydomain.com&lt;/code&gt; and:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pasted &lt;code&gt;cert.pem&lt;/code&gt; as the Certificate (CRT)&lt;/li&gt;
&lt;li&gt;Pasted &lt;code&gt;privkey.pem&lt;/code&gt; as the Private Key&lt;/li&gt;
&lt;li&gt;Pasted &lt;code&gt;chain.pem&lt;/code&gt; as the CA Bundle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Boom - SSL activated.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Have to Do Every 90 Days
&lt;/h2&gt;

&lt;p&gt;Let’s Encrypt certs expire every 90 days, so I:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run the same &lt;code&gt;certbot&lt;/code&gt; command&lt;/li&gt;
&lt;li&gt;Update the TXT record again&lt;/li&gt;
&lt;li&gt;Get fresh certs&lt;/li&gt;
&lt;li&gt;Paste them into cPanel manually&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It takes 5–10 minutes and works reliably.&lt;/p&gt;




&lt;h2&gt;
  
  
  What This Setup Gives Me
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;[]&lt;/th&gt;
&lt;th&gt;Outcome&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;Main domain + API remain fully functional on Render&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;Blog subdomain is hosted separately on Namecheap&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;SSL for blog via Let's Encrypt (manual install)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;Legacy hosting saves $$$ vs cloud WordPress hosting&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;I have a full-featured WordPress blog securely running&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Who This Is For
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You host your main domain/app on &lt;strong&gt;Render, Vercel, Netlify&lt;/strong&gt;, etc.&lt;/li&gt;
&lt;li&gt;You want a &lt;strong&gt;separate WordPress blog&lt;/strong&gt; under the same domain&lt;/li&gt;
&lt;li&gt;You need &lt;strong&gt;HTTPS on a subdomain&lt;/strong&gt; via Let’s Encrypt&lt;/li&gt;
&lt;li&gt;You want to avoid high-cost WordPress hosting platforms&lt;/li&gt;
&lt;li&gt;You’re okay pasting a cert every 90 days&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This setup might sound like a hack, but it’s a clean and modular way to get:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance and simplicity (Render)&lt;/li&gt;
&lt;li&gt;Flexibility and blogging power (WordPress on Namecheap)&lt;/li&gt;
&lt;li&gt;No extra monthly cost for cloud blog hosting&lt;/li&gt;
&lt;li&gt;SSL across your stack, with control over all parts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the best part? I didn't have to touch the main domain's DNS or sacrifice performance.&lt;/p&gt;




&lt;p&gt;Let me know if you have any suggestions or questions in the comments.&lt;br&gt;
You can reach me on &lt;a href="https://www.linkedin.com/in/haastrupea/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/killcodeNG" rel="noopener noreferrer"&gt;Twitter: killcodeNG&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>dns</category>
      <category>ssl</category>
      <category>domain</category>
      <category>hosting</category>
    </item>
    <item>
      <title>MICROSERVICES IN MY OWN WORDS</title>
      <dc:creator>Haastrup Elijah</dc:creator>
      <pubDate>Tue, 25 Jul 2023 15:34:02 +0000</pubDate>
      <link>https://forem.com/haastrupea/microservices-in-my-own-words-24ge</link>
      <guid>https://forem.com/haastrupea/microservices-in-my-own-words-24ge</guid>
      <description>&lt;p&gt;Microservices are an effective way to scale products in terms of operations and developments (DevOps). This involves separating each part of a product base on services e.g. report generation, transaction processing, user management, etc.&lt;/p&gt;

&lt;p&gt;SCALING DEVELOPMENT&lt;/p&gt;

&lt;p&gt;These components can then become backend services that are contained in their own separate codebases and each handled by a separate individual/team leading to specialization, quick bug detection and fixes, quick customer escalation handling, and deeper observability ( app monitoring), all of these help to scale the development processes. One last example, microservices allow for the use of different stacks/languages that is suitable for a service instead of the monolith one stack/language for all. &lt;/p&gt;

&lt;p&gt;SCALING OPERATION&lt;/p&gt;

&lt;p&gt;Each of these services can be deployed separately without affecting the entire product. Bug fixes and feature updates get deployed to production easily and fast. During deployment, only the tests for that service will run leading to a quick deployment time. microservices can also take advantage of multi-cloud. &lt;/p&gt;

&lt;p&gt;These are a few ways that microservices can help to scale operations.&lt;/p&gt;

&lt;p&gt;EVENT DRIVEN ARCHTECTURE(EDA)&lt;/p&gt;

&lt;p&gt;Microservices gave birth to the rise in the need for EDA(event-driven architecture), most time these services depend on each other, and they need to communicate in a consistent and deterministic manner, especially in the transaction processing in the wallet management system, order of processing matter, and downtime in the transaction processing doesn't have to lead to a loss of transactions at that moment. EDA comes to solve that issue and many more with products like Apache Kafka and RabbitMQ etc.&lt;/p&gt;

&lt;p&gt;SECURING MICROSERVICES &lt;/p&gt;

&lt;p&gt;API gateway architecture is the only way that comes to mind while writing this. An API gateway setup can help protect the services and bring about a more secure microservices system. NGINX published a very good article on this topic that I have used in the past and it helped me to create a lucid image of the concept in my head. Here is a link to the article: &lt;a href="https://www.nginx.com/learn/api-gateway/" rel="noopener noreferrer"&gt;What Is an API Gateway?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MICRO FRONT-END&lt;/p&gt;

&lt;p&gt;On the front-end(client side) as well, there is now a micro front-end just coming up. The micro front-end is microservices on the client side. I have not seen this architecture used in production nor have I tried it out personally before but I think I am willing to explore. &lt;a href="https://micro-frontends.org/" rel="noopener noreferrer"&gt;Micro-front-ends.org&lt;/a&gt; is a great place to deepen your understanding and get started as well.&lt;/p&gt;

&lt;p&gt;DISADVANTAGES OF MICROSERVICES&lt;/p&gt;

&lt;p&gt;It requires a large team to maintain and it is expensive to run. Big companies use it, recently, startups are beginning to use it to scale their products. Well if they can generate enough revenue or rake in more investor's cash, I guess they can keep it.&lt;/p&gt;

&lt;p&gt;NOTE TO ALL READERS&lt;/p&gt;

&lt;p&gt;The content of this article is spontaneous i.e. apart from the links to articles and sites I have visited in the past that I had to look up, every other thing came from years of learning and developing with microservices. If there is something you think I missed or not accurate, please let me know, it would be a great opportunity for me to learn and relearn.&lt;/p&gt;

&lt;p&gt;Let me know if you have any suggestions or questions in the comments.&lt;br&gt;
You can reach me on &lt;a href="https://www.linkedin.com/in/haastrupea/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt; and &lt;a href="https://twitter.com/killcodeNG" rel="noopener noreferrer"&gt;Twitter: killcodeNG&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>apigateway</category>
      <category>systemdesign</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript Iteration Protocol</title>
      <dc:creator>Haastrup Elijah</dc:creator>
      <pubDate>Tue, 18 Jul 2023 22:49:31 +0000</pubDate>
      <link>https://forem.com/haastrupea/javascript-iteration-protocol-2n4c</link>
      <guid>https://forem.com/haastrupea/javascript-iteration-protocol-2n4c</guid>
      <description>&lt;p&gt;If there is a concept I have had to learn and relearn from time to time,  it is the concept of iteration in JavaScript. This time around I put in extra effort to ensure each idea and concept settles in my head. So, I made a chart &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftphun1leasvg89nvu86w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftphun1leasvg89nvu86w.png" alt="JavaScript Iteration protocols chart" width="800" height="597"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;let me explain what each component in the chart is about. In case you need to learn more, I will leave a link at the end of this article.&lt;/p&gt;

&lt;p&gt;For beginners' sake, protocols are sets of rules to follow to achieve something. So in this case, &lt;code&gt;Iteration protocols&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Iteration Protocols
&lt;/h2&gt;

&lt;p&gt;In JavaScript, the &lt;code&gt;Iteration protocols&lt;/code&gt; consist of &lt;code&gt;iterable protocol&lt;/code&gt; and &lt;code&gt;iterator protocol&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterable protocol
&lt;/h2&gt;

&lt;p&gt;This rule allows an object to customize how Javascript iterates over it. JavaScript will not iterate over any object that doesn't follow this protocol.&lt;/p&gt;

&lt;p&gt;The rule states that,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In order to be iterable, an object must implement a zero-argument method(function) named &lt;code&gt;@@iterator&lt;/code&gt;, meaning that the object (or its parent) must have a property with a &lt;code&gt;@@iterator&lt;/code&gt; key which is available via constant &lt;code&gt;Symbol.iterator&lt;/code&gt; -- MDN web docs&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This function must return an &lt;code&gt;iterator Object&lt;/code&gt; which is important when looping with &lt;code&gt;for...of&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterable object
&lt;/h2&gt;

&lt;p&gt;An object that obeys &lt;code&gt;iterable protocol&lt;/code&gt;. Built-in types, such as &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Map&lt;/code&gt;, and so on, are all &lt;code&gt;iterable objects&lt;/code&gt; types. Below is an example of a custom object that is an &lt;code&gt;iterable Object&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iteratableObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;](){&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="c1"&gt;// we can also use normal function instead of generator function&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;INTERESTING FACT: &lt;code&gt;Generator function&lt;/code&gt; obeys both iteration protocols&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterator protocol
&lt;/h2&gt;

&lt;p&gt;This is the rule that must be followed to turn any object into an &lt;code&gt;iterator&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The rule states that,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In order to be an &lt;code&gt;iterator&lt;/code&gt;, an object must implement a &lt;code&gt;next&lt;/code&gt; method and optionally &lt;code&gt;return&lt;/code&gt; and &lt;code&gt;throw&lt;/code&gt; methods. These methods can take zero or one argument and must return an object that implements &lt;code&gt;IteratorResult&lt;/code&gt; interface.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Iterator Object
&lt;/h2&gt;

&lt;p&gt;Any object that obeys &lt;code&gt;iterator Protocol&lt;/code&gt; i.e implement at least the &lt;code&gt;next&lt;/code&gt; method that returns &lt;code&gt;IteratorResult&lt;/code&gt; object.&lt;br&gt;
Example of Iterator object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iteratorObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  next, return and throw method
&lt;/h2&gt;

&lt;p&gt;These are &lt;code&gt;iterator Object&lt;/code&gt; methods. Of the three, only &lt;code&gt;next&lt;/code&gt; is required for an &lt;code&gt;iterator Object&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;next method&lt;/code&gt;: get called to produce &lt;code&gt;IteratorResult&lt;/code&gt; with the next value in the sequence.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;return method&lt;/code&gt;: when this method is defined, it gets called when &lt;code&gt;break&lt;/code&gt; is used in &lt;code&gt;for...of&lt;/code&gt; loop. It is used for clean-up operations like closing an open file descriptor, ending a connection to a database, and so on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;throw method&lt;/code&gt; -  Gets called when there is an exception during iteration. This function is called to inform iterator object that an error has occurred. This is also for cleanup operations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  IteratorResult Interface
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;next&lt;/code&gt;, &lt;code&gt;return&lt;/code&gt;, and &lt;code&gt;throw&lt;/code&gt; methods must return an object that implements this interface. The interface has two optional properties &lt;code&gt;value&lt;/code&gt; and &lt;code&gt;done&lt;/code&gt;. The presence or absence of one or both properties carries some meanings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;when both properties  are missing, the default  will be
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Done property
&lt;/h3&gt;

&lt;p&gt;This property stores a boolean value. when it is &lt;code&gt;true&lt;/code&gt;, it means we are done with the iteration and there are no values left to present. &lt;code&gt;false&lt;/code&gt; means the opposite.&lt;/p&gt;

&lt;h3&gt;
  
  
  Value property
&lt;/h3&gt;

&lt;p&gt;This property can hold any valid JavaScript data type. This is the value we see when we loop over an &lt;code&gt;iterable Object&lt;/code&gt; using &lt;code&gt;for...of&lt;/code&gt; loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iteratableObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;iterator&lt;/span&gt;&lt;span class="p"&gt;](){&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;iteratableObject&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;you&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value from IteratorResult.value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need more information on these components from the source, here is an &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols" rel="noopener noreferrer"&gt;iteration protocols&lt;/a&gt; article on MDN web docs.&lt;/p&gt;

&lt;p&gt;Let me know if you have any suggestions or questions in the comments.&lt;br&gt;
You can reach me on &lt;a href="https://www.linkedin.com/in/haastrupea/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt; and &lt;a href="https://twitter.com/killcodeNG" rel="noopener noreferrer"&gt;Twitter: killcodeNG&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Asynchronous Programming in JavaScript: The Basics</title>
      <dc:creator>Haastrup Elijah</dc:creator>
      <pubDate>Tue, 11 Apr 2023 09:09:52 +0000</pubDate>
      <link>https://forem.com/haastrupea/asynchronous-programming-in-javascript-the-basics-4b0</link>
      <guid>https://forem.com/haastrupea/asynchronous-programming-in-javascript-the-basics-4b0</guid>
      <description>&lt;h2&gt;
  
  
  Making Sense of Asynchronous
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Just another English word with its own meaning.
&lt;/h3&gt;

&lt;p&gt;In the most basic sense, asynchronous is used to describe anything that does not depend on time nor keep to time. Not because It is always showing up late, just that it does not have any contract with time so it is not tied to time.&lt;/p&gt;

&lt;p&gt;Let's establish a dictionary meaning of asynchronous.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;: not simultaneous or concurrent in time : not synchronous&lt;br&gt;
asynchronous sound --&amp;gt; &lt;a href="https://www.merriam-webster.com/dictionary/asynchronous" rel="noopener noreferrer"&gt;Merriam Webster dictionary&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Asynchronous programming
&lt;/h2&gt;

&lt;h2&gt;
  
  
  introduction
&lt;/h2&gt;

&lt;p&gt;The focus of asynchronous programing is to achieve non-blocking running of tasks within a program. Some programming languages allow programmers to execute any task using the non-blocking feature within a programming language.&lt;/p&gt;

&lt;h3&gt;
  
  
  Non-blocking:
&lt;/h3&gt;

&lt;p&gt;This approach allows us to run a task(usually non-time dependent) in the background without waiting for the result before moving on to the next task, unless the programmer decides that the next task(either asynchronous or synchronous) depends on the result from the current task.&lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous Operations in JavaScript
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Timer operation: using setTimeOut, setTimeInterval and setimmediate(in nodeJs)&lt;/li&gt;
&lt;li&gt;File read/write&lt;/li&gt;
&lt;li&gt;HTTP Requests(API calls with built-in functions like XMLHttpRequest and fetch)&lt;/li&gt;
&lt;li&gt;CPU-intensive operations: sorting or searching large arrays of data&lt;/li&gt;
&lt;li&gt;CSS animations and transition/transform&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Callback Function
&lt;/h2&gt;

&lt;p&gt;If you have been following, you would have thought about it: How do we know that the asynchronous task is done and the result is ready?&lt;/p&gt;

&lt;p&gt;JavaScript uses Callback function. Callback functions are regular functions that are passed as an argument to another function.&lt;/p&gt;

&lt;p&gt;It is the responsibility of a callback in JavaScript to allow us act on the result of an asynchronous operation immediately they are available.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//show result now&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// get data from api call&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="c1"&gt;// Call the callback with the fetched data&lt;/span&gt;
  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;// calling the callback when result is available&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showResult&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;// using showResult function as a callback&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Caution on the use of callbacks
&lt;/h3&gt;

&lt;p&gt;Callbacks are good if used carefully, but when it gets too much, it results in what we call "callback hell" i.e. callback inside another callback - nested callbacks&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;getUserProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//callback 1&lt;/span&gt;
    &lt;span class="nf"&gt;getUserPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userPosts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//callback 2&lt;/span&gt;
      &lt;span class="nf"&gt;getPostComments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userPosts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;postComments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//callback 3&lt;/span&gt;
        &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;postComments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//callback 4&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as you can see, it will require some level of extra effort to read this code. The code readability suffers with more nested callback.&lt;/p&gt;

&lt;p&gt;In the next article, I will explain PROMISES, in basic terms as usual, and explore how they help take away the problem of callback hell by providing an alternative way of organizing our callbacks in a less nested manner.&lt;/p&gt;

&lt;p&gt;Let me know if you have any suggestions or questions in the comments.&lt;br&gt;
You can reach me on &lt;a href="https://www.linkedin.com/in/haastrupea/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt; and &lt;a href="https://twitter.com/killcodeNG" rel="noopener noreferrer"&gt;Twitter: killcodeNG&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Understanding Hoisting in JavaScript: Different forms of Hoisting - part 2</title>
      <dc:creator>Haastrup Elijah</dc:creator>
      <pubDate>Fri, 31 Mar 2023 15:11:57 +0000</pubDate>
      <link>https://forem.com/haastrupea/understanding-hoisting-in-javascript-different-forms-of-hoisting-part-2-1lc4</link>
      <guid>https://forem.com/haastrupea/understanding-hoisting-in-javascript-different-forms-of-hoisting-part-2-1lc4</guid>
      <description>&lt;p&gt;If you are reading this, but have not read part one of this article, where we discussed the mental model for hoisting using reader's understanding of English Language, I will advise you pause here and read the &lt;a href="https://dev.to/haastrupea/understanding-hoisting-in-javascript-building-a-mental-model-part-1-45og"&gt;part one&lt;/a&gt; before you continue.&lt;/p&gt;

&lt;p&gt;Function, variable and class declaration experiences hoisting in different ways. Below are three different ways that hoisting can be experienced in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Value hoisting
&lt;/h2&gt;

&lt;p&gt;This is when the value of a declared variable is hoisted along with its declaration.&lt;br&gt;
In this form of hoisting, you can reference a variable before the line it was declared and get access to its value and declaration. &lt;br&gt;
This is only possible with function declaration, at the moment, in JavaScript and I guess this is because the value of a function is in its body(the part of the function declaration embedded inside &lt;code&gt;{}&lt;/code&gt;) and as it stands, the body of a function is part of its declaration.&lt;/p&gt;

&lt;p&gt;Sample of how function is hoisted&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; 
&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;// function call before the being declared on line 9&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;  &lt;span class="nf"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="c1"&gt;// function declaration&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
&lt;span class="mi"&gt;10&lt;/span&gt;   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my salary is $&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;12&lt;/span&gt;
&lt;span class="mi"&gt;13&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Declaration hoisting
&lt;/h2&gt;

&lt;p&gt;In this type of hoisting, only the declaration is moved up the scope. This type of hoisting affects only &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, if you write your code as seen in example 1 below, JavaScript interpreter will break it down as seen in example 2 and then move the declarations to the top of their respective scopes.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CEO&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// declaration + assignment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declaration &lt;/span&gt;
&lt;span class="nx"&gt;postion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CEO&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Assignment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a JavaScript rule that prevents referencing &lt;code&gt;let&lt;/code&gt; before the line it was declared in&lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical declaration hoisting
&lt;/h2&gt;

&lt;p&gt;Well, this type of hoisting is not really considered hoisting but after reading the article from MDN on &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" rel="noopener noreferrer"&gt;hoisting&lt;/a&gt;. I agree with the proof. I would advise you check it out, it's a short article.&lt;/p&gt;

&lt;p&gt;This type of hoisting affect variables declared with &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;class&lt;/code&gt; declaration. Code Example 3 below will throw &lt;code&gt;ReferenceError: can't access lexical declaration 'salary' before initialization&lt;/code&gt; error.&lt;/p&gt;

&lt;p&gt;In example 4 below, I removed the class declaration and got &lt;code&gt;ReferenceError: 'salary' is not defined&lt;/code&gt; error. The only difference between example 3 and example 4 is the absence of salary class declaration  in example 4 code.&lt;/p&gt;

&lt;p&gt;Example 3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; 
&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;// class reference before the being declared on line 9&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="c1"&gt;// class declaration&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;salary&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
&lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;12&lt;/span&gt;
&lt;span class="mi"&gt;13&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 4:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; 
&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;// ref undefined variable&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For JavaScript to know and throw a different error in example 3 and 4, it must have hoisted the class declaration in example 3 somehow.&lt;/p&gt;

&lt;p&gt;The rest of the proofs can be found on &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" rel="noopener noreferrer"&gt;MDN Glossary on hoisting&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let me know if you have any suggestions or questions in the comments.&lt;br&gt;
You can reach me on &lt;a href="https://www.linkedin.com/in/haastrupea/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt; and &lt;a href="https://twitter.com/killcodeNG" rel="noopener noreferrer"&gt;Twitter: killcodeNG&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding Hoisting in JavaScript: Building a mental model- part 1</title>
      <dc:creator>Haastrup Elijah</dc:creator>
      <pubDate>Fri, 31 Mar 2023 15:11:21 +0000</pubDate>
      <link>https://forem.com/haastrupea/understanding-hoisting-in-javascript-building-a-mental-model-part-1-45og</link>
      <guid>https://forem.com/haastrupea/understanding-hoisting-in-javascript-building-a-mental-model-part-1-45og</guid>
      <description>&lt;p&gt;Hoisting is an idea in JavaScript that the ECMAScript specification never really did justice to clarifying and yet holds a very important secret to demystifying JavaScript.&lt;/p&gt;

&lt;p&gt;Since the idea is not clearly defined, I will take the liberty to help you as a beginner build a mental model from relatively familiar concept to the closest idea to the unfamiliar behavior of Hoisting in JavaScript and in the end I will leave link to an article( more like an academic paper) that I believe went really deeper than most articles I have read over the years in a beginner friendly tone.&lt;/p&gt;

&lt;p&gt;If you are reading this In English language I will assume you have basic understanding of the language enough to pick up a new word from the English dictionary. I believe English Language is relatively familiar language to you and I will be building on that understanding.&lt;/p&gt;

&lt;p&gt;To &lt;a href="https://www.merriam-webster.com/dictionary/hoist#h1" rel="noopener noreferrer"&gt;Hoist&lt;/a&gt; according to Merriam Webster dictionary is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;:raise, lift&lt;br&gt;
to raise into position by or as if by means of &lt;a href="https://www.merriam-webster.com/dictionary/tackle#h1" rel="noopener noreferrer"&gt;tackle&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hoist a flag&lt;/li&gt;
&lt;li&gt;hoist the sails&lt;/li&gt;
&lt;li&gt;Cargo was hoisted up into the ship. &lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simple terms, hoisting is moving something from a lower position to a higher position. &lt;br&gt;
Check out the following analogy&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A raise on your salary at work, we can say your Salary has been hoisted up.&lt;/li&gt;
&lt;li&gt;Promotion at work from a junior to senior position means your position has just been hoisted up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now is the time to apply this concept of hoisting in a way that clarifies what hoisting is in JavaScript.&lt;/p&gt;

&lt;p&gt;From MDN hoisting is defined as&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code - &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" rel="noopener noreferrer"&gt;MDN hoisting&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From our last analogy, let's align some of the terms to MDN important terms&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Work/company/startup ----- scope&lt;/li&gt;
&lt;li&gt;Salary/Position ---- functions, variables and classes&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Scope in JavaScript
&lt;/h2&gt;
&lt;h3&gt;
  
  
  What does JavaScript consider as scope?
&lt;/h3&gt;

&lt;p&gt;There are two types of scope in JavaScript; local and global scope&lt;br&gt;
Here is a cheat— anything inside &lt;code&gt;{}&lt;/code&gt; is local scope and every other thing outside of &lt;code&gt;{}&lt;/code&gt; is global scope.&lt;/p&gt;

&lt;p&gt;You can also refer to anything inside &lt;code&gt;{}&lt;/code&gt; as block scope.&lt;/p&gt;

&lt;p&gt;Any &lt;code&gt;{}&lt;/code&gt; that belongs to a function - is a type of local scope that we can call function scope, if it belongs to an if statement, I guess we can call it &lt;code&gt;if scope&lt;/code&gt;, what if it belongs to a &lt;code&gt;switch&lt;/code&gt; statement, Let's call it it &lt;code&gt;switch scope&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Functions, variables and classes in JavaScript
&lt;/h2&gt;
&lt;h3&gt;
  
  
  What is JavaScript moving up?
&lt;/h3&gt;

&lt;p&gt;There is nothing really to talk about here. If you have ever written and then run any line of code in JavaScript, you definitely know what a function, variable and class is in JavaScript.&lt;br&gt;
Just to be clear, hoisting is particular about declaration of &lt;code&gt;Functions, Variables and classes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The following are not hoisted&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function calls&lt;/li&gt;
&lt;li&gt;Variable assignments&lt;/li&gt;
&lt;li&gt;Class instances&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Hoisting Explained
&lt;/h2&gt;
&lt;h3&gt;
  
  
  How Variables and Functions are Moved to the Top of Their Scope at Runtime
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Anatomy of scope
&lt;/h4&gt;

&lt;p&gt;Whatever is being moved by JavaScript is moved to the top of the scope. Below is a visual representation of how JavaScript labels parts of your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// Top of the global scope&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; 
&lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello global world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt; 
&lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt;  &lt;span class="c1"&gt;// Top of the local/block scope&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt; 
&lt;span class="mi"&gt;11&lt;/span&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello local world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="mi"&gt;12&lt;/span&gt;
&lt;span class="mi"&gt;13&lt;/span&gt;
&lt;span class="mi"&gt;14&lt;/span&gt; &lt;span class="c1"&gt;// Bottom of the local/block scope&lt;/span&gt;
&lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;16&lt;/span&gt;
&lt;span class="mi"&gt;17&lt;/span&gt;
&lt;span class="mi"&gt;18&lt;/span&gt;
&lt;span class="mi"&gt;19&lt;/span&gt; &lt;span class="c1"&gt;// Bottom of the global scope&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  What is function, variable and class declaration in JavaScript?
&lt;/h4&gt;

&lt;p&gt;When JavaScript is ready to move a function, variable or class, it moves only the declaration to the top of the respective scope. Below is a sample of what JavaScript considers to be declaration&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function declaration
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="cm"&gt;/* do something here*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;variable declaration
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;postion&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NOTE: &lt;code&gt;const&lt;/code&gt; must be assigned at the point of declaration.&lt;/p&gt;

&lt;p&gt;JavaScript has some shortcuts that allow programmers to declare and assign a value to a variable at once.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CEO&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// declaration + assignment&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;10000.00&lt;/span&gt; &lt;span class="c1"&gt;// this is the only way to declare and assign const variable&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;🇳🇬&lt;/span&gt; &lt;span class="c1"&gt;// declaration + assignment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The long version would have been&lt;br&gt;
Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declaration &lt;/span&gt;
&lt;span class="nx"&gt;postion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CEO&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Assignment&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;10000.00&lt;/span&gt; &lt;span class="c1"&gt;// this is the only way to declare and assign const variable&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declaration&lt;/span&gt;
&lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;🇳🇬&lt;/span&gt;  &lt;span class="c1"&gt;// Assignment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Class declaration
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;salary&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="cm"&gt;/* put class methods and properties here*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We just went through a process of mental model building. If you are  a beginner, you will need some time for all of this to settle in, you might need to read over and over. I recommend that you follow along with testing your understanding, if possible, in your code editor.&lt;/p&gt;

&lt;p&gt;When you are confident enough, you can go on to read the &lt;a href="https://dev.to/haastrupea/understanding-hoisting-in-javascript-different-forms-of-hoisting-part-2-1lc4"&gt;part 2 here&lt;/a&gt;. Moreover, you can always come back to part 1 for clarification.&lt;/p&gt;

&lt;p&gt;Let me know if you have any suggestions or questions in the comments.&lt;br&gt;
You can reach me on &lt;a href="https://www.linkedin.com/in/haastrupea/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt; and &lt;a href="https://twitter.com/killcodeNG" rel="noopener noreferrer"&gt;Twitter: killcodeNG&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript code block with Switch statements</title>
      <dc:creator>Haastrup Elijah</dc:creator>
      <pubDate>Wed, 15 Feb 2023 10:41:04 +0000</pubDate>
      <link>https://forem.com/haastrupea/javascript-code-block-with-switch-statements-1m2i</link>
      <guid>https://forem.com/haastrupea/javascript-code-block-with-switch-statements-1m2i</guid>
      <description>&lt;p&gt;I thought I knew about &lt;code&gt;switch&lt;/code&gt; statement until I tried this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nc"&gt;Switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;drag&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
     &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It didn't run not because &lt;code&gt;switch&lt;/code&gt; statement is mean(😠), just to be clear, it has been my go-to when I need saving from &lt;code&gt;if else if else if&lt;/code&gt; block hell.&lt;/p&gt;

&lt;p&gt;It's just variable declaration with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;, playing a fast one on me.&lt;/p&gt;

&lt;p&gt;Well here is one unpopular fact about &lt;code&gt;let&lt;/code&gt;, you cannot redeclare a variable that is already declared with &lt;code&gt;let&lt;/code&gt; keyword within the same code block, same thing goes for &lt;code&gt;const&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Why would I want to do redeclare &lt;code&gt;let&lt;/code&gt; variable within a &lt;code&gt;switch&lt;/code&gt; statement. You see, most of the time, the logic within each &lt;code&gt;case&lt;/code&gt; in a switch statement could have similar logic with some modifications and I really should be able to redeclare variable in this situation, &lt;code&gt;var&lt;/code&gt; can help but I have issue with it, it's global/function scoped, we don't want to risk global or function scope knowing what we are cooking inside our &lt;code&gt;switch&lt;/code&gt; unless we really want them (the scopes i.e global and function) to.&lt;/p&gt;

&lt;p&gt;So, I thought, If &lt;code&gt;let&lt;/code&gt; want a block, I am just gonna give it to em,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nc"&gt;Switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
       &lt;span class="nx"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;drag&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
       &lt;span class="nx"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, I got the genie trapped! The truth is, I have always seen this pattern in codebases, but never really figured what it was meant for. Now, I do. And it's interesting to tell you that this has nothing to do with &lt;code&gt;switch&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;Yes, you 'read' me right, you see, JavaScript's code block can exist within and independent of function/class declaration, &lt;code&gt;switch&lt;/code&gt;, loop and conditional statements.&lt;/p&gt;

&lt;p&gt;The piece of code below is a legal citizen of JavaScript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;baz&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don't believe me? Go ahead and see for yourself.&lt;/p&gt;

&lt;p&gt;Using code block could also help resolve &lt;a href="https://eslint.org/docs/latest/rules/no-case-declarations" rel="noopener noreferrer"&gt;eslint no-case-delcaration&lt;/a&gt; issue.&lt;/p&gt;

&lt;p&gt;A switch statement by default is considered a single code block, &lt;code&gt;switch {}&lt;/code&gt;. Therefore, every declaration within each &lt;code&gt;case&lt;/code&gt; belong to the same &lt;code&gt;switch&lt;/code&gt; block scope and every block scoped declaration like &lt;code&gt;function&lt;/code&gt;, &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; in any of the cases declared within the switch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;eventName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;drag&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
     &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
         &lt;span class="nx"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;drag&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the &lt;code&gt;case&lt;/code&gt; where any &lt;code&gt;function&lt;/code&gt;, &lt;code&gt;class&lt;/code&gt; or variable( &lt;code&gt;let&lt;/code&gt;and &lt;code&gt;const&lt;/code&gt; only) is declared in is not executed, we have buggy code in our hands.&lt;/p&gt;

&lt;p&gt;Code like this in JavaScript is one reason you never trust JavaScript not to trip you off no matter how long you have been speaking the language.&lt;/p&gt;

&lt;p&gt;There is so much to learn in JavaScript, till next time. &lt;br&gt;
Happy Coding!&lt;/p&gt;

&lt;p&gt;Let me know if you have any suggestions or questions in the comments.&lt;br&gt;
You can reach me on &lt;a href="https://www.linkedin.com/in/haastrupea/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt; and &lt;a href="https://twitter.com/killcodeNG" rel="noopener noreferrer"&gt;Twitter: killcodeNG&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>vibecoding</category>
    </item>
  </channel>
</rss>
