<?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>DEV Community: Kawal Jain</title>
    <description>The latest articles on DEV Community by Kawal Jain (@kawaljain).</description>
    <link>https://dev.to/kawaljain</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%2F2189468%2F3683726c-d5bb-4a70-9e44-cd1d24b57597.jpeg</url>
      <title>DEV Community: Kawal Jain</title>
      <link>https://dev.to/kawaljain</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kawaljain"/>
    <language>en</language>
    <item>
      <title>Building My Own Markdown Parser: A Developer's Journey 🚀</title>
      <dc:creator>Kawal Jain</dc:creator>
      <pubDate>Sun, 20 Oct 2024 03:38:27 +0000</pubDate>
      <link>https://dev.to/kawaljain/building-my-own-markdown-parser-a-developers-journey-3b26</link>
      <guid>https://dev.to/kawaljain/building-my-own-markdown-parser-a-developers-journey-3b26</guid>
      <description>&lt;p&gt;Hello &lt;strong&gt;Readers&lt;/strong&gt;,&lt;/p&gt;

&lt;p&gt;In this post, I'm going to explain what is Markdown, how Markdown works and why it's so useful. We'll dive into its key features, common uses, and show you how to get started with it. &lt;br&gt;
Markdown has always been a go-to for developers when it comes to writing clean and readable documentation. But have you ever wondered how the text you write in markdown turns into beautifully formatted HTML? 🤔&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's explore!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  What is Markdown ?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Markdown file&lt;/strong&gt; (with a .md or .markdown extension) is a simple, lightweight format for writing structured text that can easily be converted into HTML or other formats.The core idea behind Markdown is to allow users to write text that is both easy to read in plain form and can be converted into HTML without complex code or tags.&lt;/p&gt;
&lt;h2&gt;
  
  
  Key Features of Markdown:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Plain Text-Based:&lt;/strong&gt; Markdown files are readable in any text editor and don't require special software to view.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lightweight Syntax:&lt;/strong&gt; With simple symbols like #, *, and &lt;a href=""&gt;&lt;/a&gt;, you can create headings, lists, links, and more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extensible:&lt;/strong&gt; Many platforms and tools support custom extensions, so you can add tables, footnotes, or even code blocks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Common Use Case
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Documentation&lt;/li&gt;
&lt;li&gt;Content Creation&lt;/li&gt;
&lt;li&gt;Note-Taking and Writing&lt;/li&gt;
&lt;li&gt;Technical Communication&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  A Developer's Journey 🚀
&lt;/h2&gt;

&lt;p&gt;My journey with Markdown began when I used it to document my projects on &lt;strong&gt;&lt;em&gt;&lt;a href="https://github.com/kawaljain" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;. Its simplicity and versatility amazed me, sparking my curiosity about how such a markup language could be created from scratch. &lt;br&gt;
I embarked on a fun and challenging journey - building my own Markdown parser from scratch! It was a deep dive into parsing and lexing, but also an excellent way to sharpen my understanding of text transformation and regular expressions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Key Challenges:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Handling Edge Cases:&lt;/strong&gt; Markdown is simple, but edge cases like nested lists or inline HTML can make parsing tricky.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficient Parsing:&lt;/strong&gt; Ensuring the parser handles both small and large documents efficiently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Regex Mastery:&lt;/strong&gt; Understanding and leveraging regular expressions to correctly interpret different markdown elements like headings, lists, links, and code blocks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Project Step
&lt;/h2&gt;

&lt;p&gt;Here are the steps to set up the project, which is developed in React.js. You can find an &lt;em&gt;&lt;strong&gt;&lt;a href="https://markdown.kawaljain.com/" rel="noopener noreferrer"&gt;online demo link&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt; for your reference. Additionally, be sure to check out the GitHub &lt;strong&gt;&lt;em&gt;&lt;a href="https://github.com/kawaljain/markdown-parser/blob/master/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt; file for detailed instructions and information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Clone this repository:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/kawaljain/markdown-parser.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Navigate to the project directory:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd markdown-parser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Install dependencies::&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Now run the following commands assuming you are in markdown-parser directory or simply follow this &lt;em&gt;&lt;strong&gt;&lt;a href="https://github.com/kawaljain/markdown-parser/blob/master/README.md" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The app will run on your localhost. Simply &lt;strong&gt;Ctrl + Click&lt;/strong&gt; the provided link to open it in your browser. And Congrats! You've successfully built your very own Markdown parser!&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;This project has been a truly rewarding experience! It not only deepened my understanding of Markdown but also enhanced my skills in writing parsers and working with text processing overall. I look forward to applying what I've learned to future projects!&lt;/p&gt;

&lt;h2&gt;
  
  
  Contributions &amp;amp; Feedback!
&lt;/h2&gt;

&lt;p&gt;I'm excited to announce that I'm open-sourcing this project and would love for others to contribute or share feedback on how I can make it even better. Whether you're passionate about markup languages, parsers, or just want to collaborate, your ideas and input are welcome! Stay tuned for the repo link.&lt;/p&gt;

&lt;p&gt;let's build something amazing together! 📂&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference Links
&lt;/h2&gt;

&lt;p&gt;Here are some helpful resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;React Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.markdownguide.org/" rel="noopener noreferrer"&gt;Markdown Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.markdownguide.org/basic-syntax/" rel="noopener noreferrer"&gt;Basic Markdown Syntax Guid&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://markdown.kawaljain.com/" rel="noopener noreferrer"&gt;Online Demo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/kawaljain/markdown-parser" rel="noopener noreferrer"&gt;Github Repository&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Mission Accomplished: Unraveling the Code!
&lt;/h2&gt;

&lt;p&gt;As I wrap up this journey of creating my own Markdown parser, I'm thrilled with the insights and skills I've gained along the way. This project has not only challenged me but has also deepened my appreciation for the beauty and simplicity of Markdown.&lt;br&gt;
I encourage you to dive into the code, experiment with your own features, and share your ideas. Whether you're a seasoned developer or just starting, there's always something new to learn and explore. Thank you for joining me on this adventure - let's keep pushing the boundaries of what we can create together!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks for reading&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Happy coding!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you enjoyed the article and would like to show your support, be sure to:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Follow me On &lt;strong&gt;&lt;a href="https://dev.to/kawaljain"&gt;&lt;em&gt;Dev&lt;/em&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Follow me On &lt;strong&gt;&lt;a href="https://kawaljain.medium.com/" rel="noopener noreferrer"&gt;&lt;em&gt;Medium&lt;/em&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Checkout more &lt;strong&gt;&lt;a href="https://kawaljain.com/" rel="noopener noreferrer"&gt;&lt;em&gt;Portfolio&lt;/em&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>react</category>
      <category>markdown</category>
      <category>javascript</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Javascript Interview Question Explanation -Asynchronous Behaviour</title>
      <dc:creator>Kawal Jain</dc:creator>
      <pubDate>Tue, 15 Oct 2024 12:40:42 +0000</pubDate>
      <link>https://dev.to/kawaljain/javascript-interview-question-explanation-asynchronous-behaviour-10ma</link>
      <guid>https://dev.to/kawaljain/javascript-interview-question-explanation-asynchronous-behaviour-10ma</guid>
      <description>&lt;h2&gt;
  
  
  Asynchronous Behaviour of JavaScript with setTimeout
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article, we will explore a fascinating piece of JavaScript code that demonstrates the asynchronous nature of the language, particularly how closures and the &lt;code&gt;setTimeout&lt;/code&gt; function work together. If you've ever been puzzled by why your loop outputs unexpected results, you're in the right place!&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Concept
&lt;/h2&gt;

&lt;p&gt;Before diving into the code, Let discuss few concept.&lt;/p&gt;

&lt;h3&gt;
  
  
  Asynchronous Programming: 
&lt;/h3&gt;

&lt;p&gt;JavaScript is single-threaded, meaning it can only execute one piece of code at a time. However, it can handle asynchronous operations, allowing certain tasks to run in the background while the main thread continues executing.&lt;/p&gt;

&lt;h2&gt;
  
  
  SetTimeout
&lt;/h2&gt;

&lt;p&gt;This function is used to execute a piece of code after a specified delay. It takes two parameters: a callback function and a delay in milliseconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closures
&lt;/h2&gt;

&lt;p&gt;A closure is a function that retains access to its lexical scope, even when the function is executed outside that scope. This is crucial for understanding how variables are accessed in asynchronous 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="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&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="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;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&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;Before we dive into the details, take a moment to look at this code snippet. Try to guess what the output will be based on your current understanding. This approach not only helps reinforce your JavaScript skills but also makes the explanation that follows much more meaningful&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Think about how JavaScript will process each line. Once you've made your guess, keep reading to see if you got it right!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Let break down the code step by step:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Loop Execution&lt;/strong&gt; : The loop runs five times, incrementing &lt;code&gt;i&lt;/code&gt; from 0 to 4.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;setTimeout&lt;/strong&gt;: For each value of &lt;code&gt;i&lt;/code&gt;, a &lt;code&gt;setTimeout&lt;/code&gt; is scheduled to log &lt;code&gt;i&lt;/code&gt; after &lt;code&gt;i * 1000&lt;/code&gt; milliseconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Closure&lt;/strong&gt;: By the time the &lt;code&gt;setTimeout&lt;/code&gt; callbacks execute, the loop has already completed, and &lt;code&gt;i&lt;/code&gt; has a final value of 5. Therefore, all the callbacks will log &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you might except
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;0&lt;/span&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What actually Happens
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this is not the actual output that you would see. The reason for this is a common JavaScript behavior related to the scope of the &lt;code&gt;i&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;In the provided code, the &lt;code&gt;i&lt;/code&gt; variable is declared using &lt;code&gt;var&lt;/code&gt;, which means it has function scope. When the &lt;code&gt;setTimeout()&lt;/code&gt; functions are executed, the loop has already completed, and the value of &lt;code&gt;i&lt;/code&gt; is 5. Therefore, all the &lt;code&gt;setTimeout()&lt;/code&gt; functions will log the value 5 to the console, regardless of the delay.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing the Issue
&lt;/h2&gt;

&lt;p&gt;To achieve the expected output of &lt;code&gt;0, 1, 2, 3, 4&lt;/code&gt;,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you can use an &lt;code&gt;Immediately Invoked Function Expression (IIFE)&lt;/code&gt;to create a new scope for each iteration.&lt;/li&gt;
&lt;li&gt;We can use &lt;code&gt;let&lt;/code&gt; keyword instead of &lt;code&gt;var&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Immediately Invoked Function Expression
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&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="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;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;})(&lt;/span&gt;&lt;span class="nx"&gt;i&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;Now,  each &lt;code&gt;setTimeout&lt;/code&gt; will captures the current value of &lt;code&gt;i&lt;/code&gt;, and output will be:&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;0&lt;/span&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  By Using &lt;code&gt;Let&lt;/code&gt; keyword
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;let&lt;/code&gt; keyword creates a block-scoped variable, which means that each iteration of the loop will have its own copy of the &lt;code&gt;i&lt;/code&gt; variable, and the &lt;code&gt;setTimeout()&lt;/code&gt; functions will capture the correct value of &lt;code&gt;i&lt;/code&gt; for each iteration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding how JavaScript handles asynchronous operations and closures is crucial for writing effective code. The original code snippet serves as a great example of how the &lt;code&gt;setTimeout&lt;/code&gt; function interacts with the loop variable &lt;code&gt;i&lt;/code&gt;. By using an &lt;code&gt;IIFE&lt;/code&gt;, we can ensure that each timeout logs the correct value. So, the next time you encounter a similar situation, remember the power of closures and how they can help you manage asynchronous behaviour in JavaScript&lt;/p&gt;

&lt;h2&gt;
  
  
  Mission Accomplished: Unraveling the Code!
&lt;/h2&gt;

&lt;p&gt;I hope this explanation not only clarified the code but also sparked some curiosity to explore further. JavaScript is full of surprises and powerful tools, and each piece you learn brings you closer to mastering it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks for reading&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I hope you enjoyed this breakdown! Feel free to share your thoughts, questions, or ideas for future topics in the comments.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Happy Coding&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you enjoyed the article and would like to show your support, be sure to:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Follow me&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>coding</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Javascript Interview Question Explanation- Object Properties</title>
      <dc:creator>Kawal Jain</dc:creator>
      <pubDate>Wed, 09 Oct 2024 13:47:21 +0000</pubDate>
      <link>https://dev.to/kawaljain/javascript-interview-question-explanation-object-properties-1hof</link>
      <guid>https://dev.to/kawaljain/javascript-interview-question-explanation-object-properties-1hof</guid>
      <description>&lt;p&gt;Hello Reader,&lt;/p&gt;

&lt;p&gt;I am going to explain javascript interview coding question. How javascript compiler works and what it actually produces as output.&lt;br&gt;
I'll break down each part of the output, explain why it appears this way, and connect it back to the specific lines of code responsible&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = {};
let b = { key: 'b' };
let c = { key: 'c' };

a[b] = 123;
a[c] = 456;

console.log(a[b])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we dive into the details, take a moment to look at this code snippet. Try to guess what the output will be based on your current understanding. This approach not only helps reinforce your JavaScript skills but also makes the explanation that follows much more meaningful&lt;br&gt;
"Think about how JavaScript will process each line. Once you've made your guess, keep reading to see if you got it right!"&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation of above code
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;For Line-1
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = {};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above code, creates an empty object, and assign to the variable 'a'.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For Line-2
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let b = { key: 'b' };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line creates an object with a single property key and the value 'b', and assigns it to the variable 'b'.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For Line-3
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let c = { key: 'c' };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line creates an object with a single property key and the value 'c', and assign it to the variable 'c'.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For Line-4
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a[b] = 123;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;a[b] = 123&lt;/strong&gt; This line sets a property of the object &lt;strong&gt;a&lt;/strong&gt; using the object &lt;strong&gt;b&lt;/strong&gt; as the key. In JavaScript, when an object is used as a key in another object, the object is first converted to a string using the &lt;strong&gt;toString()&lt;/strong&gt; method. In this case, the string representation of the object b is &lt;strong&gt;"[object Object]"&lt;/strong&gt; . So, the property &lt;strong&gt;"[object Object]"&lt;/strong&gt; of the object a is set to the value &lt;strong&gt;123&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For Line-5
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a[c] = 456;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similar to the Previous step, &lt;strong&gt;"c"&lt;/strong&gt; is also a object and converted to string, it becomes &lt;strong&gt;""[object Object]""&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, the line &lt;strong&gt;a[c] = 456;&lt;/strong&gt; is equivalent to &lt;strong&gt;"a[object Object]"=456&lt;/strong&gt;, which means that object has &lt;strong&gt;"[object Object]"&lt;/strong&gt; property and its value is &lt;strong&gt;456&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For Line-6
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a[b])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output is &lt;strong&gt;456&lt;/strong&gt;. when you try to access the the property &lt;strong&gt;"a[b]"&lt;/strong&gt;, javascript again convert b to string, which is &lt;strong&gt;"[object Object]"&lt;/strong&gt;. Since object has a property with the key &lt;strong&gt;"[object Object]"&lt;/strong&gt; and its value is &lt;strong&gt;456&lt;/strong&gt;. So it will print the output.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In Summary, the code demonstrate that when an object is used as a key in the another object, the object is first converted to a string representation, which is "[object Object]" by default. This means that the objects b and c are treated as the same key, and the last value assigned to that key is the one that is retrieved.&lt;/p&gt;




&lt;h2&gt;
  
  
  Mission Accomplished: Unraveling the Code!
&lt;/h2&gt;

&lt;p&gt;I hope this explanation not only clarified the code but also sparked some curiosity to explore further. JavaScript is full of surprises and powerful tools, and each piece you learn brings you closer to mastering it.&lt;br&gt;
Thanks for reading, and I hope you enjoyed this breakdown! Feel free to share your thoughts, questions, or ideas for future topics in the comments.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Happy coding!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>coding</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
