<?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: Anoop Rajoriya</title>
    <description>The latest articles on Forem by Anoop Rajoriya (@anoop-rajoriya).</description>
    <link>https://forem.com/anoop-rajoriya</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%2F3269787%2Fc166c1cd-cb7f-4d02-b28c-f5f4a52a8e41.jpg</url>
      <title>Forem: Anoop Rajoriya</title>
      <link>https://forem.com/anoop-rajoriya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anoop-rajoriya"/>
    <language>en</language>
    <item>
      <title>Spread vs Rest Operators in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Wed, 18 Mar 2026 08:15:10 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/spread-vs-rest-operators-in-javascript-3nh1</link>
      <guid>https://forem.com/anoop-rajoriya/spread-vs-rest-operators-in-javascript-3nh1</guid>
      <description>&lt;p&gt;Spread and Rest both operators are used same three dots (&lt;code&gt;...&lt;/code&gt;) But the difference is how it work? the are opposite in working to each other. Spread is expend the collections and in other hand rest collect those items. In this article we learn about both, what its syntax and how you can use it with arrya or object. Seeing some real world usecases.&lt;/p&gt;




&lt;h2&gt;
  
  
  What spread operator does
&lt;/h2&gt;

&lt;p&gt;Spread operator is used to getting out all items form existing collections like arrays or objects. Its always used right side of the assignment operator (&lt;code&gt;variable = ...something&lt;/code&gt;) and many times you seeing its use with function call (&lt;code&gt;fn(...something)&lt;/code&gt;) pass all items to function infividually.&lt;/p&gt;

&lt;p&gt;Think it like you passing entire container of items to some one and in the middle a guy picking all items one by one and giveing it to intended person.&lt;/p&gt;

&lt;h2&gt;
  
  
  What rest operator does
&lt;/h2&gt;

&lt;p&gt;Rest operator collect each individual item and wrap it into container which will passing to you. You seeing it's use with functions declaration as a parameter (&lt;code&gt;fn(...params){}&lt;/code&gt;) which collect all arguments passing to function and give a single parameter containing all those items.&lt;/p&gt;

&lt;p&gt;Think it like you adding items in cart on amazon but you recieve a single parcel containing all you items. In the middle the amazon workers wrap you items into singel box so you only recieve one box rather than multiple boxes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Differences between spread and rest
&lt;/h2&gt;

&lt;p&gt;The only differences between them is direction of the data flow. Spread operator results data as individual pieces but rest operator result a single unit contain all items.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Features&lt;/th&gt;
&lt;th&gt;Spread Operator&lt;/th&gt;
&lt;th&gt;Rest Operator&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Purpose&lt;/td&gt;
&lt;td&gt;gettin out items form container&lt;/td&gt;
&lt;td&gt;putting items within container&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Syntax&lt;/td&gt;
&lt;td&gt;with collections (&lt;code&gt;[1, ...nums, 2]&lt;/code&gt;) and function calls (&lt;code&gt;Math.max(...nums)&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;only within function declarations (&lt;code&gt;function sum(...nums){}&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Results&lt;/td&gt;
&lt;td&gt;individual items form given collection&lt;/td&gt;
&lt;td&gt;single collection contains every items&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Using spread with arrays and objects
&lt;/h2&gt;

&lt;p&gt;With help of spread operator you can join multiple arrays into one or can add other values to it. Can create copy of the objects with some external configurations like adding some properties with defaults. Using this copy is good way for manipulating objects rather than directly assignments because it copy reference but spread operator copy objects by values.&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;postiveNums&lt;/span&gt; &lt;span class="o"&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="mi"&gt;2&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="mi"&gt;4&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;negativeNums&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;anoop&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;50k&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="c1"&gt;// Merging + with 0&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;negativeNums&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="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;postiveNums&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;// [-1, -2, -3, -4, 0, 1, 2, 3, 4,]&lt;/span&gt;

&lt;span class="c1"&gt;// Copy by value&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;copy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;80k&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;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// salary: "50k"&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;copy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// salary: "80k"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Practical use cases
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Creating Copy
&lt;/h3&gt;

&lt;p&gt;You can create shalow copy with spread operator which copy values in one level depth which is safe way for data manipulation in object and arrays becuas it not mutate original one.&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;anoop&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;50k&lt;/span&gt;&lt;span class="dl"&gt;"&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;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dipesh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;25k&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;temp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// {name: "dipesh", isActive: true, salary: "25k"}&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;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// {name: "anoop", isActive: true, salary: "50k"}&lt;/span&gt;
&lt;span class="c1"&gt;// Not mutate original one&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Merging Values
&lt;/h3&gt;

&lt;p&gt;For creating new object or array with properties of another one can be done with the help of it spread operator. Also can add some other properties if you want.&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;defaultSettings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;font&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="na"&gt;notification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;userPreference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;font&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="na"&gt;notification&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;settings&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="nx"&gt;defaultSettings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;userPreference&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;settings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="cm"&gt;/*
  {
    theme: "dark",
    font: 24,
    notification: false
  }
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  With Functions
&lt;/h3&gt;

&lt;p&gt;If you don't know the how much numbers of arguments passing by user in function you can use rest Parameter to collect all those arguments with ease. It give array to you which containing all passed arguments.&lt;/p&gt;

&lt;p&gt;In function call for passing collections items individually use spread operator for that. It spread each items for you.&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="c1"&gt;// Rest Operator&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nf"&gt;sumAll&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;crr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="nx"&gt;crr&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="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="nf"&gt;sumAll&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="mi"&gt;4&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// 15&lt;/span&gt;

&lt;span class="c1"&gt;// Spread operator&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&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="mi"&gt;4&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="mi"&gt;3&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;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;nums&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;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Spread:&lt;/strong&gt; Getting out items form container&lt;br&gt;
&lt;strong&gt;Rest:&lt;/strong&gt; Putting in items from container&lt;br&gt;
&lt;strong&gt;Spread:&lt;/strong&gt; Used for data creating or passing&lt;br&gt;
&lt;strong&gt;Rest:&lt;/strong&gt; Used for captureing items&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Now you fully understand what is the main difference between rest or spread operators. With this knowledge you can combine those operators use in you workflow. It gives you flexibility to work with arrays and objects for effectivly.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>chaicod</category>
    </item>
    <item>
      <title>Template Literals in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Tue, 17 Mar 2026 10:35:47 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/template-literals-in-javascript-3pje</link>
      <guid>https://forem.com/anoop-rajoriya/template-literals-in-javascript-3pje</guid>
      <description>&lt;p&gt;This is a modern way to define string it introduce with JavaScript ES6 features. Now its become standard for dealing with complex string manipulation because of this it become foundation concept which every developer should know it. In this article we discussing what it is? how syntax look like? and what is the use case of it?&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems with traditional string concatenation
&lt;/h2&gt;

&lt;p&gt;In early developers use single (&lt;code&gt;'&lt;/code&gt;) and double (&lt;code&gt;"&lt;/code&gt;) quotes to define string. With this if they need to add variable with string they need to use (&lt;code&gt;+&lt;/code&gt;) operator to do this, called string concatenation. This approach work well but if there are many variables then it becomes massy and hard to read or manage it.&lt;/p&gt;

&lt;p&gt;But with template literals its easy to add any number of variables directly within the string, no need concatenation here. This approach is good for work with dozens of variables and also provide good readability.&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Anoop&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;

&lt;span class="c1"&gt;// old way&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="s2"&gt;Hello my name is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; and i am &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; years old&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// new way&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="s2"&gt;`Hello my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and i am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Template literal syntax
&lt;/h2&gt;

&lt;p&gt;Traditional syntax of string use single or double quotes to define string but in the template literals syntax its use backtick symbols to define it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Old Syntax:&lt;/strong&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="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;Defining strings with single quotes&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Defining strings with double quotes&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;&lt;strong&gt;New Syntax:&lt;/strong&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="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="s2"&gt;`Defining strings with backticks`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Embedding variables in strings
&lt;/h2&gt;

&lt;p&gt;In old way to embed variables developers need to use concatenation with (&lt;code&gt;${}&lt;/code&gt;) operator or built-in method. In new way no need to do this anymore, template literals use dolor symbol and curly braces to wrap any kind of variable and place it within intended place of string, this called string interpolation. It also support expressions and function call which provide more flexibility to work with data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&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="s2"&gt;`Sum of &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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;capitalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;()}${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&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="s2"&gt;`&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;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;wOrLd&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="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nf"&gt;capitalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Multi-line strings
&lt;/h2&gt;

&lt;p&gt;Working with multiline text data with older way developers use new line character () to add new line in string this method is not readable for larger data set. But with template literals it's easy as pressing enter, no need to put these characters here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&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;multiLine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`This is line 1
This is line 2
This is line 3`&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;multiLine&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use cases in modern JavaScript
&lt;/h2&gt;

&lt;p&gt;You seeing it lot's of time in multiple places during development. These are few uses i encounter during development:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic HTML Code Blocks:&lt;/strong&gt; defining html code blocks in JavaScript to use it dynamically with html.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Constructing URLs:&lt;/strong&gt; it used construct URLs with adding id's, parameters dynamically to fetch resources on apis.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Logs Messages:&lt;/strong&gt; creating meaning full messages with system information which used in logging during debugging.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Math Expressions:&lt;/strong&gt; creating math expressions explanations is very helpful because in complex logic older way become massy and hard to read.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;No need to worry about it just read this and remember its concepts not syntax because syntax is only one google search far from you. Practice it and remember what problem it solve and what it's use cases is.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>chaicode</category>
    </item>
    <item>
      <title>Array Flatten in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Mon, 16 Mar 2026 12:48:08 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/array-flatten-in-javascript-53pj</link>
      <guid>https://forem.com/anoop-rajoriya/array-flatten-in-javascript-53pj</guid>
      <description>&lt;p&gt;Array flatten is the foundation concept of programming, in javascript you will seeing it's use in lots of places like in data fetching form api points, showing data tree as list on pages. In this article we seeing what and why it is, differenct approaches and scenerios you facing in interview on that.&lt;/p&gt;




&lt;h2&gt;
  
  
  What nested arrays are
&lt;/h2&gt;

&lt;p&gt;In javascript, nested arrays are a simple array whose contain elments or another arrays those can conaint either elments or more arrays. This structure of arrays called nested arrays and also known as multidimentional arrays.&lt;/p&gt;

&lt;p&gt;You can think it like a big container box in that we place multiple elements or other boxes as elments those further contain elements in sequence.&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&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="mi"&gt;3&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="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="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="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here example nested array contain 1, then array &lt;code&gt;[2,3]&lt;/code&gt;, another elment 4, one more array &lt;code&gt;[5, [6]]&lt;/code&gt;, and lastly element 7. In that there are two nested array first contain 2, 3 and other contain 5 and single value arrya with 6.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why flattening arrays is useful
&lt;/h2&gt;

&lt;p&gt;Flattening arrays simplify the complex heirarchical structure (a structure contain more structured elments) into a simple linear array structure (a simple version contain only single element). There are several reasons describe why flattening arrays useful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simplifies Data Processing :&lt;/strong&gt; many data processing and visualizing library like &lt;code&gt;Chart.js&lt;/code&gt;, and &lt;code&gt;Plotly.js&lt;/code&gt; requried linear datasets to perform operation in a optimistic way.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Integration :&lt;/strong&gt; data fetching from api, reading form other files or databases often give nested structured data to use those data flatteing process are required.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Common Use Cases :&lt;/strong&gt; rendering cards information into dom, using flattening to create well suited formate of cards information are used.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Concept of flattening arrays
&lt;/h2&gt;

&lt;p&gt;Flattening arrays simply means is transforming nested array into an array containing only non-arrya elements. All elments are puting into arrya with concating all nested arryas elements.&lt;/p&gt;

&lt;p&gt;Think it like get out all small boxes from container which have elments, taking all those elements and putting it down in sequence into container box.&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="c1"&gt;// Nested array&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="mi"&gt;2&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="mi"&gt;4&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="mi"&gt;6&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="c1"&gt;// Flatten array&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="mi"&gt;2&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="mi"&gt;4&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="mi"&gt;6&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Different approaches to flatten arrays
&lt;/h2&gt;

&lt;p&gt;There are multiple ways exist for flatteing arrays in programming but in JavaScript we have native methods supports lets seeing these with other approaches:&lt;/p&gt;

&lt;h3&gt;
  
  
  Native &lt;code&gt;Array.prototype.flat()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;JavaScript support native array flatten method &lt;code&gt;flat()&lt;/code&gt; which flatten arrya. By default it only flatten single level nesting. You can pass argument &lt;code&gt;flat(number)&lt;/code&gt; represent nesting level which you want to flatten.&lt;/p&gt;

&lt;p&gt;Like &lt;code&gt;nested.flat(2)&lt;/code&gt; it flatten 2 level nesting, for flattening  array completely you can use &lt;code&gt;infinity&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;deeplyNested&lt;/span&gt; &lt;span class="o"&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="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="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="mi"&gt;4&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="mi"&gt;6&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;shalowFlat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;deeplyNested&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// 1 level flat&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;shalowFlat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output: [1,2,[3,4],5,6]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deepFlat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;deeplyNested&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;Infinity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// completely flat&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;deepFlat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output: [1,2,3,4,5,6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Recursive approach
&lt;/h3&gt;

&lt;p&gt;A recursive approach implemented using custom logic function. In that function call itself to break down nested arrays into single array. To do this there are two important cases need to handle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Base Case: a condition which return elment if it's not an array&lt;/li&gt;
&lt;li&gt;Recursive Case: a condition which call itself with current elment if its a array and evaluated into flatten arrya.
&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;flattenRecursively&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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;res&lt;/span&gt; &lt;span class="o"&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;let&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="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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="c1"&gt;// recursively call for element which is arrya and merge it into result&lt;/span&gt;
            &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;flattenRecursively&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// merge current not-array elment into result end&lt;/span&gt;
            &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&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;highlyNested&lt;/span&gt; &lt;span class="o"&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="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="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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&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="mi"&gt;6&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="nf"&gt;flattenRecursively&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;highlyNested&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: [1, 2, 3, 4, 5, 6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Iterative approach
&lt;/h2&gt;

&lt;p&gt;Instead of relies on recursion implicit stack, iterative approach use explicit stack or queue data structure to keep track of array flattening iteration.&lt;/p&gt;

&lt;p&gt;In that two array used one for original array copy and other for result. In each iteration, for element shift it form copy and push into result but in case of array shift it form copy and un-wrape all elements and unshift back into copy so in next iteration it will moved into result.&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;flattenIteratively&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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;result&lt;/span&gt; &lt;span class="o"&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;copy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// copy original so we don't modify orignal&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;firstItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;copy&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstItem&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
            &lt;span class="c1"&gt;// if is array shift it, spread it and unshift it&lt;/span&gt;
            &lt;span class="nx"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nx"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;firstItem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// if element shift it from copy and push into result&lt;/span&gt;
            &lt;span class="nx"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&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="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstItem&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="k"&gt;return&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;const&lt;/span&gt; &lt;span class="nx"&gt;heavilyNested&lt;/span&gt; &lt;span class="o"&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="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="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="mi"&gt;4&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="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="nf"&gt;flattenIteratively&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;heavilyNested&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Output: [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Common interview scenarios
&lt;/h2&gt;

&lt;p&gt;Prepare about the following points because demonstrating only knowledge of how implementation and working is not enough, you should know why these are and what are the trade-offs for each one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation without native approaches
&lt;/h2&gt;

&lt;p&gt;Most of the cases interviewer forbid the use of &lt;code&gt;array.prototype.flat()&lt;/code&gt;, ask for the implement this becuase thay want to test you thinking ability how you tackle problem and what approach should you prefer.&lt;/p&gt;

&lt;p&gt;Here in this scenerio you should need to give the both flatten logic implementation along with its comparison and trade-offs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Recursive approach with its stack overflow scenerios.&lt;/li&gt;
&lt;li&gt;Iterative approach and efficiency of shift-unshift.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Performance optimizetion trade-offs
&lt;/h3&gt;

&lt;p&gt;Clearly explain each approach optimizetion trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Native &lt;code&gt;array.prototype.flat()&lt;/code&gt;: explain why its optimized for other approaches.&lt;/li&gt;
&lt;li&gt;Recursive approach: clearly dicuss its call overhead probelm in heavely deep and large arrays.&lt;/li&gt;
&lt;li&gt;Iterative approach: describe how its use of explicit stack solve recursive stack overflow problem but its unshift-shift reduce efficiency.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Clearly explian shalow vs deep flat
&lt;/h3&gt;

&lt;p&gt;Explain difference of shalow and deep flat, which approach support deep and shalow flat.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Native &lt;code&gt;flat()&lt;/code&gt;: demonstrate its defalut behaviour and passing level arguments with &lt;code&gt;infinity&lt;/code&gt; for completely flatten array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Recursive approach: proper implementation support deep flatten but not efficient for larger array input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Iterative approach: also support deep flatten but have efficiancy problem via use of shift-unshift.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Practising all approaches only not enough for taking control of interview, proper mastering need to build a mental understading. Understading how logic work and tracing on paper for different examples help to build solid understading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>chaicode</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Arrays 101</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Wed, 11 Mar 2026 17:18:39 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/javascript-arrays-101-3a51</link>
      <guid>https://forem.com/anoop-rajoriya/javascript-arrays-101-3a51</guid>
      <description>&lt;p&gt;Today i reviste JS with arryas late night. Creating dozens of variable to store every new items feel cluttered. If you feel this pain point. Than this guide help you to fix it. &lt;/p&gt;

&lt;h2&gt;
  
  
  What arrays are and why we need them
&lt;/h2&gt;

&lt;p&gt;Think you are building a shoping list. Without arryas you're creating fresh varialbe each time when item added.&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;item1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;milk&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;item2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bread&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;item3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ots&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there are 100 of items were adding, you need to create dozens of variables for that. With this approach deleting one of them like thired one or managing feel impossible.&lt;/p&gt;

&lt;p&gt;**So the solution is array. Arrya is a data type in javascript, which allow to store collection of data items within a single variable and you can access these item by using indexes. You can think it like a single box with multiple compartments and each have a index to identify each compartment.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create an array
&lt;/h2&gt;

&lt;p&gt;In JavaScript is vary simple to create arrays. Use &lt;code&gt;[]&lt;/code&gt; square bracket to wrap  &lt;code&gt;,&lt;/code&gt; comma separated items and assign them to any variable.&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="c1"&gt;// A collection of employees name&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;employees&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;anoop&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pankaj&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nikhil&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;Instead of creating three separate varibales there only one varible used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing elements using index
&lt;/h2&gt;

&lt;p&gt;Items in arryas are easily access by index. In JavaScript indexing start from 0 means first element stored at 0, second at 1 and thired at 2. Every items follow this indexing from 0 to "number of items - 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="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;employees&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="c1"&gt;// output: anoop&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;employees&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="c1"&gt;// output: nikhil&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To access the items use intentional item index and used this within &lt;code&gt;[]&lt;/code&gt; square bracket followed by variable name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating elements
&lt;/h2&gt;

&lt;p&gt;In javascript arryas are mutable means if you're figuring you add something wrong by mistakes. Dont worry about it you can simply update its value by using it's index like how you're accessing it, here you just assigning newer value instead of getting value form it.&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="c1"&gt;// nikhil is not employee any more&lt;/span&gt;
&lt;span class="c1"&gt;// kartik take place of him&lt;/span&gt;
&lt;span class="nx"&gt;employees&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="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;kartik&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Array length property
&lt;/h2&gt;

&lt;p&gt;If you ever need to know how many items in you array. You don't need to count it manually, JavaScript do it for you. Every time you add item in arrya JS track its count and add it into &lt;code&gt;.length&lt;/code&gt; property which every arrya have. So you can use it to access how many items are exist in you arrya.&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="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;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lenths&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Pro Tip: You can access last elment directly by using length - 1. Which return last valid index.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Basic looping over arrays
&lt;/h2&gt;

&lt;p&gt;Arrays are iterable means you can iterate over it by using loops. Think if you're array have 1000 or more items, accessing or updating them manully take soo much time. Its like wast of time. So you can use &lt;code&gt;for&lt;/code&gt; loop to count index from 0 to array last index (length - 1) and by using these indexes can easily access or do what ever you want with that.&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;let&lt;/span&gt; &lt;span class="nx"&gt;index&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;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&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="s2"&gt;`Employee &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="s2"&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;In that loop &lt;code&gt;let index = 0;&lt;/code&gt; initialize starting point where loop start counting. &lt;code&gt;index &amp;lt; employees.length;&lt;/code&gt; this is the condition where stop loop if evaluated into &lt;code&gt;false&lt;/code&gt;. The last increamenting &lt;code&gt;index++&lt;/code&gt; which increament index in every step of loop. These whole work together so employees printing code print next name in each iteration.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arrya:&lt;/strong&gt; collection of items within single varialbe.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Creating:&lt;/strong&gt; &lt;code&gt;const name = [1,2,3]&lt;/code&gt;, use &lt;code&gt;[]&lt;/code&gt; bracket.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accessing:&lt;/strong&gt; &lt;code&gt;name[1]&lt;/code&gt;, use variable + [index]. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Updating:&lt;/strong&gt; &lt;code&gt;name[0] = 4&lt;/code&gt;, just use &lt;code&gt;=&lt;/code&gt; assignment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterating:&lt;/strong&gt; use &lt;code&gt;for&lt;/code&gt; loop with increamenting index.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Your Turn: The Movie Challenge
&lt;/h2&gt;

&lt;p&gt;Try it your self. Here is the quick assignment to test how much you grab through this guide:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;favMovies&lt;/code&gt; variable with 5 movies names.&lt;/li&gt;
&lt;li&gt;Print the first and the last movie name on the console.&lt;/li&gt;
&lt;li&gt;Update the thired movie name to something else in you list.&lt;/li&gt;
&lt;li&gt;Print length of you movies arrya list on console.&lt;/li&gt;
&lt;li&gt;Loop through it and print each movie name with its index.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Give it a Shot! Mastering Arrays data type in JavaScript unlock the most important tool in programming tool-kit.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>chaicode2026</category>
    </item>
    <item>
      <title>Arrow Functions in JavaScript: A Simpler Way to Write Functions</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Tue, 10 Mar 2026 15:25:31 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/arrow-functions-in-javascript-a-simpler-way-to-write-functions-473</link>
      <guid>https://forem.com/anoop-rajoriya/arrow-functions-in-javascript-a-simpler-way-to-write-functions-473</guid>
      <description>&lt;p&gt;In this article we will discuss about different way of declaring function, how you use and declare them? and the major difference between them. By the end of the article you will understand more about the Regular and Arrow function in JavaScript.&lt;/p&gt;




&lt;h2&gt;
  
  
  What arrow functions are
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Arrow function is the consice way of writing function in javascript. It give ability so you can skip the boilerplate code which you write every time in function declaration before ES6.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Arrow function support more features like lexical &lt;code&gt;this&lt;/code&gt; binding, implicit return, more predictability than normal function.&lt;/p&gt;

&lt;p&gt;Every normal function declaration required boilderplate setup code like &lt;code&gt;function&lt;/code&gt; keyword, &lt;code&gt;{}&lt;/code&gt; curly braces, and &lt;code&gt;return&lt;/code&gt; keyword.&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;greet&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello! Good Morning &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But with arrow function it become optional it can be skipped. It only needed in some cases.&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;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;`Hello! Good Morning &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Basic arrow function syntax
&lt;/h2&gt;

&lt;p&gt;Any basic arrow function requried three things for declaration parameters for accepting arguments, fat arrow symbol &lt;code&gt;=&amp;gt;&lt;/code&gt;, and function body.&lt;/p&gt;

&lt;p&gt;It always created as anonymously (not contain any name) and assigned to a variable to create reference so can be used later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Normal Function Syntax:&lt;/strong&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="cm"&gt;/* Basic setup required
function name (parameters){
    body
    return value
}
*/&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Arrow Function Syntax:&lt;/strong&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;squre&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Arrow functions with one parameter
&lt;/h2&gt;

&lt;p&gt;As we shown above examples if your arrow function only have one parameter than you can skip &lt;code&gt;()&lt;/code&gt; parentheses and direct write name of the parameter.&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;squre&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Arrow functions with multiple parameters
&lt;/h2&gt;

&lt;p&gt;In case of multiple parameters you need to wrape you arguments in &lt;code&gt;()&lt;/code&gt; parentheses else first parameters are skipped.&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="cm"&gt;/* Wrong syntax: a will skipped
const sum = a,b =&amp;gt; a+b
*/&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Implicit return vs explicit return
&lt;/h2&gt;

&lt;p&gt;Returning value form normal function declaration required to use of &lt;code&gt;return&lt;/code&gt; keyword, but in case of arrow function you can skip it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implicit Return
&lt;/h3&gt;

&lt;p&gt;When you funciton body can fit in one line or it can be a expression than you can skip &lt;code&gt;{}&lt;/code&gt; curly braces and &lt;code&gt;return&lt;/code&gt; keyword in arrow function.&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="c1"&gt;// a+b --&amp;gt; expression&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case of returning object literals you need to wrap it by &lt;code&gt;()&lt;/code&gt; parentheses to aboud JS interpeting object braces as function body.&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explicit Return
&lt;/h3&gt;

&lt;p&gt;If you function contain more than one line of code than you need to wrap it in &lt;code&gt;{}&lt;/code&gt; curly bracess and use &lt;code&gt;return&lt;/code&gt; statement for returning value.&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;checkOdd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&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="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Odd&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Even&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;h2&gt;
  
  
  Basic difference between arrow function and normal function
&lt;/h2&gt;

&lt;p&gt;Both are used used to abstract code block and perform task in same way but still both have some key difference:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Concise way to write code
&lt;/h3&gt;

&lt;p&gt;We discuss it already discuss about that, normal function use &lt;code&gt;function&lt;/code&gt; keyword and the arrow function use shorthand way to reduce this boiler plate 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="c1"&gt;// normal function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Concise way to write it&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;code&gt;this&lt;/code&gt; keyword behaviour
&lt;/h3&gt;

&lt;p&gt;In normal function &lt;code&gt;this&lt;/code&gt; is dynamically bound to the function (means it have its own &lt;code&gt;this&lt;/code&gt; object) but in arrow function it inherit &lt;code&gt;this&lt;/code&gt; from its lexical scope (code surrounding it).&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="c1"&gt;// Timeout with normal function print undefined becuase its detached during execution and have its own this, which not have name&lt;/span&gt;

&lt;span class="c1"&gt;// But Arrow function print "Arrow Timeout" because it inherit code surrounded them (lexical inheriting)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Arrow Timeout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;sayName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;function &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="nf"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;function(){}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&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="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;()=&amp;gt;{}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&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="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;obj3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sayName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// function(){}: undefined&lt;/span&gt;
&lt;span class="c1"&gt;// ()=&amp;gt;{}: Arrow Timeout&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. &lt;code&gt;arguments&lt;/code&gt; object
&lt;/h3&gt;

&lt;p&gt;Regular function have &lt;code&gt;arguments&lt;/code&gt; object which os a iterable list object which holds all the arguments passed to it and you can access it within.&lt;/p&gt;

&lt;p&gt;But in arrow function you don't have access &lt;code&gt;arguments&lt;/code&gt; object because it not have that But we can use rest paramets to acheiving it.&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;showArgs&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;arguments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;showArgs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: { '0': 'a', '1': 'b', '2': 'c' }&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;showArgsArrow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ReferenceError: arguments is not defined&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;arguments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nf"&gt;showArgsArrow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&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;Both have those tradoffs some times you need regular function and some types you will need arrow function. Understanding both and the differece: syntax, this binding, arguments object and usecase will makes you to predict code flow required in today world of development. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicode2026</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Function Declaration vs Function Expression: What’s the Difference?</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Mon, 02 Mar 2026 18:21:29 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/function-declaration-vs-function-expression-whats-the-difference-kjf</link>
      <guid>https://forem.com/anoop-rajoriya/function-declaration-vs-function-expression-whats-the-difference-kjf</guid>
      <description>&lt;h2&gt;
  
  
  Topics To Cover
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What functions are and why we need them&lt;/li&gt;
&lt;li&gt;Function declaration syntax&lt;/li&gt;
&lt;li&gt;Function expression syntax&lt;/li&gt;
&lt;li&gt;Key differences between declaration and expression&lt;/li&gt;
&lt;li&gt;Basic idea of hoisting (very high level)&lt;/li&gt;
&lt;li&gt;When to use each type&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What functions are and why we need them
&lt;/h2&gt;

&lt;p&gt;A function in programming is the self-contained, named block of code it used to perform a specific task. Its like a sub-programm or black-box which take some input, process it and return a output.&lt;/p&gt;

&lt;p&gt;Think it like a vending machine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Input (arguments): it take some mony the input and punch button start running.&lt;/li&gt;
&lt;li&gt;Body (function): its internal machanics of the vending machine. it identify is stock available and which item need to drop, calculate changes mony.&lt;/li&gt;
&lt;li&gt;Output (return value): machine drop item and changes in the slot.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why we need it? You dont need to knwo how to build the vending machine every time when you need snacks, its electronics, complex coin slote mechanics. You only need to know how to use its interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function declaration syntax
&lt;/h2&gt;

&lt;p&gt;The standard JavaScript function declaration defined by the &lt;code&gt;function&lt;/code&gt; keyword followed by name, with an optional parameters. Its body contain statements which executed when function will call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax :&lt;/strong&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;function&lt;/span&gt; &lt;span class="nf"&gt;functionName&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parameter1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;parameter2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...){&lt;/span&gt;
    &lt;span class="c1"&gt;// code to be executed&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// optional return statement&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;
&lt;code&gt;function&lt;/code&gt; keyword: A required keyword, used to tell you are defining funciton.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;functionName&lt;/code&gt; name: Function idetifire its a name, it use rules same as variable declaration (uses camel case). It used to called function later for using it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(...)&lt;/code&gt; parantheses: It enclosed optional, parameters a local variables used to hold arguments a input vlaues provided during calling.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{...}&lt;/code&gt; curlybraceses: It enclosed bunch of statements which executed when function call. It has a access of there parameters.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;return&lt;/code&gt; optional statement: it used to return some values at places where function is called.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example :&lt;/strong&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="c1"&gt;// Funciton declaration&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;calculateSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Invoking (calling) the function&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="nf"&gt;calculateSum&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="mi"&gt;5&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;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output: 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the given example i define a function named &lt;code&gt;calculateSum&lt;/code&gt;, its job is to take two arguments in &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; parameters. These parameters used to calculate sum by using addition operator &lt;code&gt;+&lt;/code&gt;. After that function return the calcualted sum value of given arguments.&lt;/p&gt;

&lt;p&gt;Bellow that declaration i defined a constant variable named &lt;code&gt;result&lt;/code&gt; which hold the ouput of &lt;code&gt;calculateSum&lt;/code&gt; function. I use assignment operator &lt;code&gt;=&lt;/code&gt; to hold the returned value.&lt;/p&gt;

&lt;p&gt;Function calling syntax is, you can used its named with &lt;code&gt;()&lt;/code&gt; parantheses which indicating that your calling this function mean executing there body. In the given example i put tow arguments &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;5&lt;/code&gt; these will holded by the function parameters &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;. After the execution finished the final valued returned by function.&lt;/p&gt;

&lt;p&gt;Finally useing &lt;code&gt;console.log&lt;/code&gt; to log the &lt;code&gt;result&lt;/code&gt; on the console which print vlaue &lt;code&gt;7&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function expression syntax
&lt;/h2&gt;

&lt;p&gt;Function declaraing a part of larger expression, typicaly assigning it into a variable. It only execute when code reaches to execution. Unline the funciton declaration, fuction expression can be unonymous (without name).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax :&lt;/strong&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;greet&lt;/span&gt; &lt;span class="o"&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;name&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello $(name)`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// The function is invoked using variable name&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="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Anthony&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// output: Hello Anthony&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most common syntax involve assigning a anonymous function to a variable using &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, and &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It can also be named for debugging purpose or recursion, though the it only accessible within the its own scope.&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;factorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;recursion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;recursion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&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;fact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factorial&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="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;fact&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output: 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key differences between declaration and expression
&lt;/h2&gt;

&lt;p&gt;The key difference is, function expression is evaluated into a value but funciton declaration is a statement which do action not evaluated into values. The most practicle difference is how they are handle hoisting and there use in certain context.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Evaluat to value: statements cannot be evaluated into values, its ony perform actions. The expression can evaluats to a single value like number, string or function object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hoisting: Declaration is hoisted on the top of their scope allowing them to invok before they apear in code. The expressions are not hoisted in same way. The variable holding expression may hoisted depending on keyword used for variable declaration, but the assignment happen when the code execution reach that line.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Naming: Function declaration must have name but function expression can be anonymous.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Basic idea of hoisting (very high level)
&lt;/h2&gt;

&lt;p&gt;Hoisting means at very high level, you can use variable or function before you formaly declare them in code.&lt;/p&gt;

&lt;p&gt;Its a mechanism in javascript in there function and variable declaration conceptually move to top of their containing scope during the compilation phase before the code actually executes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Declaration
&lt;/h3&gt;

&lt;p&gt;It is fully hoisted means the name and the body of the function is moved to top of the scope. It allow you to call it before it defined.&lt;/p&gt;

&lt;p&gt;The javascript engine read its name and its body at the compilation time, that's why its 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="c1"&gt;// Works!&lt;/span&gt;
&lt;span class="nf"&gt;sayHello&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;sayHello&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;It is the function stored into the variable, it's not hoisted in the same way. Only the variable declaration is hoisted but the assignment stays in place.&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="c1"&gt;// Throws TypeError: greet is not a function&lt;/span&gt;
&lt;span class="nf"&gt;greet&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;greet&lt;/span&gt; &lt;span class="o"&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;greet&lt;/code&gt; is declared and assigned &lt;code&gt;undefined&lt;/code&gt;. When you called it, you'r trying to call &lt;code&gt;undefined()&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: let and const are not hoisted so you can not access it before it's declaration. If you trying to do it you got reference errors.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  When to use each type
&lt;/h2&gt;

&lt;p&gt;Choise of using funciton declaration or expression is depends on wheter you need hoisting, how funciton is used, and the desired scope and code style.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Funciton Declaration when
&lt;/h3&gt;

&lt;p&gt;Its a standalone statement use it when you need to access it from anywhere in you code. It hoisted so you can call it before its declaration apears. The keyword &lt;code&gt;function&lt;/code&gt; at the start makes it easy to spot, improving code structure for top level funcitons. The funciton name is always available withitn its own scope, useful for recurion function.&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="c1"&gt;// This is work because "add" is hoisted to the level&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="nf"&gt;add&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="mi"&gt;8&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use Funciton Expression when
&lt;/h3&gt;

&lt;p&gt;It create a funciton as a value which is than assigned to variable. In callback -- use it to pass as argument inside other funcitons. When you need to invok funciton immediatly and want to prevent variable polution of global scope. Used in conditional function definition -- defining function logic based on certain condition. Since they are not hoisted so it only exist when interpreter reaches it, which can lead more predictable or structured code flow.&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="c1"&gt;// This will cause error if call before expression&lt;/span&gt;
&lt;span class="c1"&gt;// console.log(multiply(2,3)) // ReferenceError: cannot access 'multiply' before initialization&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;multiply&lt;/span&gt; &lt;span class="o"&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;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Must be called after definition&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="nf"&gt;multiply&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// output: 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>function</category>
      <category>chaicode2026</category>
    </item>
    <item>
      <title>Control Flow in JavaScript: If, Else, and Switch Explained</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Fri, 27 Feb 2026 18:00:47 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/control-flow-in-javascript-if-else-and-switch-explained-1f5g</link>
      <guid>https://forem.com/anoop-rajoriya/control-flow-in-javascript-if-else-and-switch-explained-1f5g</guid>
      <description>&lt;p&gt;In JavaScript If, Else, and Switch are conditional statements. Those are a piece of code executed based on certain conditions if evaluated into &lt;code&gt;true&lt;/code&gt;. Like JavaScript every programming languages has those statements, those are the fundamentals of the programming.&lt;/p&gt;

&lt;p&gt;In this guide we will discuss about what are those? why you need it? and how you can use them to improve you programming skills.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What control flow means in programming&lt;/li&gt;
&lt;li&gt;The if statement&lt;/li&gt;
&lt;li&gt;The if-else statement&lt;/li&gt;
&lt;li&gt;The else if ladder&lt;/li&gt;
&lt;li&gt;The switch statement&lt;/li&gt;
&lt;li&gt;When to use switch vs if-else&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What control flow means in programming
&lt;/h2&gt;

&lt;p&gt;In JavaScript code execute line-by-line. When code load into JS environment, the interpreter read it from top line to the bottom line and executre it. The order of reading code by JavaScript interpreter in sequence called control flow.&lt;/p&gt;

&lt;p&gt;look at the bellow image:&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%2F0dmre5y9lfed15t2v237.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%2F0dmre5y9lfed15t2v237.png" alt="Control Flow Visualization" width="800" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code visualization you see the 15 lines of code. When it load in Js environment, Interpreter read and execute it form line 1 to 16. During the execution iterpreter see the conditional statements at line 9 &lt;code&gt;if(user.isLoggedIn)&lt;/code&gt; and 11 &lt;code&gt;else&lt;/code&gt;. It allow program to either be executed or skipped line 10 &lt;code&gt;console.log("Jump to profile screen")&lt;/code&gt; or 12 &lt;code&gt;console.log("Jumpt to login screen")&lt;/code&gt;. in our case line 10 statement will executed and statement at line 12 will skipped.&lt;/p&gt;

&lt;h2&gt;
  
  
  The if statement
&lt;/h2&gt;

&lt;p&gt;If statements use to define a control flow statement.&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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// statement&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It use a condition and a statement, if condition evaluated into true the statement code will executed other wise it will skiped. Lets see example:&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;100&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;valid age: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&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;In above code example if the given condition evaluated into true than code statement will execute otherwise its skipped.&lt;/p&gt;

&lt;h2&gt;
  
  
  The if-else statement
&lt;/h2&gt;

&lt;p&gt;The else clause used to execute statement when condition evaluated to &lt;code&gt;false&lt;/code&gt;.&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%2Fqewmilusn4mv1b2xz7q4.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%2Fqewmilusn4mv1b2xz7q4.png" alt="If and if-else statement visualization" width="800" height="412"&gt;&lt;/a&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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// Statement A&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Statement B&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It always need to used with &lt;code&gt;if&lt;/code&gt; clause other wise interpreter throws error. If your statments is inline than you can skip the curly braces &lt;code&gt;{}&lt;/code&gt;. Lets see Example:&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;100&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Age is valid: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Age is invalid: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&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;In the given code if the condition evaluated into &lt;code&gt;true&lt;/code&gt; than statement A will executed other wise statement B.&lt;/p&gt;

&lt;h2&gt;
  
  
  The else if ladder
&lt;/h2&gt;

&lt;p&gt;Using else if ladder allow to use multiple condition to executed as many statements as we want. You can use &lt;code&gt;else if&lt;/code&gt; statement to do this.&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%2F9t8hst3vtsplarvri7cg.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%2F9t8hst3vtsplarvri7cg.png" alt="Else-if ladder visualization" width="800" height="430"&gt;&lt;/a&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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// Statement A&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// Statement B&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Statement C&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The flow is go throw every statement, first check for condition A if its evaluted to false then move to next condition B and so on untile reach to last condition or else statement if exist. Lets see Example:&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="c1"&gt;// Vote System&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You can vote, your eligible: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;18&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You can not vote, your not eligible: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please give valid age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&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;In the given code if the age is greater than or equal to 18 and lesser or equal to 100 than it's valid age for statement A, in second condition if age is greather than 0 and lesser to 18 than it's valid for staement B, if both condition is failed than last else caluse handle it. In our case the first condition setisfy it so you see the statement A will executed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do It Your Self: Create a grade system in there check score if score is greather than 80 or lesser or equal to 100 than give a grade A+. If score is greather than 50 or lesser or equal to 80 than give a grade A. If score is greather than 35 or lesser or equal to 50 than give grade B and handle the fail condition using else with grade F.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The switch statement
&lt;/h2&gt;

&lt;p&gt;If you check a multiple conditions based on single value so instead using if-else statements you can use switch-case statements. Which are more readable and optimized for these types of control flow.&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%2F2ataeibw229amxzey40i.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%2F2ataeibw229amxzey40i.png" alt="Switch-case statement visualization" width="800" height="430"&gt;&lt;/a&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="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expression&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;lable&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nx"&gt;statement&lt;/span&gt; &lt;span class="nx"&gt;A&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="nx"&gt;lable&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nx"&gt;statement&lt;/span&gt; &lt;span class="nx"&gt;B&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="nx"&gt;lable&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nx"&gt;statement&lt;/span&gt; &lt;span class="nx"&gt;C&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nx"&gt;statement&lt;/span&gt; &lt;span class="nx"&gt;D&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here a single expression is evaluated and matched to each label in the flow if any one is matched than corresponding statement will executed and control flow transfered to outside the switch statement by break keyword. If non of them are matched than default statement are executed if exist.&lt;/p&gt;

&lt;p&gt;The break keyword is optional but it mostly used with it. It normally stop to matching remaing labels when one them had matched. If not using it the control flow is not transfered untile all labels are matched.&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;dayNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;getDay&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;dayName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dayNumber&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="k"&gt;case&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;dayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sunday&lt;/span&gt;&lt;span class="dl"&gt;"&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;dayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Monday&lt;/span&gt;&lt;span class="dl"&gt;"&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;dayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tuesday&lt;/span&gt;&lt;span class="dl"&gt;"&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;dayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wednesday&lt;/span&gt;&lt;span class="dl"&gt;"&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;dayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Thursday&lt;/span&gt;&lt;span class="dl"&gt;"&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;dayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Friday&lt;/span&gt;&lt;span class="dl"&gt;"&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="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;dayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Saturday&lt;/span&gt;&lt;span class="dl"&gt;"&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="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;dayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Unknown Day&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="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="s2"&gt;Today is: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dayName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, day number is matched to every day in the week one-by-one using switch-case stament. if one of them matched. The control flow transfered to the last console which print today day. If non of them are matched than default value is used.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do It Your Self: Try to replicate this example by own self. Run it in you JavaScript environment and test will it give the correct output or not.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  When to use switch vs if-else
&lt;/h2&gt;

&lt;p&gt;There are few consideration for using switch-case and if-else statements to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use if-else statements when you are handling complex logical conditions. But if you are only need to checking fixed value like numbers, and strings and matching it to specific case value than using switch-case is batter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using if-else with multiple statements and nesting them each other making it hard to read. Its easier to read switch-case labaled statements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Its rarely noticable with small code base. Using if-else statements making code slower as compare to switch-case. Because switch-case use some type of jump table which help it to jumpt to specific label and than evaluate each condition sequenctially.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Using switch-case and if-else statement claverly make you a good programmer. If you following all practise than it will make you understanding good on these conditional statements. If you need to read official or more docs you can try bellow resources which i read to write this guid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling" rel="noopener noreferrer"&gt;MDN JavaScript Conditional Statements&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/if-else-vs-switch-case-in-javascript/" rel="noopener noreferrer"&gt;Difference Between &lt;code&gt;if-else&lt;/code&gt; and &lt;code&gt;switch-case&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>controlflow</category>
      <category>beginners</category>
      <category>chaicodewebdev2026</category>
    </item>
    <item>
      <title>JavaScript Operators: The Basics You Need to Know</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Thu, 26 Feb 2026 19:13:01 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/javascript-operators-the-basics-you-need-to-know-4jfk</link>
      <guid>https://forem.com/anoop-rajoriya/javascript-operators-the-basics-you-need-to-know-4jfk</guid>
      <description>&lt;p&gt;If you are learning JavaScript, operator is neccessary for you because they help you to perform mathmatical or logical operations in the programs. In this guide we are learning about: expressions, operators, their types, and how to work with them with examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What operators are&lt;/li&gt;
&lt;li&gt;Arithmetic operators (+, -, *, /, %)&lt;/li&gt;
&lt;li&gt;Comparison operators (==, ===, !=, &amp;gt;, &amp;lt;)&lt;/li&gt;
&lt;li&gt;Logical operators (&amp;amp;&amp;amp;, ||, !)&lt;/li&gt;
&lt;li&gt;Assignment operators (=, +=, -=)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What operators are
&lt;/h2&gt;

&lt;p&gt;Operators are just a symbols or keyword those instruct compiler or interpreter to perform specific mathmatical, logical, and relational manipulation on data (also called operands).&lt;/p&gt;

&lt;p&gt;In simple term you can say these are the things tell computer what to do with data.&lt;/p&gt;

&lt;p&gt;For Example: in the expression &lt;code&gt;2+3&lt;/code&gt;, &lt;code&gt;+&lt;/code&gt; is the operator and &lt;code&gt;2&lt;/code&gt;, and &lt;code&gt;3&lt;/code&gt; are the oprators.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Compilers and Interpreters are tools those converte programs (human readable) into machine-code (computer readable). Expressions are the combinations of constants, variables, operators that are prouced a single value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets see some most important operators in JavaScript with examples:&lt;/p&gt;

&lt;h2&gt;
  
  
  Arithmetic operators
&lt;/h2&gt;

&lt;p&gt;Arithmetics operators takes numerical values as operands and evaluated into a single numeric value. In JavaScript there are some operators with there uses:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator Name&lt;/th&gt;
&lt;th&gt;Operator Symbol&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Addition&lt;/td&gt;
&lt;td&gt;&lt;code&gt;+&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return result of arithmatic addition of given two numbers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Negation&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return result of artihmatic negation of given two numbers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiplication&lt;/td&gt;
&lt;td&gt;&lt;code&gt;*&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return multiplication of given two numbers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Division&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return result of arithmatic division of given two numbers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Remainder&lt;/td&gt;
&lt;td&gt;&lt;code&gt;%&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return a left over remainder result of given numbers division&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Remember number division by zero return a infinity. Lets see some examples:&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="cm"&gt;/*-------------- Arithmatic Addition ---------------*/&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;7&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;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 17&lt;/span&gt;

&lt;span class="cm"&gt;/*-------------- Arithmatic Negation ---------------*/&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;7&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;sub&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;

&lt;span class="cm"&gt;/*-------------- Arithmatic Multiplication ---------------*/&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mul&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;7&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;mul&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 70&lt;/span&gt;

&lt;span class="cm"&gt;/*-------------- Arithmatic Division ---------------*/&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;7&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;div&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 1.4285714285714286&lt;/span&gt;

&lt;span class="cm"&gt;/*-------------- Arithmatic Remainder ---------------*/&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;7&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;rem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Do It Your Self: Open browser console tab. Copy paset each snippet one-by-one and ovserve its output.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Comparison operators
&lt;/h2&gt;

&lt;p&gt;Comparison operators used to compare two operands and results logical values like &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. Operands can be string, numbers or logical values.&lt;/p&gt;

&lt;p&gt;String operands are compare each other based on &lt;strong&gt;standard lexicographical ordering, using unicode values&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When operands type differe to each other, than JavaScript try to convert them into appropriate type (generally result numerically). It also called &lt;strong&gt;implicit type conversion&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But with the &lt;code&gt;===&lt;/code&gt; or &lt;code&gt;!==&lt;/code&gt; operators, it firstly check those types if not match than no need to compare next. It also known as &lt;strong&gt;strict comparison&lt;/strong&gt;. Most developers prefer to use them instead of normal comparison, which reduce the potential bugs in code.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator Name&lt;/th&gt;
&lt;th&gt;Operator Symbol&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Equal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return &lt;code&gt;true&lt;/code&gt; if operands are equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Not Equal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;!=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return &lt;code&gt;true&lt;/code&gt; if operands are not equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Strict Equal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;===&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return &lt;code&gt;true&lt;/code&gt; if operands are equal and have same type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Strict Not Equal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;!==&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return &lt;code&gt;true&lt;/code&gt; if operands are same type but are not equal or are of different type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Less Than&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return &lt;code&gt;true&lt;/code&gt; if left side operand are lesser to right side&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Greather Than&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return &lt;code&gt;true&lt;/code&gt; if left side operand are greather to right side&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Less Than or Equal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return &lt;code&gt;true&lt;/code&gt; if left side operand are lesser or equal to right side&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Greather Than or Equal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;return &lt;code&gt;true&lt;/code&gt; if left side operand are greather or equal to right side&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Lets see some examples:&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;five&lt;/span&gt; &lt;span class="o"&gt;=&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;ten&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="cm"&gt;/*----------------- Equal / Not Equal ------------------*/&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="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;five&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="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;ten&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="cm"&gt;/*------------- Strict Equal / Not Equal ---------------*/&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="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;five&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="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;ten&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="cm"&gt;/*------------- Less Than / Greater Than ---------------*/&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="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;five&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="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;ten&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="cm"&gt;/*----- Less Than or Equal / Greater Than or Equal -----*/&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="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;five&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;ten&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;five&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Do It Your Self: Try to guess the output of each console.log of snippet than execution them on the console after that to check your understanding.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Logical operators
&lt;/h2&gt;

&lt;p&gt;Logical operator are used work with boolean values. Using &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; return boolean value if the all operands are boolean else return on of them. These values also known as value selection operators. Lets see them:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator Name&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Logical OR&lt;/td&gt;
&lt;td&gt;If on of them are true it return true or return value on of them if operands are not boolean.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Logical AND&lt;/td&gt;
&lt;td&gt;If on of them are false it return false or return first operand.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Logical Negation&lt;/td&gt;
&lt;td&gt;If operand is boolean it invers it or if not boolean the it convert it type into boolean than invers it.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Logic table for Logical OR &lt;code&gt;||&lt;/code&gt; operator
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;First Operand&lt;/th&gt;
&lt;th&gt;Second Operand&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Lets see some examples:&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="cm"&gt;/*------ With Booelan Values -------*/&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="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true&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="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true&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="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true&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="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="cm"&gt;/*------ With Non-Booelan Values -------*/&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="s2"&gt;dogs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cats&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true || true return "dogs"&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="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cats&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// false || true return "cats"&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="s2"&gt;dogs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true || false return "dogs"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Logic table for Logical AND &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;First Operand&lt;/th&gt;
&lt;th&gt;Second Operand&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Lets see some examples:&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="cm"&gt;/*------ With Booelan Values -------*/&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="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true&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="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// false&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="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// false&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="kc"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="cm"&gt;/*------ With Non-Booelan Values -------*/&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="s2"&gt;dogs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cats&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true || true return "dogs"&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="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cats&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// false || true return false&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="s2"&gt;dogs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true || false return false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Logic table for Logical Nagetion &lt;code&gt;!&lt;/code&gt; operator
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operand&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Lets see some examples:&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="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="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// false&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="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true&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="o"&gt;!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;false&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Do It Your Self: Try to guess the output of each console.log of snippet than execution them on the console after that to check your understanding.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Assignment operators
&lt;/h2&gt;

&lt;p&gt;Assignment operators used to assign right operands to its left operand. Assignment expression &lt;code&gt;x = f()&lt;/code&gt; here value of&lt;code&gt;f()&lt;/code&gt; is assigned to &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lets see some common assignemts operators with examples:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator Name&lt;/th&gt;
&lt;th&gt;Operator Symbol&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Assignment&lt;/td&gt;
&lt;td&gt;&lt;code&gt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;assigne right operands to right operand&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Addition Assignment&lt;/td&gt;
&lt;td&gt;&lt;code&gt;+=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;its a assignemt with addition it's a shorthand of &lt;code&gt;x = x + f()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Subtraction Assignment&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;its a assignemt with subtraction it's a shorthand of &lt;code&gt;x = x - f()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiplicaiton Assignment&lt;/td&gt;
&lt;td&gt;&lt;code&gt;*=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;its a assignemt with multiplication it's a shorthand of &lt;code&gt;x = x * f()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Division Assignment&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;its a assignemt with division it's a shorthand of &lt;code&gt;x = x / f()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Remainder Assignment&lt;/td&gt;
&lt;td&gt;&lt;code&gt;%=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;its a assignemt with remainder it's a shorthand of &lt;code&gt;x = x % f()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*--------- Assignment ---------*/&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;something@gmail.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;26&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="mi"&gt;26000&lt;/span&gt;

&lt;span class="cm"&gt;/*--------- Addition Assignment ---------*/&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&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="mi"&gt;50&lt;/span&gt;

&lt;span class="cm"&gt;/*--------- Subtraction Assignment ---------*/&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;2&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="mi"&gt;50&lt;/span&gt;

&lt;span class="cm"&gt;/*--------- Multiplication Assignment ---------*/&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;2&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="mi"&gt;50&lt;/span&gt;

&lt;span class="cm"&gt;/*--------- Division 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="mi"&gt;50&lt;/span&gt;

&lt;span class="cm"&gt;/*--------- Remainder 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="mi"&gt;50&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Do It Your Self: Replicate those examples on you browser console and print them to see its output.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;These operators are essential for building foundation of performing mathmatical or logical operaiton in javaScript. Here are more than those operators in JavaScript but those are essentials if you wan't to lean more about than visite MDN official &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_operators#assignment_operators" rel="noopener noreferrer"&gt;Expression and Operators&lt;/a&gt; article.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>operations</category>
      <category>beginners</category>
      <category>chaicodewebdev2026</category>
    </item>
    <item>
      <title>Understanding Variables and Data Types in JavaScript</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Wed, 25 Feb 2026 15:41:51 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/understanding-variables-and-data-types-in-javascript-33pd</link>
      <guid>https://forem.com/anoop-rajoriya/understanding-variables-and-data-types-in-javascript-33pd</guid>
      <description>&lt;p&gt;Welcome to the world of programming. If you are learning programming this is the essential thing you should master. Think you are shifting from current house to new one. You get many cardboard boxes and labeled them like library for books, foot-wears for shoes and slippers etc.&lt;/p&gt;

&lt;p&gt;Just like it in the programming there are thing called variables just like you labels and the data types like your stuffs you putting into boxs labeled by there categories.&lt;/p&gt;




&lt;h2&gt;
  
  
  What variables are and why they are needed
&lt;/h2&gt;

&lt;p&gt;In the computer programming, variables are the named or symbolic container which used to store data values within computer memroy. It act as a placeholder or label for information such as user input, component states or calculation which can be used or updated during the programm execution without that storing, manipulation data whould be nearly impossible.&lt;/p&gt;

&lt;p&gt;Varialbes are essential for creating efficient or flexible programms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexible and Reusable:&lt;/strong&gt; instead of hard coding fixed value like &lt;code&gt;3+5&lt;/code&gt; you can use &lt;code&gt;a+b&lt;/code&gt;, allow to use same code for different data inputs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Storage and Manipulation:&lt;/strong&gt; programmes need to remember data like user names, game state, raw data form files. Variables allow developers to use store this in memory so it can be reused later.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Readability and Maintainability:&lt;/strong&gt; using discriptive name make programm easy to read, understand so it can be updated in future reduce errors during debugging.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Managing Program Logic:&lt;/strong&gt; variables used to manage the program flow logic such as using boolean state to decide which part of the code block should be execute during run time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Always use descirptive names like use user_name or userName instead of only name, for data you can use common naming cases like camelCase, snack_case, PascelCase, and kabab-case etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How to declare variables using var, let, and const
&lt;/h2&gt;

&lt;p&gt;Inside a JavaScript "declare" variables just means of creating a box and giving it a name. There are three possible way to do this:&lt;/p&gt;

&lt;h3&gt;
  
  
  The Old Way
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;var&lt;/code&gt; keyword was a original way to declare variables. You seeing it many times in old codebase but modern developers rarly use it because of it's quirky behaviours which some times lead bugs in 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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Modern Way
&lt;/h3&gt;

&lt;p&gt;There were &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords released in js new version to reduce this quirky behaviours.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;let&lt;/code&gt; when you know the vlaue of the variable need to be change later like playerScore in a game.&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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;playerScore&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;const&lt;/code&gt; this is short for constants. It used to declare variables with the fixed value which can not be changed in any cost within the program execution like userDOB.&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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userDOB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1990&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: Always avoid of using &lt;code&gt;var&lt;/code&gt; keyword for declaring variables because that can lead potential errors in you code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Primitive data types
&lt;/h2&gt;

&lt;p&gt;Think when you bring some kitchen grocery, you never put item within wrong container like you never put oil in box or rice in bottle. You can do but naturally we did not do this. Just like it Javascript use proper data types for storing information within computer. There are some common primitive data types:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. String Data Type
&lt;/h3&gt;

&lt;p&gt;it's a sequence of characters. It's used to store textual informaiton in computer. Strings are immutable meaning it can not be changed after creating, but you can assingned new string to the same variable.&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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userEmail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;something@email.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.Number Data Type
&lt;/h3&gt;

&lt;p&gt;it can be a interger or floating point numbers, JavaScript didn't differentiate between different kids of number it store all them as floating point number. It is also immutable data type. JavaScript also has a special type of numbers &lt;code&gt;infinity&lt;/code&gt; and &lt;code&gt;-inifinity&lt;/code&gt; both represent mathmetical infinities for positive side or negative side. &lt;code&gt;NaN&lt;/code&gt; is also a number type which represent values those are Not a Number.&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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;integer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;floating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;10.550&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;negative&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&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;infy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;infinity&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;negInfy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;infinity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Boolean Data Type
&lt;/h3&gt;

&lt;p&gt;it use to represen &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; values majority used to run specific block of 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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isLogin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;isAdmin&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;
  
  
  4. Null Data Type
&lt;/h3&gt;

&lt;p&gt;in JavaScript programmer explicitly use &lt;code&gt;null&lt;/code&gt; data type to intenally represent absance of any object value of empty values. It only have one possible value &lt;code&gt;null&lt;/code&gt;. The well-knowen quirky or historical bug is that using &lt;code&gt;typeof&lt;/code&gt; operator with &lt;code&gt;null&lt;/code&gt; give a string of &lt;code&gt;object&lt;/code&gt; not &lt;code&gt;null&lt;/code&gt;. This behaviour of JavaScript is maintain for backword compatibility to avoid breaking existing websites.&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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;typof&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "object"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Truthy &amp;amp; Falsy values
&lt;/h3&gt;

&lt;p&gt;Meaning of &lt;strong&gt;Truthy and Falsy&lt;/strong&gt; is if we convert these values into &lt;strong&gt;Boolean Type&lt;/strong&gt; the truthy values are converted into &lt;code&gt;true&lt;/code&gt; and falsy values are converted into &lt;code&gt;false&lt;/code&gt;. There are some values treat as falsy values those are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Booelan value &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Number zero including &lt;code&gt;0.0&lt;/code&gt; or &lt;code&gt;-0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The big int zero value &lt;code&gt;0n&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Empty string &lt;code&gt;""&lt;/code&gt;, &lt;code&gt;''&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Intentinoal absance of value &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Undefined&lt;/li&gt;
&lt;li&gt;Not a number &lt;code&gt;NaN&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Excluding these, all values are treated as a truthy values.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Undefined Data Type
&lt;/h3&gt;

&lt;p&gt;It also used to represent absence of a value like &lt;code&gt;null&lt;/code&gt; do. Major different is JavaScript automatically assinged &lt;code&gt;undefined&lt;/code&gt; to a varialbes has declare but not yet assinged explicitly value.&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="c1"&gt;// Syntax:&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;score&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;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Do It Your Self: Open browser go inside console tab in dev tools. Try to declaring some variables with differnet data types and try convert into boolean using &lt;code&gt;!&lt;/code&gt; operator. You understand the concept of truty or falsy values.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is scope
&lt;/h2&gt;

&lt;p&gt;Scope is the region where variables, function or objects are accessible and visible. It determin how the JavaScript engine looks for the identifires (names) in different part of you code. There are several types of scopes form a heirarchy known as &lt;strong&gt;Scope Chain&lt;/strong&gt;&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%2Fr1bc209bivhz67sgski6.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%2Fr1bc209bivhz67sgski6.png" alt="Scopes visualization with hallway analogy" width="800" height="494"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Global Scope
&lt;/h3&gt;

&lt;p&gt;Variables declare outside of any block or function resides in global scope. These variables are accessible for every one in the program. Think it like a light bulb in hallway so every one can see or use it in house.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Funciton (Local) Scope
&lt;/h3&gt;

&lt;p&gt;Every function has its own scope. Declaring variables inside function its only accessible within that function, variables not visible outside that funciton or global scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Block Scope
&lt;/h3&gt;

&lt;p&gt;It introduce in ES6 with the &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; keywords. The variables declared inside the block scope (area inside the curly bracket &lt;code&gt;{}&lt;/code&gt;) are only accessible inside, not outside. It include if statement, loops and standalone blocks.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Module Scope
&lt;/h3&gt;

&lt;p&gt;When using ES6 modules (via &lt;code&gt;import&lt;/code&gt; or &lt;code&gt;export&lt;/code&gt;), variables declare inside module are scoped to that specific file. They are not accessible globally or inside any other module unless explicitly exported.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Lexical Scope
&lt;/h3&gt;

&lt;p&gt;It is known as &lt;strong&gt;Scope Chaining&lt;/strong&gt; or &lt;strong&gt;Static Scope&lt;/strong&gt;. It means the scope of variables determins by their physical location within sorce code at compile time, not where they are called in run time. When interpreter need to access the specific variable it following scope chain:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It first find in current scope (innermost scope).&lt;/li&gt;
&lt;li&gt;If not found, then it move to the its parent scope.&lt;/li&gt;
&lt;li&gt;Continue untile it reach to the global scope.&lt;/li&gt;
&lt;li&gt;If variable is still not found, a &lt;code&gt;ReferenceError&lt;/code&gt; is thrown.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Basic difference between &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Each of these keyword has certain purpose and distinct scope. We shall examin the difference between the &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;consts&lt;/code&gt; with example:&lt;/p&gt;

&lt;h3&gt;
  
  
  Var keyword
&lt;/h3&gt;

&lt;p&gt;Var-declared variables are has function scoped. Which means they are accessible any where inside the function it declared. Here's an example of using &lt;code&gt;var&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;function&lt;/span&gt; &lt;span class="nf"&gt;verDeclaration&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 10&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In that example x is declared inside the if statement so it can accessible inside that. But it also accessible inside the entire &lt;code&gt;verDeclaration&lt;/code&gt; function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let keyword
&lt;/h3&gt;

&lt;p&gt;Let-declared variables has block scope. Which means &lt;code&gt;let&lt;/code&gt; declaration limit the variable into block where they are specified. It specifically helpfull to prevent unintentional &lt;strong&gt;Hoisting&lt;/strong&gt; or lowering the scope related errors.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Hoisting is behaviour of JS whre declaration of variable or functions are apeared move to the top of their scope during compilation phase.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets see above example with let declaration:&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;letDeclaration&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&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;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: y is not defined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here y is declared with let keyword means it have block scope so it only accessible within the if statement. Try to access &lt;code&gt;y&lt;/code&gt; outside of that state will throws an &lt;code&gt;ReferenceError: y is not defined&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Const keyword
&lt;/h3&gt;

&lt;p&gt;Declareing variable with &lt;code&gt;const&lt;/code&gt; keyword is also has block scope like &lt;code&gt;let&lt;/code&gt;. It primarly helpfull for declaring variables those will not mutate. It also throws an &lt;code&gt;ReferenceErrors&lt;/code&gt; if trying to access outside its scope.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do It Your Self: Learn the way to execute JS code in browser (hint: use html file or console). Declare some variables like name, age, isStudnet. Print them in console use console.log() method. Try to change these values of with let and consts and observe the behaviours.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Understanding difference between &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, and &lt;code&gt;var&lt;/code&gt; is the essential fundation for web development. Eash developer should know these difference for writing bug free code. For learning these concepts i read articles like &lt;a href="https://medium.com/@ananthvishnu/javascript-variables-60a53fda1211" rel="noopener noreferrer"&gt;varialbes in JS&lt;/a&gt;, &lt;a href="https://medium.com/@akashsdas_dev/scope-in-javascript-fc439e1ac4fe" rel="noopener noreferrer"&gt;scopes in JS&lt;/a&gt;, &lt;a href="https://medium.com/@jackpritomsoren/difference-between-var-let-and-const-in-javascript-c6236899ca4d" rel="noopener noreferrer"&gt;Difference of let, const, and var&lt;/a&gt;,&lt;/p&gt;

</description>
      <category>datatypes</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>chaicodecohort2026</category>
    </item>
    <item>
      <title>Emmet for HTML: A Beginner’s Guide to Writing Faster Markup</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Mon, 23 Feb 2026 19:16:51 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/emmet-for-html-a-beginners-guide-to-writing-faster-markup-4nne</link>
      <guid>https://forem.com/anoop-rajoriya/emmet-for-html-a-beginners-guide-to-writing-faster-markup-4nne</guid>
      <description>&lt;p&gt;Ever you figure out yourself writing &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;/div&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; than &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; and so on. This slow down you for just writing repetitive code in the modern world you need to be a faster.&lt;/p&gt;

&lt;p&gt;Building house by preparing each brick from scratch like molding it, baking it this way take years for just building a wall. Instead of that think having a machine which prepare a wall just by taking few commands. This is the what like Emmet do things.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is Emmet
&lt;/h2&gt;

&lt;p&gt;In very simple term, Emmet is a free, open source plugin for code editors which help you to write HTML, CSS code much, much faster. Thinks its like a faster shorthand language for web development in that you just need to write a short abbreviation and when press enter or tab it generated into full structured code block.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it useful for HTML beginners
&lt;/h2&gt;

&lt;p&gt;Emmet is useful for beginners coder for several reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Save Time &amp;amp; Reduce Typos :&lt;/strong&gt; lesser you typed faster you code, Emmet handle closing tags and proper nesting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Focus on structure not syntax :&lt;/strong&gt; instead bogged into the every tag syntax focusing on what you build and let Emmet to complete repetitive work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Building Good habits :&lt;/strong&gt; it encourage to think about the logical structure of page which is crucial.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Boost confidence :&lt;/strong&gt; see code generated by just short abbreviation empowering confidence and make coding feel fun.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How Emmet works inside code editors
&lt;/h2&gt;

&lt;p&gt;Emmet is usually built in most popular code editors like vs-code or available as a simple extension can plugged in just one click.&lt;/p&gt;

&lt;p&gt;Let's see the workflow Emmet: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Typing Abbreviation :&lt;/strong&gt; you typing short string of abbreviations like &lt;code&gt;div&lt;/code&gt; in HTML or &lt;code&gt;df&lt;/code&gt; in CSS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trigger to Expend :&lt;/strong&gt; in most editors it's &lt;code&gt;tab&lt;/code&gt; key, &lt;code&gt;Ctr+E&lt;/code&gt;, and &lt;code&gt;Cmd+E&lt;/code&gt; commands in some setup which trigger the abbreviation expending.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expend to Code :&lt;/strong&gt; When expending key pressed the abbreviation is converted into the calculated code structure by its engine.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Basic Emmet syntax and abbreviations
&lt;/h2&gt;

&lt;p&gt;Emmet is use CSS selectors like syntax to predict what you want to create. lets dive into some fundamental abbreviations patterns to understand it batter:&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating some elements by typing some basics abbreviation patterns
&lt;/h3&gt;

&lt;p&gt;For creating elements we only need to use their names. Emmet doesn't has predefined tags so we can type any tag name which converted into HTML tag.&lt;/p&gt;

&lt;p&gt;Example: creating &lt;code&gt;div&lt;/code&gt; element&lt;br&gt;
Type: &lt;code&gt;div&lt;/code&gt; and &lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: creating &lt;code&gt;h1&lt;/code&gt; element&lt;br&gt;
Type: &lt;code&gt;h1&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Try It Your Self:&lt;/strong&gt; Open you code editor and just typing some common elements like &lt;code&gt;ul&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;input&lt;/code&gt;, &lt;code&gt;span&lt;/code&gt;, and &lt;code&gt;section&lt;/code&gt; and press &lt;code&gt;tab&lt;/code&gt; key.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Adding classes, IDs, and attributes
&lt;/h2&gt;

&lt;p&gt;Here are the real use case of Emmet. You can combine these attributes with the any element abbreviation pattern. Without any element the abbreviation pick the div element by default.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Class Attribute:&lt;/strong&gt; for class abbreviation pattern using &lt;code&gt;.&lt;/code&gt; CSS selector with following class name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ID Attribute:&lt;/strong&gt; for id abbreviation pattern using &lt;code&gt;#&lt;/code&gt; CSS selector with following id name.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Other Attribute:&lt;/strong&gt; for any other attribute abbreviation pattern using &lt;code&gt;[attribute="value"]&lt;/code&gt; CSS selector for creating element with any attribute.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: Element With Class&lt;br&gt;
Type: &lt;code&gt;div.container&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: Element With Id&lt;br&gt;
Type: &lt;code&gt;header#main-header&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;header&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"main-header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: Element With Class and ID&lt;br&gt;
Type: &lt;code&gt;p.lead#intro-text&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lead"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"intro-text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: Element With Custom attribute&lt;br&gt;
Type: &lt;code&gt;a[href="#contact" title="Contact Us"]&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#contact"&lt;/span&gt; &lt;span class="na"&gt;title=&lt;/span&gt;&lt;span class="s"&gt;"Contact Us"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Try It Your Self:&lt;/strong&gt; create an &lt;code&gt;button&lt;/code&gt; with class name &lt;code&gt;btn&lt;/code&gt; and id name &lt;code&gt;submit-btn&lt;/code&gt;. Then create an &lt;code&gt;img&lt;/code&gt; element with &lt;code&gt;src="Image.jpg"&lt;/code&gt; and &lt;code&gt;alt="My Image"&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Creating nested elements
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;&amp;gt;&lt;/code&gt; (child) operator it allow you to nest element each other. It is useful for creating little bit complex layouts with ease. lets see some examples:&lt;/p&gt;

&lt;p&gt;Example: &lt;code&gt;li&lt;/code&gt; inside &lt;code&gt;ul&lt;/code&gt;&lt;br&gt;
Type: &lt;code&gt;ul&amp;gt;li&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You cannot nesting text by use &lt;code&gt;&amp;gt;&lt;/code&gt; (child) operator alone you need something else. nesting text you need to use &lt;code&gt;{}&lt;/code&gt; brackets to wrap the text:&lt;/p&gt;

&lt;p&gt;Example: Text inside H1&lt;br&gt;
Type: &lt;code&gt;h1&amp;gt;{Title}&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Title&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are one more operator are used called "+" (sibling) operator which is not used for nesting, it used for create multiple elements in same level. Like two element site side by side. Lets see example:&lt;/p&gt;

&lt;p&gt;Example: &lt;code&gt;span&lt;/code&gt; with logo id, &lt;code&gt;button&lt;/code&gt; with Signup text inside &lt;code&gt;header&lt;/code&gt;&lt;br&gt;
Type: &lt;code&gt;header&amp;gt;span#logo+button&amp;gt;{Signup}&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;header&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"logo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Signup&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Try It Your Self:&lt;/strong&gt; create a &lt;code&gt;div&lt;/code&gt; element has a class &lt;code&gt;card&lt;/code&gt; with &lt;code&gt;h1&lt;/code&gt; and &lt;code&gt;p&lt;/code&gt; elements both have some title and description text inside them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Repeating elements using multiplication
&lt;/h2&gt;

&lt;p&gt;Some times you need to create multiple elements like list items in unorder list or multiple card components in grid component. Than here you can use &lt;code&gt;*&lt;/code&gt; with number N represent how many items you want. lets see examples:&lt;/p&gt;

&lt;p&gt;Example: 5 &lt;code&gt;li&lt;/code&gt; inside &lt;code&gt;ul&lt;/code&gt;&lt;br&gt;
Type: &lt;code&gt;ul&amp;gt;li*5&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: 3 &lt;code&gt;a&lt;/code&gt; inside &lt;code&gt;nav&lt;/code&gt;&lt;br&gt;
Type: &lt;code&gt;nav&amp;gt;a*3&amp;gt;{links}&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;nav&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;links&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;links&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;links&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are one more thins you often see many coder do. when they create multiple item they use &lt;code&gt;$&lt;/code&gt; symbols in Ids, Class or inside other attributes and some times inside elements as a part of text. Purpose of using it is to create indexes in sequences. or some times it used with &lt;code&gt;@-&lt;/code&gt; to reverse the order. Lets see examples:&lt;/p&gt;

&lt;p&gt;Example: 3 &lt;code&gt;a&lt;/code&gt; with link counts inside &lt;code&gt;nav&lt;/code&gt;&lt;br&gt;
Type: &lt;code&gt;nav&amp;gt;a*3&amp;gt;{links$}&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;nav&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;links1&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;links2&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;links3&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: 4 &lt;code&gt;li&lt;/code&gt; with class &lt;code&gt;items&lt;/code&gt; and text index with reverse order inside &lt;code&gt;ul&lt;/code&gt;&lt;br&gt;
Type: &lt;code&gt;ul&amp;gt;li.items*4&amp;gt;{list$@-}&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"items"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;list4&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"items"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;list3&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"items"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;list2&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"items"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;list1&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Try It Your Self:&lt;/strong&gt; create a &lt;code&gt;nav&lt;/code&gt; with &lt;code&gt;ul&lt;/code&gt; containing 4 list items &lt;code&gt;li&lt;/code&gt; has class &lt;code&gt;item&lt;/code&gt; each item has its own &lt;code&gt;a&lt;/code&gt; element with text &lt;code&gt;link-index&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Generating full HTML boilerplate with Emmet
&lt;/h2&gt;

&lt;p&gt;One of the most usable thing is to create a full HTML boiler plate code. That is the starting code every page needed. You need to use &lt;code&gt;!&lt;/code&gt; symbol or sometimes &lt;code&gt;html:5&lt;/code&gt; for older version editors. Lets see final example:&lt;/p&gt;

&lt;p&gt;Example: HTML boiler plate code&lt;br&gt;
Type: &lt;code&gt;!&lt;/code&gt; or &lt;code&gt;html:5&lt;/code&gt;&lt;br&gt;
Press &lt;code&gt;tab&lt;/code&gt; key&lt;br&gt;
Expended To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Document&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Emmet is optional tool to learn but if you learned it it change the whole level of speed and understanding of writing HTML and CSS code. It might takes practice to write abbreviation but once you do it you'll wonder how ever you coded without it. Once you get comfortable you naturally discover more advanced Emmets.&lt;/p&gt;

&lt;p&gt;I use &lt;a href="https://docs.emmet.io/" rel="noopener noreferrer"&gt;Emmet official documentation&lt;/a&gt; or &lt;a href="https://docs.emmet.io/cheat-sheet/" rel="noopener noreferrer"&gt;cheat sheet&lt;/a&gt; for learning it you can also read it if want to know more about Emmet concepts.&lt;/p&gt;

</description>
      <category>emmet</category>
      <category>beginners</category>
      <category>chaicode2026</category>
      <category>webdev</category>
    </item>
    <item>
      <title>CSS Selectors 101: Targeting Elements with Precision</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Sat, 31 Jan 2026 18:12:13 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/css-selectors-101-targeting-elements-with-precision-2c4</link>
      <guid>https://forem.com/anoop-rajoriya/css-selectors-101-targeting-elements-with-precision-2c4</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Understanding, How to precisely select HTML elemtn is criticle concept for every frontend developer. These article provide you a good knowledge about What are selectors and How to use them to select elements with precision.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What Selectors &amp;amp; Why Needed&lt;/li&gt;
&lt;li&gt;Basics Selectors&lt;/li&gt;
&lt;li&gt;Combined Selectors&lt;/li&gt;
&lt;li&gt;Selectors Priority&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What CSS Selectors &amp;amp; Why Need It
&lt;/h2&gt;

&lt;p&gt;Selectors are patterns alow pointing HTML element from the CSS code. This pointing enable to apply styles to specific or group of elements. The selectors are the fundamental concept used in CSS without these its hard to apply styles efficiently. Selectors enable to create complex and interactive layouts, applying consistant styles across entire page, help to seperate styles to HTML which make code more readable.&lt;/p&gt;

&lt;p&gt;If There multiple rules are defined for the same HTML elements the browser determin which one wins by using the rules called &lt;strong&gt;Cascading&lt;/strong&gt; (means styles read later by browser is that one get applied) &amp;amp; &lt;strong&gt;Specificity&lt;/strong&gt; (rules prioritize styles).&lt;/p&gt;

&lt;h2&gt;
  
  
  Basics CSS Selectors
&lt;/h2&gt;

&lt;p&gt;These are the foundation selectors which provide a very straightforword way to select HTML elements. It include four major selectors Universal Selector, Element Selectors, Id Selectors, and Class Selectors.&lt;/p&gt;

&lt;h3&gt;
  
  
  1: Universal Selector
&lt;/h3&gt;

&lt;p&gt;The universal selector used to select every element a HTML page contain. It mostly used to apply test styles quickly on a entire page. It implemented by using &lt;code&gt;*&lt;/code&gt; symbol. This selector recomandade to not use it in production because it push weight on browser and affact performance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;border-box&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;'Arial'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&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;&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%2Fht3wlz9wdbws02k5t68p.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%2Fht3wlz9wdbws02k5t68p.png" alt="universal selector" width="800" height="395"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2: Element Selectors
&lt;/h3&gt;

&lt;p&gt;Element selector used to select any HTML element by using their tag name. When you apply some style to element by using element selector remember it apply given style to every element that tag name used as selector.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;h2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;text-transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;uppercase&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="nb"&gt;solid&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;&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%2Fa6ht49dq84tm6pulcnwn.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%2Fa6ht49dq84tm6pulcnwn.png" alt="element selector" width="785" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3: Id Selectors
&lt;/h3&gt;

&lt;p&gt;Id selector use HTML id name provided to element which uniquly identified it within entire page. It implemented by using &lt;code&gt;#&lt;/code&gt; symbol with the following id name of element. It mostly used to targeting unique element for applying specific styles.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nf"&gt;#main-header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#f4f4f4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;40px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8px&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;&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%2Faxtssjtszwtkcwvlixp1.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%2Faxtssjtszwtkcwvlixp1.png" alt="id selector" width="798" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4: Class Selectors
&lt;/h3&gt;

&lt;p&gt;This selector use class attribute name to target elements in HTML page. It use &lt;code&gt;.&lt;/code&gt; dot symobe along with the class name. It mostly used to select group of elements to apply same type styles, its a common standard in web-dev.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.subtitle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;italic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#555&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin-top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&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;&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%2F90efhh2snozztixbdaj1.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%2F90efhh2snozztixbdaj1.png" alt="class selector" width="756" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Combined CSS Selectors
&lt;/h2&gt;

&lt;p&gt;Combined selectors are the high level selecors these selectors are implement with the help of basics selectors. Majorly used combined selecors are Grouped Selector, Chained Selector, Descendants Child Selector, Direct Child Selector, Adjacent Sibling Selector, and General Sibling Selector&lt;/p&gt;

&lt;h3&gt;
  
  
  1: Grouped Selector
&lt;/h3&gt;

&lt;p&gt;Grouped selectors are used to target multiple elements with different type selectors. It implement those by writing basics selectors and separating them with &lt;code&gt;,&lt;/code&gt; comma. It mostly used to reduce redundant styles by grouping them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;h3&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;'Helvetica'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;darkblue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;letter-spacing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-1px&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;&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%2Fh7252cmkc6seui7gcxj0.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%2Fh7252cmkc6seui7gcxj0.png" alt="grouped selector" width="733" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2: Chained Selector
&lt;/h3&gt;

&lt;p&gt;It is defined by writing multiple selectors wihout any spaces between them. It target element only if it has all these qulifires at the same time. It also a good way to narrow down the targeting view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.footer-note.important&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#fff566&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#ffeeba&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;padding-inline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;padding-block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;margin-block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;6px&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;&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%2Fa01iyjcm8xebvccgn3uc.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%2Fa01iyjcm8xebvccgn3uc.png" alt="chained selector" width="751" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3: Descendants Child Selector
&lt;/h3&gt;

&lt;p&gt;It implemented by just writing selectors by seperating spaces. It used when you want to select all perticular y elements inside of another x element. Ensure y do not have to be direct childe of x elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.parent-box&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;underline&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;&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%2F51cs1iwwiu98vatzy1ul.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%2F51cs1iwwiu98vatzy1ul.png" alt="descendant child selector" width="761" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4: Direct Child Selector
&lt;/h3&gt;

&lt;p&gt;It use &lt;code&gt;&amp;gt;&lt;/code&gt; symbol to target direct childs of elements. It target all direct y elements of the another elements x, here it only target all y elements those are a direct childes of x elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.parent-box&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#d1ecf1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&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;&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%2Fodehf7tquvwfx4szjt2s.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%2Fodehf7tquvwfx4szjt2s.png" alt="direct child selector" width="750" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  5: Adjacent Sibling Selector
&lt;/h3&gt;

&lt;p&gt;It used when you want to apply styles to a element that comes just after the another element. You write a selector targeting a element, than &lt;code&gt;+&lt;/code&gt; plus symbol, and than writing a selector that targeting element want to apply style on that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;h3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#8e44ad&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;700&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1.1rem&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;&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%2Fvkzxhks0qfdz6shgkc0u.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%2Fvkzxhks0qfdz6shgkc0u.png" alt="adjacent sibling selector" width="754" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  6: General Sibling Selector
&lt;/h3&gt;

&lt;p&gt;Just like a adjacent sibling selector its also use to select siblings but it select all the siblings of an element. First write selector of element x and &lt;code&gt;~&lt;/code&gt; symbol, than write another selecor which target all the elements y those come after the x element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;h3&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#333&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding-left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;14px&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;&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%2F6oojomivdty45lysrv8p.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%2F6oojomivdty45lysrv8p.png" alt="general sibling selectors" width="765" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Selector Priority (Specificity)
&lt;/h2&gt;

&lt;p&gt;In starting we have discussesd about how CSS styles applied to elements it used &lt;strong&gt;Cascading&lt;/strong&gt; (we already disscueesed in starting) and &lt;strong&gt;Specificity&lt;/strong&gt; rules:&lt;/p&gt;

&lt;p&gt;Specificty is like a weight system a browser use to identified which rule win when multiple styles applyied on element. Think it like a scoring system, when more specific selector is the higher score it get. We can assure in the specificity hierarchy each selector have a specificty score which determins its priority:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Inline styles have a &lt;code&gt;1000&lt;/code&gt; most higher priority score which is higher than others.&lt;/li&gt;
&lt;li&gt;ID Selectors have a &lt;code&gt;0100&lt;/code&gt; second higher priority scores.&lt;/li&gt;
&lt;li&gt;Class Selectors have a &lt;code&gt;0010&lt;/code&gt; third higher priority scores.&lt;/li&gt;
&lt;li&gt;Element Selectors have a &lt;code&gt;0001&lt;/code&gt; least higher priority scores.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Based on these scores we can determine which style rule wins. Lets see an example which clearify the specificity rules:&lt;/p&gt;

&lt;p&gt;We have a three different styles rule with different selectors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/*&amp;lt;p id="intro" class="highlight"&amp;gt;Hello World&amp;lt;/p&amp;gt;*/&lt;/span&gt;

&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.highlight&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;#intro&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&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;Think which style wins and apply color to paragraph element. If your answer is green you nailed it, its right answer. Lets see score calculation:&lt;/p&gt;

&lt;p&gt;Here element selector style get score &lt;code&gt;0001&lt;/code&gt;, class selector get &lt;code&gt;0010&lt;/code&gt;, and id selector get &lt;code&gt;0100&lt;/code&gt;. To seeing all that we figured the higher score is &lt;code&gt;0100&lt;/code&gt; so here id selector wins.&lt;/p&gt;

&lt;p&gt;What if there are multiple styles rule those have same specificity?&lt;br&gt;
Than here the cascading rules decides the result, in same case if specificity apears than styles rules wins which apears in last.&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%2Fhkr0qirrwl9a1s4rdg25.jpg" 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%2Fhkr0qirrwl9a1s4rdg25.jpg" alt="targeting element with precision and class vs Ids in css" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;CSS selectors function as a foundational pattern-matching system that dictates how styles are targeted and prioritized through a weighted scoring mechanism known as specificity.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understanding CSS selectors and cascading.&lt;/li&gt;
&lt;li&gt;Basics CSS selectors.&lt;/li&gt;
&lt;li&gt;Combined CSS selectors.&lt;/li&gt;
&lt;li&gt;Selector priority (Specificity) scores.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To maintain a maintable code prioritize use of classes and ids for reusabilty, readability in large projects and separation of concerns.&lt;/p&gt;




</description>
      <category>beginners</category>
      <category>css</category>
      <category>chaicode2026</category>
    </item>
    <item>
      <title>Understanding HTML Tags and Elements</title>
      <dc:creator>Anoop Rajoriya</dc:creator>
      <pubDate>Thu, 29 Jan 2026 12:12:12 +0000</pubDate>
      <link>https://forem.com/anoop-rajoriya/understanding-html-tags-and-elements-1p</link>
      <guid>https://forem.com/anoop-rajoriya/understanding-html-tags-and-elements-1p</guid>
      <description>&lt;h2&gt;
  
  
  Content List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is HTML and why we using it&lt;/li&gt;
&lt;li&gt;What is HTML tags&lt;/li&gt;
&lt;li&gt;What is elements &amp;amp; elements vs tags&lt;/li&gt;
&lt;li&gt;Self closing elements&lt;/li&gt;
&lt;li&gt;Inline and blcok level elements&lt;/li&gt;
&lt;li&gt;Rerences&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What HTML is and why we use it?
&lt;/h2&gt;

&lt;p&gt;HTML stand for Hyper Text Markup Language. In that &lt;code&gt;hyper text&lt;/code&gt; refere to system that provide a link to access other pages on web, &lt;code&gt;markup&lt;/code&gt; means use of annotate text give instruction to browser how to structure and display page content.&lt;/p&gt;

&lt;p&gt;Brower can not read html like humans do it need a instruction to differentiate between instruction and the actual content. Html provide this information so browser can understand which part of the content is display as heading and which part is the paragraph or links without that browser show all information as a big paragpah of text.&lt;/p&gt;

&lt;p&gt;Think, in building a house the blueprint and fram of the house is html provide the initial structre. In human body the skeleton is the html not skin and mucles which provide styling (css) and interactibity (js) to web page.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is HTML tags?
&lt;/h2&gt;

&lt;p&gt;In html tags are the commands that tells browswer how to handle specific piece of content. It is a fundamental building block it used to structre and apply content formating on web pages.&lt;/p&gt;

&lt;p&gt;Every tag contain tree parts open angle bracket &lt;code&gt;&amp;lt;&lt;/code&gt;, tag name like &lt;code&gt;head, body, h1&lt;/code&gt;, and close angle bracket &lt;code&gt;&amp;gt;&lt;/code&gt; this tree thing complete every tag like &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;. Most of the tags in html comes with pair, opening and closing tags which enclose content. Closing tag contain &lt;code&gt;/&lt;/code&gt; wich differentiate closing tag with opening tag.&lt;/p&gt;

&lt;p&gt;without a closing tag, browser get confused. If you open a heading tag and put heading content on to it but if you don't close it broswer assume all content after that are the part of heading and all content print like a big giant heading every thing big and bold. Closing tag tell borswer "stop" instruction heading ends right here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
&lt;span class="nt"&gt;&amp;lt;section&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello world&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Think its like a putting object into box. You open the box lid (Opening tag), put object into that (content) and close the box lid (Closing tag).&lt;/p&gt;




&lt;h2&gt;
  
  
  HTML element &amp;amp; element vs tag
&lt;/h2&gt;

&lt;p&gt;Most of the peoples assume the tags and elemetns are interchangable but there are slightly technical difference here. Element is the package of the tree main thinks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Opening Tag: &lt;code&gt;start&lt;/code&gt; command. It consist element name wrap with the opening and closing angle bracket &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tells the browser i'm starting paragraph now.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Content: this is the actual &lt;code&gt;text&lt;/code&gt;, &lt;code&gt;image&lt;/code&gt;, and &lt;code&gt;link&lt;/code&gt; you want to user see.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Closing Tag: &lt;code&gt;stop&lt;/code&gt; command. it has same configuration like &lt;code&gt;start&lt;/code&gt; command with forward slash &lt;code&gt;/&lt;/code&gt; make the difference &lt;code&gt;&amp;lt;/p&amp;gt;&lt;/code&gt; and tell the browser this here paragraph ends.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To understand difference between element and tag think element like a sandwich:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Opening tag&lt;/strong&gt; is the &lt;strong&gt;top slice of bread&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Content&lt;/strong&gt; is the &lt;strong&gt;middle part (cheez, butter, and jelly)&lt;/strong&gt; of the sandwich.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Closing tag&lt;/strong&gt; is the &lt;strong&gt;bottom slice of bread&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Element&lt;/strong&gt; is the &lt;strong&gt;entire sandwich&lt;/strong&gt;, you can't have sandwich without all part working together.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Self closing (Void) element
&lt;/h2&gt;

&lt;p&gt;In the world of HTML there are the some tags have opening and closing becase thay wrap around the content to tell the broswer where this specific content ended. What if tag is the content.&lt;/p&gt;

&lt;p&gt;There are type of tags in html which only have one tag no need to close it because this type of tags are work as content not wrap around content. Think its like a sticker or stamp you don't need to open sticker, put someting in it, and close it. You just directly put it down.&lt;/p&gt;

&lt;p&gt;These element called &lt;strong&gt;self closing Element and Void Element&lt;/strong&gt;, these some major elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;: used to put image on page don't put text on it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;br&amp;gt;&lt;/code&gt;: a simple line break like enter in keyboard.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;hr&amp;gt;&lt;/code&gt;: a horizontal rule, line acrose the page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the older version of the html you might see the self closing tags written with forward slash at end like &lt;code&gt;&amp;lt;img/&amp;gt;&lt;/code&gt; but in the latest version of the html in HTML5 the slash is optional and usually omitted.&lt;/p&gt;




&lt;h2&gt;
  
  
  Block vs Inline elements &amp;amp; commonly used elements
&lt;/h2&gt;

&lt;p&gt;Think as your webpage is the grid where every element is box. There are two main ways to arrange them:&lt;/p&gt;

&lt;h3&gt;
  
  
  Block Level Element
&lt;/h3&gt;

&lt;p&gt;This type of elements are always start on new line and take up full width. It stratch out to left and right as far as it can. Think these as bricks when you stack these bricks each other it always start new layer. You can put two bricks side by side in same verticle space without some extra support. There are some common example of block level elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;header&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Logo&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;About&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Contact&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;main&amp;gt;&lt;/span&gt;Block level elements&lt;span class="nt"&gt;&amp;lt;/main&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Inline Level Element
&lt;/h3&gt;

&lt;p&gt;An inline elements does not start on new line it only takes as much width as necessary for its content. it sits "in line" with elements around it. Think it like a marker. If you using marker to highlith a word in sentant it does not break the sentace in apart, the highlight just site right there on top of the word. There are common inline words:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;b&amp;gt;&lt;/span&gt;Lorem Ipsum&lt;span class="nt"&gt;&amp;lt;/b&amp;gt;&lt;/span&gt; is simply dummy text of the &lt;span class="nt"&gt;&amp;lt;i&amp;gt;&lt;/span&gt;printing&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt; and typesetting industry. &lt;span class="nt"&gt;&amp;lt;strong&amp;gt;&lt;/span&gt;Lorem Ipsum&lt;span class="nt"&gt;&amp;lt;/strong&amp;gt;&lt;/span&gt; has been the &lt;span class="nt"&gt;&amp;lt;em&amp;gt;&lt;/span&gt;industry's&lt;span class="nt"&gt;&amp;lt;/em&amp;gt;&lt;/span&gt; standard dummy text ever since the &lt;span class="nt"&gt;&amp;lt;time&amp;gt;&lt;/span&gt;1500s&lt;span class="nt"&gt;&amp;lt;time&amp;gt;&lt;/span&gt;, when an unknown printer took a &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;galley of type and scrambled&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;    &lt;span class="nt"&gt;span&amp;gt;&lt;/span&gt; it to make a type &lt;span class="nt"&gt;&amp;lt;a&amp;gt;&lt;/span&gt;specimen book&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






</description>
      <category>html</category>
      <category>beginners</category>
      <category>chaicode</category>
      <category>2026</category>
    </item>
  </channel>
</rss>
