<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[PallabDev]]></title><description><![CDATA[console.log("I believe in Learning and Spreading")
let grab_knowledge=["Engineering"]
return [...grab_knowledge]]]></description><link>https://blog.studyhex.in</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Apr 2026 13:04:41 GMT</lastBuildDate><atom:link href="https://blog.studyhex.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Understanding this, call(), apply(), and bind() in JavaScript]]></title><description><![CDATA[JavaScript provides a special keyword called this that plays an important role in how functions access data. Many beginners find it confusing because its value changes depending on how a function is c]]></description><link>https://blog.studyhex.in/understanding-this-call-apply-and-bind-in-javascript</link><guid isPermaLink="true">https://blog.studyhex.in/understanding-this-call-apply-and-bind-in-javascript</guid><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Sun, 15 Mar 2026 17:50:38 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/d0fd2a2d-5789-449d-929e-6899149dc9f3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>JavaScript provides a special keyword called <code>this</code> that plays an important role in how functions access data. Many beginners find it confusing because its value changes depending on <strong>how a function is called</strong>.</p>
<p>In this article, we will understand what <code>this</code> means in JavaScript and how functions like <code>call()</code>, <code>apply()</code>, and <code>bind()</code> help us control it. Instead of diving into complex internal mechanics, we will focus on the practical idea of <strong>“who is calling the function.”</strong></p>
<p>Once you understand this idea, the behavior of <code>this</code> becomes much easier to reason about.</p>
<h3>What <code>this</code> Means in JavaScript</h3>
<p>In JavaScript, the value of <code>this</code> depends on the <strong>object that calls the function</strong>.</p>
<p>A simple way to remember this is:</p>
<p><code>this</code> <strong>refers to the object that is calling the function.</strong></p>
<p>Let us start with a simple example.</p>
<pre><code class="language-plaintext">function greet() {
    console.log(this);
}

greet();
</code></pre>
<p>When this function runs in the browser, <code>this</code> usually refers to the <strong>global object (</strong><code>window</code><strong>)</strong>. In Node.js it refers to the global environment object.</p>
<p>This happens because the function is <strong>called directly</strong>, not by any object.</p>
<p>So the caller of the function is the global environment.</p>
<h3><code>this</code> Inside Normal Functions</h3>
<p>When a normal function is called by itself, there is no specific object calling it. In such cases, JavaScript assigns the global context.</p>
<p>Example:</p>
<pre><code class="language-plaintext">function showName() {
    console.log(this.name);
}

var name = "Rahul";

showName();
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">Rahul
</code></pre>
<p>Here the variable <code>name</code> exists in the global scope. Since the function is called normally, <code>this</code> refers to the global object, so <code>this.name</code> resolves to the global variable.</p>
<p>This behavior can sometimes cause confusion, which is why developers usually prefer calling functions through objects when they rely on <code>this</code>.</p>
<h3><code>this</code> Inside Objects</h3>
<p>When a function is defined as a method of an object and that object calls the function, <code>this</code> refers to that object.</p>
<p>Example:</p>
<pre><code class="language-plaintext">const person = {
    name: "Amit",
    greet: function () {
        console.log("Hello " + this.name);
    }
};

person.greet();
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">Hello Amit
</code></pre>
<p>In this case the function <code>greet()</code> is called by <code>person</code>, so <code>this</code> refers to the <code>person</code> object.</p>
<p>You can visualize this relationship like this:</p>
<pre><code class="language-plaintext">Function  ← called by ←  Object
   greet()              person
        this → person
</code></pre>
<p>The important point is that <strong>the caller determines the value of</strong> <code>this</code><strong>.</strong></p>
<h3>The Problem: Losing the Original Caller</h3>
<p>Sometimes we want to use a method from one object inside another object. This is called <strong>function borrowing</strong>.</p>
<p>Consider the following example.</p>
<pre><code class="language-plaintext">const person1 = {
    name: "Rahul",
    greet: function () {
        console.log("Hello " + this.name);
    }
};

const person2 = {
    name: "Anita"
};
</code></pre>
<p>Now suppose we want <code>person2</code> to use the <code>greet()</code> method from <code>person1</code>.</p>
<p>This is where <code>call()</code>, <code>apply()</code>, and <code>bind()</code> become useful.</p>
<h3>What <code>call()</code> Does</h3>
<p>The <code>call()</code> method allows us to manually set the value of <code>this</code> while calling a function.</p>
<p>Syntax</p>
<pre><code class="language-plaintext">functionName.call(object, arg1, arg2)
</code></pre>
<p>Example:</p>
<pre><code class="language-plaintext">person1.greet.call(person2);
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">Hello Anita
</code></pre>
<p>Even though the function belongs to <code>person1</code>, we explicitly tell JavaScript that <code>this</code> should refer to <code>person2</code>.</p>
<p>So the method runs using the data of <code>person2</code>.</p>
<p>Example with arguments:</p>
<pre><code class="language-plaintext">function introduce(city, country) {
    console.log(this.name + " from " + city + ", " + country);
}

const user = {
    name: "Riya"
};

introduce.call(user, "Delhi", "India");
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">Riya from Delhi, India
</code></pre>
<p>Here the first argument sets <code>this</code>, and the remaining arguments are passed normally.</p>
<h3>What <code>apply()</code> Does</h3>
<p>The <code>apply()</code> method works almost the same as <code>call()</code>. The only difference is how arguments are passed.</p>
<p>Syntax</p>
<pre><code class="language-plaintext">functionName.apply(object, [arg1, arg2])
</code></pre>
<p>Arguments are passed as an <strong>array</strong>.</p>
<p>Example:</p>
<pre><code class="language-plaintext">introduce.apply(user, ["Mumbai", "India"]);
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">Riya from Mumbai, India
</code></pre>
<p>Both <code>call()</code> and <code>apply()</code> execute the function immediately. The main difference lies in the argument format.</p>
<h3>What <code>bind()</code> Does</h3>
<p>Unlike <code>call()</code> and <code>apply()</code>, the <code>bind()</code> method does not execute the function immediately.</p>
<p>Instead, it <strong>creates a new function with a fixed</strong> <code>this</code> <strong>value</strong>.</p>
<p>Syntax</p>
<pre><code class="language-plaintext">const newFunction = functionName.bind(object)
</code></pre>
<p>Example:</p>
<pre><code class="language-plaintext">const greetUser = person1.greet.bind(person2);

greetUser();
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">Hello Anita
</code></pre>
<p>Here <code>bind()</code> returns a new function where <code>this</code> is permanently set to <code>person2</code>.</p>
<p>This is very useful when passing functions as callbacks.</p>
<h3>Difference Between <code>call</code>, <code>apply</code>, and <code>bind</code></h3>
<p>The following comparison helps summarize the behavior.</p>
<table>
<thead>
<tr>
<th>Method</th>
<th>Executes Immediately</th>
<th>Argument Format</th>
<th>Returns New Function</th>
</tr>
</thead>
<tbody><tr>
<td>call</td>
<td>Yes</td>
<td>Arguments passed individually</td>
<td>No</td>
</tr>
<tr>
<td>apply</td>
<td>Yes</td>
<td>Arguments passed as an array</td>
<td>No</td>
</tr>
<tr>
<td>bind</td>
<td>No</td>
<td>Arguments passed individually</td>
<td>Yes</td>
</tr>
</tbody></table>
<p>Another way to think about it:</p>
<ul>
<li><p><code>call()</code> → run the function now</p>
</li>
<li><p><code>apply()</code> → run the function now with array arguments</p>
</li>
<li><p><code>bind()</code> → create a new function for later use</p>
</li>
</ul>
<h3>Assignment</h3>
<p>Try implementing the following small exercise.</p>
<p>Create an object with a method that uses <code>this</code>.</p>
<p>Example structure:</p>
<pre><code class="language-plaintext">const student = {
    name: "Arjun",
    showName: function () {
        console.log(this.name);
    }
};
</code></pre>
<p>Create another object.</p>
<pre><code class="language-plaintext">const student2 = {
    name: "Meera"
};
</code></pre>
<p>Tasks:</p>
<p>Use <code>call()</code> to borrow the <code>showName</code> method.</p>
<p>Use <code>apply()</code> to pass arguments using an array.</p>
<p>Use <code>bind()</code> and store the returned function in a variable, then execute it.</p>
<p>These small experiments help build intuition about how <code>this</code> it behaves.</p>
<h3>Function and Caller</h3>
<p>You can visualize the concept like this.</p>
<pre><code class="language-plaintext">Object --------&gt; Function
   |                |
   | calls          |
   ↓                |
 person.greet()     |
        this → person
</code></pre>
<p>This shows that the caller decides what <code>this</code> it refers to.</p>
<h3>Conclusion</h3>
<p>Understanding <code>this</code> in JavaScript becomes much easier when you think about <strong>who is calling the function</strong>.</p>
<p>If a function is called normally, <code>this</code> usually refers to the global context. If it is called by an object, <code>this</code> refers to that object.</p>
<p>Methods like <code>call()</code>, <code>apply()</code>, and <code>bind()</code> give us control over the value of <code>this</code>. They allow functions to be reused across different objects, which improves flexibility and code reuse.</p>
]]></content:encoded></item><item><title><![CDATA[Object-Oriented Programming in JavaScript: Classes and Objects Explained]]></title><description><![CDATA[In this article, we are going to learn about Object-Oriented Programming (OOP) in JavaScript and how this programming approach helps us to write code in a manner that can improve the overall readabili]]></description><link>https://blog.studyhex.in/object-oriented-programming-in-javascript-classes-and-objects-explained</link><guid isPermaLink="true">https://blog.studyhex.in/object-oriented-programming-in-javascript-classes-and-objects-explained</guid><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Sun, 15 Mar 2026 16:51:53 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/b66d0c25-22cf-478d-a362-6636f212d108.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, we are going to learn about Object-Oriented Programming (OOP) in JavaScript and how this programming approach helps us to write code in a manner that can improve the overall readability, reusability, and maintainability.</p>
<h3>What is Object-Oriented Programming (OOP)</h3>
<p>It's a programming paradigm where we structure our program using objects that represent real-world entities. In this approach, without writing multiple variables and function which ultimately make our code verbose use <strong>built-in</strong> classes that group related data and write methods to build encapsulation.</p>
<p>For example</p>
<p>A <strong>Car</strong> has properties like:</p>
<ul>
<li><p>brand</p>
</li>
<li><p>color</p>
</li>
<li><p>speed</p>
</li>
</ul>
<p>And behaviors like:</p>
<ul>
<li><p>start()</p>
</li>
<li><p>stop()</p>
</li>
<li><p>accelerate()</p>
</li>
</ul>
<p>OOP helps us model such real-world structures directly in code.</p>
<h3>How Object-Oriented Programming Works</h3>
<p>To understand OOPS better, let's take a simple analogy. Think about a car blueprint used in a factory.</p>
<p>It mostly describes these two things.</p>
<ul>
<li><p>What properties the car will have (eg, color, engine, wheels)</p>
</li>
<li><p>What behaviour can it perform? (eg. start, stop, accelerate)</p>
</li>
</ul>
<p>But this is a blueprint, not a car. But all cars must have these features.</p>
<p>So we can say that the class is the blueprint and the object is the actual implementation of that blueprint.</p>
<pre><code class="language-javascript">        Car Blueprint (Class)
               |
      -----------------------
      |          |          |
   Car1       Car2       Car3
 (Object)    (Object)    (Object)
</code></pre>
<h2>What is a Class in JavaScript</h2>
<p>In object-oriented programming, we wrap code around classes, which act like a blueprint.</p>
<p>In classes, we have Properties and methods.</p>
<ol>
<li><p>Properties are data</p>
</li>
<li><p>Methods are functions.</p>
</li>
</ol>
<h3>Syntax</h3>
<pre><code class="language-javascript">class ClassName {
    constructor() {
        // initialize properties
    }

    methodName() {
        // behavior
    }
}
</code></pre>
<blockquote>
<p>Note: The class does not create object automatically. When we create a instance of the class then the object is actually created.</p>
</blockquote>
<h2>Creating Objects Using Classes</h2>
<p>To create objects from a class, we use the <strong>new</strong> keyword.</p>
<p>Example</p>
<pre><code class="language-javascript">class Car {
    constructor(brand, color) {
        this.brand = brand;
        this.color = color;
    }

    start() {
        console.log(`${this.brand} car started`);
    }
}

// Creating Object
let car1 = new Car("Toyota", "Red");
let car2 = new Car("BMW", "Black");

car1.start(); // Toyota car started
car2.start(); // BMW car started
</code></pre>
<blockquote>
<p>Here Car is the blueprint and car1 and car2 is the Object.</p>
</blockquote>
<h3>Constructor Method</h3>
<p>The constructor is a special method that can only be used in a class. It automatically runs when a new object is created. It is used to initialize properties of the object.</p>
<p>Example:</p>
<pre><code class="language-javascript">class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
        console.log(`Constructor called for ${this.name}`);
    }
}

// Creation time
let p1 = new Person("Rahul", 25);
let p2 = new Person("Anita", 30);

console.log(p1.name); // Rahul
console.log(p2.age);  // 30

/* Output
Constructor called for Rahul
VM64:5 Constructor called for Anita
VM64:13 Rahul
VM64:14 30
*/
</code></pre>
<p>The constructor helps us avoid manually assigning properties for each object.</p>
<h3>Methods Inside a Class</h3>
<p>Methods are the functions that define the behaviour of objects.</p>
<p>Example</p>
<pre><code class="language-javascript">class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    introduce() {
        console.log(`Hello, my name is \({this.name} and I am \){this.age} years old.`);
    }
}

// Method usages
let person1 = new Person("Amit", 22);
let person2 = new Person("Riya", 24);

person1.introduce();
person2.introduce();
</code></pre>
<blockquote>
<p>Note: Each object has different reference in memory, so one object properties is different from another object's properties.</p>
</blockquote>
<p>Example</p>
<pre><code class="language-javascript">class Student {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    printDetails() {
        console.log(`Student Name: ${this.name}`);
        console.log(`Student Age: ${this.age}`);
    }
}
let student1 = new Student("Arjun", 20);
let student2 = new Student("Meera", 19);

student1.printDetails();
student2.printDetails();

/* Output
Student Name: Arjun
Student Age: 20

Student Name: Meera
Student Age: 19
*/
</code></pre>
<p>Here we wrote the logic once but used it multiple times. An advanced usage case is LinkedList, where we create a node class, then use it every time to insert another node.</p>
<h3>What is Encapsulation</h3>
<p>Encapsulation means <strong>bundling data and methods together inside a single unit (class).</strong></p>
<p>Instead of writing separate variables and functions across the program, we organize them inside the class.</p>
<p>Encapsulation helps us:</p>
<ul>
<li><p>Keep code organized</p>
</li>
<li><p>Protect data</p>
</li>
<li><p>Control how data is accessed</p>
</li>
</ul>
<p>For example:</p>
<pre><code class="language-javascript">class BankAccount {
    constructor(owner, balance) {
        this.owner = owner;
        this.balance = balance;
    }

    deposit(amount) {
        this.balance += amount;
    }

    withdraw(amount) {
        this.balance -= amount;
    }
}
</code></pre>
<p>Here:</p>
<ul>
<li><p><strong>Balance is data</strong></p>
</li>
<li><p><strong>deposit() and withdraw() are methods</strong></p>
</li>
</ul>
<p>Both are wrapped inside the same class.</p>
<p>This structure improves code maintainability and security.</p>
<h3>Assignment</h3>
<p>Try implementing the following exercise.</p>
<h3>Task</h3>
<p>Create a class called <strong>Student</strong>.</p>
<p>Requirements:</p>
<ul>
<li><p>Add properties:</p>
<ul>
<li><p>name</p>
</li>
<li><p>age</p>
</li>
</ul>
</li>
<li><p>Add a method:</p>
<ul>
<li>printDetails()</li>
</ul>
</li>
</ul>
<h3>Example Implementation</h3>
<pre><code class="language-javascript">class Student {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    printDetails() {
        console.log(`\({this.name} is \){this.age} years old`);
    }
}

let s1 = new Student("Rahul", 21);
let s2 = new Student("Priya", 20);

s1.printDetails();
s2.printDetails();
</code></pre>
<p>Try creating <strong>multiple student objects</strong> to understand how classes work.</p>
<h3>Diagram Ideas for This Topic</h3>
<p>You can include these diagrams in the blog for better understanding.</p>
<h3>Blueprint to Object</h3>
<pre><code class="language-plaintext">     Car Blueprint (Class)

        ↓       ↓       ↓

      Car1    Car2    Car3
     (Object) (Object) (Object)
</code></pre>
<h3>Class to Instance Relationship</h3>
<pre><code class="language-plaintext">Class: Student
   |
   |--- student1
   |--- student2
   |--- student3
</code></pre>
<p>These visuals help beginners quickly understand the concept.</p>
<h3>Conclusion</h3>
<p>Object-Oriented Programming is a powerful way to structure code.</p>
<p>By using <strong>classes and objects</strong>, we can write programs that are easier to understand, reuse, and maintain.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Operators Explained — Arithmetic, Logical, Compare]]></title><description><![CDATA[When we write JavaScript programs, we often need to perform operations on values. Sometimes we add numbers, sometimes we compare values, and sometimes we check conditions to decide what should happen ]]></description><link>https://blog.studyhex.in/javascript-operators-explained-arithmetic-logical-compare</link><guid isPermaLink="true">https://blog.studyhex.in/javascript-operators-explained-arithmetic-logical-compare</guid><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Sun, 15 Mar 2026 14:13:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/3b9f65dd-70ab-4aca-a085-970fbdcc462c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When we write JavaScript programs, we often need to perform operations on values. Sometimes we add numbers, sometimes we compare values, and sometimes we check conditions to decide what should happen next in the program.</p>
<p>This is where <strong>operators</strong> come into play.</p>
<p>In simple terms, <strong>operators are symbols that perform operations on values or variables</strong>.</p>
<p>For example:</p>
<pre><code class="language-plaintext">let sum = 5 + 3;
</code></pre>
<p>Here <code>+</code> is an operator that adds two numbers.</p>
<p>JavaScript provides many types of operators, but in this article we will focus on the most commonly used ones:</p>
<ul>
<li><p>Arithmetic Operators</p>
</li>
<li><p>Comparison Operators</p>
</li>
<li><p>Logical Operators</p>
</li>
<li><p>Assignment Operators</p>
</li>
</ul>
<p>Let’s understand each one step by step.</p>
<hr />
<h2>Arithmetic Operators</h2>
<p>Arithmetic operators are used to perform <strong>mathematical calculations</strong>.</p>
<p>These are the most basic operators you will use while working with numbers.</p>
<table>
<thead>
<tr>
<th>Operator</th>
<th>Meaning</th>
</tr>
</thead>
<tbody><tr>
<td><code>+</code></td>
<td>Addition</td>
</tr>
<tr>
<td><code>-</code></td>
<td>Subtraction</td>
</tr>
<tr>
<td><code>*</code></td>
<td>Multiplication</td>
</tr>
<tr>
<td><code>/</code></td>
<td>Division</td>
</tr>
<tr>
<td><code>%</code></td>
<td>Modulus (Remainder)</td>
</tr>
</tbody></table>
<h3>Example</h3>
<pre><code class="language-plaintext">let a = 10;
let b = 5;

console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2
console.log(a % b); // 0
</code></pre>
<h3>Understanding Modulus <code>%</code></h3>
<p>The modulus operator returns the <strong>remainder of a division</strong>.</p>
<p>Example:</p>
<pre><code class="language-plaintext">console.log(10 % 3); // 1
</code></pre>
<p>Because:</p>
<pre><code class="language-plaintext">10 ÷ 3 = 3 remainder 1
</code></pre>
<p>This operator is very useful when checking if a number is <strong>even or odd</strong>.</p>
<p>Example:</p>
<pre><code class="language-plaintext">let number = 8;

if (number % 2 === 0) {
  console.log("Even number");
} else {
  console.log("Odd number");
}
</code></pre>
<hr />
<h2>Comparison Operators</h2>
<p>Comparison operators are used to <strong>compare two values</strong>.</p>
<p>They always return a <strong>boolean value</strong>:</p>
<ul>
<li><p><code>true</code></p>
</li>
<li><p><code>false</code></p>
</li>
</ul>
<p>Here are some common comparison operators:</p>
<table>
<thead>
<tr>
<th>Operator</th>
<th>Meaning</th>
</tr>
</thead>
<tbody><tr>
<td><code>==</code></td>
<td>Equal (loose comparison)</td>
</tr>
<tr>
<td><code>===</code></td>
<td>Strict equal</td>
</tr>
<tr>
<td><code>!=</code></td>
<td>Not equal</td>
</tr>
<tr>
<td><code>&gt;</code></td>
<td>Greater than</td>
</tr>
<tr>
<td><code>&lt;</code></td>
<td>Less than</td>
</tr>
</tbody></table>
<h3>Example</h3>
<pre><code class="language-plaintext">let a = 10;
let b = 5;

console.log(a &gt; b);  // true
console.log(a &lt; b);  // false
console.log(a == b); // false
</code></pre>
<hr />
<h3>The Difference Between <code>==</code> and <code>===</code></h3>
<p>This is one of the <strong>most important things beginners should understand</strong>.</p>
<h4><code>==</code> (Loose Equality)</h4>
<p><code>==</code> compares values <strong>after converting their types if necessary</strong>.</p>
<p>Example:</p>
<pre><code class="language-plaintext">console.log(5 == "5"); 
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">true
</code></pre>
<p>Because JavaScript converts <code>"5"</code> (string) into a number before comparison.</p>
<hr />
<h4><code>===</code> (Strict Equality)</h4>
<p><code>===</code> compares <strong>both value and type</strong>.</p>
<p>Example:</p>
<pre><code class="language-plaintext">console.log(5 === "5");
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">false
</code></pre>
<p>Because:</p>
<pre><code class="language-plaintext">5 is a number
"5" is a string
</code></pre>
<p>They are not the same type.</p>
<hr />
<h3>Quick Comparison Table</h3>
<table>
<thead>
<tr>
<th>Expression</th>
<th>Result</th>
</tr>
</thead>
<tbody><tr>
<td><code>5 == "5"</code></td>
<td>true</td>
</tr>
<tr>
<td><code>5 === "5"</code></td>
<td>false</td>
</tr>
<tr>
<td><code>10 &gt; 7</code></td>
<td>true</td>
</tr>
<tr>
<td><code>3 &lt; 1</code></td>
<td>false</td>
</tr>
</tbody></table>
<p>In most modern JavaScript code, developers prefer <code>===</code> <strong>because it avoids unexpected type conversions</strong>.</p>
<hr />
<h2>Logical Operators</h2>
<p>Logical operators are used when we want to <strong>combine multiple conditions</strong>.</p>
<p>They are commonly used inside <strong>if statements</strong>.</p>
<table>
<thead>
<tr>
<th>Operator</th>
<th>Meaning</th>
</tr>
</thead>
<tbody><tr>
<td><code>&amp;&amp;</code></td>
<td>AND</td>
</tr>
<tr>
<td>`</td>
<td></td>
</tr>
<tr>
<td><code>!</code></td>
<td>NOT</td>
</tr>
</tbody></table>
<hr />
<h3>Logical AND <code>&amp;&amp;</code></h3>
<p>The AND operator returns <strong>true only if both conditions are true</strong>.</p>
<p>Example:</p>
<pre><code class="language-plaintext">let age = 20;
let hasID = true;

if (age &gt;= 18 &amp;&amp; hasID) {
  console.log("You can enter");
}
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">You can enter
</code></pre>
<p>Both conditions are true.</p>
<hr />
<h3>Logical OR <code>||</code></h3>
<p>The OR operator returns <strong>true if at least one condition is true</strong>.</p>
<p>Example:</p>
<pre><code class="language-plaintext">let isAdmin = false;
let isEditor = true;

if (isAdmin || isEditor) {
  console.log("Access granted");
}
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Access granted
</code></pre>
<p>Only one condition needs to be true.</p>
<hr />
<h3>Logical NOT <code>!</code></h3>
<p>The NOT operator <strong>reverses a boolean value</strong>.</p>
<p>Example:</p>
<pre><code class="language-plaintext">let isLoggedIn = false;

console.log(!isLoggedIn);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">true
</code></pre>
<p>Because NOT flips the value.</p>
<hr />
<h3>Logical Operator Truth Table</h3>
<p>| A | B | A &amp;&amp; B | A || B |
| --- | --- | --- | --- |
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |</p>
<p>This table helps understand how logical operators behave with different combinations.</p>
<hr />
<h2>Assignment Operators</h2>
<p>Assignment operators are used to <strong>assign values to variables</strong>.</p>
<p>The most basic one is:</p>
<pre><code class="language-plaintext">=
</code></pre>
<p>Example:</p>
<pre><code class="language-plaintext">let number = 10;
</code></pre>
<p>Here we assign the value <code>10</code> to the variable <code>number</code>.</p>
<hr />
<h3>Other Assignment Operators</h3>
<p>JavaScript also provides <strong>shortcut assignment operators</strong>.</p>
<table>
<thead>
<tr>
<th>Operator</th>
<th>Example</th>
<th>Meaning</th>
</tr>
</thead>
<tbody><tr>
<td><code>+=</code></td>
<td><code>x += 5</code></td>
<td>x = x + 5</td>
</tr>
<tr>
<td><code>-=</code></td>
<td><code>x -= 3</code></td>
<td>x = x - 3</td>
</tr>
</tbody></table>
<hr />
<h3>Example</h3>
<pre><code class="language-plaintext">let score = 10;

score += 5;
console.log(score);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">15
</code></pre>
<p>Because:</p>
<pre><code class="language-plaintext">score = score + 5
</code></pre>
<p>Another example:</p>
<pre><code class="language-plaintext">let points = 20;

points -= 4;

console.log(points);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">16
</code></pre>
<hr />
<h2>Small Practice Assignment</h2>
<p>To understand operators better, try this small exercise.</p>
<h3>1. Perform Arithmetic Operations</h3>
<pre><code class="language-plaintext">let a = 12;
let b = 4;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
</code></pre>
<hr />
<h3>2. Compare Values</h3>
<pre><code class="language-plaintext">console.log(5 == "5");
console.log(5 === "5");
</code></pre>
<p>Observe the difference carefully.</p>
<hr />
<h3>3. Logical Condition Example</h3>
<pre><code class="language-plaintext">let age = 22;
let hasTicket = true;

if (age &gt;= 18 &amp;&amp; hasTicket) {
  console.log("You can watch the movie");
}
</code></pre>
<hr />
<h2>Final Thoughts</h2>
<p>Operators are one of the <strong>core building blocks of JavaScript</strong>.</p>
<p>They allow us to:</p>
<ul>
<li><p>Perform calculations</p>
</li>
<li><p>Compare values</p>
</li>
<li><p>Combine conditions</p>
</li>
<li><p>Assign values to variables</p>
</li>
</ul>
<p>Once you understand operators, writing logic in JavaScript becomes much easier.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Objects in JavaScript]]></title><description><![CDATA[Overview
In this article, we are trying to understand the Object in JavaScript, how it help use to store real word entity into program.
Why do we need Objects?
To understand the need of Object, let's ]]></description><link>https://blog.studyhex.in/understanding-objects-in-javascript</link><guid isPermaLink="true">https://blog.studyhex.in/understanding-objects-in-javascript</guid><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Sun, 15 Mar 2026 12:50:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/c980083d-48ae-402b-9f66-24879c066076.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3>Overview</h3>
<p>In this article, we are trying to understand the Object in JavaScript, how it help use to store real word entity into program.</p>
<h3>Why do we need Objects?</h3>
<p>To understand the need of Object, let's take an example. Assume there is a librarian and they want to store some books, so in the traditional method try to do it</p>
<pre><code class="language-javascript">let book1Title = "Harry Potter";
let book1Author = "J.K. Rowling";
let book1Pages = 309;

let book2Title = "The Hobbit";
let book2Author = "J.R.R. Tolkien";
let book2Pages = 310;
</code></pre>
<p>As you can see, to store 2 books, we need to declare 6 variables. Now you may ask what the problem is.</p>
<p>The key problem here is the creation of multiple variables and verbose code. To fix that, we can use objects in JavaScript.</p>
<h3>What is Object</h3>
<p>An object is a way to store values with their corresponding keys. That value may be anything; it may be any data type. But the key is only a string or a symbol. Object is non-primitive, and it is stored and copied by reference.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">let obj_name={
    key1: "value",
    key2: 167,
    key3:[1,2,3,4],
    key4:{
        another_key:"value45"
    }
}
console.log(obj_name)
console.log(obj_name.key1) // It return of value of key1
console.log(obj_name["key1"] // Another method to see the value of key1 
console.log(obj_name.key4.another_key) // Nested values 
</code></pre>
<blockquote>
<p>Note: If you want more knowledge about data types, read this article.</p>
</blockquote>
<pre><code class="language-javascript">let book1 = {
  title: "Harry Potter",
  author: "J.K. Rowling",
  pages: 309
};
let book2 = {
  title: "The Hobbit",
  author: "J.R.R. Tolkien",
  pages: 310
};
</code></pre>
<img src="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/0be6be46-f8a1-4e7e-b7ef-6c2766225553.png" alt="" style="display:block;margin:0 auto" />

<p>As we can see, we just need 2 variables to store 2 books, and objects are easy to understand and less verbose.</p>
<p>Now, let's take a few examples to understand how to insert and read values from an object.</p>
<h3>Example 1</h3>
<pre><code class="language-javascript">// Example 1: Adding a new key-value pair
let student = {
    name: "John",
    age: 20
};
// Add new key
student.grade = "A"; // or: student["grade"] = "A";

// Example 2: Modifying a value
student.age = 21; // change age from 20 to 21

// Example 3: Deleting a key
delete student.grade; // removes "grade" property from student
</code></pre>
<h3>Example 2</h3>
<pre><code class="language-javascript">// Example 4: Input value from user (prompt - works in browser environment)
let car = {};
car.brand = "Toyota"; 
car.model = "Corolla"; 
car.year = 2022; 

// Example 5: Access and modify using bracket notation
car["color"] = "Red";
car["year"] = 2023;

// Example 6: Deleting key with bracket notation
delete car["color"];

// Example 7: Looping through object keys and showing values
for (let key in car) {
    console.log(key + ": " + car[key]);
}

// Example 8: Check if a key exists
if ("brand" in car) {
    console.log("Brand exists in car object");
}

// Example 9: Get all keys and values
let keys = Object.keys(student); // ["name", "age"]
let values = Object.values(student); // ["John", 21]
</code></pre>
<h3>Example 3: For in Loop</h3>
<p>We can run a <code>for in</code> loop for an object which run for all</p>
<pre><code class="language-javascript">// Example: Using for...in loop with a fresh object
let book = {
    title: "Atomic Habits",
    author: "James Clear",
    year: 2018,
    price: 350
};

for (let key in book) {
    console.log(key + ": " + book[key]);
}
// Output 
// title: Atomic Habits
// author: James Clear
// year: 2018
// price: 350
</code></pre>
<h3>Some Important Methods of Object</h3>
<p>1. HashOwnProperty / Object.hasOwn()</p>
<p>When working with objects in JavaScript, sometimes we need to check whether a property <strong>actually belongs to that object</strong> or if it is coming from the <strong>prototype chain</strong>.</p>
<p>To understand this, let’s look at a simple example.</p>
<pre><code class="language-javascript">const user = {
  name: "Pallab",
};

console.log(user.name); // Pallab
</code></pre>
<p>Now, suppose we try to access something like this:</p>
<pre><code class="language-javascript">console.log(user.toString());
</code></pre>
<p>Even though <code>toString</code> It is not inside our object; JavaScript still finds it.</p>
<p>That is because <code>toString</code> it comes from <code>Object.prototype</code>.</p>
<p>So if we want to check whether a property <strong>really belongs to the object itself</strong>, we can use <code>hasOwnProperty</code>.</p>
<pre><code class="language-javascript">const user = {
  name: "Pallab",
};

console.log(user.hasOwnProperty("name")); // true
console.log(user.hasOwnProperty("toString")); // false
</code></pre>
<p>This method returns <strong>true only if the property exists directly inside the object</strong>.</p>
<p>However, modern JavaScript introduced a safer alternative:</p>
<pre><code class="language-javascript">Object.hasOwn(user, "name");
</code></pre>
<h3>Why is this safer?</h3>
<p>Because sometimes an object may <strong>override</strong> <code>hasOwnProperty</code>.</p>
<p>Example:</p>
<pre><code class="language-javascript">const obj = {
  hasOwnProperty: () =&gt; false,
  name: "JS",
};

console.log(obj.hasOwnProperty("name")); // false wrong
</code></pre>
<p>To avoid this issue, we use:</p>
<pre><code class="language-javascript">Object.hasOwn(obj, "name"); // true
</code></pre>
<p>So the key idea is:</p>
<p><code>Object.hasOwn()</code> safely checks if a property exists directly on an object.</p>
<h2>2. Structured Clone</h2>
<p>In JavaScript, sometimes we need to <strong>create a completely independent copy of complex data</strong>, including nested objects, arrays, maps, sets, etc.</p>
<p>For this purpose, modern JavaScript provides a built-in function called <strong>structured cloning</strong>.</p>
<pre><code class="language-javascript">const original = {
  name: "Pallab",
  skills: ["JS", "Node"],
};

const copy = structuredClone(original);

copy.skills.push("React");

console.log(original.skills); // ["JS", "Node"]
console.log(copy.skills); // ["JS", "Node", "React"]
</code></pre>
<p>Here, both objects are completely independent.</p>
<p>Structured clone creates a <strong>deep clone automatically</strong>.</p>
<h2>Difference Between Object and Array in JavaScript</h2>
<p>Many beginners think that <strong>Array and Object are completely different data types</strong>.<br />But in JavaScript, the reality is slightly different.</p>
<p>An <strong>Array is actually a special implementation of an Object</strong>.</p>
<p>That means internally, arrays behave like objects, but they have <strong>additional behavior designed for ordered data</strong>.</p>
<h3>An array is internally an Object</h3>
<p>Consider the following example:</p>
<pre><code class="language-javascript">let arr = [10, 20, 30];
console.log(typeof arr);
// Output 
// Object
</code></pre>
<p>As we can see, JavaScript returns <code>"object"</code> instead of <code>"array"</code>.</p>
<p>This happens because <strong>arrays are implemented using objects internally</strong>.</p>
<p>JavaScript internally stores it similarly to this structure:</p>
<pre><code class="language-javascript">{
  0: 10,
  1: 20,
  2: 30,
  length: 3
}
</code></pre>
<img src="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/5e5bdc87-5eca-4bd0-8aa2-c21342829c82.png" alt="" style="display:block;margin:0 auto" />

<p>But JavaScript also gives some extra methods like map(), push(), pop(), etc., with arrays. So we can clearly see that an array is an advanced implementation of an array.</p>
<blockquote>
<p>If you want to know whether a data is an array or an object instead of using the typeof keyword, you should use the Array.isArray(PassYourArrayHere) for the final answer.</p>
</blockquote>
<h3><strong>Conclusion</strong></h3>
<p>We can use objects to organize related data using key-value pairs, making the code look cleaner and easier to manage compared to using multiple variables to hold related data. To effectively use objects in JavaScript programming, we need to understand object operations such as Object.hasOwn(), as well as concepts such as shallow copy, deep copy, and structured cloning.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Variables and Data Types in JavaScript]]></title><description><![CDATA[Overview
JavaScript was initially a lightweight scripting language used to provide interactivity to web pages. However, nowadays JavaScript is much more modern and has a great ecosystem. Because of th]]></description><link>https://blog.studyhex.in/understanding-variables-and-data-types-in-javascript</link><guid isPermaLink="true">https://blog.studyhex.in/understanding-variables-and-data-types-in-javascript</guid><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Sun, 15 Mar 2026 12:31:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/83b8153d-38f8-4e83-ab17-7ed008ca682a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Overview</h2>
<p>JavaScript was initially a lightweight scripting language used to provide interactivity to web pages. However, nowadays JavaScript is much more modern and has a great ecosystem. Because of this, we can easily build highly scalable systems using it.</p>
<p>For any programming language, we usually follow a simple process: first, we provide some data, then the program processes that data, and finally, the program shows the output to the user.</p>
<p>If we cannot understand data and data types properly, we cannot write business logic effectively. So let's discuss data types and how JavaScript behaves with data.</p>
<h2>Data Types</h2>
<p>In real life, many things are happening around us, and our software needs to interact with the real world. Understanding real-life data and representing that data inside a computer is a challenge. For that reason, we need to understand data types.</p>
<p>In JavaScript, we mainly have <strong>two primary categories of data types</strong>:</p>
<ul>
<li><p>Primitive</p>
</li>
<li><p>Non-Primitive</p>
</li>
</ul>
<p>Each of these categories contains several specific data types.</p>
<h2>Primitive Data Types</h2>
<p>Primitive data types are basic types used to represent simple values from real life.</p>
<h3>String</h3>
<p>A <strong>string</strong> is a combination of multiple characters, such as the name of a person. Strings are usually written inside <strong>single quotes (' ')</strong> or <strong>double quotes (" ")</strong>.</p>
<p>Example:</p>
<pre><code class="language-javascript">"John"
'Hello World'
</code></pre>
<h3>Number</h3>
<p>The <strong>number</strong> type stores numeric values, such as the age of a person.</p>
<p>Example:</p>
<pre><code class="language-javascript">25
3.14
</code></pre>
<h3>Boolean</h3>
<p>A <strong>Boolean</strong> is used when we need to represent only two possible outcomes: <strong>true</strong> or <strong>false</strong>.</p>
<p>Example:</p>
<pre><code class="language-javascript">true
false
</code></pre>
<h3>BigInt</h3>
<p><strong>BigInt</strong> is used to store very large integers that cannot be safely represented by the normal number type. In JavaScript, we denote a BigInt by adding <code>n</code> at the end of the number.</p>
<p>Example:</p>
<pre><code class="language-javascript">124n
</code></pre>
<h3>Symbol</h3>
<p>A <strong>symbol</strong> is a unique and immutable value often used to create unique identifiers for object properties.</p>
<h3>Undefined</h3>
<p><strong>Undefined</strong> represents a value that has been declared but has not been assigned yet.</p>
<p>You can think of it like discovering an unknown animal in a zoo. At first, you do not know anything about it, so it is undefined from your perspective. Once someone explains what it is, it is no longer undefined for you.</p>
<h3>Null</h3>
<p><strong>Null</strong> represents the intentional absence of any value. It is used when we explicitly want to indicate that something has <strong>no value</strong>.</p>
<h2>Non-Primitive Data Types</h2>
<p>Non-primitive data types are more complex and can store collections of values or more structured data.</p>
<h3>Objects</h3>
<p>An <strong>object</strong> in JavaScript is used to store <strong>multiple related values in a single variable</strong>.<br />These values are stored in <strong>key–value pairs</strong>.</p>
<p>You can think of an object like a <strong>real-life entity</strong>. For example, a <strong>person</strong> has many properties such as name, age, and city. Instead of creating separate variables, we store everything inside one object.</p>
<pre><code class="language-javascript">{
  name: "Rahul",
  age: 25,
  city: "Kolkata"
}
</code></pre>
<h3>Array</h3>
<p>An <strong>array</strong> is used to store <strong>multiple values in a single container</strong>. We can use it to store the marks of 100 students for a class.<br />Unlike objects, arrays store values using <strong>index numbers</strong> instead of keys.</p>
<p>In an array <strong>index mostly starts from</strong> <code>0</code>.</p>
<pre><code class="language-plaintext">marks [34,45,65,74]
roll   1  2  3  4   (This is denoted as index)
</code></pre>
<p>Now we are aware of data that can represent more than 70% usecases. Now it's time to understand the connection between variables in JavaScript.</p>
<h3>Variables</h3>
<p>Variables are like a container where we can store the data that we discussed now. So it's like in a household there is a container storing sugar, rice, variable is saminar like those containers. Now let's see how JavaScript actually handles them.</p>
<h3>syntax</h3>
<pre><code class="language-javascript">//keyword variable_name assignment opeartor value
let variable_name="Value";
let variable2 = 2323;
</code></pre>
<p>We have 3 keywords in JavaScript to declare a variable</p>
<ol>
<li><p>Var (Mostly avoided these days by devs)</p>
</li>
<li><p>Let</p>
</li>
<li><p>Const (Used to declare a constant)</p>
</li>
</ol>
<p>Now, let's see some JavaScript code snippets of various data types</p>
<h3>Number in JavaScript</h3>
<pre><code class="language-javascript">let decimalNumber=69
let hexadecimalNumber=0xff; // 0x____ it automatically converted into hex number
let binaryNumber=0b101; // 0b____ it automatically converted into binary
let exponetialNumber=1e5; // 1*10^5

console.log(decimalNumber, hexadecimalNumber, binaryNumber, exponetialNumber);

// Output
// 69 255 5 100000
</code></pre>
<h3>Strings in JavaScript</h3>
<pre><code class="language-javascript">let singleQuoteString = 'Hello World';
let doubleQuoteString = "JavaScript";
let templateString = `Learning JS`;
let escapedString = "He said \"Hello\""; // using escape character

// variables for interpolation
let name = "Pallab";
let age = 23;

let interpolatedString = `My name is \({name} and I am \){age} years old`;
// here you can put variable and expression between string
console.log(
  singleQuoteString,
  doubleQuoteString,
  templateString,
  escapedString,
  interpolatedString
);

// Output
// Hello World JavaScript Learning JS He said "Hello" My name is Pallab and I am 23 years old
</code></pre>
<h3>Boolean in JavaScript</h3>
<pre><code class="language-javascript">let hasAccess=false; // This Stores whether a user has access or not
let isLoggedIn=true; // This stores wheather a user is loggedin or not

// Based on that you can write bussiness logic
if(isLoggedIn &amp;&amp; hasAccess){
    console.log("Access Granted");
}
else{
    console.log("Access Granted");
}
</code></pre>
<h3>BigInt in JavaScript</h3>
<p>To store bigint data in JavaScript, add an extra n at the end. Now JavaScript treats that as a bigint.</p>
<pre><code class="language-javascript">let bigIntValue=125n;
console.log(typeof bigIntValue);
// Output is bigint
</code></pre>
<h3>Symbols in JavaScript</h3>
<h3>Undefined in JavaScript</h3>
<p>Undefined means that at that point variable is storing nothing. So in that case, we don't know the type of that variable.</p>
<pre><code class="language-javascript">let value;
console.log(value, typeof value);
// Output undefined 'undefined'

let value2;
console.log(value2, typeof value2);
// Output undefined 'undefined'

value2=5;
console.log(value2, typeof value2);
// Output 5 'number'
</code></pre>
<h3>Null in JavaScript</h3>
<pre><code class="language-javascript">let value=null;
console.log(value, typeof null);
// Output null 'object'
</code></pre>
<p>In this example, as you can see, we have a variable named <code>value</code> holding <code>null</code>, which means nothing. During console log, we got null, and typeof that variable should be null, but it gives object because it is a JavaScript production bag, it is there to provide backward compatibility.</p>
<h3>Array in JavaScript</h3>
<p>An <strong>array</strong> in JavaScript is used to store multiple values inside a single variable.</p>
<h3>Array Literal (Most Common)</h3>
<pre><code class="language-javascript">let numbers = [1, 2, 3, 4, 5];
let fruits = ["Apple", "Banana", "Mango"];

console.log(numbers);
console.log(fruits);
</code></pre>
<h3>Using the Array Constructor</h3>
<p>JavaScript also provides a built-in <code>Array</code> constructor that can be used to create arrays.</p>
<pre><code class="language-javascript">let numbers = new Array(1, 2, 3, 4, 5);
console.log(numbers);
</code></pre>
<h3>Object in JavaScript</h3>
<p>An object is used to store the real-world entity.</p>
<h3>Using Object Literal</h3>
<pre><code class="language-javascript">let person = {
  name: "Rahul",
  age: 25,
  city: "Kolkata"
};

console.log(person);
</code></pre>
<h3>Using the Object Constructor</h3>
<pre><code class="language-javascript">let person = new Object();

person.name = "Rahul";
person.age = 25;
person.city = "Kolkata";

console.log(person);
</code></pre>
<h3>Using <code>Object.create()</code></h3>
<pre><code class="language-javascript">let personPrototype = {
  greet() {
    console.log("Hello!");
  }
};

let person = Object.create(personPrototype);

person.name = "Rahul";
person.age = 25;

console.log(person.name);
person.greet();
</code></pre>
<h3>Using Constructor Functions</h3>
<pre><code class="language-javascript">function Person(name, age, city) {
  this.name = name;
  this.age = age;
  this.city = city;
}

let person1 = new Person("Rahul", 25, "Kolkata");
let person2 = new Person("Amit", 28, "Delhi");

console.log(person1);
console.log(person2);
</code></pre>
<h3>Using ES6 Classes</h3>
<pre><code class="language-javascript">class Person {
  constructor(name, age, city) {
    this.name = name;
    this.age = age;
    this.city = city;
  }
}

let person1 = new Person("Rahul", 25, "Kolkata");
console.log(person1);
</code></pre>
<p>Now we know how to create a variable that can store data of various data types.</p>
<p>Now we have different keywords like var, let, and const. Now you may ask when to use which keyword.</p>
<h2>Difference between Var, Let, and Const</h2>
<p>To understand the difference between this 3 keyword, we have to understand the hoisting first.</p>
<h2><strong>Basic Idea of Hoisting</strong></h2>
<p>In JavaScript, Hosting is the concept where the JavaScript interpreter moves the function declaration, variables, classes, or imports to the top of their scope before executing the code.</p>
<p>To understand this, let's take a few examples</p>
<p>Example 1:</p>
<pre><code class="language-javascript">console.log(variable1); // Line 1
var variable1="holding a string"; // Line 2
console.log(variable1); // Line 3

// Instead of throwing a error the output looks like this
// undefined
// holding a string
</code></pre>
<p>As you can see here, instead of throwing errors in line 1, it says undefined because <code>variable1</code> The value is still not defined, but on line 2, the value is defined. <code>it's holding a string</code>, so when we use that in line 3, we get our expected output.</p>
<p>Example 2:</p>
<pre><code class="language-javascript">console.log(value1)
</code></pre>
<p>Now, in this example, you can clearly see that there is no variable named value1, so it throws an error <code>value1 is not defined.</code></p>
<h3><strong>Function Declaration Hoisting Example</strong></h3>
<pre><code class="language-javascript">sayHello(); // Works

function sayHello() {
  console.log("Hello!");
}

// Output 
// Hello!
</code></pre>
<p>This happens because <strong>function declarations are fully hoisted to the top of their scope</strong> during the creation phase of execution.</p>
<blockquote>
<p><em><strong>Note that function expression are not hoisted</strong></em></p>
</blockquote>
<pre><code class="language-javascript">sayHi(); // Error

const sayHi = function () {
  console.log("Hi!");
};

// Output Error
// sayHi is not defined
</code></pre>
<p>Now we are ready to understand the difference between Var, Let &amp; Const. Let's take a few examples</p>
<p>Example 1</p>
<p>When we use <strong>var</strong></p>
<pre><code class="language-javascript">console.log(a); // undefined

var a = 10;

console.log(a); // 10
</code></pre>
<p>Output</p>
<pre><code class="language-javascript">undefined
10
</code></pre>
<p>Here, the variable <strong>a</strong> is hoisted to the top of the scope, but only the declaration is hoisted, not the value.</p>
<p>So internally JavaScript treats it like this:</p>
<pre><code class="language-javascript">var a;

console.log(a); // undefined

a = 10;

console.log(a); // 10
</code></pre>
<p>This is why we get <strong>undefined instead of an error</strong>.</p>
<h3>var is Function Scoped</h3>
<p>Another important behavior of <strong>var</strong> is that it does not follow block scope.</p>
<p>Example</p>
<pre><code class="language-javascript">if (true) {
    var message = "Hello";
}

console.log(message);
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">Hello
</code></pre>
<p>Even though the variable was declared inside the <strong>if block</strong>, we can still access it outside.</p>
<p>This happens because <strong>var is function scoped</strong>, not block-scoped.</p>
<p>So the variable becomes available in the whole function or global scope.</p>
<h3>Problem with var</h3>
<p>Because <strong>var ignores block scope</strong>, it can create unexpected bugs in large programs.</p>
<p>Example</p>
<pre><code class="language-javascript">for (var i = 0; i &lt; 3; i++) {
    console.log(i);
}

console.log(i);
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">0
1
2
3
</code></pre>
<p>Here, the variable <strong>i</strong> still exists outside the loop.</p>
<p>This behavior is one of the reasons why modern JavaScript prefers <strong>let</strong> and <strong>const</strong>.</p>
<h3>let and const</h3>
<p>The <strong>let</strong> and <strong>const</strong> keywords were introduced in <strong>ES6 (ECMAScript 2015)</strong> to solve problems created by <strong>var</strong>.</p>
<p>Both <strong>let</strong> and <strong>const</strong> follow <strong>block scope</strong>.</p>
<hr />
<h3>let Example</h3>
<pre><code class="language-javascript">if (true) {
    let age = 25;
}

console.log(age);
</code></pre>
<p>Output</p>
<pre><code class="language-javascript">ReferenceError: age is not defined
</code></pre>
<p>Here, the variable <strong>age</strong> exists only inside the block.</p>
<p>Once the block ends, the variable is no longer accessible.</p>
<h3>Temporal Dead Zone (TDZ)</h3>
<p>The Temporal Dead Zone refers to the period between the entering of a scope and the actual declaration of a variable using let or const. During this period, the variable is "uninitialized", and accessing it will result in a ReferenceError.</p>
<ul>
<li><p>The TDZ starts from the beginning of the block until the variable is declared.</p>
</li>
<li><p>Variables declared with let and const are hoisted but not initialized.</p>
</li>
<li><p>Accessing the variable in the TDZ results in a ReferenceError.</p>
</li>
<li><p>var declarations do not have a TDZ and are initialized as undefined.</p>
</li>
</ul>
<p>Example</p>
<pre><code class="language-javascript">console.log(num);

let num = 10;
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">ReferenceError: Cannot access 'num' before initialization
</code></pre>
<p>Even though the variable is hoisted, JavaScript <strong>does not allow access before initialization</strong>.</p>
<p>This period is called the <strong>Temporal Dead Zone (TDZ)</strong>.</p>
<h3>Const Example</h3>
<p>The <strong>const</strong> keyword is similar to <strong>let</strong>, but the value cannot be reassigned.</p>
<p>Example</p>
<pre><code class="language-javascript">const pi = 3.14;

console.log(pi);
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">3.14
</code></pre>
<p>If we try to change it:</p>
<pre><code class="language-plaintext">pi = 3.14159;
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">TypeError: Assignment to constant variable
</code></pre>
<p>So <strong>const variables cannot be reassigned</strong>.</p>
<hr />
<h3>Const Must Be Initialized</h3>
<p>Unlike <strong>let</strong>, a <strong>const</strong> variable must be initialized at the time of declaration.</p>
<p>Example</p>
<pre><code class="language-javascript">const value;
</code></pre>
<p>Output</p>
<pre><code class="language-javascript">SyntaxError: Missing initializer in const declaration
</code></pre>
<p>Correct way:</p>
<pre><code class="language-javascript">const value = 10;
</code></pre>
<h3>Block Scope Example</h3>
<pre><code class="language-javascript">{
    let a = 5;
    const b = 10;
}

console.log(a); 
console.log(b);
</code></pre>
<p>Output</p>
<pre><code class="language-plaintext">ReferenceError
ReferenceError
</code></pre>
<p>Both <strong>let</strong> and <strong>const</strong> stay inside the block.<br />Quick Comparison</p>
<table>
<thead>
<tr>
<th>Feature</th>
<th>var</th>
<th>let</th>
<th>const</th>
</tr>
</thead>
<tbody><tr>
<td>Scope</td>
<td>Function</td>
<td>Block</td>
<td>Block</td>
</tr>
<tr>
<td>Hoisting</td>
<td>Yes (undefined)</td>
<td>Yes (TDZ)</td>
<td>Yes (TDZ)</td>
</tr>
<tr>
<td>Reassignment</td>
<td>Allowed</td>
<td>Allowed</td>
<td>Not Allowed</td>
</tr>
<tr>
<td>Initialization required</td>
<td>No</td>
<td>No</td>
<td>Yes</td>
</tr>
</tbody></table>
<p>Now that you understand the difference between <strong>var, let, and const</strong>, try completing the following tasks.</p>
<h2>Assignment: Try it yourself</h2>
<h3>Task 1: Declare Variables</h3>
<p>Create three variables:</p>
<ul>
<li><p><code>name</code></p>
</li>
<li><p><code>age</code></p>
</li>
<li><p><code>isStudent</code></p>
</li>
</ul>
<p>Use appropriate keywords (<code>var</code>, <code>let</code>, or <code>const</code>) to declare them.</p>
<p>Example starting point:</p>
<pre><code class="language-javascript">let name = "Your Name";
let age = 20;
const isStudent = true;
</code></pre>
<h3>Task 2: Print the Values</h3>
<p>Print all the variables using <code>console.log</code>.</p>
<p>Example:</p>
<pre><code class="language-javascript">console.log(name);
console.log(age);
console.log(isStudent);
</code></pre>
<h3>Task 3: Change Variable Values</h3>
<p>Try changing the values of the variables declared with <strong>let</strong> and <strong>const</strong>.</p>
<p>Example:</p>
<pre><code class="language-javascript">age = 21;       // Should work
isStudent = false; // Try this and observe what happens
</code></pre>
<p>Observe the behavior in the console.</p>
<h3>Task 4: Experiment with Hoisting</h3>
<p>Try accessing a variable before declaring it.</p>
<p>Example:</p>
<pre><code class="language-javascript">console.log(testVar);
var testVar = "Hello";
</code></pre>
<p>Now try the same with <strong>let</strong> or <strong>const</strong> and observe the difference.</p>
<h3>Conclusion:</h3>
<ul>
<li><p>JavaScript programs work by <strong>processing data and producing output</strong>.</p>
</li>
<li><p>We learned about <strong>primitive data types</strong> like <code>string</code>, <code>number</code>, <code>boolean</code>, <code>bigint</code>, <code>symbol</code>, <code>undefined</code>, and <code>null</code>.</p>
</li>
<li><p>We also explored <strong>non-primitive types,</strong> such as <strong>objects</strong> and <strong>arrays,</strong> used to store structured or multiple values.</p>
</li>
<li><p>Variables act as <strong>containers to store data</strong>, and JavaScript provides <strong>var, let, and const</strong> to declare them.</p>
</li>
<li><p>We understood the concept of <strong>hoisting</strong> and how it affects variable behavior.</p>
</li>
<li><p>In modern JavaScript, developers mostly prefer <strong>let and const</strong> for safer and more predictable code.</p>
</li>
</ul>
<p>These concepts form the <strong>foundation of JavaScript programming</strong> and are essential for writing clean and scalable applications.</p>
]]></content:encoded></item><item><title><![CDATA[Arrays in JavaScript: Beginner Guide]]></title><description><![CDATA[One of the first problems we encounter when learning programming is effectively managing multiple values. Consider a scenario in which you wish to store a student's grades across five distinct subject]]></description><link>https://blog.studyhex.in/arrays-in-javascript-beginner-guide</link><guid isPermaLink="true">https://blog.studyhex.in/arrays-in-javascript-beginner-guide</guid><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Sun, 15 Mar 2026 10:56:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/9b3278c8-b1dc-44d0-b322-f7b149c02c62.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>One of the first problems we encounter when learning programming is effectively managing multiple values. Consider a scenario in which you wish to store a student's grades across five distinct subjects. You might create five different variables if you don't have a systematic way to store them.</p>
<pre><code class="language-javascript">let math = 85;
let science = 90;
let english = 78;
let history = 88;
let geography = 92;
</code></pre>
<p>This strategy appears straightforward at first. However, as the number of values rises, issues begin to arise. What happens if you have to manage hundreds of tasks in a to-do list or store grades for fifty different subjects? It soon becomes messy and challenging to maintain and create distinct variables for every value.</p>
<p>Arrays are very helpful in this situation. We can store multiple values in a single variable while preserving their order using arrays. They facilitate the structured organization, access, and manipulation of data collections.</p>
<h2>What is an Array?</h2>
<p>An array is a collection of values stored in a specific order. Instead of creating many variables, we store multiple values inside a single structure.</p>
<p>You can think of an array like a list.</p>
<p>For example, suppose we want to store a list of fruits.</p>
<pre><code class="language-plaintext">Apple
Banana
Mango
Orange
Grapes
</code></pre>
<p>Instead of writing five separate variables, we can store all of them inside an array.</p>
<pre><code class="language-javascript">let fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"];
</code></pre>
<p>Now the variable <code>fruits</code> holds five values.</p>
<p>Arrays are helpful whenever we need to store a group of related values, such as:</p>
<ul>
<li><p>a list of fruits</p>
</li>
<li><p>marks of students</p>
</li>
<li><p>names of users</p>
</li>
<li><p>tasks in a to-do list</p>
</li>
</ul>
<blockquote>
<p>The key idea: whenever we want to store and access something sequentially, the array data structure is the most preferred to use.</p>
</blockquote>
<h2>Why Do We Need Arrays?</h2>
<p>To understand the importance of arrays, let's compare two approaches.</p>
<h3>Storing values individually</h3>
<pre><code class="language-javascript">let task1 = "Complete homework";
let task2 = "Buy groceries";
let task3 = "Call friend";
let task4 = "Read a book";
</code></pre>
<p>This works for small cases. But if we want to display all tasks, we must write separate code for each variable.</p>
<pre><code class="language-javascript">console.log(task1);
console.log(task2);
console.log(task3);
console.log(task4);
</code></pre>
<p>Now imagine there are 100 tasks. Managing them individually becomes difficult.</p>
<h3>Using an array</h3>
<pre><code class="language-javascript">let tasks = [
"Complete homework",
"Buy groceries",
"Call friend",
"Read a book"
];
</code></pre>
<p>All of this is now contained in a single variable named tasks. They are easily accessible or processed by us. The code becomes more structured and cleaner as a result.</p>
<h3>How to Create an Array</h3>
<p>In JavaScript, arrays are used to store multiple values in a single variable. They are ordered collections, which means every value inside the array has a specific position called an index.</p>
<p>There are two common ways to create an array in JavaScript. The first and most commonly used way is using square brackets <code>[]</code>. The second way is using the <code>new Array()</code> constructor.</p>
<h3>Creating an Array Using Square Brackets</h3>
<p>The simplest and most common way to create an array is by using square brackets.</p>
<p>Inside the brackets, values are separated by commas.</p>
<p>Example:</p>
<pre><code class="language-javascript">let numbers = [10, 20, 30, 40];
</code></pre>
<p>This creates an array called <code>numbers</code> that contains four elements.</p>
<p>You can visualize it like this:</p>
<pre><code class="language-plaintext">Index:  0   1   2   3
Value: 10  20  30  40
</code></pre>
<p>Each value has its own index, which allows JavaScript to access it quickly.</p>
<p>Another example:</p>
<pre><code class="language-javascript">let fruits = ["Apple", "Banana", "Mango"];
</code></pre>
<p>This array stores three fruit names.</p>
<p>Arrays can also contain different types of values.</p>
<p>Example:</p>
<pre><code class="language-javascript">let mixed = ["Hello", 25, true];
</code></pre>
<p>In this array:</p>
<ul>
<li><p><code>"Hello"</code> is a string</p>
</li>
<li><p><code>25</code> is a number</p>
</li>
<li><p><code>true</code> is a boolean</p>
</li>
</ul>
<p>Although JavaScript allows mixed types, in real programs, it is usually better to store similar kinds of values together.</p>
<p>Example using marks:</p>
<pre><code class="language-javascript">let marks = [85, 90, 78, 88, 92];
</code></pre>
<h3>Creating an Array Using the <code>new</code> Keyword</h3>
<p>Arrays can also be created using the <code>Array</code> constructor with the <code>new</code> keyword.</p>
<p>Example:</p>
<pre><code class="language-javascript">let fruits = new Array("Apple", "Banana", "Mango");
</code></pre>
<p>This also creates an array containing the same values.</p>
<p>Another example:</p>
<pre><code class="language-javascript">let numbers = new Array(10, 20, 30);
</code></pre>
<p>The result is the same as using brackets.</p>
<p>However, the constructor can behave differently when a single number is passed.</p>
<p>Example:</p>
<pre><code class="language-javascript">let arr = new Array(5);
</code></pre>
<p>This does <strong>not</strong> create <code>[5]</code>.</p>
<p>Instead, it creates an array with <strong>five empty slots</strong>.</p>
<pre><code class="language-plaintext">Index:  0   1   2   3   4
Value:  _   _   _   _   _
</code></pre>
<p>These slots exist but do not contain values yet.</p>
<p>Because of this behavior, most developers prefer using the square bracket syntax instead of the <code>Array</code> constructor.</p>
<h3>Difference Between <code>[]</code> and <code>new Array()</code></h3>
<p>Both methods create arrays, but there is one important difference.</p>
<p>If multiple values are passed, both behave the same.</p>
<pre><code class="language-plaintext">[1,2,3]          → normal array
new Array(1,2,3) → This also creates array and return that result
</code></pre>
<p>But when a <strong>single number</strong> is passed:</p>
<pre><code class="language-plaintext">[5]           → array containing value 5 // so it contain 1 element
new Array(5)  → empty array with length 5 // so it contains 5 element
</code></pre>
<h3>Arrays Are Reference Types</h3>
<p>Arrays in JavaScript are <strong>reference types</strong>. This means the variable does not directly hold the values. Instead, it holds a reference to a location in memory where the array is stored.</p>
<p>Example:</p>
<pre><code class="language-javascript">let arr1 = [10, 20, 30];
let arr2 = arr1;

arr2[0] = 100;

console.log(arr1);
// Output
// [100, 20, 30]
</code></pre>
<p>Even though we changed <code>arr2</code>, the value in <code>arr1</code> also changed. This happens because both variables refer to the <strong>same array in memory</strong>.</p>
<h3>Shallow Copy of Arrays</h3>
<p>A shallow copy creates a new array but copies the values from the original array.</p>
<p>One simple way to do this is using the spread operator.</p>
<p>Example:</p>
<pre><code class="language-javascript">let arr1 = [1, 2, 3];
let arr2 = [...arr1];

arr2[0] = 100;

console.log(arr1);
console.log(arr2);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">[1, 2, 3]
[100, 2, 3]
</code></pre>
<p>Here <code>arr1</code> remains unchanged because <code>arr2</code> is a separate array.</p>
<p>This works well for simple arrays containing primitive values like numbers or strings.</p>
<h3>Deep Copy of Arrays</h3>
<p>A deep copy means creating a completely independent copy of an array so that changes in one array do not affect the other.</p>
<p>When arrays contain nested arrays or objects, a normal copy is not enough because inner elements may still share references. A deep copy ensures that every level of the structure is duplicated.</p>
<p>One modern way to create a deep copy in JavaScript is using <code>structuredClone()</code>.</p>
<p>Example:</p>
<pre><code class="language-javascript">let arr1 = [[1, 2], [3, 4]];

let arr2 = structuredClone(arr1);

arr2[0][0] = 100;

console.log(arr1);
console.log(arr2);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">[ [1, 2], [3, 4] ]
[ [100, 2], [3, 4] ]
</code></pre>
<p>Here, <code>arr2</code> is a completely separate copy of <code>arr1</code>.</p>
<p>When we modify <code>arr2</code>, the original array <code>arr1</code> remains unchanged.</p>
<p>The <code>structuredClone()</code> method creates a deep copy of the entire data structure, including nested arrays and objects. It is supported in modern JavaScript environments and is generally a better approach than older workarounds.</p>
<h2>Accessing Elements Using Index</h2>
<p>To get a value from an array, we use the index.</p>
<blockquote>
<p>Note: Array index start form <code>zero</code>in JavaScript</p>
</blockquote>
<p>Syntax:</p>
<pre><code class="language-plaintext">arrayName[index]
</code></pre>
<p>Example:</p>
<pre><code class="language-javascript">let fruits = ["Apple", "Banana", "Mango", "Orange"];

console.log(fruits[0]);
// Output:: Apple
</code></pre>
<h2>Updating Array Elements</h2>
<p>Arrays are not fixed. We can modify any value by assigning a new value to its index.</p>
<p>Example:</p>
<pre><code class="language-javascript">let fruits = ["Apple", "Banana", "Mango"];

fruits[1] = "Orange";

console.log(fruits);
// Output : ["Apple", "Orange", "Mango"]
</code></pre>
<p>Here we replaced "Banana" with "Orange".</p>
<h2>The Array Length Property</h2>
<p>Every array has a special property called <code>length</code>.</p>
<p>It tells us how many elements are inside the array.</p>
<p>Example:</p>
<pre><code class="language-javascript">let fruits = ["Apple", "Banana", "Mango", "Orange"];

console.log(fruits.length); // Output 4
</code></pre>
<p>This means there are four elements in the array.</p>
<p>The length property is very useful in many situations. For example, if we want to find the last element of an array.</p>
<blockquote>
<p>Since indexing starts at 0, the last index is always:</p>
</blockquote>
<pre><code class="language-plaintext">length - 1
</code></pre>
<p>Example:</p>
<pre><code class="language-javascript">let fruits = ["Apple", "Banana", "Mango", "Orange"];

console.log(fruits[fruits.length - 1]); // Output Orange
</code></pre>
<p>This technique works even if the array size changes.</p>
<h2>Looping Through Arrays</h2>
<p>Often, we need to access every element in an array. Instead of writing many statements manually, we use loops.</p>
<p>The most common loop used with arrays is the <strong>for loop</strong>.</p>
<p>Example:</p>
<pre><code class="language-javascript">let fruits = ["Apple", "Banana", "Mango", "Orange"];

for (let i = 0; i &lt; fruits.length; i++) {
console.log(fruits[i]);
}
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Apple
Banana
Mango
Orange
</code></pre>
<p>Here is another example with numbers.</p>
<pre><code class="language-javascript">let numbers = [10, 20, 30, 40, 50];

for (let i = 0; i &lt; numbers.length; i++) {
console.log(numbers[i]);
}
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">10
20
30
40
50
</code></pre>
<p>Loops make arrays powerful because they allow us to handle many values with very little code.</p>
<h2>How Array Stores the Value</h2>
<p>To better understand arrays, imagine memory blocks.</p>
<p>Example array:</p>
<pre><code class="language-javascript">let numbers = [5, 10, 15, 20];
</code></pre>
<p>Visual representation:</p>
<pre><code class="language-plaintext">Index:   0    1    2    3
        -------------------
Value: | 5 | 10 | 15 | 20 |
        -------------------
</code></pre>
<p>Each block stores a value.</p>
<p>The index tells us which block to access.</p>
<p>If we want the value <code>15</code>, we access index 2.</p>
<pre><code class="language-plaintext">numbers[2]
</code></pre>
<p>This structured storage makes it easy for the program to retrieve data quickly.</p>
<h2>Real Life Example of Arrays</h2>
<p>Consider a simple to-do list application.</p>
<p>Instead of storing tasks separately, we use an array.</p>
<pre><code class="language-javascript">let todoList = [
"Finish project",
"Study JavaScript",
"Exercise",
"Read a book"
];
</code></pre>
<p>Now we can easily display all tasks.</p>
<pre><code class="language-javascript">for (let i = 0; i &lt; todoList.length; i++) {
console.log(todoList[i]);
}
</code></pre>
<p>If a task changes, we update it.</p>
<pre><code class="language-plaintext">todoList[1] = "Study arrays in JavaScript";
</code></pre>
<p>This flexibility makes arrays essential in programming.</p>
<h2>Array Inbuilt Methods in JavaScript</h2>
<p>Arrays in JavaScript are not just simple collections of values. They come with many <strong>built-in methods</strong> that help us perform common operations such as looping through elements, transforming values, filtering data, or combining values.</p>
<p>These methods make working with arrays much easier and reduce the need for writing manual loops.</p>
<h3>forEach()</h3>
<p>The <code>forEach()</code> method is used to iterate over an array and execute a function for each element. It is mainly used when we want to perform an action for every element in the array.</p>
<p>Unlike some other array methods, <code>forEach()</code> <strong>does not return a new array</strong>.</p>
<p>Example:</p>
<pre><code class="language-javascript">let fruits = ["Apple", "Banana", "Mango", "Orange"];

fruits.forEach(function(element, index) {
    console.log(index, element);
});
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">0 Apple
1 Banana
2 Mango
3 Orange
</code></pre>
<p>Here, the callback function receives the element and its index, and we print them using <code>console.log</code>.</p>
<p>Arguments accepted by the callback:</p>
<pre><code class="language-plaintext">(element, index, array)
</code></pre>
<p>Return value:</p>
<pre><code class="language-plaintext">undefined
</code></pre>
<h3>map()</h3>
<p>The <code>map()</code> method is used to create a <strong>new array by transforming each element of the original array</strong>.</p>
<p>It runs a callback function on every element and stores the returned values in a new array.</p>
<p>Example:</p>
<pre><code class="language-javascript">let numbers = [1, 2, 3, 4];

let doubled = numbers.map(function(num) {
    return num * 2;
});

console.log(doubled);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">[2, 4, 6, 8]
</code></pre>
<p>In this example, every number in the array is multiplied by 2, and the result is stored in a new array.</p>
<p>Arguments accepted by the callback:</p>
<pre><code class="language-plaintext">(element, index, array)
</code></pre>
<p>Return value:</p>
<pre><code class="language-plaintext">A new transformed array
</code></pre>
<p>The original array remains unchanged.</p>
<h3>filter()</h3>
<p>The <code>filter()</code> method creates a <strong>new array containing only elements that satisfy a given condition</strong>.</p>
<p>It runs the callback function on each element and includes the element in the new array only if the function returns <code>true</code>.</p>
<p>Example:</p>
<pre><code class="language-javascript">let numbers = [10, 15, 20, 25, 30];

let result = numbers.filter(function(num) {
    return num &gt; 20;
});

console.log(result);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">[25, 30]
</code></pre>
<p>Here, the callback checks if the number is greater than 20. Only those numbers are included in the resulting array.</p>
<p>Arguments accepted by the callback:</p>
<pre><code class="language-plaintext">(element, index, array)
</code></pre>
<p>Return value:</p>
<pre><code class="language-plaintext">A new filtered array
</code></pre>
<h3>reduce()</h3>
<p>The <code>reduce()</code> The method is used when we want to combine all elements of an array into a <strong>single value</strong>.</p>
<p>It processes the array from left to right and keeps updating an accumulated value.</p>
<p>Example:</p>
<pre><code class="language-javascript">let numbers = [1, 2, 3, 4];

let sum = numbers.reduce(function(accumulator, currentValue) {
    return accumulator + currentValue;
}, 0);

console.log(sum);
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">10
</code></pre>
<p>Explanation:</p>
<ul>
<li><p><code>accumulator</code> stores the running result</p>
</li>
<li><p><code>currentValue</code> is the current element being processed</p>
</li>
<li><p><code>0</code> is the initial value of the accumulator</p>
</li>
</ul>
<p>Arguments accepted by the callback:</p>
<pre><code class="language-plaintext">(accumulator, currentValue, index, array)
</code></pre>
<p>Return value:</p>
<pre><code class="language-plaintext">A single accumulated value
</code></pre>
<p>This method is often used for operations like calculating sums, averages, or combining values.</p>
<h2>Summary</h2>
<p>JavaScript arrays organize several values into a single variable. Every element has an index that begins at 0, making updates and access simple. The number of elements is displayed by the length property, and loops facilitate effective processing.</p>
<h2>Assignment</h2>
<p>Create a program that performs the following tasks.</p>
<ul>
<li><p>Create an array containing five of your favorite movies.</p>
</li>
<li><p>Print the first movie and the last movie from the array.</p>
</li>
<li><p>Change one movie in the array and print the updated array.</p>
</li>
<li><p>Use a loop to print every movie in the array.</p>
</li>
</ul>
<p>Example starting point:</p>
<pre><code class="language-javascript">let movies = ["Movie1", "Movie2", "Movie3", "Movie4", "Movie5"];
</code></pre>
<p>Try implementing the tasks step by step. This will help reinforce your understanding of arrays and how they work.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding JavaScript Functions: Declaration vs Expression]]></title><description><![CDATA[In today's blog, we are going to learn about functions in JavaScript and the use cases of functions. Our main focus for today's blog is understanding functions, function declarations, and function exp]]></description><link>https://blog.studyhex.in/understanding-javascript-functions-declaration-vs-expression</link><guid isPermaLink="true">https://blog.studyhex.in/understanding-javascript-functions-declaration-vs-expression</guid><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Sun, 15 Mar 2026 10:50:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/ff093636-d0d5-481c-821f-2c1df6a20884.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In today's blog, we are going to learn about functions in JavaScript and the use cases of functions. Our main focus for today's blog is understanding functions, function declarations, and function expressions.</p>
<h3>What is a Function?</h3>
<p>A function is a block of code designed to perform a specific task. Instead of writing the same code again and again, we wrap that logic inside a function and call it whenever we want to use it or needed.</p>
<p>For example, assume you want to add two numbers multiple times.</p>
<p>Let's take an example</p>
<pre><code class="language-javascript">let result1 = 5 + 10;
let result2 = 7 + 3;
let result3 = 8 + 12;
</code></pre>
<p>Without a function, if we need to add two numbers multiple time our code looks like this. This does not follow the DRY (Do not Repeat Yourself ) principle.</p>
<p>Instead, we can you function</p>
<pre><code class="language-javascript">function add(a, b) {
  return a + b;
}

// Now we can reuse it:

add(5, 10); // 15
add(7, 3);  // 10
add(8, 12); // 20
</code></pre>
<p>It improved code readability and maintainability.</p>
<h2>Function Declaration</h2>
<p>A <strong>function definition</strong> (also called a <strong>function declaration</strong>, or <strong>function statement</strong>) consists of the <code>function</code> keyword, followed by:</p>
<ul>
<li><p>The name of the function.</p>
</li>
<li><p>A list of parameters to the function, enclosed in parentheses and separated by commas.</p>
</li>
<li><p>The JavaScript statements that define the function, enclosed in curly braces, <code>{ /* … */ }</code>.</p>
</li>
</ul>
<h3>Syntax</h3>
<pre><code class="language-javascript">function functionName(parameters) {
  // code to execute
}
</code></pre>
<p>Example:</p>
<pre><code class="language-javascript">function multiply(a, b) {
  return a * b;
}
</code></pre>
<h2>Function Expression</h2>
<p>A function expression is another way of writing a function, and the syntax is almost similar to a <strong>function declaration</strong>. The main difference between a function expression and a function declaration is that the function name in a function expression can be <strong>omitted</strong> to create an anonymous function. A function expression can be used as an <strong>IFEE ( Immediately Invoked Function Expression)</strong>, which runs as soon as it is defined.</p>
<p><strong>Syntax</strong></p>
<pre><code class="language-javascript">const variableName = function(parameters) {
  // code
};
</code></pre>
<p><strong>Example</strong></p>
<pre><code class="language-javascript">const getRectArea = function (width, height) {
  return width * height;
};

console.log(getRectArea(3, 4));
// Expected output: 12
</code></pre>
<p>Here we store the function in a variable.</p>
<blockquote>
<p>Note: Function expressions in JavaScript are not hoisted, unlike function decleartions. You can't use function expression before you create them:</p>
</blockquote>
<pre><code class="language-javascript">console.log(notHoisted); // undefined
// Even though the variable name is hoisted,
// the definition isn't. so it's undefined.
notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function () {
  console.log("bar");
};
</code></pre>
<table>
<thead>
<tr>
<th>Feature</th>
<th>Function Declaration</th>
<th>Function Expression</th>
</tr>
</thead>
<tbody><tr>
<td>Definition style</td>
<td>Defined using <code>function name()</code></td>
<td>Stored inside a variable</td>
</tr>
<tr>
<td>Naming</td>
<td>Must have a function name</td>
<td>Usually anonymous</td>
</tr>
<tr>
<td>Hoisting behavior</td>
<td>Fully hoisted</td>
<td>Not fully hoisted</td>
</tr>
<tr>
<td>Use case</td>
<td>General reusable functions</td>
<td>Dynamic or conditional functions</td>
</tr>
</tbody></table>
<img src="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/44a0303a-1999-4090-9bb8-530747431df8.png" alt="" style="display:block;margin:0 auto" />

<h2>Basic Idea of Hoisting</h2>
<p>In JavaScript, Hosting is the concept where the JavaScript interpreter moves the function declaration, variables, classes, or imports to the top of their scope before executing the code.</p>
<p>To understand this, let's take a few examples</p>
<p>Example 1:</p>
<pre><code class="language-javascript">console.log(variable1); // Line 1
var variable1="holding a string"; // Line 2
console.log(variable1); // Line 3

// Instead of throwing a error the output looks like this
// undefined
// holding a string
</code></pre>
<p>As you can see here, instead of throwing errors in line 1, it says undefined because <code>variable1</code> The value is still not defined, but on line 2, the value is defined. <code>it's holding a string</code>, so when we use that in line 3, we get our expected output.</p>
<p>Example 2:</p>
<pre><code class="language-javascript">console.log(value1)
</code></pre>
<p>Now, in this example, you can clearly see that there is no variable named value1, so it throws an error <code>value1 is not defined.</code></p>
<h3>Function Declaration Hoisting Example</h3>
<pre><code class="language-javascript">sayHello(); // Works

function sayHello() {
  console.log("Hello!");
}

// Output 
// Hello!
</code></pre>
<p>This happens because <strong>function declarations are fully hoisted to the top of their scope</strong> during the creation phase of execution.</p>
<blockquote>
<p>Note that function expression are not hoisted</p>
</blockquote>
<pre><code class="language-javascript">sayHi(); // Error

const sayHi = function () {
  console.log("Hi!");
};

// Output Error
// sayHi is not defined
</code></pre>
<h3>Conclusion</h3>
<p>Functions are one of the most important building blocks in JavaScript. They allow us to write reusable and maintainable code.</p>
<p>Practice Assignment: Try Yourself</p>
<h3>Practice Assignment</h3>
<p>Write a function declaration that multiplies two numbers.</p>
<p>Example:</p>
<pre><code class="language-javascript">function multiply(a, b) {
  return a * b;
}

console.log(multiply(4, 5));
</code></pre>
<p>Write the same logic using a function expression.</p>
<pre><code class="language-javascript">const multiplyNumbers = function(a, b) {
  return a * b;
};

console.log(multiplyNumbers(4, 5));
</code></pre>
]]></content:encoded></item><item><title><![CDATA[Understanding JavaScript Promises: The JavaScript CID Bureau.]]></title><description><![CDATA[In our childhood, most of us read crime thrillers. My favorite show at that time was CID.In JavaScript, when we write code, it feels like a new CID case has arrived. Most of the time, CID gives priori]]></description><link>https://blog.studyhex.in/understanding-javascript-promises-the-javascript-cid-bureau</link><guid isPermaLink="true">https://blog.studyhex.in/understanding-javascript-promises-the-javascript-cid-bureau</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Javascript Promises]]></category><category><![CDATA[promises]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Sun, 01 Mar 2026 10:54:41 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/46dabdac-9854-4385-9bab-ffbd9a7615b6.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In our childhood, most of us read crime thrillers. My favorite show at that time was <strong>CID</strong>.<br />In JavaScript, when we write code, it feels like a new CID case has arrived. Most of the time, CID gives priority to certain cases because we all know some cases are more demanding, like a serial killer case, while others, like a robbery, may receive less priority.<br />JavaScript works similarly. Some tasks have higher priority, so JavaScript handles them immediately. Less demanding tasks are outsourced to the Web APIs. Because of this, the normal flow of code execution changes a little, and we get JavaScript’s asynchronous nature.</p>
<h2>Promises: Every CID Case Has a Status</h2>
<p>A <strong>Promise</strong> is a special JavaScript object that represents something that will happen in the future.</p>
<p>In JavaScript, sometimes we need to perform a task that takes time to complete. However, we don’t want to freeze the normal flow of execution because that would make the user experience slow and unresponsive. JavaScript solves this using its <strong>asynchronous features</strong> (for example, fetching API data through a network request in the browser).</p>
<p>Promises help us achieve this. Since the final result is not immediately available, JavaScript returns a <strong>Promise</strong> rather than the final value.</p>
<p>A Promise has three states:</p>
<ul>
<li><p><strong>Pending</strong> – In this state, the Promise internal operation is not completed.</p>
</li>
<li><p><strong>Resolved (Fulfilled)</strong> – The task completed successfully.</p>
</li>
<li><p><strong>Rejected</strong> – The task failed; this throws errors.</p>
</li>
</ul>
<p>Just like in CID, a case may be <strong>pending</strong>, may be <strong>solved</strong>, or may remain <strong>unsolved</strong>.</p>
<h2>Why do we need Promises (History)</h2>
<p>Before <strong>ECMAScript 2015 (ES6)</strong> introduced native Promises, JavaScript handled asynchronous programming entirely through <strong>callbacks</strong>. A callback is simply a function passed to another function that is executed later when a task finishes.</p>
<p>Imagine a new case arrives at the CID bureau. ACP Pradyuman assigns Abhijeet to investigate. Abhijeet says he will proceed only after Freddy checks the CCTV footage. Freddy says he can check the footage only after the technician restores the system. The technician says he will fix the system after receiving approval from another department.</p>
<p>So here the problem is that no one can proceed if the other person does not complete their task first. Everything is connected like a chain. Before asynchronous JavaScript, if we needed this behavior, the code would become complicated and verbose.</p>
<pre><code class="language-javascript">function assignCaseCB(caseName, cb) {
  setTimeout(() =&gt; cb(null, { caseName, status: "assigned by ACP" }), 100);
}

function investigateCaseCB(caseDetails, cb) {
  setTimeout(() =&gt; cb(null, { ...caseDetails, status: "investigating by Abhijeet" }), 100);
}

function checkCCTVCB(caseDetails, cb) {
  setTimeout(() =&gt; cb(null, { ...caseDetails, status: "CCTV checked by Freddy" }), 100);
}

function restoreSystemCB(caseDetails, cb) {
  setTimeout(() =&gt; cb(null, { ...caseDetails, status: "system restored by Technician" }), 100);
}

function approveCaseCB(caseDetails, cb) {
  setTimeout(() =&gt; cb(null, { ...caseDetails, status: "approved and ready to proceed" }), 100);
}

assignCaseCB("Bank Robbery", (err, caseDetails) =&gt; {
  if (err) return console.log(err);

  investigateCaseCB(caseDetails, (err, caseDetails) =&gt; {
    if (err) return console.log(err);

    checkCCTVCB(caseDetails, (err, caseDetails) =&gt; {
      if (err) return console.log(err);

      restoreSystemCB(caseDetails, (err, caseDetails) =&gt; {
        if (err) return console.log(err);

        approveCaseCB(caseDetails, (err, caseDetails) =&gt; {
          if (err) return console.log(err);

          console.log(`\({caseDetails.caseName}: \){caseDetails.status}`);
        });
      });
    });
  });
});
</code></pre>
<blockquote>
<p>As you can see here, the code is barely understandable. Also, if any phase throws an error, troubleshooting the problem becomes another hard challenge.</p>
</blockquote>
<h2>How to write promises in JavaScript</h2>
<p>In JavaScript, we create a Promise using an <code>Promise</code> object, which takes a callback function. This callback function receives two parameters: <code>resolve</code> and <code>reject</code>. Inside the Promise, we call <code>resolve()</code> when the task is successful and <code>reject()</code> when an error occurs.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">let res=true;
let promise = new Promise((res, rej) =&gt; {
  setTimeout(() =&gt; {
    if(res) res("Promise is resolved");
    rej(new Error("Promise is rejected"));
  }, 2000);
});
</code></pre>
<p>As you can see in the above example, there is a <code>resource</code> variable set to <code>true</code>. On the next line, we create a Promise with a callback that contains <code>resolve</code> and <code>reject</code>. Inside the callback, we use <code>setTimeout()</code>, and if <code>resource</code> is true, we resolve the Promise; otherwise, we reject it after a delay of 2000 milliseconds.</p>
<h2>To consume this promise, we have then() and catch().</h2>
<p>To understand <code>then()</code> and <code>catch()</code>, We can think of it like this: when a Promise is created, it acts like ACP Pradyuman, who says, “We always deliver justice.” Then we have Abhijeet, who represents <code>then()</code>, and Daya, who represents <code>catch()</code>.</p>
<p>In this situation, Abhijeet receives the criminal caught by ACP Pradyuman (the resolved value of the Promise). Abhijeet tries to calmly extract the truth from the criminal. If he is not able to find the truth, the case is passed to Daya — and we all know how Daya handles things.</p>
<p>Similarly, in JavaScript, when a Promise is resolved, <code>then()</code> takes a callback function and receives the resolved value. If the Promise is rejected and throws an error, it can also be handled inside <code>then()</code> because <code>then()</code> accepts a second callback for errors. However, nowadays, we usually use <code>catch()</code> (our Daya) to handle errors.</p>
<pre><code class="language-javascript">promise.then(
  (data) =&gt; console.log(data),
  (err) =&gt; console.log(err.message),
); // Note that then can handle the error also

promise
  .then((data) =&gt; data.toUpperCase())
  .then(console.log) // we can chain .then() for process the value
  .catch((err) =&gt; console.log(err.message));
</code></pre>
<p>As we can see here, the syntax is much cleaner and less verbose compared to the older callback chaining approach. Also, we have better control over how we operate on data, and error handling and troubleshooting become much easier.</p>
<h3>Event Chain .then(): Error propagation</h3>
<p>If we write a Promise and consume it using <code>then()</code>, we can chain multiple <code>.then()</code> Methods to process data step by step. However, if the Promise gets rejected, the error is automatically passed to the next <code>.then()</code>. If the error is not handled there, it continues to propagate down the chain until it reaches a <code>.catch()</code> block that properly handles the error.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/fc42a00b-5716-48c8-a805-0ce1528a0f69.png" alt="" style="display:block;margin:0 auto" />

<h2>Event Loop: Case Priority.</h2>
<p>To understand the Event Loop, it is very important to first understand the execution flow of normal code and Promises.</p>
<p>In CID, when the team goes to a criminal’s house to catch him, normal inspectors like Abhijeet and Freddy try to find a way to enter, such as breaking a glass window, because the house is locked. But if Daya suddenly arrives at the scene, we all know what happens — he breaks down the door immediately with high priority and gets the job done.</p>
<p>In JavaScript, something similar happens. We have synchronous code that runs on the Call Stack with the highest priority. Then we have the Microtask Queue, which has the second-highest priority, and finally the Macrotask Queue, which has lower priority.</p>
<ul>
<li><p><strong>Call Stack</strong>: Where synchronous code executes sequentially.</p>
</li>
<li><p><strong>Macrotask Queue</strong>: Holds callbacks originating from <code>setTimeout</code>, <code>setInterval</code> etc.</p>
</li>
<li><p><strong>Microtask Queue:</strong> It holds Promises.</p>
</li>
</ul>
<p>Priority Order is CallStack &gt; Microtask &gt; Macrotask</p>
<blockquote>
<p>The Event Loop is responsible for executing tasks. It decides which code should be executed at a given time and manages the overall flow of execution.</p>
</blockquote>
<p>Flow Diagram of Event Loop</p>
<img src="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/63a9c8fc-487d-4b16-a742-49b0c0a5bc1b.png" alt="" style="display:block;margin:0 auto" />

<pre><code class="language-javascript">console.log("1 Case Registered (Synchronous - Call Stack)");

setTimeout(() =&gt; {
  console.log("2 Macrotask: setTimeout executed");
}, 0);

Promise.resolve().then(() =&gt; {
  console.log("3 Microtask: Promise resolved");
});

console.log("4 Investigation Started (Synchronous - Call Stack)");

/* Output
1 Case Registered (Synchronous - Call Stack)
4 Investigation Started (Synchronous - Call Stack)
3 Microtask: Promise resolved
2 Macrotask: setTimeout executed
*/
</code></pre>
<h2>All Promise Static Methods</h2>
<p>To understand the static methods easily, let’s take the CID case example again. Assume the CID team is trying to solve a case, and ACP sir assigns three officers — Abhijeet, Freddy, and Daya — to catch the criminal.</p>
<p>Now, let’s discuss all the available JavaScript static methods we have for Promises.</p>
<ul>
<li><p>All()</p>
</li>
<li><p>Any()</p>
</li>
<li><p>AllSettled()</p>
</li>
<li><p>Race()</p>
</li>
<li><p>Resolve()</p>
</li>
<li><p>Reject()</p>
</li>
</ul>
<p>These are the most used ones.</p>
<h3>All() Method</h3>
<p>Assume it like a CID investigation where officers go on a mission. Three officers are assigned to catch three criminals together. If any one of them fails to catch their criminal, the entire mission fails.</p>
<p><code>Promise.all()</code> JavaScript works in a similar way. It receives multiple promises as an array, and if all the promises are successful, it returns all their results. However, if even one promise fails, it immediately rejects everything.</p>
<pre><code class="language-javascript">const officer1 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; resolve("Criminal 1 caught"), 1000);
});

const officer2 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; resolve("Criminal 2 caught"), 1500);
});

const officer3 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; resolve("Criminal 3 caught"), 2000);
});

Promise.all([officer1, officer2, officer3])
  .then((results) =&gt; {
    console.log("Mission Success");
    console.log(results);
  })
  .catch((error) =&gt; {
    console.log("Mission Failed");
    console.log(error);
  });

/* Output
Mission Success
[ 'Criminal 1 caught', 'Criminal 2 caught', 'Criminal 3 caught' ]
*/
</code></pre>
<h3>Any() Method</h3>
<p>Here, the scenario changes a little. Now, all three officers go to catch three criminals. However, if even one officer successfully catches a criminal, that criminal is brought to ACP, sir. After interrogation, they reveal the locations of the other criminals, and the overall mission is considered a success.</p>
<p>In JavaScript, this works similarly to <code>Promise.any()</code>. It receives an array of promises, and if at least one of them is successfully resolved, the whole Promise is resolved. It only rejects if all the promises fail.</p>
<pre><code class="language-javascript">const officer1 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; reject("Officer 1 failed"), 1000);
});

const officer2 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; resolve("Criminal caught by Officer 2"), 1500);
});

const officer3 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; reject("Officer 3 failed"), 2000);
});

Promise.any([officer1, officer2, officer3])
  .then((result) =&gt; {
    console.log("Mission Success");
    console.log(result);
  })
  .catch((error) =&gt; {
    console.log("Mission Failed");
    console.log(error);
  });
/* Output
Mission Success
*/
</code></pre>
<h3>Race() Method</h3>
<p>In this scenario, all three officers try to catch the criminal, but whoever catches the criminal first hands them over to ACP Sir, and the mission is considered successful.</p>
<p>In JavaScript, <code>Promise.race()</code> settles as soon as the first Promise settles (either resolves or rejects).</p>
<pre><code class="language-javascript">const officer1 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; resolve("Criminal caught by Officer 1"), 2000);
});

const officer2 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; resolve("Criminal caught by Officer 2"), 1000);
});

const officer3 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; resolve("Criminal caught by Officer 3"), 3000);
});

Promise.race([officer1, officer2, officer3])
  .then((result) =&gt; {
    console.log("Mission Result:");
    console.log(result);
  })
  .catch((error) =&gt; {
    console.log("Mission Failed:");
    console.log(error);
  });

/* Output
Mission Success
Criminal caught by Officer 2
*/
</code></pre>
<h3>allSettled() Method</h3>
<p>In this scenario, ACP Sir gives strict instructions to all three officers that, whether the mission fails or succeeds, he wants a full and final report. So, whether the officers catch the criminal or not, they must report back to ACP sir. In the end, ACP Sir receives the complete report from everyone.</p>
<p>In JavaScript, <code>Promise.allSettled()</code> works in a similar way. It returns the detailed result of all the promises it receives as parameters, whether they are fulfilled or rejected.</p>
<pre><code class="language-javascript">const officer1 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; resolve("Officer 1 caught the criminal"), 1000);
});

const officer2 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; reject("Officer 2 failed"), 1500);
});

const officer3 = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; resolve("Officer 3 caught the criminal"), 2000);
});

Promise.allSettled([officer1, officer2, officer3])
  .then((results) =&gt; {
    console.log("Final Report:");
    console.log(results);
  });

/* Output
Final Report:
[
  { status: 'fulfilled', value: 'Officer 1 caught the criminal' },
  { status: 'rejected', reason: 'Officer 2 failed' },
  { status: 'fulfilled', value: 'Officer 3 caught the criminal' }
]    
/*
</code></pre>
<h3>Promise.resolve()</h3>
<p><code>Promise.resolve()</code> creates a Promise that is already fulfilled. It does not wait for anything and immediately returns a successful result.</p>
<p>To understand this, imagine a criminal who thinks his plan is perfect and that no one will catch him, but CID has already gathered all the evidence against him. This means the case is already solved.</p>
<pre><code class="language-javascript">const caseStatus = Promise.resolve("Case already solved. Criminal arrested.");

caseStatus.then((message) =&gt; {
  console.log(message);
});
// Output Case already solved. Criminal arrested.
</code></pre>
<h3>Promise.reject()</h3>
<p><code>Promise.reject()</code> creates a Promise that is already rejected. It immediately returns a failed result without waiting for anything.</p>
<p>To understand this, imagine a situation where ACP Sir receives confirmed information that the criminal has escaped before the team even starts the mission. The case fails instantly — no investigation is needed.</p>
<pre><code class="language-javascript">const caseStatus = Promise.reject("Mission failed. Criminal escaped.");

caseStatus
  .then((message) =&gt; {
    console.log(message);
  })
  .catch((error) =&gt; {
    console.log(error);
  });
// Output Mission failed. Criminal escaped.
</code></pre>
<h2>Conclusion</h2>
<p>JavaScript Promises make asynchronous programming more structured, organized, and easy to understand. Just like CID sir’s investigation, every task has its own status, priority, and execution flow. We don’t need to mess around with deeply nested callbacks because Promises make asynchronous programming more readable, clean, and organized.</p>
<p>We can handle both the success and failure scenarios of the asynchronous task with the help of the then() and catch() methods, respectively. We know that the Event Loop is the main component that makes JavaScript handle asynchronous programming with the help of the Call Stack, Microtask Queue, and Macrotask Queue, giving us priority for the asynchronous task execution. And with the static methods like Promise.all(), Promise.any(), Promise.race(), Promise.allSettled(), we can easily handle multiple asynchronous task executions.</p>
<p>In simple words, Promises make JavaScript handle asynchronous programming like ACP sir handling different officers during the mission execution, i.e., with priority, order, and results.</p>
<p>As long as we know the Promises, JavaScript’s asynchronous programming is not confusing anymore, but it becomes more organized, structured, and powerful.</p>
]]></content:encoded></item><item><title><![CDATA[Control Flow in JavaScript: If, Else, and Switch Explained]]></title><description><![CDATA[In this article, we will learn about Control flow in JavaScript. It determines how your code makes logical decisions.
Control Flow
In JavaScript, the primary control flow statements are:

if

if...els]]></description><link>https://blog.studyhex.in/control-flow-in-javascript-if-else-and-switch-explained</link><guid isPermaLink="true">https://blog.studyhex.in/control-flow-in-javascript-if-else-and-switch-explained</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[control flow]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Sat, 28 Feb 2026 07:38:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/18b326e8-8a45-4ec9-ac81-236d063ff87d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, we will learn about Control flow in JavaScript. It determines how your code makes logical decisions.</p>
<h2>Control Flow</h2>
<p>In JavaScript, the primary control flow statements are:</p>
<ul>
<li><p><code>if</code></p>
</li>
<li><p><code>if...else</code></p>
</li>
<li><p><code>if...else if...else</code></p>
</li>
<li><p><code>switch</code></p>
</li>
</ul>
<p>All of this control flow block mostly operates on conditions, except the switch block. In JavaScript condition may be truthy and falsy. Many beginners don't know the range of falsy and truthy values.</p>
<p>In JavaScript, there are only 6 falsy values, and everything else is truthy.</p>
<ul>
<li><p><code>false</code> (the boolean primitive)</p>
</li>
<li><p><code>0</code>, <code>-0</code>, and <code>0n</code> (zero, negative zero, and BigInt zero)</p>
</li>
<li><p><code>""</code>, <code>''</code>, (an empty string)</p>
</li>
<li><p><code>null</code></p>
</li>
<li><p><code>undefined</code></p>
</li>
<li><p><code>NaN</code> (Not-a-Number)</p>
</li>
</ul>
<p>Now, let's understand all the Control Flow blocks one by one</p>
<h3>If Block</h3>
<p>When we try to run some specific code block if a particular condition is true, in that case we have an if-block to achieve this. Assume it like the gateman of an office, if you have a valid id card, then only you are allowed to enter the premises.</p>
<p>Note: this condition should be evaluated into boolean.</p>
<pre><code class="language-javascript">if(condition){
    // Your code that you want to run on particular condition
}
</code></pre>
<h3>Example: Checking Age</h3>
<pre><code class="language-javascript">let age = 20;

if (age &gt;= 18) {
  console.log("You are eligible to vote.");
// This code only runs if age is greater or equal to 18
}
</code></pre>
<h3>If-else block</h3>
<p>An if-else block is like an extra edition in an if block. In this block, if part works as expected, it means if you wrap some code or logic inside an if block that runs when the condition is true, but if that condition is false, then the code or logic wrapped around the else block executes.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">if(condition){
    console.log("Condition is true")// this runs if the condition is true
}
else{
    console.log("Condition is false") // this runs if the condition is false
}
</code></pre>
<h3>Example 1</h3>
<p>If we take an example, then think like an HR receives your resume, and wants to sort it based on your CGPA for the next round of interview, if your CGPA from the previous degree is greater than <code>8.5</code>, then you are allowed to continue the interview process, else not.</p>
<pre><code class="language-javascript">function HR_analyse(candidateName,CGPA){
    if(CGPA &gt;=8.5){
        console.log(`${candidateName} is allowed for interview round`)
    }
    else {
        console.log(`${candidateName} is not allowed for interview round`)
    }
}
let candidateName="Akash"
let cgpa=8.5
HR_analyse(candidateName,cgpa)
// Output 
// Akash is allowed for interview round
</code></pre>
<h3>Example 2</h3>
<p>Let's you have a marks and you want to tell the student pass or fail.</p>
<pre><code class="language-javascript">let marks = 40;

if (marks &gt;= 50) {
  console.log("Pass");
} else {
  console.log("Fail");
}
// here if the marks is greater equal to 50 then it gives pass else fail
</code></pre>
<p>Let’s understand the code flow briefly:</p>
<ul>
<li><p>The value of <code>marks</code> is 40.</p>
</li>
<li><p>The program checks whether <code>marks &gt;= 50</code>.</p>
</li>
<li><p>Since 40 is less than 50, the condition is false.</p>
</li>
<li><p>The <code>if</code> block is skipped.</p>
</li>
<li><p>Control moves to the <code>else</code> block.</p>
</li>
<li><p><code>"Fail"</code> is printed.</p>
</li>
</ul>
<h3>If-else-If ladder</h3>
<p>In some cases, only an if-else block is not enough to solve the decision-making process, like if we need to take a chain of decisions in those cases, we can use <code>if-else-if</code> a ladder.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">if (condition1){
    // if condition1 is true it's executes this part of code
}
else if (condition2){
    // if condition 1 if false and condition 2 is true then this part executes
}
else if( condition3){
    // if all avobe condition is false and this condition3 is true this code will execute.
}
else{
    // if none of the avobe condition is true then this code will execute.
}
</code></pre>
<h3>Example 1</h3>
<p>For example, imagine someone gives you a task to build a system that calculates a student’s grade based on their marks. The system should follow specific rules:</p>
<ul>
<li><p>If the student scores more than 90, they receive Grade A.</p>
</li>
<li><p>If the marks are more than 70 but less than or equal to 90, they receive Grade B.</p>
</li>
<li><p>If the marks are more than 50 but less than or equal to 70, they receive Grade C.</p>
</li>
<li><p>If the marks are more than 40 but less than or equal to 50, the student simply passes.</p>
</li>
<li><p>If the marks are 40 or below, the student fails.</p>
</li>
</ul>
<pre><code class="language-javascript">function getGrade(marks) {
    // Validate input range
    if (marks &lt; 0 || marks &gt; 100) {
        return "Invalid Marks";
    }

    if (marks &gt; 90) {
        return "Grade A";
    } else if (marks &gt; 70) {
        return "Grade B";
    } else if (marks &gt; 50) {
        return "Grade C";
    } else if (marks &gt; 40) {
        return "Pass";
    } else {
        return "Fail";
    }
}

// Examples
console.log(getGrade(75));   // Grade B
console.log(getGrade(101));  // Invalid Marks
console.log(getGrade(-5));   // Invalid Marks
</code></pre>
<p>As you can see, above we write a function getGrade which take marks parameter, and internally it checks which grade should be returned based on if else if ladder condition.</p>
<p>Let's try to understand the flow of the above code</p>
<ol>
<li><p>First, the if block checks whether the mark is valid or not.</p>
</li>
<li><p>Then it checks the if-else-if ladder and returns the appropriate value based on which condition is true.</p>
</li>
</ol>
<p>Note: In an if-else-if ladder, many beginner made mistake here to put conditions, because if we change the order of conditions, it may return unexpected results. For example</p>
<pre><code class="language-javascript">function getGrade(marks) {
    // Validate input range
    if (marks &lt; 0 || marks &gt; 100) {
        return "Invalid Marks";
    }

    if (marks &gt; 70) {
        return "Grade B"; // here I swap the condition of 90 and 70
    } else if (marks &gt; 90) {
        return "Grade A"; 
    } else if (marks &gt; 50) {
        return "Grade C";
    } else if (marks &gt; 40) {
        return "Pass";
    } else {
        return "Fail";
    }
}

// Examples
console.log(getGrade(90));   // Grade B 
console.log(getGrade(101));  // Invalid Marks
console.log(getGrade(-5));   // Invalid Marks
</code></pre>
<p>Let’s understand step by step:</p>
<ul>
<li><p>Suppose <code>marks = 95</code></p>
</li>
<li><p>The program first checks: <code>marks &gt; 70</code> which always evaluated <code>True</code></p>
</li>
<li><p>Since this condition is true, it immediately returns <code>"Grade B"</code></p>
</li>
<li><p>The next condition <code>marks &gt; 90</code> is <strong>never checked</strong></p>
</li>
</ul>
<img src="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/dcd30979-0c26-4845-9b4d-ace1da0be631.png" alt="" style="display:block;margin:0 auto" />

<h2>Switch Case</h2>
<p>Switch cases are also another control flow block to make logical but simpler use cases, where you know the condition you received is predefined. Instead of an if-else block received condition may be anything; in a switch, it should be predefined.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">switch(expression) {
    case value1:
        // Code to execute if expression === value1
        break;

    case value2:
        // Code to execute if expression === value2
        break;

    default:
        // Code to execute if none of the cases match
}
</code></pre>
<h3>Important Points</h3>
<ul>
<li><p><code>switch</code> compares values using strict comparison (<code>===</code>).</p>
</li>
<li><p><code>break</code> is very important. without break, it executes all the conditions one by one. If there is a case suppose the case 1 matches, and you do something in that and break, then the control flow of the code exited form the switch block.</p>
</li>
<li><p>In the switch block, we have a default, which is like an else block in if else block. If none of the avobe condition is true default will run in that case. Default is optional but recommended.</p>
</li>
</ul>
<h3>Example 1: Day of the Week</h3>
<pre><code class="language-javascript">let day = 3;

switch (day) {
    case 1:
        console.log("Monday");
        break;
    case 2:
        console.log("Tuesday");
        break;
    case 3:
        console.log("Wednesday");
        break;
    case 4:
        console.log("Thursday");
        break;
    case 5:
        console.log("Friday");
        break;
    default:
        console.log("Invalid day");
}
</code></pre>
<h3>Flow Explanation</h3>
<ul>
<li><p>The value of <code>day</code> is 3.</p>
</li>
<li><p>The switch compares it with each case.</p>
</li>
<li><p>When it matches <code>case 3</code>, it prints <code>"Wednesday"</code>.</p>
</li>
<li><p><code>break</code> stops further execution.</p>
</li>
</ul>
<h3>Example 2: Simple Calculator</h3>
<pre><code class="language-javascript">function calculator(num1, num2, operator) {
    switch (operator) {
        case "+":
            return num1 + num2;
        case "-":
            return num1 - num2;
        case "*":
            return num1 * num2;
        case "/":
            return num1 / num2;
        default:
            return "Invalid Operator";
    }
}

console.log(calculator(10, 5, "+")); // 15
// Here, switch works perfectly because we are matching exact operator values.
</code></pre>
<p>Here, the switch works because we are basically checking the exact operation we want to perform, so here we can observe that operations are predefined and well known, so using a switch case block makes sense here.</p>
<img src="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/e37894ec-3aa8-48a2-b4bd-a86624275628.png" alt="" style="display:block;margin:0 auto" />

<h2>When to Use switch vs if-else</h2>
<h2>Use <code>if-else</code> when:</h2>
<ul>
<li><p>You are checking ranges (<code>marks &gt; 90</code>)</p>
</li>
<li><p>You are checking multiple conditions</p>
</li>
<li><p>Conditions are complex (logical operators like <code>&amp;&amp;</code>, <code>||</code>)</p>
</li>
<li><p>Comparisons are not simple equality</p>
</li>
</ul>
<h2>Use <code>switch</code> when:</h2>
<ul>
<li><p>You are comparing one variable to multiple exact values</p>
</li>
<li><p>Conditions are simple equality checks</p>
</li>
<li><p>You want cleaner and more readable code</p>
</li>
</ul>
<h3>Conclusion</h3>
<p>These control flows are more useful when you are trying to build robust software. For example, if we have some wrote some logic to check whether the user is authenticated or not.</p>
<pre><code class="language-javascript">function isAuth(user) {
    // Defensive check: if user is null, undefined, or falsy
    if (!user) {
        console.log("No user provided");
        return;
    }

    // Check if email exists
    if (!user?.email) {
        throw new Error("Email does not exist");
    }

    console.log("Authenticated");
}
</code></pre>
<p>If you want to learn this in a better way, I suggest trying these questions by yourself to gain more control over those control flow blocks.</p>
<h2>A few questions to try</h2>
<h3>1. ATM Withdrawal Checker</h3>
<p>Build a function that checks whether a user can withdraw money from their bank account.</p>
<ul>
<li><p>If the withdrawal amount is greater than the balance, show "Insufficient Balance".</p>
</li>
<li><p>If the amount is less than or equal to the balance, show "Transaction Successful".</p>
</li>
<li><p>If the amount is negative or zero, return "Invalid Amount".</p>
</li>
</ul>
<h3>2. Temperature Advisor</h3>
<p>Create a program that suggests clothing based on temperature.</p>
<ul>
<li><p>If the temperature is above 30°C, "Wear light clothes".</p>
</li>
<li><p>If the temperature is between 20°C and 30°C, "Weather is pleasant".</p>
</li>
<li><p>If the temperature is below 20°C, "Wear warm clothes".</p>
</li>
</ul>
<h3>3. Login Attempt System</h3>
<p>Build a system that checks login attempts.</p>
<ul>
<li><p>If attempts are less than 3, "Login allowed".</p>
</li>
<li><p>If attempts equal 3, "Last attempt remaining".</p>
</li>
<li><p>If attempts are more than 3, "Account locked".</p>
</li>
</ul>
<h3>4. Online Shopping Discount</h3>
<p>Create a function that applies a discount based on order amount.</p>
<ul>
<li><p>If the order amount is greater than $200, then 20% discount.</p>
</li>
<li><p>If greater than $100, then 10% discount.</p>
</li>
<li><p>Otherwise, no discount.</p>
</li>
</ul>
<p>If you try to solve this manually, then definitely, if control over the control flow is much stronger.</p>
]]></content:encoded></item><item><title><![CDATA[Arrow Functions in JavaScript: A Simpler Way to Write Functions]]></title><description><![CDATA[In JavaScript, most developers use functions to maintain the DRY (do not repeat) principle. JavaScript actually has 2 way to write a function.

Normal Function

Arrow Function


In this article, we ar]]></description><link>https://blog.studyhex.in/arrow-functions-in-javascript-a-simpler-way-to-write-functions</link><guid isPermaLink="true">https://blog.studyhex.in/arrow-functions-in-javascript-a-simpler-way-to-write-functions</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[#arrowfunction]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Fri, 27 Feb 2026 11:28:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/c3ad2091-13e0-4a19-8994-6b6f7e118728.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In JavaScript, most developers use functions to maintain the DRY (do not repeat) principle. JavaScript actually has 2 way to write a function.</p>
<ol>
<li><p>Normal Function</p>
</li>
<li><p>Arrow Function</p>
</li>
</ol>
<p>In this article, we are going to see what the use cases of arrow functions are, and why they make much more sense than the normal function.</p>
<h2>Tradition Function</h2>
<p>In JavaScript, writing a function involves first using the keyword "function," then providing a name for the function. If parameters are needed, they are placed within parentheses. The code logic of the function is then written inside the block.</p>
<pre><code class="language-javascript">function func_name(paramenter1, parameter2){
    // You Can write function logic here
    console.log(parameter1,parameter2);
}
</code></pre>
<p>Let's take a practical example of a function to observe things more clearly.</p>
<pre><code class="language-javascript">function isVote(age){
    if(!Number.isInteger(age)) return false;
    if(age&gt;17) return true;
    return false;
}
</code></pre>
<p>In this function, we are just checking whether a user is able to vote or not based on the user's age.</p>
<h2>Arrow Function</h2>
<p>As we can see in the traditional way of writing a function, the code look little bit verbose, and for a simple task, it may look overwhelming. To fix, we can use an arrow function.</p>
<h3>Syntax</h3>
<pre><code class="language-javascript">const func1=(parameter1,paramenter2)=&gt; {
    // our code logic should be here
} 
</code></pre>
<p>Now, for better understanding, try to convert our old isVote function into an arrow function.</p>
<pre><code class="language-javascript">const isVote=(age)=&gt; age&gt;17?true:false
// or we can use 
const isVote=(age)=&gt; (age&gt;17?true:false) // both are valid
// if we not use {} and our task done in single line it automatically return the value. Implicit Return
</code></pre>
<p>Now, what we can observe from this, it is actually less verbose unlike tradition function. And here, for a simple task, we don't need to do many things.</p>
<p>Let's take another example.</p>
<pre><code class="language-javascript">const isValidVoter(name,age)=&gt;{
    if(typeof name!=="string" &amp;&amp; age&gt;17) return true;
    else return false;
}
// Note when we use {} we it is our responsibilty to return stuff. This is explicit return.
</code></pre>
<h3>More Use Cases</h3>
<p>If you have an array with a few items, you can use an arrow function in map and filter to do some operation on them.</p>
<ol>
<li>Map()</li>
</ol>
<pre><code class="language-javascript">let arr=[1,2,3,4,5]
let multiOf2=arr.map((item)=&gt;item*2)
// Output
// [2, 4, 6, 8, 10]
</code></pre>
<ol>
<li>Filter()</li>
</ol>
<pre><code class="language-javascript">let fruits = ["Apple", "Orange", "Pineapple", "Mango"];

let fruitStartWithA = fruits.filter(
  (item) =&gt; item.charAt(0).toLowerCase() === "a"
);

console.log(fruitStartWithA);
// Output
// ['Apple']
</code></pre>
<h3>Differences between Normal Function and Arrow Function</h3>
<h2>Arguments</h2>
<ul>
<li><p>Normal function → has its own <code>arguments</code>.</p>
</li>
<li><p>Arrow function → does NOT have <code>arguments</code>.<br />It borrows from the outer scope.</p>
</li>
</ul>
<pre><code class="language-javascript">function func1() {
  console.log("function 1", arguments);
} // Normal Function
const func2 = () =&gt; {
  console.log("function 2", arguments);
}; // Arrow Function
func1(); // Calling the Normal Function
func2(); // Calling the Arrow Function 

// normal function → empty [Arguments] {}
// Arrow → borrowed Node wrapper arguments (exports, require, etc.)
</code></pre>
<h2>Constructor</h2>
<p>In normal function, we can create a Constructor function and use the new keyword to create multiple objects of that Constructor, these are known as a constructor function.</p>
<h3>Normal Function Example</h3>
<pre><code class="language-javascript">function TataCar(chassisNumber, modelName) {
  
  this.chassisNumber = chassisNumber;
  
  this.modelName = modelName;
  this.fuelLevel = 100;
  
}
// Polyfill
TataCar.prototype.status = function () {
  return `Tata \({this.modelName} #\){this.chassisNumber} | Fuel: ${this.fuelLevel}`;
};

const car1 = new TataCar("MH-101", "Nexon");
const car2 = new TataCar("DL-202", "Harrier");

console.log(car1.modelName);
console.log(car2.modelName);
console.log(car1.status());
console.log(car2.status());

// Output 
// Nexon
// Harrier
// Tata Nexon #MH-101 | Fuel: 100
// Tata Harrier #DL-202 | Fuel: 100
</code></pre>
<h3>Arrow Function Example</h3>
<pre><code class="language-javascript">const TataCar = (chassisNumber, modelName) =&gt; {
  chassisNumber = chassisNumber;

  modelName = modelName;
  fuelLevel = 100;
  status = function () {
    return `Tata \({this.modelName} #\){this.chassisNumber} | Fuel: ${this.fuelLevel}`;
  };
};

const car1 = new TataCar("MH-101", "Nexon");
const car2 = new TataCar("DL-202", "Harrier");

console.log(car1.modelName);
console.log(car2.modelName);
console.log(car1.status());
console.log(car2.status());
// This throws an error 
// const car1 = new TataCar("MH-101", "Nexon");
// TypeError: TataCar is not a constructor
</code></pre>
<h2>Use of "This"</h2>
<h3>Normal Function and <code>this</code></h3>
<p>In a normal function:</p>
<p><code>this</code> depends on how the function is called.</p>
<h3>Example 1: Global Call</h3>
<pre><code class="language-javascript">function normalFuncThis() {
  return [this, typeof this];
}

console.log(normalFuncThis());
// Output 
// [Window, "object"]
// Output in Node environment Strict Mode
// [ undefined, 'undefined' ]
</code></pre>
<h3>Example 2: Method Call</h3>
<pre><code class="language-javascript">const obj = {
  a: 10,
  normal() {
    return this.a;
  },
};

console.log(obj.normal()); //this = obj
// Output 
// 10
</code></pre>
<h3>Arrow Function and <code>this</code></h3>
<p>Arrow functions do not create their own this. They capture <code>this</code> from where they are defined (lexical scope).</p>
<h3>Example 1</h3>
<pre><code class="language-javascript">const obj = {
  a: 10,
  arrow: () =&gt; {
    return this.a;
  },
};

console.log(obj.arrow());
// Output
// undefined
</code></pre>
<p>Arrow function can't borrow this form from the object. It only borrows this to its lexical parent scope</p>
<h3>Example 2</h3>
<pre><code class="language-javascript">const obj = {
  a: 10,
  method1() {
    const func1 = () =&gt; {
      return this.a;
    };
    return func1();
  },
};

console.log(obj.method1());
// Output
// This
</code></pre>
<h3>Example 3</h3>
<pre><code class="language-javascript">const filmSet = {
  crew: "Spot boys",
  prepareProps() {
    console.log(`Outer this.crew: ${this.crew}`);

    function arrangeChairs() {
      console.log(`Inner this.crew: ${this.crew}`);
    }
    arrangeChairs();

    const arrangeLights = () =&gt; {
      console.log(`Arrow this.crew: ${this.crew}`);
    };
    arrangeLights();
  },
};

filmSet.prepareProps();
// Output 
// Outer this.crew: Spot boys
// Inner this.crew: undefined
// Arrow this.crew: Spot boys
</code></pre>
<p>So let's try to understand the behaviour of the above code</p>
<h3>Calling <code>prepareProps()</code></h3>
<ul>
<li><p>here the this == flimSet</p>
</li>
<li><p>here prepareProps() is a normal method, so it has access to this, which is flimSet crew: "spot boys"</p>
</li>
<li><p>Output: <code>Outer this.crew: Spot boys</code></p>
</li>
</ul>
<h3>Calling <code>arrangeChairs()</code></h3>
<ul>
<li><p>arrangeCharis() is a normal method again, but it is inside the prepareProps(). Normal methods has there own this, so here this !== flimSet</p>
</li>
<li><p>Not called as a method of <code>filmSet</code></p>
</li>
<li><p>In strict mode → <code>this = undefined</code></p>
</li>
<li><p>In a non-strict browser → <code>this = window</code></p>
</li>
<li><p>Output <code>Inner this.crew: undefined</code></p>
</li>
</ul>
<h3>Inside <code>arrangeLights()</code> (Arrow Function)</h3>
<ul>
<li><p>Arrow functions do not create their own <code>this</code></p>
</li>
<li><p>They capture <code>this</code> from the parent lexical scope</p>
</li>
<li><p>Defined inside <code>prepareProps()</code></p>
</li>
<li><p>And there <code>this = filmSet</code></p>
</li>
<li><p>Output: <code>Arrow this.crew: Spot boys</code></p>
</li>
</ul>
<h3>Conclusion:</h3>
<p>In JavaScript, the arrow function at the beginning may feel overwhelming. To understand the concept of arrow function, you should always run this code in both the browser console and node environment so you can understand its behavior.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Array Methods You Must Know before jumping into coding]]></title><description><![CDATA[Overview
Arrays are one of the most critical concepts in JavaScript. In almost every project, we use arrays to store and manage data.
In this article, we will discuss the most frequently used JavaScri]]></description><link>https://blog.studyhex.in/javascript-array-methods-you-must-know-before-jumping-into-coding</link><guid isPermaLink="true">https://blog.studyhex.in/javascript-array-methods-you-must-know-before-jumping-into-coding</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[jsarraymethods]]></category><category><![CDATA[arrays]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Wed, 25 Feb 2026 14:56:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6953c37709349c47ab7f5fc0/83cd537f-6769-4425-869a-7a210b8db6ab.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Overview</h2>
<p>Arrays are one of the most critical concepts in JavaScript. In almost every project, we use arrays to store and manage data.</p>
<p>In this article, we will discuss the most frequently used JavaScript array methods that every developer should know before starting serious coding. These methods will help you write cleaner, shorter, and more efficient code instead of using long and complex loops.</p>
<h2>Commonly used array methods</h2>
<h3>Method 1: Push()</h3>
<p><code>push()</code> is an array method used to <strong>add one or more elements to the end of an array</strong>. Need to know that this actually modifies the array. <code>push()</code> Returns the <strong>length of the newly made array</strong>, not the array itself.</p>
<p>The flow is like first it goes to the last index, then it inserts the new value there at the length position, after increasing the array length. Finally, it returns the updated length.</p>
<h3>Basic Syntax</h3>
<pre><code class="language-javascript">arr1.push(element1, element2, ..., elementN);
</code></pre>
<pre><code class="language-javascript">const numbers = [1, 2, 3];

const newLength = numbers.push(4);

console.log(numbers);    // [1, 2, 3, 4]
console.log(newLength);  // 4
</code></pre>
<h3>Method 2: Pop()</h3>
<p><code>pop()</code> is an array method used to <strong>remove the last element</strong> from an array. It also <strong>modifies the original array</strong>. <code>pop()</code> Returns the <strong>removed element</strong>. If the array is empty, it returns <code>undefined</code>.</p>
<p>The flow is like this - JavaScript finds the last index. It stores that value. It reduces the array. It returns the removed value.</p>
<h2>Basic Syntax</h2>
<pre><code class="language-javascript">array.pop()
</code></pre>
<pre><code class="language-javascript">const numbers = [1, 2, 3];

const removed = numbers.pop();

console.log(numbers); // [1, 2]
console.log(removed); // 3
/*
Here:
3 is removed from the end
The original array changes
The return value is the removed element
*/
</code></pre>
<h3>Method 3: Shift()</h3>
<p><code>shift()</code> is an array method used to <strong>remove the first element</strong> from an array. It <strong>modifies the original array</strong> and returns the <strong>removed element</strong>.<br />If the array is empty, it returns <code>undefined</code>.</p>
<p>The flow of shifts is like - JavaScript stores the value at the index <code>0</code> Then it shifts all remaining elements one position to the left. It decreases the array length by 1. It returns the removed value.</p>
<h4>Basic Syntax</h4>
<pre><code class="language-javascript">array.shift()
</code></pre>
<h4>Example</h4>
<pre><code class="language-javascript">const numbers = [10, 20, 30];

const removed = numbers.shift();

console.log(numbers); // [20, 30]
console.log(removed); // 10
// Here:
// 10 is removed from the beginning.
// The original array changes.
// The return value is the removed element.
</code></pre>
<h3>Method 5: Unshift()</h3>
<p><code>unshift()</code> is an array method used to <strong>add one or more elements to the beginning</strong> of an array. It <strong>modifies the original array</strong> and returns the <strong>new length</strong> of the array.</p>
<h4>Step-by-step:</h4>
<ul>
<li><p>JavaScript shifts all existing elements one position to the right.</p>
</li>
<li><p>It inserts the new value(s) at the index <code>0</code>.</p>
</li>
<li><p>It increases the array length.</p>
</li>
<li><p>It returns the updated length.</p>
</li>
</ul>
<h4>Basic Syntax</h4>
<pre><code class="language-javascript">array.unshift(element1, element2, ..., elementN)
</code></pre>
<h4>Example</h4>
<pre><code class="language-javascript">const numbers = [20, 30];

const newLength = numbers.unshift(10);

console.log(numbers);   // [10, 20, 30]
console.log(newLength); // 3

// Here:
// 10 is added at the beginning.
// The original array changes.
// The return value is the new length.
</code></pre>
<h3>Method 6: Filter()</h3>
<p><code>filter()</code> is an array method used to <strong>select specific elements</strong> from an array based on a condition. It runs a function on every element and returns a <strong>new array</strong> containing only the elements that pass the test.</p>
<p>Note: <code>filter()</code> <strong>does NOT modify the original array.</strong></p>
<h3>Step-by-step:</h3>
<ul>
<li><p>JavaScript loops through each element.</p>
</li>
<li><p>It runs the callback function.</p>
</li>
<li><p>If the function returns <code>true</code>, the element is added to a new array.</p>
</li>
<li><p>If it returns <code>false</code>, the element is skipped.</p>
</li>
<li><p>Finally, it returns the new filtered array.</p>
</li>
</ul>
<h3>Basic Syntax</h3>
<pre><code class="language-javascript">array.filter(callback(currentValue, index, array), thisArg)
</code></pre>
<h3>Example 1</h3>
<pre><code class="language-javascript">const numbers = [1, 2, 3, 4, 5, 6];

const evenNumbers = numbers.filter(function(num) {
  return num % 2 === 0;
});

console.log(evenNumbers); // [2, 4, 6]
console.log(numbers);     // [1, 2, 3, 4, 5, 6] (unchanged)
// Here:
// The condition checks if a number is even.
// Only numbers that return true are included.
// The original array stays the same
</code></pre>
<h3>What Arguments Does filter() Take?</h3>
<p>The callback receives 3 arguments:</p>
<ul>
<li><p><strong>currentValue</strong> → current element</p>
</li>
<li><p><strong>index</strong> → current index</p>
</li>
<li><p><strong>array</strong> → full array</p>
</li>
</ul>
<pre><code class="language-javascript">const nums = [10, 20, 30];

nums.filter(function(value, index, array) {
  console.log(value, index, array);
  return value &gt; 15;
});

// Output:
// 10 0 [10, 20, 30]
// 20 1 [10, 20, 30]
// 30 2 [10, 20, 30]
</code></pre>
<h3>Example 2: Filter an Array of Objects</h3>
<pre><code class="language-javascript">const users = [
  { name: "Pallab", age: 22 },
  { name: "Rahul", age: 25 },
  { name: "Amit", age: 18 }
];

const adults = users.filter(user =&gt; user.age &gt;= 21);

console.log(adults);
// [
//   { name: "Pallab", age: 22 },
//   { name: "Rahul", age: 25 }
// ]
</code></pre>
<h3>Method 1: Map()</h3>
<p><code>map()</code> is an <strong>array method</strong> in JavaScript. Takes an array, runs a function on <strong>each element,</strong> and returns a <strong>new array</strong> with modified values</p>
<p>Note: It does <strong>NOT</strong> change the original array.</p>
<p>Sample Code</p>
<pre><code class="language-javascript">const numbers = [1, 2, 3, 4];

const doubled = numbers.map(function(num) {
  return num * 2;
});

console.log(doubled); // [2, 4, 6, 8]
// For better understading always run the code in console
</code></pre>
<p>What Arguments Does <code>map()</code> Take?</p>
<h3>Syntax:</h3>
<pre><code class="language-plaintext">array.map(callback(currentValue, index, array), thisArg)
</code></pre>
<h3>callback</h3>
<p>This callback means a function that runs for every element.</p>
<p>It receives <strong>3 arguments</strong>:</p>
<ol>
<li><p><strong>currentValue</strong>: means for that iteration, what is the array element.</p>
</li>
<li><p><strong>index</strong>: this is the currentValue index.</p>
</li>
<li><p><strong>array</strong>: it is the whole array.</p>
</li>
</ol>
<pre><code class="language-javascript">const nums = [10, 20, 30];

nums.map(function(value, index, array) {
  console.log(value, index, array);
});

// Output 
// 10 0 [10, 20, 30]
// 20 1 [10, 20, 30]
// 30 2 [10, 20, 30]
</code></pre>
<h3>thisArg</h3>
<p>It is used to set this value on the callback.</p>
<pre><code class="language-javascript">const obj = {
  multiplier: 3
};

const nums = [1, 2, 3];

const result = nums.map(function(num) {
  return num * this.multiplier;
}, obj);

console.log(result); // [3, 6, 9]
</code></pre>
<h3>You can <code>Map()</code> array of objects</h3>
<pre><code class="language-javascript">const users = [
  { name: "Pallab", age: 22 },
  { name: "Rahul", age: 25 }
];

const names = users.map(user =&gt; user.name);
// ["Pallab", "Rahul"]
</code></pre>
<h3>Method 7: reduce()</h3>
<p><code>reduce()</code> is an array method used to <strong>combine all elements into a single value</strong>.</p>
<p>That single value can be:</p>
<ul>
<li><p>A number (sum, total)</p>
</li>
<li><p>A string</p>
</li>
<li><p>An object</p>
</li>
<li><p>Another array</p>
</li>
</ul>
<p>It does <strong>not modify the original array</strong>.</p>
<h3>Basic Syntax</h3>
<pre><code class="language-javascript">array.reduce(callback(accumulator, currentValue), initialValue)
</code></pre>
<h3>Important Terms</h3>
<ul>
<li><p><strong>accumulator</strong> → stores the running result</p>
</li>
<li><p><strong>currentValue</strong> → current element in the loop</p>
</li>
<li><p><strong>initialValue</strong> → starting value (recommended to always provide)</p>
</li>
</ul>
<h3>Example 1: Sum of Numbers</h3>
<pre><code class="language-javascript">const numbers = [1, 2, 3, 4];

const total = numbers.reduce(function(acc, num) {
  return acc + num;
}, 0);

console.log(total); // 10
</code></pre>
<p>Step idea:</p>
<ul>
<li><p>0 + 1 = 1</p>
</li>
<li><p>1 + 2 = 3</p>
</li>
<li><p>3 + 3 = 6</p>
</li>
<li><p>6 + 4 = 10</p>
</li>
</ul>
<p>Final result → <code>10</code></p>
<h3>When Do We Use reduce()?</h3>
<ul>
<li><p>Total price calculation</p>
</li>
<li><p>Counting items</p>
</li>
<li><p>Grouping data</p>
</li>
<li><p>Flattening arrays</p>
</li>
</ul>
<p>Think of <code>reduce()</code> as:</p>
<p><strong>"Take everything and turn it into one final value."</strong></p>
<h2>Method 8: forEach()</h2>
<p><code>forEach()</code> is an array method used to <strong>loop through each element</strong> of an array.</p>
<p>It runs a function for every element.</p>
<p>Important:</p>
<ul>
<li><p>It <strong>does not return anything</strong> (returns <code>undefined</code>)</p>
</li>
<li><p>It <strong>does not create a new array</strong></p>
</li>
</ul>
<p>It is mainly used for:</p>
<ul>
<li><p>Printing values</p>
</li>
<li><p>Updating something</p>
</li>
<li><p>Running side effects</p>
</li>
</ul>
<h3>Basic Syntax</h3>
<pre><code class="language-javascript">array.forEach(callback(currentValue, index, array))
</code></pre>
<h3>Example</h3>
<pre><code class="language-javascript">const numbers = [10, 20, 30];

numbers.forEach(function(num, index) {
  console.log("Index:", index, "Value:", num);
});
</code></pre>
<p>Output:</p>
<pre><code class="language-plaintext">Index: 0 Value: 10
Index: 1 Value: 20
Index: 2 Value: 30
</code></pre>
<h2>Now The question is, if we have a for loop, then why Map() and Filter()?</h2>
<h3>forEach()</h3>
<p><code>forEach()</code> is used to <strong>loop through every element</strong> of an array and perform an action.</p>
<ul>
<li><p>It does <strong>not return a new array</strong></p>
</li>
<li><p>It is used when you just want to <strong>do something</strong> with each item</p>
</li>
<li><p>It cannot break or stop early (unlike a normal <code>for</code> loop)</p>
</li>
</ul>
<h3>Important Difference</h3>
<ul>
<li><p><code>for</code> → full control (break, continue, manual index)</p>
</li>
<li><p><code>map</code> → transform and return a new array</p>
</li>
<li><p><code>filter</code> → select some elements</p>
</li>
<li><p><code>forEach</code> → just run code for each element (no return)</p>
</li>
</ul>
<h3>Conclution</h3>
<p>Array methods help you write clean, efficient, and professional JavaScript.<br />Use the right method for the right task, and your code becomes simpler, more readable, and easier to maintain.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Internal: How JavaScript works.]]></title><description><![CDATA[In today's article, we are going to discuss how JavaScript works internally. Before starting today's discussion, follow this blog for proper understanding

How a browser works

Introduction
We all kno]]></description><link>https://blog.studyhex.in/javascript-internal-how-javascript-works</link><guid isPermaLink="true">https://blog.studyhex.in/javascript-internal-how-javascript-works</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[callstack]]></category><category><![CDATA[Global execution context]]></category><category><![CDATA[javascript internal]]></category><category><![CDATA[local execution context]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Mon, 23 Feb 2026 16:25:27 GMT</pubDate><enclosure url="https://cloudmate-test.s3.us-east-1.amazonaws.com/uploads/covers/6953c37709349c47ab7f5fc0/ca7caccc-6795-4e20-a7c7-84ea36f8ca12.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In today's article, we are going to discuss how JavaScript works internally. Before starting today's discussion, follow this blog for proper understanding</p>
<ol>
<li><a href="https://blog.studyhex.in/how-a-browser-works-a-beginner-friendly-guide-to-browser-internals">How a browser works</a></li>
</ol>
<h2>Introduction</h2>
<p>We all know that on a website, there are mainly 3 thing exists</p>
<ol>
<li><p>HTML (Hyper Text Markup Language)</p>
</li>
<li><p>CSS ( Cascading Style Sheet)</p>
</li>
<li><p>JavaScript</p>
</li>
</ol>
<p>HTML is responsible for the structure of a website, CSS gives the styling of that structure, but JavaScript usually gives the functionallity part to it.</p>
<h2>History of JavaScript</h2>
<p>JavaScript was created in May 1995 by Brendan Eich at Netscape to make websites interactive. Originally named <em>Mocha</em> and then <em>LiveScript</em>, it was renamed <em>JavaScript</em> in December 1995 to ride the popularity of Java. It was designed as a lightweight scripting language for client-side web development and was quickly built into the Netscape Navigator browser.</p>
<p>In 1996, JavaScript was submitted to ECMA International, leading to the standardized <strong>ECMAScript</strong> specification, with ECMAScript 1 released in 1997. During the late 1990s, browser competition caused compatibility issues, but the language continued evolving. ECMAScript 3 (1998) introduced key features like regular expressions and error handling.</p>
<p>Around 1997, JavaScript became central to <strong>Dynamic HTML (DHTML)</strong>, combining HTML, CSS, and JavaScript to create interactive pages without reloading—laying the foundation for modern web applications.</p>
<h2>JavaScript and Factory</h2>
<p>To understand the internal workings of JavaScript, let's take an example of a Wood Factory. We all know some kind of factory where furniture are build up. Let's try to understand their process, what are the step they follow to build furniture?</p>
<h3>Step 1: Preparing the Wood</h3>
<p>We know that there are many kinds of wood available in the market.<br />1. Deodar Wood<br />2. Sheesham<br />3. Sal Wood</p>
<p>Each wood has a few pros and cons like this</p>
<p><strong>Deodar Wood:</strong> Lightweight, aromatic, and insect-resistant, but softer and less durable for heavy furniture.</p>
<p><strong>Sheesham:</strong> Strong, long-lasting hardwood with beautiful grain, but heavy and relatively expensive.</p>
<p><strong>Sal Wood:</strong> Extremely tough and rot-resistant construction wood, but very heavy and hard to work with.<br />So the point is, if we know which wood will serve our purpose, our upcoming work become more easy.</p>
<h3>Step 2: Cutting The Wood</h3>
<p>Once the wood enters the factory, each factory has a few sections to solve specific tasks. Like a furniture factory, after it receives the wood, it should cut it based on the requirements.</p>
<h3>Step 3: Framing the Wood</h3>
<p>Once the wood is cut into peices it should be send the the next segment of the factory where each peice combined to build the final furntiure</p>
<h3>Step 4: Polishing &amp; QA</h3>
<p>In this step, polishing and quality assurance are checked on the final furniture.</p>
<h3>The Manager (Ji)</h3>
<p>In a furniture factory, a manager controls the whole process and keeps all departments connected. Each department tells the manager when they finish their work. For example, if the cutting team finds damaged wood, they inform the manager. The manager decides to send it for repair. After the repair is done, the repair team tells the manager, and the manager tells the cutting team to continue. This system keeps work organized, avoids mistakes, and makes production smooth.</p>
<img src="https://cloudmate-test.s3.us-east-1.amazonaws.com/uploads/covers/6953c37709349c47ab7f5fc0/b1897bd3-233f-47ba-beb1-1599c43a3490.png" alt="" style="display:block;margin:0 auto" />

<h2>Now let's connect the JavaScript with the Factory.</h2>
<h3>Datatypes</h3>
<p>In a factory, remember the wood, in JavaScript, they are Datatypes. Each datatype has its own capabilities and features. Like wood, here also we need to understand which datatypes do the job for us. Sometimes selecting the perfect datatype also does major role in the final product.</p>
<h3>Global Execution Context</h3>
<p>This is a fancy name for a factory. Here same like a factory in JS, we have a global execution context. Like a factory, we need some flow, like if a furtinue to be build we need to follow each step. Global execution context is actually the code that is not bind in a function or object. We can say this holds the code that is important to run the factory.</p>
<h3>Local Execution Context</h3>
<p>This is another fancy name for a department. Like in a factory, we have a cutting department, then framing, and so on. In JS, the Local Execution Context is the function that does the specific job as required.</p>
<h3>Space</h3>
<p>For Global Execution Context and Local Execution Context, both has thier own space. Each space has 2 parts itself-</p>
<ol>
<li><p>Execution Thread</p>
</li>
<li><p>Memory</p>
</li>
</ol>
<p>For example</p>
<pre><code class="language-bash"># This is an JavaScript code
let n=2
funtion sum(num){
    return num+n;
}
# Here n goes into global execution context memory
# and num goes into local execution context memory
</code></pre>
<img src="https://cloudmate-test.s3.us-east-1.amazonaws.com/uploads/covers/6953c37709349c47ab7f5fc0/5fbd2163-706b-44cf-ae55-f8e9264e828c.png" alt="" style="display:block;margin:0 auto" />

<h3>Call Stack</h3>
<p>It maintains the function flow. Same as manager ji. It manages all the function calls, and it follows the stack (LIFO) principle.</p>
<h2>Conclusion</h2>
<p>JavaScript may seem complex internally, but when broken down into concepts like execution contexts, memory, and the call stack, it follows a clear and logical process. Understanding these basics builds a strong foundation that makes learning advanced JavaScript concepts much easier.</p>
]]></content:encoded></item><item><title><![CDATA[CSS Selectors 101: Targeting Elements with Precision]]></title><description><![CDATA[Overview
In this article, we are going to learn about CSS selectors, why we need selectors, and the meaning and working of CSS. But before starting the CSS journey, if you don’t know HTML, follow the HTML article.
Why we need CSS
As we all know, HTML...]]></description><link>https://blog.studyhex.in/css-selectors-101-targeting-elements-with-precision</link><guid isPermaLink="true">https://blog.studyhex.in/css-selectors-101-targeting-elements-with-precision</guid><category><![CDATA[HTML5]]></category><category><![CDATA[CSS]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Thu, 29 Jan 2026 12:32:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769689910500/6952ec68-53ab-4f59-969c-3525567c573a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-overview">Overview</h2>
<p>In this article, we are going to learn about CSS selectors, why we need selectors, and the meaning and working of CSS. But before starting the CSS journey, if you don’t know HTML, follow the <a target="_blank" href="https://blog.studyhex.in/understanding-html-tags-and-elements">HTML</a> article.</p>
<h2 id="heading-why-we-need-css">Why we need CSS</h2>
<p>As we all know, HTML can provide structure to a web page. But how the webpage should look, how each HTML element should be positioned, everything can’t be managed using HTML only. So we need some kind of rules that can be defined to give html element a proper look and feel with the corresponding position of each HTML element. Here CSS comes into the picture, we can write all rules, then the browser reads those rules and apply accordingly to the elements, and we can see some beautiful UI (User Interface) / UX (User Experience).</p>
<h2 id="heading-meaning-of-css">Meaning of CSS</h2>
<p>CSS stands for <strong>Cascading Style Sheets.</strong> Let’s try to understand the meaning of each word.</p>
<p><strong>Cascading</strong></p>
<p>If you are going on a vacation with 3 of your friends, and you are the driver, then you rely only on the friend who actually knows the place. So here you have a priority preference. Cascading in CSS also does the same. If multiple CSS rule applied to a single element, CSS decides which one to use based on order and its specific priority. CSS calls this the specificity algorithm.</p>
<p><strong>Style Sheets</strong></p>
<p>It is a file that contains the CSS rules.</p>
<p>Syntax Example</p>
<pre><code class="lang-plaintext">h1 {
    color:red;
}
</code></pre>
<h2 id="heading-different-ways-to-write-css-rules">Different Ways to Write CSS Rules</h2>
<p>We have mainly these three ways to write CSS</p>
<ol>
<li><p>Inline CSS</p>
</li>
<li><p>Internal CSS</p>
</li>
<li><p>External CSS</p>
</li>
</ol>
<p><strong>Inline CSS</strong></p>
<p>Here, we write CSS directly into the element within a style attribute</p>
<pre><code class="lang-plaintext">&lt;h1 style="color:red;"&gt;Heading&lt;/h1&gt;
</code></pre>
<h2 id="heading-selector-in-css">Selector in CSS</h2>
<p>As we all know, in HTML, we have elements, and to apply CSS rules, we have a selector to select those elements. We have multiple selectors in CSS, which are as follows</p>
<ol>
<li><p>Element Selector</p>
</li>
<li><p>Class Selector</p>
</li>
<li><p>ID Selector</p>
</li>
<li><p>Group Selector</p>
</li>
<li><p>Desendent Selector</p>
</li>
</ol>
<h3 id="heading-1-element-selector">1) Element Selector</h3>
<p>The element selector targets elements by their tag name. It applies the same style to all matching elements on the page.</p>
<p><strong>Example</strong></p>
<pre><code class="lang-plaintext">p {
  color: blue;
  font-size: 18px;
}
</code></pre>
<pre><code class="lang-plaintext">&lt;p&gt;Hello, this is a paragraph&lt;/p&gt;
</code></pre>
<p><strong>Output</strong></p>
<p><strong>Before CSS output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769688807482/f597d7c4-ab6b-4429-8ad0-de38e8e10780.png" alt class="image--center mx-auto" /></p>
<p><strong>After CSS Output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769688749027/d33e867a-e7d6-45f9-991e-f1cbe6596318.png" alt /></p>
<h3 id="heading-2-class-selector">2) Class Selector</h3>
<p>The class selector targets elements that have a specific <code>class</code> attribute. We can reuse classes. A single element can belong to different classes, and a single class can be used in multiple elements.</p>
<p><strong>Example</strong></p>
<pre><code class="lang-plaintext">&lt;p class="highlight fontRed"&gt;This is highlighted text&lt;/p&gt;
&lt;p&gt;This is normal text&lt;/p&gt;
</code></pre>
<pre><code class="lang-plaintext">.highlight {
  font-weight: bold;
}
.fontRed {
    color:Red;
}
</code></pre>
<p><strong>Before applying CSS output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769688860266/3d06abf2-9cfa-4491-98be-081465fbb6fe.png" alt /></p>
<p><strong>After CSS output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769688881868/c49613f9-de41-4386-ba03-21489c4f3cd9.png" alt /></p>
<h3 id="heading-3-id-selector">3) ID Selector</h3>
<p>The ID selector targets a single unique element using its <code>id</code> attribute.</p>
<p>Note: ID is unique and can be used only once on a page. One Element only have single ID</p>
<p><strong>Example</strong></p>
<pre><code class="lang-plaintext">&lt;h1 id="main-title"&gt;Welcome to My Website&lt;/h1&gt;
</code></pre>
<pre><code class="lang-plaintext">#main-title {
  color: green;
  text-align: center;
}
</code></pre>
<p><strong>Before CSS Output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769688941581/cc5f1167-cb13-45f0-b7f1-2752d68c5f86.png" alt /></p>
<p><strong>After CSS Output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769688959379/efa84cb1-6854-4999-852e-ad91d7f99a50.png" alt /></p>
<h2 id="heading-class-vs-id">Class Vs ID</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769689260257/5fc1865d-7bdb-4857-be6e-2239412f8781.png" alt /></p>
<h3 id="heading-4-group-selector">4) Group Selector</h3>
<p>The group selector is used when we want to apply the same CSS rules to multiple selectors at once. We separate selectors using commas.</p>
<p><strong>Example</strong></p>
<pre><code class="lang-plaintext">h1, h2, p {
  font-family: Arial, sans-serif;
  color: #333;
}
</code></pre>
<pre><code class="lang-plaintext">  &lt;h1&gt;This is Heading 1&lt;/h1&gt;
  &lt;h2&gt;This is Heading 2&lt;/h2&gt;
  &lt;p&gt;This is a paragraph text&lt;/p&gt;
</code></pre>
<p><strong>Before CSS Output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769689023919/8e9818a4-2b6d-47d5-af5b-e6f1a3b634ec.png" alt /></p>
<p><strong>After CSS Output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769689052291/b8a7dbd5-69fa-4a68-88d1-7093c454b7d8.png" alt /></p>
<h3 id="heading-5-descendant-selector">5) Descendant Selector</h3>
<p>The descendant selector targets elements that are inside another element. It is useful when you want to style only elements within a specific section.</p>
<p><strong>Example</strong></p>
<pre><code class="lang-plaintext">&lt;div class="card"&gt;
  &lt;p&gt;This paragraph is inside the card&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;This paragraph is outside&lt;/p&gt;
</code></pre>
<pre><code class="lang-plaintext">.card p {
  color: purple;
}
</code></pre>
<p><strong>Before CSS output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769689092270/069e74a3-a231-4053-a69f-38f9b6691540.png" alt /></p>
<p><strong>After CSS Output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769689113349/9aa85d0d-602e-441f-a7e0-36bc65478a6a.png" alt /></p>
<h3 id="heading-how-to-use">How to Use</h3>
<p>During the design of a web page, it’s always a good practice to use all of this method with a combination based on your usecase. Assume you can define the same font-family for all heading then you can use the element selector. If you have a few beautiful cards to display processing you can use classes. If you want to change the heading text color which is the main heading of the page, then you can use ID to particularly target that.</p>
<h2 id="heading-mechanism-of-cascading-and-specificity-algorithm">Mechanism of Cascading and Specificity Algorithm</h2>
<p><strong>Let’s try to understand the Cascading first.</strong></p>
<p>As you can see, we have a heading Specificity Algorithm</p>
<pre><code class="lang-plaintext">&lt;h1&gt;Heading&lt;/h1&gt;
</code></pre>
<p>Let’s apply some CSS here</p>
<pre><code class="lang-plaintext">h1 {
    text-align:right;
    color:red;
    border: 2px solid blue;

}
</code></pre>
<p>As you can see The heading Text color changed to red, and it shifted to the right side.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769687969734/82f4b8d7-32f7-43f7-9552-1fcc26fdc520.png" alt class="image--center mx-auto" /></p>
<p>Here you can see we did some changes to already applied properties, and it successfully take effect as well, and not made are working fine. This is cascading, where priority goes to the order, which means which property at the bottom of the CSS only takes that.</p>
<pre><code class="lang-plaintext">  h1 {
    text-align:right;
    color:red;
    border: 2px solid blue;
  }
  h1{
    text-align:center;
    color:gray;
  }
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769688025650/28fc3824-3a8a-4b3d-9b71-a2070c962793.png" alt class="image--center mx-auto" /></p>
<p><strong>Now time to understand the Specificity Algorithm</strong></p>
<p>Let’s take a heading which has an ID and a class</p>
<pre><code class="lang-plaintext">&lt;h1 id="color-pink" class="color-blue"&gt;Heading&lt;/h1&gt;
</code></pre>
<p>Now apply some CSS</p>
<pre><code class="lang-plaintext">  #color-pink{
      color:pink;
  }
  .color-blue{
      color:blue;
  }
  h1 {
    color:red;
    border: 2px solid blue;
  }
</code></pre>
<p>As you can see in the final output, instead of following the properties order, it always gives ID to the height priority, the class, and then the element selector gets the priority.<br />The priority Order (High to Low)</p>
<ol>
<li><p>Inline CSS</p>
</li>
<li><p>ID Selector</p>
</li>
<li><p>Class Selector</p>
</li>
<li><p>Element Selector</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769688286086/56d8a31f-8e9d-4896-a80a-6f27a6e13e23.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-list-of-most-used-basic-css-properties">List of Most Used Basic CSS Properties</h3>
<p>These are the fundamental properties you will use in almost every project.</p>
<h4 id="heading-text-amp-fonts"><strong>Text &amp; Fonts</strong></h4>
<ul>
<li><p><code>color</code>: Sets the color of the text (e.g., <code>red</code>, <code>#333</code>).</p>
</li>
<li><p><code>font-family</code>: Sets the font type (e.g., <code>Arial</code>, <code>sans-serif</code>).</p>
</li>
<li><p><code>font-size</code>: Sets the size of the text (e.g., <code>16px</code>, <code>1.2rem</code>).</p>
</li>
<li><p><code>font-weight</code>: Sets the thickness of the text (e.g., <code>bold</code>, <code>400</code>).</p>
</li>
<li><p><code>text-align</code>: Aligns text horizontally (e.g., <code>center</code>, <code>left</code>, <code>right</code>).</p>
</li>
<li><p><code>line-height</code>: Sets the vertical space between lines of text.</p>
</li>
</ul>
<h4 id="heading-the-box-model-size-amp-spacing"><strong>The Box Model (Size &amp; Spacing)</strong></h4>
<ul>
<li><p><code>width</code> / <code>height</code>: Sets the width and height of an element.</p>
</li>
<li><p><code>margin</code>: Space <strong>outside</strong> the border (pushes other elements away).</p>
</li>
<li><p><code>padding</code>: Space <strong>inside</strong> the border (pushes content away from the border).</p>
</li>
<li><p><code>border</code>: A line around the element (between margin and padding).</p>
</li>
<li><p><code>border-radius</code>: Rounds the corners of the border.</p>
</li>
</ul>
<h4 id="heading-backgrounds"><strong>Backgrounds</strong></h4>
<ul>
<li><p><code>background-color</code>: Sets a solid background color.</p>
</li>
<li><p><code>background-image</code>: Sets an image as the background.</p>
</li>
</ul>
<h4 id="heading-layout-amp-positioning"><strong>Layout &amp; Positioning</strong></h4>
<ul>
<li><p><code>display</code>: Determines how an element behaves.</p>
<ul>
<li><p><code>block</code>: Takes up the full width (like <code>&lt;div&gt;</code>, <code>&lt;h1&gt;</code>).</p>
</li>
<li><p><code>inline</code>: Takes up only the necessary width (like <code>&lt;span&gt;</code>).</p>
</li>
<li><p><code>flex</code>: Enables Flexbox layout (puts items in a row).</p>
</li>
</ul>
</li>
<li><p><code>position</code>: Used for advanced positioning (<code>relative</code>, <code>absolute</code>, <code>fixed</code>).</p>
</li>
</ul>
<p>Example</p>
<pre><code class="lang-plaintext">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;Basic CSS Properties&lt;/title&gt;
    &lt;style&gt;
        /* 1. Base Styles (Text and Background) */
        body {
            background-color: #f0f2f5; /* Light grey background */
            font-family: Arial, sans-serif;
            color: #333333; /* Dark grey text */
            margin: 0px; /* Reset default browser margin */
        }

        /* 2. Main Container (Layout) */
        .main-container {
            width: 600px;
            margin: 50px auto; /* auto centers the container horizontally */
            background-color: #ffffff;
            border: 1px solid #cccccc; /* Thin grey border */
            padding: 30px; /* Space inside the container */
        }

        /* 3. Header Section (Text Alignment) */
        .header {
            text-align: center;
            border-bottom: 2px solid #eeeeee;
            padding-bottom: 20px;
            margin-bottom: 20px; /* Space below the header */
        }

        .header h1 {
            color: #2c3e50;
            font-size: 28px;
            margin: 0px; /* Remove default margin */
        }

        .header span {
            color: #7f8c8d; /* Lighter text for subtitle */
            font-size: 14px;
        }

        /* 4. Flexbox Layout (Side by Side) */
        .content-box {
            display: flex; /* Makes children (divs) sit in a row */
            gap: 20px; /* Space between the two columns */
        }

        /* 5. Columns */
        .left-column {
            width: 30%;
            background-color: #3498db;
            color: white;
            padding: 15px;
            text-align: center;
            border-radius: 8px; /* Rounded corners */
        }

        .right-column {
            width: 70%;
        }

        /* 6. Typography adjustments */
        .right-column h2 {
            font-size: 20px;
            margin-top: 0px;
        }

        .right-column p {
            line-height: 1.6; /* Makes text easier to read */
            font-size: 16px;
        }

        /* 7. Button Style (Static - No Hover) */
        .btn {
            display: inline-block;
            background-color: #27ae60;
            color: white;
            padding: 10px 20px;
            text-decoration: none; /* Removes underline from link */
            border-radius: 5px;
            font-weight: bold;
            margin-top: 10px;
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

    &lt;div class="main-container"&gt;

        &lt;div class="header"&gt;
            &lt;h1&gt;Jane Doe&lt;/h1&gt;
            &lt;span&gt;Senior Web Developer&lt;/span&gt;
        &lt;/div&gt;

        &lt;div class="content-box"&gt;

            &lt;div class="left-column"&gt;
                &lt;strong&gt;Status:&lt;/strong&gt;&lt;br&gt;
                Active
            &lt;/div&gt;

            &lt;div class="right-column"&gt;
                &lt;h2&gt;About Me&lt;/h2&gt;
                &lt;p&gt;
                    I am a developer learning CSS basics. 
                    I understand how margin, padding, and borders work. 
                    This layout uses a simple flexbox row to put items side by side.
                &lt;/p&gt;
                &lt;a class="btn" href="#"&gt;Contact Me&lt;/a&gt;
            &lt;/div&gt;

        &lt;/div&gt;

    &lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><strong>Output</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769689681085/93dfb85e-4999-4342-8ba2-ab05821291c8.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>You don’t need to memorise all CSS properties; CSS is a skill which become strong eventually with practice. When you build 20 or 30 projects using CSS, you willbe able to build an understanding of each property</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769689842026/3c2199f5-7133-4eeb-be61-6d69085cb76c.png" alt class="image--center mx-auto" /></p>
<p>.</p>
]]></content:encoded></item><item><title><![CDATA[How a Browser Works: A Beginner-Friendly Guide to Browser Internals]]></title><description><![CDATA[Overview
In this article, we are going to have a proper discussion about browsers and how a browser works. That is behind the scenes, how a user types a URL and actually sees the webpage.
What a Browser Actually Is (Beyond “It Opens Websites”)
When w...]]></description><link>https://blog.studyhex.in/how-a-browser-works-a-beginner-friendly-guide-to-browser-internals</link><guid isPermaLink="true">https://blog.studyhex.in/how-a-browser-works-a-beginner-friendly-guide-to-browser-internals</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Browsers]]></category><category><![CDATA[engineering]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Wed, 28 Jan 2026 20:17:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769631373926/a8074260-56e1-44b6-b42f-eda6534ab740.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-overview">Overview</h3>
<p>In this article, we are going to have a proper discussion about browsers and how a browser works. That is behind the scenes, how a user types a URL and actually sees the webpage.</p>
<h2 id="heading-what-a-browser-actually-is-beyond-it-opens-websites">What a Browser Actually Is (Beyond “It Opens Websites”)</h2>
<p>When we think browser, in our mind first comes something like Chrome or Firefox, we have a URL input box, we type something like google.com, and we see the actual page. But in reality, there is so much happening behind the scenes.</p>
<p>A browser is almost a mini Operating System itself. It downloads web files (HTML/CSS/JS/images), understands them, and turns them into what you see on screen.</p>
<h2 id="heading-a-browser-is-a-team-of-components-not-one-single-app">A Browser is a Team of Components (Not One Single App)</h2>
<p>Assume Browser is a football match. To complete the match successfully, we need multiple players with different roles (Defender, Midfielder, etc.)</p>
<p>The same happened in the browser. It has multiple components for multiple tasks</p>
<ul>
<li><p>downloading files</p>
</li>
<li><p>understanding HTML</p>
</li>
<li><p>understanding CSS</p>
</li>
<li><p>running JavaScript</p>
</li>
<li><p>drawing pixels on screen</p>
</li>
</ul>
<h2 id="heading-here-is-the-high-level-flow">Here is the high-level flow:</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769628753838/efe4b9da-87e5-42ea-b8e4-ce005a7122b5.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-main-parts-of-a-browser-high-level-overview">Main Parts of a Browser (High-Level Overview)</h2>
<h3 id="heading-1-user-interface-ui">1. User Interface (UI)</h3>
<p>This is what you interact with:</p>
<ul>
<li><p>address bar</p>
</li>
<li><p>back/forward buttons</p>
</li>
<li><p>tabs</p>
</li>
<li><p>bookmark menu</p>
</li>
<li><p>download bar</p>
</li>
</ul>
<h3 id="heading-2-browser-engine">2. Browser Engine</h3>
<p>This acts like the <strong>manager</strong>.</p>
<p>It connects UI actions to the rendering engine. If the user types a website URL, the browser sends that request to the rendering engine to display the website to the user.</p>
<h3 id="heading-3-rendering-engine">3. Rendering Engine</h3>
<p>This is the most important worker.</p>
<p>It uses HTML and CSS to render (draw**)** the page.</p>
<p>Examples:</p>
<ul>
<li><p>Chromium browsers → Blink rendering engine</p>
</li>
<li><p>Firefox → Gecko rendering engine</p>
</li>
</ul>
<h3 id="heading-4-networking">4. Networking</h3>
<p>This is the downloader team.</p>
<p>It fetches resources like:</p>
<ul>
<li><p>HTML</p>
</li>
<li><p>CSS</p>
</li>
<li><p>JS</p>
</li>
<li><p>images</p>
</li>
<li><p>fonts</p>
</li>
</ul>
<h3 id="heading-5-javascript-interpreter-js-engine">5. JavaScript Interpreter (JS Engine)</h3>
<p>This runs JavaScript.</p>
<p>Example:</p>
<ul>
<li><p>Chromium → V8</p>
</li>
<li><p>Firefox → SpiderMonkey</p>
</li>
</ul>
<p>This is what makes sites <strong>interactive</strong>.</p>
<h3 id="heading-6-ui-backend-diskstorage">6. UI Backend + Disk/Storage</h3>
<p>Used for:</p>
<ul>
<li><p>drawing basic UI elements</p>
</li>
<li><p>saving cache</p>
</li>
<li><p>cookies</p>
</li>
<li><p>localStorage</p>
</li>
<li><p>history</p>
</li>
</ul>
<h1 id="heading-browser-engine-vs-rendering-engine-simple-distinction">Browser Engine vs Rendering Engine (Simple Distinction)</h1>
<p>Many beginners confuse these two. Here’s a very simple rule:</p>
<h3 id="heading-browser-engine">Browser Engine</h3>
<ul>
<li><p>connects UI actions to the rendering process</p>
</li>
<li><p>handles navigation, page load</p>
</li>
</ul>
<h3 id="heading-rendering-engine">Rendering Engine</h3>
<ul>
<li><p>reads HTML + CSS</p>
</li>
<li><p>builds the visual output</p>
</li>
</ul>
<p>So, the browser engine decides what to do. The rendering engine builds what you see.</p>
<h2 id="heading-what-happens-after-you-type-a-url-and-press-enter">What Happens After You Type a URL and Press Enter?</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769629938870/d6c87cbd-35d9-4c41-be93-20a25d0e3fb3.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-step-1-the-url-is-processed">Step 1: The URL is processed</h3>
<p>You type:</p>
<p><a target="_blank" href="http://studyhex.com"><code>studyhex.com</code></a></p>
<p>Browser adds protocol if missing:</p>
<p><a target="_blank" href="https://studyhex.com"><code>https://studyhex.com</code></a></p>
<h3 id="heading-step-2-dns-lookup-find-ip-address">Step 2: DNS Lookup (Find IP Address)</h3>
<p>Websites don’t live on domain names.<br />They live on IP addresses.</p>
<p>So the browser asks DNS:</p>
<blockquote>
<p>“What is the IP of <a target="_blank" href="http://studyhex.com">studyhex.com</a>?”</p>
</blockquote>
<p>DNS replies:</p>
<p><code>123.45.67.89</code> (example)</p>
<h3 id="heading-step-3-connection-to-server">Step 3: Connection to Server</h3>
<p>The browser creates a connection using:</p>
<ul>
<li><p>TCP connection</p>
</li>
<li><p>TLS handshake (if HTTPS)</p>
</li>
</ul>
<p>Now it can securely talk to the server.</p>
<h3 id="heading-step-4-http-request-sent">Step 4: HTTP Request Sent</h3>
<p>The browser sends a request like:</p>
<blockquote>
<p>“Give me the HTML of the homepage.”</p>
</blockquote>
<p>Server replies with an HTML file.</p>
<h3 id="heading-4-networking-how-browser-fetches-html-css-js">4) Networking: How Browser Fetches HTML, CSS, JS</h3>
<p>The browser starts downloading:</p>
<p>First: HTML<br />Then finds inside HTML:</p>
<pre><code class="lang-plaintext">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;TEST&lt;/title&gt;
    &lt;link rel="stylesheet" href="style.css"&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Heading&lt;/h1&gt;
    &lt;p&gt;Paragraph&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Now the browser says:</p>
<ul>
<li><p>“Download index.html”</p>
</li>
<li><p>“Download style.css.” When it sees the link stylesheet, it goes and downloads that</p>
</li>
</ul>
<p>So the networking part becomes busy.</p>
<h3 id="heading-5-html-parsing-and-dom-creation">5) HTML Parsing and DOM Creation</h3>
<p>Now the browser has HTML text.<br />But computers don’t work well with raw text.</p>
<p>So the browser does <strong>HTML Parsing</strong>.</p>
<h3 id="heading-what-is-parsing">What is parsing?</h3>
<p>Parsing means:</p>
<p>Convert raw text into a structured meaning.</p>
<h3 id="heading-dom-document-object-model">DOM (Document Object Model)</h3>
<p>When a browser parses HTML, it creates the <strong>DOM Tree</strong>.</p>
<p>Example HTML:</p>
<pre><code class="lang-plaintext">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;!-- Meta --&gt;
    &lt;meta charset="UTF-8" /&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;

    &lt;!-- Title --&gt;
    &lt;title&gt;My First HTML Document&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;!-- H1 --&gt;
    &lt;h1&gt;Welcome to my website&lt;/h1&gt;

    &lt;!-- H2 --&gt;
    &lt;h2&gt;This is a sub heading&lt;/h2&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>DOM tree becomes like:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769630314171/5b350c4e-f94e-43f6-89a1-58f4a249c628.png" alt class="image--center mx-auto" /></p>
<p>DOM is like a <strong>tree of elements</strong>. DOM = HTML converted into a tree structure</p>
<h3 id="heading-6-css-parsing-and-cssom-creation">6) CSS Parsing and CSSOM Creation</h3>
<p>The same thing happens for CSS.</p>
<p>Example CSS:</p>
<pre><code class="lang-plaintext">* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: Arial, sans-serif;
  background: #0f172a; 
  color: #f8fafc;   
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 20px;
}

h1 {
  font-size: 2.2rem;
  font-weight: 700;
  margin-bottom: 12px;
}

h2 {
  font-size: 1.2rem;
  font-weight: 400;
  color: #cbd5e1; 
}
</code></pre>
<p>Browser parses CSS and creates:</p>
<p><strong>CSSOM (CSS Object Model)</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769630615677/61868e6e-c536-4e9c-aa9a-db710adbac7b.png" alt class="image--center mx-auto" /></p>
<p><strong>CSSOM = CSS converted into a structured model</strong></p>
<h3 id="heading-7-how-dom-and-cssom-come-together">7) How DOM and CSSOM Come Together</h3>
<p>Now we have:</p>
<ul>
<li><p>DOM (structure)</p>
</li>
<li><p>CSSOM (style rules)</p>
</li>
</ul>
<p>The browser combines them to create:</p>
<p><strong>Render Tree</strong></p>
<p>Render Tree includes only the visible elements.</p>
<p>Example:</p>
<ul>
<li>elements like <code>display:none</code> will not be included</li>
</ul>
<p>So DOM + CSSOM → Render Tree</p>
<h3 id="heading-8-layout-reflow-where-everything-gets-its-position">8) Layout (Reflow): Where Everything Gets Its Position</h3>
<p>Now the browser knows:</p>
<ul>
<li><p>What elements exist</p>
</li>
<li><p>their styles</p>
</li>
</ul>
<p>But still it doesn’t know:</p>
<ul>
<li><p>Where should each element be placed?</p>
</li>
<li><p>How big should it be?</p>
</li>
</ul>
<p>So it performs:</p>
<p><strong>Layout / Reflow</strong></p>
<p>This calculates:</p>
<ul>
<li><p>position (x, y)</p>
</li>
<li><p>width and height</p>
</li>
<li><p>spacing</p>
</li>
<li><p>alignment</p>
</li>
</ul>
<p>Example:</p>
<ul>
<li><p>“h1 should start at 20px from top”</p>
</li>
<li><p>“p should be below h1.”</p>
</li>
</ul>
<h3 id="heading-9-painting-converting-layout-into-pixels">9) Painting: Converting Layout into Pixels</h3>
<p>After the layout is done, the browser starts:</p>
<p>Painting</p>
<p>This means:</p>
<ul>
<li><p>background colors</p>
</li>
<li><p>borders</p>
</li>
<li><p>text drawing</p>
</li>
<li><p>images</p>
</li>
</ul>
<p>It paints in layers.</p>
<h3 id="heading-10-display-showing-the-final-output">10) Display: Showing the Final Output</h3>
<p>Now the browser sends the painted result to:</p>
<ul>
<li><p>the screen display system</p>
</li>
<li><p>And you finally see the website</p>
</li>
</ul>
<h2 id="heading-very-basic-idea-of-parsing">Very Basic Idea of Parsing</h2>
<pre><code class="lang-plaintext">"10 + 20 * 3 +4 % 3"
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769631026468/f4037e0f-be4c-4893-9537-f4fde00480e9.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>As a beginner, you don’t need to memorize every component. Just try to understand the basic flow.</p>
<ol>
<li><p>URL entered</p>
</li>
<li><p>request sent</p>
</li>
<li><p>HTML downloaded</p>
</li>
<li><p>DOM created</p>
</li>
<li><p>CSSOM created</p>
</li>
<li><p>Render tree created</p>
</li>
<li><p>layout (reflow)</p>
</li>
<li><p>paint</p>
</li>
<li><p>display</p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[The End of the Battle Between TCP and UDP]]></title><description><![CDATA[In this article, we will discuss the key differences between TCP and UDP, including their uses. But before starting the discussion, if you don’t know about TCP or the Internet, please prefer to read the Working of TCP article first.
Overview
As you k...]]></description><link>https://blog.studyhex.in/the-end-of-the-battle-between-tcp-and-udp</link><guid isPermaLink="true">https://blog.studyhex.in/the-end-of-the-battle-between-tcp-and-udp</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[TCP]]></category><category><![CDATA[UDP]]></category><category><![CDATA[http]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Wed, 28 Jan 2026 17:50:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769622580696/df82a05d-9c13-4271-8906-6507cfccbb8b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, we will discuss the key differences between TCP and UDP, including their uses. But before starting the discussion, if you don’t know about TCP or the Internet, please prefer to read the <a target="_blank" href="https://blog.studyhex.in/tcp-working-3-way-handshake-and-reliable-communication">Working of TCP</a> article first.</p>
<h2 id="heading-overview">Overview</h2>
<p>As you know, to send some data between devices that are connected to the internet, need some kind of rules and regulations to transmit the data. We have these two commonly used internet data transfer protocols.</p>
<ol>
<li><p>TCP (Transmission Control Protocol)</p>
</li>
<li><p>UDP (User Datagram Protocol)</p>
</li>
</ol>
<p>Let’s start by understanding the inner working is layman's terms.</p>
<h2 id="heading-working-of-tcp">Working of TCP</h2>
<p>In TCP, when a client needs some data from the server, we need a connection, and the client does not know whether the connection exists or not. Therefore, when a client wants to send some data, TCP tries to build a strong connection using 3-Way handshaking. This is nothing special, but a smart mechanism. In this mechanism, the client first says “Hey server, I want to talk to you”, if the server gets the data, then the server says “okay, along with I am ready”, and if the client gets “okay”. Then the client sends “okay” to the server again. (Note that this is only the analogy; if you want to know the real thing, follow our TCP Internal article)</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769284444328/dd3f8dd1-9b4e-4950-a673-b28a5ef06f59.png" alt /></p>
<p>Once this connection is established client actually starts sending the data.</p>
<p><strong>Benefits of TCP</strong></p>
<ol>
<li><p>Due to the smart handshaking, it is reliable.</p>
</li>
<li><p>If the client sends some data and does not receive the acknowledgment client starts to retransmit the data</p>
</li>
<li><p>TCP maintains the order of data packets by only sending the next data packet after getting an acknowledgment for the previous packet from the server.</p>
</li>
<li><p>TCP actually attaches some extra bits of data so that it can handle errors in the data during transmission.</p>
</li>
</ol>
<p><strong>Problems of TCP</strong></p>
<ol>
<li><p>Everytime clinet need to send some data, it actually creates a strong connection,n which is time-consuming.</p>
</li>
<li><p>This is slow.</p>
</li>
<li><p>The working of handshaking is confusing for beginners.</p>
</li>
</ol>
<p><strong>Usages</strong></p>
<ol>
<li><p>Where data reliability matters, TCP is widely preferred to use.</p>
</li>
<li><p>Transactional SMS of a bank.</p>
</li>
<li><p>Finance management application.</p>
</li>
</ol>
<h2 id="heading-working-of-udp">Working of UDP</h2>
<p>If you remove the handshaking and the acknowledgement part from TCP, we are actually left with the protocol named UDP.</p>
<p><strong>Benefits of UDP</strong></p>
<ol>
<li><p>It doesn’t need any connection; due to this, UDP is very fast.</p>
</li>
<li><p>It can send data in real time because the speed of data transfer is too high.</p>
</li>
</ol>
<p><strong>Problem of UDP</strong></p>
<ol>
<li><p>It is not reliable.</p>
</li>
<li><p>It doesn’t take the guarantee of successful data transmission.</p>
</li>
<li><p>Data may be received in the wrong order.</p>
</li>
</ol>
<p><strong>Usages</strong></p>
<ol>
<li><p>Where data should be transferred in real time, UDP is preferred to use.</p>
</li>
<li><p>Real-time Video calling app</p>
</li>
<li><p>Live streaming</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769622208291/734984e8-e505-497f-b8ee-7a5ca5f548c4.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-http-vs-tcp">HTTP vs TCP</h2>
<p>This is a topic where many beginners get confused between HTTP and TCP. TCP provides reliable delivery for any application data (web, mail, file transfer, etc.) in a safe and ordered manner. Whereas HTTP (HyperText Transfer Protocol) is an application-layer protocol used to transfer web resources such as HTML pages, CSS files, images, and JSON data. To send this HTTP data over the internet, HTTP usually runs on top of TCP, and TCP ensures the data is delivered correctly and in order.</p>
<p>Actually, TCP is working on the OSI layer 4 or TCP/IP layer 2. And HTTP is an application-layer protocol. So HTTP runs on top of TCP. It depends on TCP as the transport.</p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20230417045622/OSI-vs-TCP-vs-Hybrid-2.webp" alt="TCP/IP Model - GeeksforGeeks" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Both TCP and UDP has there one benefit with their own trade-offs. We as users select between these two protocol as your usecase. If reliability and data consistency are our main priority, then TCP is the best. If real-time data transmission is our main focus, reliability is a secondary concern, then UDP is the best.</p>
]]></content:encoded></item><item><title><![CDATA[Getting Started with cURL]]></title><description><![CDATA[Overview
Before understanding the CURL, we need to understand the Client and Server; on the Internet, most command word is client mean which always want some data from the server. and Server is providing that data to the client.

What is CURL
The cur...]]></description><link>https://blog.studyhex.in/getting-started-with-curl</link><guid isPermaLink="true">https://blog.studyhex.in/getting-started-with-curl</guid><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Tue, 27 Jan 2026 20:46:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769546716068/e2fa6d1d-9544-4650-b3e1-b46ca4553c91.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-overview">Overview</h2>
<p>Before understanding the CURL, we need to understand the Client and Server; on the Internet, most command word is client mean which always want some data from the server. and Server is providing that data to the client.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769544017383/309b8cba-b340-4b90-b12f-d300a5f4e21c.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-what-is-curl">What is CURL</h2>
<p>The <strong>curl</strong> command in Linux is a command-line tool for transferring data between a system and a server using various network protocols. It is primarily used to fetch web content, test APIs, and send or receive data over the network.</p>
<ul>
<li><p>Supports multiple protocols such as HTTP, HTTPS, FTP, and SCP</p>
</li>
<li><p>Used to download, upload, and send data from the terminal</p>
</li>
<li><p>Helpful for testing REST APIs and web services</p>
</li>
<li><p>Works with headers, authentication, and data formats like JSON</p>
</li>
</ul>
<h2 id="heading-why-programmers-need-curl">Why programmers need cURL</h2>
<ol>
<li><p>Without having a frontend, a programmer can test the Backend API.</p>
</li>
<li><p>Debug backend issues and fix them quickly during creation time.</p>
</li>
<li><p>With <code>curl</code>, you hit the API directly:</p>
<ul>
<li><p>No browser cache</p>
</li>
<li><p>No JavaScript</p>
</li>
<li><p>No UI bugs</p>
</li>
</ul>
</li>
<li><p>This is fast and simple.</p>
</li>
</ol>
<p>Example:</p>
<pre><code class="lang-bash">curl example.com
</code></pre>
<p>Output</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769542420960/26cea74e-9676-48df-8a9b-48f65b5830c0.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-request-and-response">Request and Response</h2>
<p>When you run a CURL command, mainly two thing happening in the behind the scenes</p>
<ol>
<li><h3 id="heading-request">Request</h3>
<p> In this case, you send a request from the terminal to the server. In this step, you can specify the request method, request header, and most important thing request URL.</p>
</li>
<li><h3 id="heading-response">Response</h3>
<p> In this case server send response, and you can see it on the terminal. This includes Status Code (200, 2001, 301, 404, 500, etc.), Data</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769544315180/29729474-0251-40d6-a4f2-6580f05312b9.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-curl-command-guide">CURL command Guide</h2>
<h3 id="heading-get-request">GET Request</h3>
<ol>
<li><em>To make a simple GET request, just type curl &lt;url&gt; this make a simple GET request.</em></li>
</ol>
<pre><code class="lang-bash">curl example.com
</code></pre>
<p>This command will retrieve the HTML content of the specified URL and display it in the terminal.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769544472025/f1f69e57-242b-4cad-9c1a-e441b9dfa5d9.png" alt class="image--center mx-auto" /></p>
<ol start="2">
<li><em>If you want to save that file in our system, you can use curl options</em></li>
</ol>
<pre><code class="lang-bash"><span class="hljs-comment"># curl -o &lt;file_name_as_you_want&gt; &lt;url&gt;</span>
curl -o index.html example.com <span class="hljs-comment"># -o to save a file. It takes a file name</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769544646228/5e3c0037-8778-4a7b-b41e-0bd8a34f7b7e.png" alt class="image--center mx-auto" /></p>
<ol start="3">
<li><em>If you want to see the response header, you can use</em> <code>-I</code> <em>option with the curl command</em></li>
</ol>
<pre><code class="lang-bash">curl -I example.com
</code></pre>
<p>As you can see here, the status we get in response is 200, which means a successful request. Also, the Data of the response and Content-Type is HTML, and the server from which the response come form is Cloudflare.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769544803955/00ff5c56-8012-4fda-a60b-3c40ee400873.png" alt class="image--center mx-auto" /></p>
<ol start="4">
<li>If you want to download a file using curl, you can use the capital -O option, which downloads the file; this takes the file's original name from the server.</li>
</ol>
<pre><code class="lang-bash">curl -O https://acrobat.adobe.com/us/en/why-adobe/about-adobe-pdf.html
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769545222313/08aeb86a-80b8-4032-9f1c-3147a8965b9f.png" alt class="image--center mx-auto" /></p>
<ol start="5">
<li>If you want to get a api data, you can use -X, then you can specify the request method, in our case, that is GET for now</li>
</ol>
<pre><code class="lang-bash">curl -X GET https://jsonplaceholder.typicode.com/posts
</code></pre>
<p>Output</p>
<pre><code class="lang-bash">[
  {
    <span class="hljs-string">"userId"</span>: 1,
    <span class="hljs-string">"id"</span>: 1,
    <span class="hljs-string">"title"</span>: <span class="hljs-string">"sunt aut facere repellat provident occaecati excepturi optio reprehenderit"</span>,
    <span class="hljs-string">"body"</span>: <span class="hljs-string">"quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"</span>
  },
  {
    <span class="hljs-string">"userId"</span>: 1,
    <span class="hljs-string">"id"</span>: 2,
    <span class="hljs-string">"title"</span>: <span class="hljs-string">"qui est esse"</span>,
    <span class="hljs-string">"body"</span>: <span class="hljs-string">"est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"</span>
  },
  {
    <span class="hljs-string">"userId"</span>: 1,
    <span class="hljs-string">"id"</span>: 3,
    <span class="hljs-string">"title"</span>: <span class="hljs-string">"ea molestias quasi exercitationem repellat qui ipsa sit aut"</span>,
    <span class="hljs-string">"body"</span>: <span class="hljs-string">"et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"</span>
  },
  {
    <span class="hljs-string">"userId"</span>: 1,
    <span class="hljs-string">"id"</span>: 4,
    <span class="hljs-string">"title"</span>: <span class="hljs-string">"eum et est occaecati"</span>,
    <span class="hljs-string">"body"</span>: <span class="hljs-string">"ullam et saepe reiciendis voluptatem adipisci\nsit amet autem assumenda provident rerum culpa\nquis hic commodi nesciunt rem tenetur doloremque ipsam iure\nquis sunt voluptatem rerum illo velit"</span>
  }...]
</code></pre>
<h3 id="heading-post-request">Post Request</h3>
<ol>
<li>We can also make a post request using -X with the POST method</li>
</ol>
<pre><code class="lang-bash">curl -X POST -d <span class="hljs-string">"title=foo&amp;body=bar&amp;userId=1"</span> https://jsonplaceholder.typicode.com/posts
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769545675175/9f48d4d7-c325-4792-94cd-302779ffc533.png" alt class="image--center mx-auto" /></p>
<p>Let’s try to talk with the API</p>
<pre><code class="lang-bash">curl -X PATCH -d <span class="hljs-string">'{"title":"foo"}'</span> https://jsonplaceholder.typicode.com/posts/1
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769545948771/90ef3597-b4be-4bc6-b8ed-9c69a42ff0df.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769546199140/ca45cc26-5342-488a-bbe0-69cefa88ea4f.png" alt class="image--center mx-auto" /></p>
<p>Using curl, a developer can Test, Debug api endpoint very fast, which improves the speed and rapid development.</p>
<h2 id="heading-things-that-a-beginner-make-mistake-about-curl">Things that a beginner make mistake about curl</h2>
<p>CURL just goes through the RAW html and api response; it does not run JavaScript and CSS.<br />Also beginner don’t use the protocol without currect protocol; curl sometime misbehave.</p>
<h2 id="heading-more-option-on-curl">More Option on curl</h2>
<h3 id="heading-examples-of-curl-with-options-reference-sourcehttpswwwgeeksforgeeksorglinux-unixcurl-command-in-linux-with-examples"><strong>Examples of Curl with Options Reference (</strong><a target="_blank" href="https://www.geeksforgeeks.org/linux-unix/curl-command-in-linux-with-examples/"><strong>Source</strong></a><strong>)</strong></h3>
<p>Given below are some examples demonstrating the use of the curl command in Linux along with their options.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Option</strong></td><td><strong>What It Does</strong></td></tr>
</thead>
<tbody>
<tr>
<td><code>[URL]</code></td><td>(No option) Prints URL content to <code>stdout</code>.</td></tr>
<tr>
<td><code>-o filename</code></td><td>Saves output to a <strong>o</strong>ne, custom <code>filename</code>.</td></tr>
<tr>
<td><code>-O</code></td><td>Saves output using the <strong>O</strong>riginal filename from the URL.</td></tr>
<tr>
<td><code>-C -</code></td><td><strong>C</strong>ontinues/Resumes an interrupted download.</td></tr>
<tr>
<td><code>-X METHOD</code></td><td>Specifies the HTTP method (e.g., <code>-X POST</code>, <code>-X DELETE</code>).</td></tr>
<tr>
<td><code>-d "data"</code></td><td>Sends <strong>d</strong>ata in a <code>POST</code> or <code>PUT</code> request.</td></tr>
<tr>
<td><code>-H "Header"</code></td><td>Adds a custom HTTP <strong>H</strong>eader (e.g., for JSON or auth tokens).</td></tr>
<tr>
<td><code>-L</code></td><td>Follows any server redirects (e.g., 301, 302).</td></tr>
<tr>
<td><code>-u user:pass</code></td><td>Provides <strong>u</strong>ser authentication credentials.</td></tr>
<tr>
<td><code>-T file.txt</code></td><td><strong>T</strong>ransfers (uploads) a local file to a destination.</td></tr>
<tr>
<td><code>-I</code></td><td>Fetches headers only (HTTP <code>HEAD</code> request).</td></tr>
<tr>
<td><code>-i</code></td><td><strong>I</strong>ncludes the HTTP response headers in the output.</td></tr>
<tr>
<td><code>-s</code></td><td><strong>S</strong>ilent mode (hides progress meter).</td></tr>
<tr>
<td><code>-#</code></td><td>Shows a simple progress bar.</td></tr>
</tbody>
</table>
</div><ol>
<li><h3 id="heading-c-option"><strong>-C - Option:</strong></h3>
</li>
</ol>
<p>This option resumes the download that has been stopped due to some reason. This is useful when downloading large files and was interrupted. </p>
<p><strong>Example:</strong></p>
<pre><code class="lang-bash">curl -C - -O ftp://speedtest.tele2.net/1MB.zip
</code></pre>
<p><strong>Output:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769546348706/42a846df-901b-4aa4-8207-57555449cae9.png" alt class="image--center mx-auto" /></p>
<ol start="2">
<li><h3 id="heading-u-option"><strong>-u Option:</strong></h3>
</li>
</ol>
<p>curl also provides options to download files from user-authenticated FTP servers. </p>
<p><strong>Syntax:</strong></p>
<pre><code class="lang-bash">curl -u {username}:{password} [FTP_URL]
</code></pre>
<p><strong>Example:</strong></p>
<pre><code class="lang-bash">curl -u demo:password -O ftp://test.rebex.net/readme.txt
</code></pre>
<p><strong>Output:</strong> </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769546445076/3597fe9f-56c0-48b1-bdfa-4493d2c4353f.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>cURL is a simple yet powerful tool that helps you understand how clients and servers communicate by letting you test APIs and servers directly from the terminal. You don’t need to remember every option—just practice the basics, and the rest will naturally make sense as you build and debug real projects.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding HTML Tags and Elements]]></title><description><![CDATA[Overview
In today’s article, we are going to understand what HTML is and why it is the foundation of Web-Development.
What HTML is and why we use it
We can assume HTML is like the skeleton of a human. This is used to define the structure of a webpage...]]></description><link>https://blog.studyhex.in/understanding-html-tags-and-elements</link><guid isPermaLink="true">https://blog.studyhex.in/understanding-html-tags-and-elements</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[HTML5]]></category><category><![CDATA[HTML]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Tue, 27 Jan 2026 11:36:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769513718010/01608a93-4575-4660-9de0-e856760818ca.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-overview">Overview</h2>
<p>In today’s article, we are going to understand what HTML is and why it is the foundation of Web-Development.</p>
<h2 id="heading-what-html-is-and-why-we-use-it">What HTML is and why we use it</h2>
<p>We can assume HTML is like the skeleton of a human. This is used to define the structure of a webpage. Then we have CSS, which wraps that skeleton with skin, and the OG JavaScript, which acts like a Brain.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769508724476/1545a2f4-431e-46f2-8d03-112f90cc3d17.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-why-html">Why HTML?</h2>
<p>Before 1989, everyone mostly shared information on computers as plain text.</p>
<p>Then, in 1989, Sir Tim Berners-Lee proposed the World Wide Web (WWW) while working at CERN. To simplify the web so that researchers can share documents and link them together over the internet.</p>
<p>In the WWW, Tim Berners-Lee proposed a few standards, which are follow:</p>
<ol>
<li><strong>HTML (Hypertext Markup Language)</strong></li>
</ol>
<ul>
<li><p>Instead of writing text, we write a language that can give structure to the content.</p>
</li>
<li><p>Example: Headings, paragraphs, links, images</p>
</li>
</ul>
<ol start="2">
<li><strong>HTTP (HyperText Transfer Protocol)</strong></li>
</ol>
<ul>
<li><p>A communication rule/protocol to <strong>send and receive web pages</strong></p>
</li>
<li><p>It defines how browsers and servers talk</p>
</li>
</ul>
<ol start="3">
<li><strong>URL (Uniform Resource Locator)</strong></li>
</ol>
<ul>
<li><p>A unique address for every document/page</p>
</li>
<li><p>Example: <code>https://example.com/page</code></p>
</li>
</ul>
<p>Because HTML became the <strong>standard way to create and structure web pages</strong>, browsers can read it and display content properly.</p>
<h2 id="heading-what-an-html-tag">What an HTML tag</h2>
<p>In HTML, to give structure to our content, we use a TAG, a tag is nothing but a writing style for a particular thing with a meaning.<br />Example:</p>
<p>If we write an article, then that is supposed to have a heading. In HTML, for that, we have the Heading tag</p>
<pre><code class="lang-bash">&lt;h1&gt;&lt;/h1&gt;
</code></pre>
<p>If we write a paragraph, in HTML, we have a paragraph tag also</p>
<pre><code class="lang-bash">&lt;p&gt;&lt;/p&gt;
</code></pre>
<p>Now, here you can see that to write a tag, we use the less-than and greater-than symbols <code>&lt; &gt;</code> , but in our code we already see that we are also use forward slash at the end of that text like this <code>&lt;/&gt;</code>. The reason behind this<br />In HTML, we start a tag with &lt;&gt;, but the browser does not know where the tag is supposed to end. To solve that, we have a closing tag, which follows this syntax &lt;/&gt;  </p>
<pre><code class="lang-bash">&lt;&gt; <span class="hljs-comment"># tag opening</span>
&lt;/&gt; <span class="hljs-comment"># tag closing</span>
<span class="hljs-comment"># Example</span>
&lt;h1&gt;&lt;/h1&gt;
</code></pre>
<h2 id="heading-what-is-an-html-element">What is an HTML Element</h2>
<p>So many beginners actually had confusion on this topic. When we wrap content with closing and opening tag then that is known as Han TML Element.</p>
<p>Example:</p>
<pre><code class="lang-bash">&lt;h1&gt;&lt;/h2&gt; <span class="hljs-comment"># Tag </span>
&lt;h1&gt; Heading &lt;/h2&gt; <span class="hljs-comment"># Element</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769510710720/0729f53c-d0c4-4514-a1d5-7b10ecfd649e.png" alt class="image--center mx-auto" /></p>
<p>In HTML, we have mainly 2 types of element is there</p>
<ul>
<li><p>Block-level Element</p>
</li>
<li><p>Inline Element</p>
</li>
</ul>
<h3 id="heading-block-level-element">Block-Level Element</h3>
<ul>
<li><p>When we use these element in our HTML, then it awasy start from a new line.</p>
</li>
<li><p>It also takes the full width or we can say the entier line.</p>
</li>
<li><p>You can set <strong>width/height/margin/padding</strong></p>
</li>
</ul>
<p>Examples of Block elements:</p>
<pre><code class="lang-bash">&lt;div&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h1&gt;&lt;/h1&gt; to &lt;h6&gt;&lt;/h6&gt;
&lt;section&gt;&lt;/section&gt;
&lt;article&gt;&lt;/article&gt;
&lt;header&gt;&lt;/header&gt;
&lt;footer&gt;&lt;/footer&gt;
&lt;main&gt;&lt;/main&gt;
&lt;nav&gt;&lt;/nav&gt;
&lt;aside&gt;&lt;/aside&gt;

&lt;ul&gt;&lt;/ul&gt;
&lt;ol&gt;&lt;/ol&gt;
&lt;li&gt;&lt;/li&gt;

&lt;form&gt;&lt;/form&gt;
&lt;table&gt;&lt;/table&gt;
</code></pre>
<h3 id="heading-inline-elements">Inline Elements</h3>
<ul>
<li><p>This element doesn’t start form new line</p>
</li>
<li><p>This only takes space, so it just perfectly fits, and no extra space is used.</p>
</li>
<li><p><strong>width/height usually won’t work</strong> properly</p>
</li>
</ul>
<p>Examples of Inline elements:</p>
<pre><code class="lang-bash">&lt;span&gt;&lt;/span&gt;
&lt;a&gt;&lt;/a&gt;
&lt;b&gt;&lt;/b&gt;
&lt;i&gt;&lt;/i&gt;
&lt;strong&gt;&lt;/strong&gt;
&lt;em&gt;&lt;/em&gt;
&lt;img /&gt;
&lt;input /&gt;
&lt;label&gt;&lt;/label&gt;
&lt;button&gt;&lt;/button&gt;
</code></pre>
<h2 id="heading-commonly-used-html-tags">Commonly used HTML tags</h2>
<h3 id="heading-yga"><code>&lt;html&gt;</code></h3>
<p>The whole webpage stays inside this tag.</p>
<pre><code class="lang-bash">&lt;html&gt;
  ...
&lt;/html&gt;
</code></pre>
<h3 id="heading-yga-1"><code>&lt;head&gt;</code></h3>
<p>Stores page info (title, CSS, meta). Not visible.</p>
<pre><code class="lang-bash">&lt;head&gt;
  &lt;title&gt;Home&lt;/title&gt;
&lt;/head&gt;
</code></pre>
<h3 id="heading-yga-2"><code>&lt;title&gt;</code></h3>
<p>Shows page name in browser tab.</p>
<pre><code class="lang-bash">&lt;title&gt;My Website&lt;/title&gt;
</code></pre>
<h3 id="heading-yga-3"><code>&lt;body&gt;</code></h3>
<p>All visible page content stays inside this.</p>
<pre><code class="lang-bash">&lt;body&gt;
  &lt;h1&gt;Hello&lt;/h1&gt;
&lt;/body&gt;
</code></pre>
<h2 id="heading-text-tags">Text tags</h2>
<h3 id="heading-to"><code>&lt;h1&gt;</code> to <code>&lt;h6&gt;</code></h3>
<p>Headings (h1 biggest, h6 smallest).</p>
<pre><code class="lang-bash">&lt;h1&gt;Main Title&lt;/h1&gt;
&lt;h3&gt;Small Title&lt;/h3&gt;
</code></pre>
<h3 id="heading-yga-4"><code>&lt;p&gt;</code></h3>
<p>Paragraph text.</p>
<pre><code class="lang-bash">&lt;p&gt;This is a paragraph.&lt;/p&gt;
</code></pre>
<h3 id="heading-yga-5"><code>&lt;br&gt;</code></h3>
<p>New line inside text.</p>
<pre><code class="lang-bash">&lt;p&gt;Hello&lt;br&gt;World&lt;/p&gt;
</code></pre>
<p>Now just look at the combined code I prepare what i learn till now</p>
<pre><code class="lang-bash">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;My Website&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;h1&gt;Main Title (h1)&lt;/h1&gt;
    &lt;p&gt;This is a paragraph under h1.&lt;/p&gt;

    &lt;h2&gt;Heading (h2)&lt;/h2&gt;
    &lt;p&gt;This is a paragraph under h2.&lt;/p&gt;

    &lt;h3&gt;Heading (h3)&lt;/h3&gt;
    &lt;p&gt;This is a paragraph under h3.&lt;/p&gt;

    &lt;h4&gt;Heading (h4)&lt;/h4&gt;
    &lt;p&gt;This is a paragraph under h4.&lt;/p&gt;

    &lt;h5&gt;Heading (h5)&lt;/h5&gt;
    &lt;p&gt;This is a paragraph under h5.&lt;/p&gt;

    &lt;h6&gt;Heading (h6)&lt;/h6&gt;
    &lt;p&gt;This is a paragraph under h6.&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Output</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769512404122/0033f6b3-166d-4c85-8477-0bd9b702f9ba.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-yga-6"><code>&lt;hr&gt;</code></h3>
<p>Horizontal line to separate sections.</p>
<pre><code class="lang-bash">&lt;hr&gt;
</code></pre>
<p><code>&lt;br&gt;</code></p>
<p>Use to give a new line break</p>
<pre><code class="lang-bash">&lt;br&gt;
</code></pre>
<p>Example:</p>
<pre><code class="lang-bash">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;HR and BR Example&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;h1&gt;HR and BR Example&lt;/h1&gt;

    &lt;p&gt;
      This is line 1 using normal text.
      &lt;br&gt;
      This is line 2 after using br tag.
      &lt;br&gt;
      This is line 3 after using br tag again.
    &lt;/p&gt;

    &lt;hr&gt;

    &lt;p&gt;
      This section is separated using hr tag.
      &lt;br&gt;
      hr creates a horizontal line between sections.
    &lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Output</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769512526700/90a3c49f-b1e1-4bc5-9cdc-1b7999b8fadd.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-groupinglayout-tags">Grouping/layout tags</h2>
<h3 id="heading-yga-7"><code>&lt;div&gt;</code></h3>
<p>Block container for grouping elements.</p>
<pre><code class="lang-bash">&lt;div&gt;
  &lt;h2&gt;Box&lt;/h2&gt;
  &lt;p&gt;Text&lt;/p&gt;
&lt;/div&gt;
</code></pre>
<h3 id="heading-yga-8"><code>&lt;span&gt;</code></h3>
<p>Inline container for a small text part.</p>
<pre><code class="lang-bash">&lt;p&gt;Hello &lt;span&gt;World&lt;/span&gt;&lt;/p&gt;
</code></pre>
<h2 id="heading-link-and-media-tags">Link and media tags</h2>
<h3 id="heading-yga-9"><code>&lt;a&gt;</code></h3>
<p>Link to other page/website.</p>
<pre><code class="lang-bash">&lt;a href=<span class="hljs-string">"https://google.com"</span>&gt;Go to Google&lt;/a&gt;
</code></pre>
<h3 id="heading-yga-10"><code>&lt;img&gt;</code></h3>
<p>Shows image.</p>
<pre><code class="lang-bash">&lt;img src=<span class="hljs-string">"photo.jpg"</span> alt=<span class="hljs-string">"my photo"</span>&gt;
</code></pre>
<h3 id="heading-yga-11"><code>&lt;video&gt;</code></h3>
<p>Shows video.</p>
<pre><code class="lang-bash">&lt;video controls&gt;
  &lt;<span class="hljs-built_in">source</span> src=<span class="hljs-string">"movie.mp4"</span>&gt;
&lt;/video&gt;
</code></pre>
<h3 id="heading-yga-12"><code>&lt;audio&gt;</code></h3>
<p>Plays audio.</p>
<pre><code class="lang-bash">&lt;audio controls&gt;
  &lt;<span class="hljs-built_in">source</span> src=<span class="hljs-string">"song.mp3"</span>&gt;
&lt;/audio&gt;
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769512713094/557ceb06-e17e-4a04-9bfc-a605d50077be.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-list-tags">List tags</h2>
<h3 id="heading-yga-13"><code>&lt;ul&gt;</code></h3>
<p>Bullet list.</p>
<pre><code class="lang-bash">&lt;ul&gt;
  &lt;li&gt;Apple&lt;/li&gt;
  &lt;li&gt;Mango&lt;/li&gt;
&lt;/ul&gt;
</code></pre>
<h3 id="heading-yga-14"><code>&lt;ol&gt;</code></h3>
<p>Number list.</p>
<pre><code class="lang-bash">&lt;ol&gt;
  &lt;li&gt;Step 1&lt;/li&gt;
  &lt;li&gt;Step 2&lt;/li&gt;
&lt;/ol&gt;
</code></pre>
<h3 id="heading-yga-15"><code>&lt;li&gt;</code></h3>
<p>List item (inside ul/ol).</p>
<pre><code class="lang-bash">&lt;li&gt;Item&lt;/li&gt;
</code></pre>
<p>Example:</p>
<pre><code class="lang-bash">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;List Tags Demo&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;h1&gt;List Tags Demo&lt;/h1&gt;
    &lt;p&gt;
      In this page we will learn unordered list (ul), ordered list (ol) and list item (li).
    &lt;/p&gt;

    &lt;hr&gt;

    &lt;h2&gt;1) Unordered List (ul) - Bullet List&lt;/h2&gt;
    &lt;p&gt;Shopping list:&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;Milk&lt;/li&gt;
      &lt;li&gt;Bread&lt;/li&gt;
      &lt;li&gt;Eggs&lt;/li&gt;
      &lt;li&gt;Fruits&lt;/li&gt;
    &lt;/ul&gt;

    &lt;hr&gt;

    &lt;h2&gt;2) Ordered List (ol) - Number List&lt;/h2&gt;
    &lt;p&gt;Steps to create a project:&lt;/p&gt;

    &lt;ol&gt;
      &lt;li&gt;Create a folder&lt;/li&gt;
      &lt;li&gt;Create an index.html file&lt;/li&gt;
      &lt;li&gt;Write HTML code&lt;/li&gt;
      &lt;li&gt;Open it <span class="hljs-keyword">in</span> browser&lt;/li&gt;
    &lt;/ol&gt;

    &lt;hr&gt;

    &lt;h2&gt;3) li tag (List Item)&lt;/h2&gt;
    &lt;p&gt;
      li tag is used inside ul or ol. Example:
    &lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;This is an li item inside ul&lt;/li&gt;
      &lt;li&gt;This is another li item inside ul&lt;/li&gt;
    &lt;/ul&gt;

    &lt;ol&gt;
      &lt;li&gt;This is an li item inside ol&lt;/li&gt;
      &lt;li&gt;This is another li item inside ol&lt;/li&gt;
    &lt;/ol&gt;

  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Output</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769512777716/ab7cc434-745e-430d-9daf-aeea4b797371.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-table-tags">Table tags</h2>
<h3 id="heading-yga-16"><code>&lt;table&gt;</code></h3>
<p>Creates a table.</p>
<pre><code class="lang-bash">&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Name&lt;/th&gt;
    &lt;th&gt;Age&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Pallab&lt;/td&gt;
    &lt;td&gt;22&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
</code></pre>
<h3 id="heading-yga-17"><code>&lt;tr&gt;</code></h3>
<p>Table row.</p>
<pre><code class="lang-bash">&lt;tr&gt;
  &lt;td&gt;A&lt;/td&gt;
  &lt;td&gt;B&lt;/td&gt;
&lt;/tr&gt;
</code></pre>
<h3 id="heading-yga-18"><code>&lt;th&gt;</code></h3>
<p>Table heading cell.</p>
<pre><code class="lang-bash">&lt;th&gt;Name&lt;/th&gt;
</code></pre>
<h3 id="heading-yga-19"><code>&lt;td&gt;</code></h3>
<p>Table data cell.</p>
<pre><code class="lang-bash">&lt;td&gt;Pallab&lt;/td&gt;
</code></pre>
<p>Example</p>
<pre><code class="lang-bash">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Table Tags Demo&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;h1&gt;Table Tags Demo&lt;/h1&gt;
    &lt;p&gt;
      In this page we will learn table tags: table, tr, th, td.
    &lt;/p&gt;

    &lt;hr&gt;

    &lt;h2&gt;Student List Table&lt;/h2&gt;

    &lt;table border=<span class="hljs-string">"1"</span> cellpadding=<span class="hljs-string">"10"</span>&gt;
      &lt;!-- table row <span class="hljs-keyword">for</span> table headings --&gt;
      &lt;tr&gt;
        &lt;th&gt;Student Name&lt;/th&gt;
        &lt;th&gt;Age&lt;/th&gt;
        &lt;th&gt;Course&lt;/th&gt;
        &lt;th&gt;City&lt;/th&gt;
      &lt;/tr&gt;

      &lt;!-- table row <span class="hljs-keyword">for</span> student data --&gt;
      &lt;tr&gt;
        &lt;td&gt;Pallab&lt;/td&gt;
        &lt;td&gt;22&lt;/td&gt;
        &lt;td&gt;Web Development&lt;/td&gt;
        &lt;td&gt;Kolkata&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td&gt;Rahul&lt;/td&gt;
        &lt;td&gt;21&lt;/td&gt;
        &lt;td&gt;JavaScript&lt;/td&gt;
        &lt;td&gt;Delhi&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td&gt;Ananya&lt;/td&gt;
        &lt;td&gt;20&lt;/td&gt;
        &lt;td&gt;HTML &amp; CSS&lt;/td&gt;
        &lt;td&gt;Mumbai&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;

    &lt;hr&gt;

    &lt;h2&gt;Explanation inside same page&lt;/h2&gt;

    &lt;p&gt;
      table: creates the table structure.
      &lt;br&gt;
      tr: creates a row.
      &lt;br&gt;
      th: creates heading cells.
      &lt;br&gt;
      td: creates data cells.
    &lt;/p&gt;

  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Output</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769512831547/4f4716b4-0879-4897-9a8f-5f5da6937eac.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-form-tags">Form tags</h2>
<h3 id="heading-yga-20"><code>&lt;form&gt;</code></h3>
<p>Used to collect user input and submit.</p>
<pre><code class="lang-bash">&lt;form&gt;
  &lt;input <span class="hljs-built_in">type</span>=<span class="hljs-string">"text"</span>&gt;
  &lt;button&gt;Submit&lt;/button&gt;
&lt;/form&gt;
</code></pre>
<h3 id="heading-yga-21"><code>&lt;label&gt;</code></h3>
<p>Label for input field.</p>
<pre><code class="lang-bash">&lt;label&gt;Email:&lt;/label&gt;
&lt;input <span class="hljs-built_in">type</span>=<span class="hljs-string">"email"</span>&gt;
</code></pre>
<h3 id="heading-yga-22"><code>&lt;input&gt;</code></h3>
<p>Input field.</p>
<pre><code class="lang-bash">&lt;input <span class="hljs-built_in">type</span>=<span class="hljs-string">"password"</span> placeholder=<span class="hljs-string">"Enter password"</span>&gt;
</code></pre>
<h3 id="heading-ya"><code>&lt;textarea&gt;</code></h3>
<p>Multi-line input.</p>
<pre><code class="lang-bash">&lt;textarea rows=<span class="hljs-string">"4"</span>&gt;&lt;/textarea&gt;
</code></pre>
<h3 id="heading-yga-23"><code>&lt;select&gt;</code></h3>
<p>Dropdown.</p>
<pre><code class="lang-bash">&lt;select&gt;
  &lt;option&gt;India&lt;/option&gt;
  &lt;option&gt;USA&lt;/option&gt;
&lt;/select&gt;
</code></pre>
<h3 id="heading-yga-24"><code>&lt;button&gt;</code></h3>
<p>Clickable button.</p>
<pre><code class="lang-bash">&lt;button <span class="hljs-built_in">type</span>=<span class="hljs-string">"submit"</span>&gt;Submit&lt;/button&gt;
</code></pre>
<p>Example</p>
<pre><code class="lang-bash">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Form Tags Demo&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;h1&gt;Form Tags Demo&lt;/h1&gt;
    &lt;p&gt;
      In this page we will learn form tags: form, label, input, textarea, select, option, button.
    &lt;/p&gt;

    &lt;hr&gt;

    &lt;h2&gt;Contact Form&lt;/h2&gt;

    &lt;form&gt;
      &lt;!-- Name --&gt;
      &lt;label&gt;Full Name:&lt;/label&gt;
      &lt;input <span class="hljs-built_in">type</span>=<span class="hljs-string">"text"</span> placeholder=<span class="hljs-string">"Enter your full name"</span>&gt;
      &lt;br&gt;&lt;br&gt;

      &lt;!-- Email --&gt;
      &lt;label&gt;Email:&lt;/label&gt;
      &lt;input <span class="hljs-built_in">type</span>=<span class="hljs-string">"email"</span> placeholder=<span class="hljs-string">"Enter your email"</span>&gt;
      &lt;br&gt;&lt;br&gt;

      &lt;!-- Password --&gt;
      &lt;label&gt;Password:&lt;/label&gt;
      &lt;input <span class="hljs-built_in">type</span>=<span class="hljs-string">"password"</span> placeholder=<span class="hljs-string">"Enter password"</span>&gt;
      &lt;br&gt;&lt;br&gt;

      &lt;!-- Country --&gt;
      &lt;label&gt;Select Country:&lt;/label&gt;
      &lt;select&gt;
        &lt;option&gt;India&lt;/option&gt;
        &lt;option&gt;USA&lt;/option&gt;
        &lt;option&gt;UK&lt;/option&gt;
        &lt;option&gt;Canada&lt;/option&gt;
      &lt;/select&gt;
      &lt;br&gt;&lt;br&gt;

      &lt;!-- Message --&gt;
      &lt;label&gt;Your Message:&lt;/label&gt;
      &lt;br&gt;
      &lt;textarea rows=<span class="hljs-string">"5"</span> placeholder=<span class="hljs-string">"Write your message here..."</span>&gt;&lt;/textarea&gt;
      &lt;br&gt;&lt;br&gt;

      &lt;!-- Button --&gt;
      &lt;button <span class="hljs-built_in">type</span>=<span class="hljs-string">"submit"</span>&gt;Submit&lt;/button&gt;
      &lt;button <span class="hljs-built_in">type</span>=<span class="hljs-string">"reset"</span>&gt;Reset&lt;/button&gt;
    &lt;/form&gt;

    &lt;hr&gt;

    &lt;p&gt;
      form tag collects data,
      label shows field name,
      input takes user value,
      textarea takes multi line input,
      select creates dropdown,
      option is dropdown item,
      button is <span class="hljs-keyword">for</span> submit/reset.
    &lt;/p&gt;

  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Output</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769512926960/9f66c7ef-3835-4870-9809-1fba40bfca84.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-semantic-tags-for-accessibility">Semantic tags for Accessibility</h2>
<p>This tag is useful in the case that a user has visual disability, then the screen reader reads this tag peoperly. Also, it is a good practice to use these Semantic tags.</p>
<h3 id="heading-yga-25"><code>&lt;header&gt;</code></h3>
<p>Top section of a page/section.</p>
<pre><code class="lang-bash">&lt;header&gt;
  &lt;h1&gt;My Blog&lt;/h1&gt;
&lt;/header&gt;
</code></pre>
<h3 id="heading-yga-26"><code>&lt;nav&gt;</code></h3>
<p>Navigation links.</p>
<pre><code class="lang-bash">&lt;nav&gt;
  &lt;a href=<span class="hljs-string">"/"</span>&gt;Home&lt;/a&gt;
  &lt;a href=<span class="hljs-string">"/about"</span>&gt;About&lt;/a&gt;
&lt;/nav&gt;
</code></pre>
<h3 id="heading-yga-27"><code>&lt;main&gt;</code></h3>
<p>Main content.</p>
<pre><code class="lang-bash">&lt;main&gt;
  &lt;h2&gt;Post&lt;/h2&gt;
&lt;/main&gt;
</code></pre>
<h3 id="heading-yga-28"><code>&lt;section&gt;</code></h3>
<p>Groups related content.</p>
<pre><code class="lang-bash">&lt;section&gt;
  &lt;h2&gt;Services&lt;/h2&gt;
&lt;/section&gt;
</code></pre>
<h3 id="heading-yga-29"><code>&lt;footer&gt;</code></h3>
<p>Bottom section.</p>
<pre><code class="lang-bash">&lt;footer&gt;
  &lt;p&gt;Copyright 2026&lt;/p&gt;
&lt;/footer&gt;
</code></pre>
<p>Example</p>
<pre><code class="lang-bash">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Semantic Tags Demo&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;

    &lt;!-- header: top part of the website --&gt;
    &lt;header&gt;
      &lt;h1&gt;StudyHex Blog&lt;/h1&gt;
      &lt;p&gt;Learn Web Development step by step&lt;/p&gt;
    &lt;/header&gt;

    &lt;hr&gt;

    &lt;!-- nav: navigation links --&gt;
    &lt;nav&gt;
      &lt;a href=<span class="hljs-string">"#"</span>&gt;Home&lt;/a&gt; |
      &lt;a href=<span class="hljs-string">"#"</span>&gt;Blogs&lt;/a&gt; |
      &lt;a href=<span class="hljs-string">"#"</span>&gt;Courses&lt;/a&gt; |
      &lt;a href=<span class="hljs-string">"#"</span>&gt;Contact&lt;/a&gt;
    &lt;/nav&gt;

    &lt;hr&gt;

    &lt;!-- main: main page content --&gt;
    &lt;main&gt;

      &lt;h2&gt;Welcome to my blog&lt;/h2&gt;
      &lt;p&gt;
        This website is built using semantic HTML tags.
        These tags <span class="hljs-built_in">help</span> screen readers understand page structure properly.
      &lt;/p&gt;

      &lt;!-- section: related content group --&gt;
      &lt;section&gt;
        &lt;h2&gt;Services&lt;/h2&gt;
        &lt;p&gt;We provide learning content <span class="hljs-keyword">for</span>:&lt;/p&gt;
        &lt;ul&gt;
          &lt;li&gt;HTML&lt;/li&gt;
          &lt;li&gt;CSS&lt;/li&gt;
          &lt;li&gt;JavaScript&lt;/li&gt;
          &lt;li&gt;Backend Development&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/section&gt;

      &lt;hr&gt;

      &lt;section&gt;
        &lt;h2&gt;Latest Blog Post&lt;/h2&gt;
        &lt;p&gt;
          Topic: How DNS works step by step.
        &lt;/p&gt;
        &lt;p&gt;
          In this blog you will learn Root server, TLD server, Authoritative server and Recursive Resolver.
        &lt;/p&gt;
      &lt;/section&gt;

    &lt;/main&gt;

    &lt;hr&gt;

    &lt;!-- footer: bottom part of the website --&gt;
    &lt;footer&gt;
      &lt;p&gt;Copyright 2026 - StudyHex&lt;/p&gt;
    &lt;/footer&gt;

  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Output</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769513021283/445b6295-7d4f-4fa7-9177-86ca97741506.png" alt class="image--center mx-auto" /></p>
<p>Conclusion:</p>
<p>HTML is the backbone of any website, and it is the basic building block of the modern internet. HTML has many tags, but remember one thing: only 20 to 30 percent of tags are used 80 percent of the time. If you want to learn more tags, you can follow the MDN HTML Elements documentation. Also, during your HTML learning time, always use the browser Inspect tool, because it helps you understand which element is responsible for what you are seeing on the page.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769513296849/647be59f-10a6-469b-afc1-14849b0005ea.png" alt class="image--center mx-auto" /></p>
<p>After building 10 to 20 projects successfully, you don’t need to remember all the tags. With practice, they automatically fit into your subconscious mind.</p>
]]></content:encoded></item><item><title><![CDATA[Emmet for HTML: A Beginner’s Guide to Writing Faster Markup]]></title><description><![CDATA[In Visual Studio Code (VS Code), we have Emmet. Emmet is a plugin that helps us write code faster by providing useful shorthand notation for writing repetitive things in HTML and CSS.
Why Emmet is useful for HTML beginners
In HTML, we code around mul...]]></description><link>https://blog.studyhex.in/emmet-for-html-a-beginners-guide-to-writing-faster-markup</link><guid isPermaLink="true">https://blog.studyhex.in/emmet-for-html-a-beginners-guide-to-writing-faster-markup</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Emmet]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Tue, 27 Jan 2026 09:52:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769507514864/9074c8d0-29d7-4132-ab02-d6d8f5885359.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In Visual Studio Code (VS Code), we have Emmet. Emmet is a plugin that helps us write code faster by providing useful shorthand notation for writing repetitive things in HTML and CSS.</p>
<h2 id="heading-why-emmet-is-useful-for-html-beginners">Why Emmet is useful for HTML beginners</h2>
<p>In HTML, we code around multiple tags. Writing these tags 1000 times, we can use Emmet Abstraction, which make coder fast.</p>
<p>example</p>
<pre><code class="lang-bash">div <span class="hljs-comment"># we just type this and hit -enter</span>

&lt;div&gt;&lt;/div&gt; <span class="hljs-comment"># emmet autocomplete this tags for us</span>
</code></pre>
<h2 id="heading-why-emmet-is-useful-for-html-beginners-1">Why Emmet is useful for HTML beginners</h2>
<p>For a beginner who is trying to learn the core concept of HTML and it’s working, emmit helps there to reduce the pressure of remembering all the syntax.</p>
<h3 id="heading-emmet-helps-you">Emmet helps you:</h3>
<ul>
<li><p>Write HTML <strong>10x faster</strong></p>
</li>
<li><p>Avoid spelling mistakes in tags</p>
</li>
<li><p>Generate nested structures easily</p>
</li>
<li><p>Create repeated elements in seconds</p>
</li>
<li><p>Build boilerplate HTML instantly</p>
</li>
</ul>
<h2 id="heading-how-emmet-works-inside-code-editors">How Emmet works inside code editors</h2>
<p>Emmet comes built-in inside most editors, especially:</p>
<h3 id="heading-how-you-use-it">How you use it</h3>
<ol>
<li><p>Open an HTML file (example: <code>index.html</code>)</p>
</li>
<li><p>Type an Emmet abbreviation</p>
</li>
<li><p>Press:</p>
<ul>
<li><p>Hit <code>Tab</code></p>
</li>
<li><p>or <code>Enter</code></p>
</li>
</ul>
</li>
</ol>
<p>Type:</p>
<pre><code class="lang-bash">h1
</code></pre>
<p>Press Enter, and it becomes:</p>
<pre><code class="lang-bash">&lt;h1&gt;&lt;/h1&gt;
</code></pre>
<h2 id="heading-useful-syntax-of-emmet">Useful Syntax of Emmet</h2>
<p>Emmet uses symbols like shortcuts.</p>
<p>Here are the <strong>most useful ones</strong>:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Symbol</td><td>Meaning</td></tr>
</thead>
<tbody>
<tr>
<td><code>&gt;</code></td><td>child (inside)</td></tr>
<tr>
<td><code>+</code></td><td>sibling (next to)</td></tr>
<tr>
<td><code>*</code></td><td>multiply/repeat</td></tr>
<tr>
<td><code>.</code></td><td>class</td></tr>
<tr>
<td><code>#</code></td><td>id</td></tr>
<tr>
<td><code>[]</code></td><td>attributes</td></tr>
</tbody>
</table>
</div><h2 id="heading-creating-html-elements-using-emmet">Creating HTML elements using Emmet</h2>
<h3 id="heading-example-1-simple-element">Example 1: Simple element</h3>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">p
</code></pre>
<p><strong>HTML output:</strong></p>
<pre><code class="lang-bash">&lt;p&gt;&lt;/p&gt;
</code></pre>
<h3 id="heading-example-2-element-with-inner-text">Example 2: Element with inner text</h3>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">p{Hello world}
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;p&gt;Hello world&lt;/p&gt;
</code></pre>
<h2 id="heading-adding-classes-ids-and-attributes">Adding classes, IDs, and attributes</h2>
<h3 id="heading-adding-a-class">Adding a class (<code>.</code>)</h3>
<p>Use tagname, then a single [dot] and classname</p>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">div.card
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;div class=<span class="hljs-string">"card"</span>&gt;&lt;/div&gt;
</code></pre>
<p>Even shorter:</p>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">.card
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;div class=<span class="hljs-string">"card"</span>&gt;&lt;/div&gt;
</code></pre>
<p>(because Emmet assumes <code>div</code> by default)</p>
<h3 id="heading-adding-an-id">Adding an ID (<code>#</code>)</h3>
<p>Use tagname then, Pound Simble ( # ) followed by Id</p>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">div<span class="hljs-comment">#main</span>
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;div id=<span class="hljs-string">"main"</span>&gt;&lt;/div&gt;
</code></pre>
<h3 id="heading-class-id-together">Class + ID together</h3>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">section<span class="hljs-comment">#hero.banner</span>
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;section id=<span class="hljs-string">"hero"</span> class=<span class="hljs-string">"banner"</span>&gt;&lt;/section&gt;
</code></pre>
<h3 id="heading-adding-attributes">Adding attributes (<code>[]</code>)</h3>
<p>Example link tag:</p>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">a[href=<span class="hljs-string">"https://google.com"</span>]
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;a href=<span class="hljs-string">"https://google.com"</span>&gt;&lt;/a&gt;
</code></pre>
<p>Example image:</p>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">img[src=<span class="hljs-string">"photo.jpg"</span> alt=<span class="hljs-string">"my image"</span>]
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;img src=<span class="hljs-string">"photo.jpg"</span> alt=<span class="hljs-string">"my image"</span>&gt;
</code></pre>
<p>Notice: <code>img</code> has no closing tag (Emmet knows that).</p>
<h2 id="heading-creating-nested-elements">Creating nested elements</h2>
<p>Nesting means element inside element.</p>
<p>Emmet uses:</p>
<p><code>&gt;</code></p>
<h3 id="heading-example-nav-with-list">Example: Nav with list</h3>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">nav&gt;ul&gt;li
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;nav&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/nav&gt;
</code></pre>
<h3 id="heading-example-card-layout">Example: Card layout</h3>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">div.card&gt;h2+p
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;div class=<span class="hljs-string">"card"</span>&gt;
  &lt;h2&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;/p&gt;
&lt;/div&gt;
</code></pre>
<h2 id="heading-creating-siblings">Creating siblings</h2>
<p>Emmet uses:</p>
<p><code>+</code></p>
<h3 id="heading-example">Example:</h3>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">h1+p
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;h1&gt;&lt;/h1&gt;
&lt;p&gt;&lt;/p&gt;
</code></pre>
<h3 id="heading-example-1">Example:</h3>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">header+main+footer
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;header&gt;&lt;/header&gt;
&lt;main&gt;&lt;/main&gt;
&lt;footer&gt;&lt;/footer&gt;
</code></pre>
<h2 id="heading-repeating-elements-using-multiplication">Repeating elements using multiplication (<code>*</code>)</h2>
<p>This is used to save a lot of time</p>
<h3 id="heading-example-5-list-items">Example: 5 list items</h3>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">ul&gt;li*5
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;ul&gt;
  &lt;li&gt;&lt;/li&gt;
  &lt;li&gt;&lt;/li&gt;
  &lt;li&gt;&lt;/li&gt;
  &lt;li&gt;&lt;/li&gt;
  &lt;li&gt;&lt;/li&gt;
&lt;/ul&gt;
</code></pre>
<h2 id="heading-combining-nesting-multiplication">Combining nesting + multiplication</h2>
<p>Note: This syntax is useful, but it's easy to forgot these syntax if you don’t practice and use it in your daily coding workflow, so always try to practice it. Also, keep in mind these are helpful for faster code writing, but if you can’t remember any of them, then it is totally fine.</p>
<h3 id="heading-example-menu-list">Example: Menu list</h3>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">nav&gt;ul&gt;li*4&gt;a[href=<span class="hljs-string">"#"</span>]
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;nav&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=<span class="hljs-string">"#"</span>&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span class="hljs-string">"#"</span>&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span class="hljs-string">"#"</span>&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span class="hljs-string">"#"</span>&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/nav&gt;
</code></pre>
<p>Now add text:</p>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">nav&gt;ul&gt;li*4&gt;a[href=<span class="hljs-string">"#"</span>]{Menu}
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">&lt;nav&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=<span class="hljs-string">"#"</span>&gt;Menu&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span class="hljs-string">"#"</span>&gt;Menu&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span class="hljs-string">"#"</span>&gt;Menu&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=<span class="hljs-string">"#"</span>&gt;Menu&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/nav&gt;
</code></pre>
<h2 id="heading-generating-a-full-html-boilerplate-with-emmet">Generating a full HTML boilerplate with Emmet</h2>
<p>This is the most famous Emmet shortcut.</p>
<h3 id="heading-html-boilerplate">HTML Boilerplate</h3>
<p>Just type:</p>
<p><strong>Emmet:</strong></p>
<pre><code class="lang-bash">! <span class="hljs-comment"># then hit enter</span>
</code></pre>
<p>Press Tab → You get:</p>
<pre><code class="lang-bash">&lt;!DOCTYPE html&gt;
&lt;html lang=<span class="hljs-string">"en"</span>&gt;
&lt;head&gt;
  &lt;meta charset=<span class="hljs-string">"UTF-8"</span>&gt;
  &lt;meta name=<span class="hljs-string">"viewport"</span> content=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;
  &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h2 id="heading-conclusion">Conclusion:</h2>
<p>Keep in mind that Emmet is a tool that helps coders to ship faster, but it is totally optional. If you just see the command and not practice them, then you definitely forgot every shotcut emmet provides you, so just use this article for reference purposes and do this trick in vs code for build your own muscle memory.</p>
]]></content:encoded></item><item><title><![CDATA[How DNS Resolution Works]]></title><description><![CDATA[DNS stands for Domain Name System. Before understanding the working of DNS, let me tell you what actually this DNS Means. In the real world, when a user tries to browse a website, that user tries to put some kind of domain, in our case, assume that w...]]></description><link>https://blog.studyhex.in/how-dns-resolution-works</link><guid isPermaLink="true">https://blog.studyhex.in/how-dns-resolution-works</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[dns]]></category><category><![CDATA[dns resolver]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Mon, 26 Jan 2026 19:39:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769456245067/b46099d6-d2ab-44f2-8afc-779188cfdcea.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>DNS stands for Domain Name System. Before understanding the working of DNS, let me tell you what actually this DNS Means. In the real world, when a user tries to browse a website, that user tries to put some kind of domain, in our case, assume that was google.com.</p>
<h2 id="heading-the-problem">The Problem:</h2>
<p>When we put a website on the internet, I need to put those files over the server so that the server can serve them to the user. now in a server, we actually get an IP address where our serveris located.<br />But here the main issue is when how the browser knows where our requested domain server IP.</p>
<h2 id="heading-dns-domain-name-system">DNS (Domain Name System)</h2>
<p>Here, DNS comes into the picture. Browser somehow reach to the DNS server then the DNS is actually tell the browser of the Actual IP address of the server. Before making a deep dive into these DNS workings, you must have an understanding of the Internet and the network.</p>
<h2 id="heading-how-dns-works">How DNS Works?</h2>
<p>When a user enter the url of an webiste like google.com, first that requeted is taken by the browser and then browser make a call to the dns resolver.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769447046029/12dba9ca-dec4-4eee-ba63-c6fc04c25657.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-recursive-dns-resolver"><strong>Recursive DNS resolver</strong></h2>
<p>DNS, a resolver is a server. When a browser request some domain to the DNS resolver, the main internal target of this DNS resolver is to find the A record of the Domain. It takes the allthe headache of finding the IP of the requested domain and return back to the Browser.</p>
<p>To do that, it takes multiple steps recursively; that way, it is also known as Recusive DNS resolver.</p>
<p>Let's try to understand the Working of it.</p>
<h2 id="heading-root-server">Root Server:</h2>
<p>In the world, we have a total of 13 root server, these server are responsivble for the TLD (Top Level Domain). Mean we have multiple DNS provider cloudflare, Google DNS providers, Cloudflare, Google DNS, etc., and we have multiple top level domain, example .com, .in, .or,g etc</p>
<h2 id="heading-tld-server">TLD server</h2>
<p>In the real world, there is the TLD ( Top Level Domain server). It is responsible for which autorative server is actually storing that Top Level domain it return that authoritative server to the dns resolver. Means, it tell the dns resolver which is the autoratativer server for .com for that request.</p>
<h2 id="heading-authoritative-server">Authoritative Server</h2>
<p>When the DNS resolver got the authoritative server location, it again sent a request to that authoritative server with the user-requested domain, it like saying, “Hey, did you know the A record of this doamin”<br />and the authoritative server says” yes.”<br />Then it returns the A record to the DNS resolver, and the DNS resolver then sends back the A record ip to the browser.</p>
<h2 id="heading-what-is-the-dig-command">What is the Dig command</h2>
<p>Now, let’s see everything in real life using the Dig command</p>
<p>Dig is a command-line tool that helps to get Domain Information. The full form of Dig is "Domain Information Groper".</p>
<h2 id="heading-example">Example</h2>
<pre><code class="lang-bash"><span class="hljs-comment"># Command</span>
dig google.com

<span class="hljs-comment"># Output</span>
; &lt;&lt;&gt;&gt; DiG 9.16.1-Ubuntu &lt;&lt;&gt;&gt; google.com
;; Got answer:
;; -&gt;&gt;HEADER&lt;&lt;- opcode: QUERY, status: NOERROR, id: 57166
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; QUESTION SECTION:
;google.com.                 IN      A

;; ANSWER SECTION:
google.com.          263     IN      A       142.251.43.174

;; Query time: 16 msec
;; SERVER: 127.0.0.11<span class="hljs-comment">#53(127.0.0.11)</span>
</code></pre>
<ul>
<li><p><code>status: NOERROR</code> → lookup success</p>
</li>
<li><p><code>A</code> record → IPv4 address</p>
</li>
<li><p><code>142.251.43.174</code> → IP of google.com</p>
</li>
<li><p><code>SERVER: 127.0.0.11</code> → your system asked a local DNS resolver (like Docker/OS stub)</p>
</li>
<li><p><code>rd ra</code> →</p>
<ul>
<li><p><code>rd</code> recursion desired</p>
</li>
<li><p><code>ra</code> recursion available<br />  meaning your resolver did all the steps for you.  </p>
</li>
</ul>
</li>
</ul>
<p>Understanding <code>dig . NS</code> and Root Name Servers</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Command</span>
dig . NS

<span class="hljs-comment"># Output</span>
.  518400  IN  NS  a.root-servers.net.
.  518400  IN  NS  b.root-servers.net.
.  518400  IN  NS  c.root-servers.net.
...
.  518400  IN  NS  m.root-servers.net.
</code></pre>
<ul>
<li><p><code>.</code> (dot) means <strong>root zone</strong></p>
</li>
<li><p>Root servers are the top of the DNS hierarchy</p>
</li>
<li><p>Root servers don’t know google.com IP</p>
</li>
<li><p>they only tell: <strong>“ask .com servers”</strong>  </p>
</li>
</ul>
<p>Understanding <code>dig com NS</code> and TLD Name Servers</p>
<pre><code class="lang-bash">dig com NS
com.  172800  IN  NS  a.gtld-servers.net.
com.  172800  IN  NS  b.gtld-servers.net.
com.  172800  IN  NS  c.gtld-servers.net.
...
com.  172800  IN  NS  m.gtld-servers.net.
</code></pre>
<ul>
<li><p><code>.com</code> is a <strong>TLD</strong></p>
</li>
<li><p>These servers are responsible for telling:</p>
</li>
<li><p>which authoritative name servers hold <code>google.com</code></p>
</li>
</ul>
<p>Understanding <code>dig google.com NS</code> and authoritative name servers</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Command</span>
dig google.com NS

<span class="hljs-comment"># Output</span>
google.com.  172800  IN  NS  ns1.google.com.
google.com.  172800  IN  NS  ns2.google.com.
google.com.  172800  IN  NS  ns3.google.com.
google.com.  172800  IN  NS  ns4.google.com.
</code></pre>
<ul>
<li><p>These are <strong>authoritative servers</strong></p>
</li>
<li><p>They contain the actual DNS records of google.com</p>
</li>
<li><p>Final A record comes from these servers</p>
</li>
</ul>
<h2 id="heading-final-flow-should-look-like-this">Final Flow Should Look like this</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769455560990/2a134962-149e-4e93-a0ff-33d298556b2a.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[DNS Record Types Explained]]></title><description><![CDATA[Before understading the DNS, we need to understand what is the Problem which solve by the DNS.
To start our discusson please follow this topic first

Internet & Network Devices

In actuall world, when a user searches for something on a browser suppos...]]></description><link>https://blog.studyhex.in/dns-record-types-explained</link><guid isPermaLink="true">https://blog.studyhex.in/dns-record-types-explained</guid><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Pallab Karmakar]]></dc:creator><pubDate>Sun, 25 Jan 2026 20:20:15 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1769372366989/eed7cba5-40f9-4665-a1cd-839fb51dd9f4.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Before understading the DNS, we need to understand what is the Problem which solve by the DNS.</p>
<p>To start our discusson please follow this topic first</p>
<ol>
<li>Internet &amp; Network Devices</li>
</ol>
<p>In actuall world, when a user searches for something on a browser suppose google.com then Google has a server set up on the internet that obviously has a ip, but the browser doesn’t know that ip. So, as we can see, it is a problem. if browser doesn’t know the ip we can’t see the page or server response.</p>
<p>DNS ( Domain Name System ):</p>
<p>In the old days, we had a phonebook or a directory. What does this phonebook acutally did, when we want to call someone, we search the name of that person in the phonebook to get his/her number so that we can make a call.</p>
<p>DNS is also act like a phonebook, but for the server and the ip. where In the place of Name we store the domain name ex. google.com, and it’s corresponding ip.</p>
<p>But actually, DNS is much more than just ip only. To know more about the DNS, please follow this article:</p>
<ul>
<li>DNS resolution</li>
</ul>
<p>In this article, we are going to understand all the DNS record types descriptively.</p>
<h2 id="heading-dns-record-types">DNS Record Types</h2>
<p>There are multiple record but the most used are these on</p>
<ol>
<li><p>NS record</p>
</li>
<li><p>A record</p>
</li>
<li><p>CNAME record</p>
</li>
<li><p>AAAA record</p>
</li>
<li><p>MX record</p>
</li>
<li><p>TXT record</p>
</li>
</ol>
<p>Let’s make proper understanding of each one.</p>
<h2 id="heading-ns-record">NS Record</h2>
<p>To understand the NS record, we need to understand its need of it.<br />Assume you buy a domain from a particular domain provide,r assume in my case that is Hostinger. but web server is hosted on Vercel or Netlify.<br />So here is the problem comes i purcase the domain from hostinger and I want that the domain should be managed by netlify or vercel or any other service like Cloudflare. In that case a need for some entry that tells that this domain is mange my some other service.</p>
<p>Now, we understand the need, so here is what the NS record does<br />Each DNS service provider has its own NS<br />example<br />For Hostinger, it is like this</p>
<ul>
<li><p>ns1.dns-parking.com</p>
</li>
<li><p>ns2.dns-parking.com</p>
</li>
</ul>
<p>and same Cloudflare also have there own Nameserver</p>
<ul>
<li><p>evangeline.ns.cloudflare.com</p>
</li>
<li><p>olof.ns.cloudflare.com</p>
</li>
</ul>
<p>Now, in the DNS record, we store this record like this</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>google.com</strong></td><td><strong>record type:</strong></td><td><strong>value:</strong></td><td><strong>TTL</strong></td></tr>
</thead>
<tbody>
<tr>
<td>@</td><td>NS</td><td>evangeline.ns.cloudflare.com</td><td>14000</td></tr>
</tbody>
</table>
</div><p>What does it mean that when a user searches for google.com, it first fetches the NS record, which is the authoritative domain of that requested domain? Then the browser goes to that DNS server for further requests until it gets an IP.</p>
<h2 id="heading-a-record">A Record</h2>
<p>The record browser actually wants to see that the record is an A record. A record is the actual record that carries the requested domain's original IP address, where the server is located.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><a target="_blank" href="http://example.com"><strong>example.com</strong></a></td><td><strong>record type:</strong></td><td><strong>value:</strong></td><td><strong>TTL</strong></td></tr>
</thead>
<tbody>
<tr>
<td>@</td><td>A</td><td>192.0.2.1</td><td>14400</td></tr>
</tbody>
</table>
</div><p>The vast majority of websites only have one A record, but it is possible to have several. Some higher-profile websites will have several different A records as part of a technique called <a target="_blank" href="https://www.cloudflare.com/learning/dns/glossary/round-robin-dns/">round robin load balancing</a>, <a target="_blank" href="https://www.cloudflare.com/learning/dns/glossary/round-robin-dns/">which can distribute requ</a>est traffic to one of several IP addresses, each hosting identical content.</p>
<h2 id="heading-aaaa-record">AAAA Record</h2>
<p>DNS AAAA records match a domain name to an IPv6 address. DNS AAAA records are exactly like DNS A records, except that they store a domain's IPv6 address instead of its IPv4 address.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><a target="_blank" href="http://example.com"><strong>example.com</strong></a></td><td><strong>record type:</strong></td><td><strong>value:</strong></td><td><strong>TTL</strong></td></tr>
</thead>
<tbody>
<tr>
<td>@</td><td>AAAA</td><td>2001:0db8:85a3:0000:  </td></tr>
</tbody>
</table>
</div><p>0000:8a2e:0370:7334 | 14400 |</p>
<p>CNAME Record</p>
<p>CNAME stands for Canonical Name. To understand this, try to understand the example first.<br />Suppose you have hosted a server on vercel and the version give you an ip. now due to some reasone may be vercel need some kind of relocation, then in that case, we need to change the ip because our server location has changed. So if in a particular region Vercel needs to chnage the server and vercel has a 1 million user base, then what Vercel does, vercel send email or some notification to all user that we change our ip you also need to change that from your end.<br />This is a problem. To solve that, what version does version give you some kind of app.vercel.com type domain, which is unique? You set a CNAME record that points to that domain, and Vercel internally sets the original server ip in the A record form on app.vercel.com  </p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><a target="_blank" href="http://blog.example.com"><strong>blog.example.com</strong></a></td><td><strong>record type:</strong></td><td><strong>value:</strong></td><td><strong>TTL</strong></td></tr>
</thead>
<tbody>
<tr>
<td>@</td><td>CNAME</td><td>is an alias of <a target="_blank" href="http://example.com">example.com</a></td><td>32600</td></tr>
</tbody>
</table>
</div><p>If a user sent a request to the browser for google.com, that request is sent to the authoritative server, and if there is no A record exist but a CNAME requred exist, it again makes a recursive call for that new domain until it finds the A record. Mean A record is the final destination.</p>
<h2 id="heading-mx-record">MX record</h2>
<p>A DNS <strong>MX (Mail Exchange) record</strong> tells which <strong>mail server will receive emails</strong> for a domain. It also helps decide <strong>how emails should be sent and routed</strong>, following <strong>SMTP</strong>, which is the standard rule/protocol used for sending email.</p>
<p>Example of an MX record:  </p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><a target="_blank" href="http://example.com"><strong>example.com</strong></a></td><td><strong>record type:</strong></td><td><strong>priority:</strong></td><td><strong>value:</strong></td><td><strong>TTL</strong></td></tr>
</thead>
<tbody>
<tr>
<td>@</td><td>MX</td><td>10</td><td><a target="_blank" href="http://mailhost1.example.com">mailhost1.example.com</a></td><td>45000</td></tr>
<tr>
<td>@</td><td>MX</td><td>20</td><td><a target="_blank" href="http://mailhost2.example.com">mailhost2.example.com</a></td><td>45000</td></tr>
</tbody>
</table>
</div><p>The email service could also configure this MX record so that both servers have equal priority and receive an equal amount of mail:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>example.com</strong></td><td><strong>record type:</strong></td><td><strong>priority:</strong></td><td><strong>value:</strong></td><td><strong>TTL</strong></td></tr>
</thead>
<tbody>
<tr>
<td>@</td><td>MX</td><td>10</td><td>mailhost1.example.com</td><td>45000</td></tr>
<tr>
<td>@</td><td>MX</td><td>10</td><td>mailhost2.example.com</td><td>45000</td></tr>
</tbody>
</table>
</div><p>This configuration enables the email provider to equally balance the load between the two servers.</p>
<p>A <strong>CNAME record</strong> is used to give a domain an <strong>alias name</strong> (like a shortcut) instead of using the real domain name. Usually, a CNAME points to an <strong>A record (IPv4)</strong> or <strong>AAAA record (IPv6)</strong>.</p>
<p>But <strong>MX records cannot point to a CNAME</strong>. An MX record must directly point to the mail server’s <strong>A or AAAA record</strong>. According to official DNS/email rules (RFC), <strong>MX → CNAME is not allowed</strong>.</p>
<h2 id="heading-txt-record">TXT Record</h2>
<p>It allows domain owners to store <strong>custom text data</strong> in DNS.</p>
<p>SPF/DKIM/DMARC need to publish things like:</p>
<ul>
<li><p>rules (policy text)</p>
</li>
<li><p>authorized mail servers list</p>
</li>
<li><p>public cryptographic key</p>
</li>
<li><p>Instructions for mail receivers</p>
</li>
</ul>
<p>These are <strong>not IP addresses</strong>, so they can’t go in A/AAAA records.<br />They are <strong>not mail server hostnames</strong>, so they can’t go in MX records.<br />So they are stored as <strong>TXT records</strong>.</p>
<h3 id="heading-meaning">Meaning:</h3>
<p>When someone receives an email from <a target="_blank" href="https://studyhex.in"><code>studyhex.in</code></a>, the receiver mail server checks your DNS TXT records to verify:</p>
<ul>
<li><p><strong>SPF</strong>: Is this sender server allowed?</p>
</li>
<li><p><strong>DKIM</strong>: Does the signature match the public key in DNS?</p>
</li>
<li><p><strong>DMARC</strong>: What action should be taken if SPF/DKIM fails?</p>
</li>
</ul>
<h2 id="heading-how-do-all-these-work-together">How do all these work together</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1769370277830/7c37adc4-76c7-4bac-b727-878ce6f6ec03.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item></channel></rss>