<?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[Understanding JavaScript]]></title><description><![CDATA[A series of blogs on various JS tipics.]]></description><link>https://understanding-javascript-101.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/69516d562b3718c1163a85f2/4df278c3-5ee1-41cd-9359-469180c79d61.png</url><title>Understanding JavaScript</title><link>https://understanding-javascript-101.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 07:08:53 GMT</lastBuildDate><atom:link href="https://understanding-javascript-101.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[The Magic of this, call(), apply(), and bind() in JavaScript]]></title><description><![CDATA[JavaScript can sometimes feel like a puzzle, and one of the most notoriously confusing pieces for many developers is the this keyword. Whether you are building complex front-end interfaces or structur]]></description><link>https://understanding-javascript-101.hashnode.dev/the-magic-of-this-call-apply-and-bind-in-javascript</link><guid isPermaLink="true">https://understanding-javascript-101.hashnode.dev/the-magic-of-this-call-apply-and-bind-in-javascript</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Divyanshu]]></dc:creator><pubDate>Sun, 15 Mar 2026 14:16:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69516d562b3718c1163a85f2/07d0256a-13e1-4fd2-8cd1-60b8239fadb9.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>JavaScript can sometimes feel like a puzzle, and one of the most notoriously confusing pieces for many developers is the <code>this</code> keyword. Whether you are building complex front-end interfaces or structuring backend logic, sooner or later, <code>this</code> will start behaving in ways you didn't expect.</p>
<p>But it doesn't have to be confusing. Once you understand a few core rules, controlling <code>this</code> becomes second nature. In this deep dive, we are going to break down exactly what <code>this</code> means, how it behaves in different scenarios, and how you can forcefully bend it to your will using the built-in methods: <code>call()</code>, <code>apply()</code>, and <code>bind()</code>.</p>
<hr />
<h2>1. What Exactly is <code>this</code> in JavaScript?</h2>
<p>The simplest way to think about <code>this</code> is to ask yourself one question: <strong>"Who is calling the function?"</strong></p>
<p>In JavaScript, functions are executed in a specific context. The <code>this</code> keyword is simply a reference to the object that is currently executing the code. It is not an unchangeable rule established when the function is written; rather, it is a dynamic reference established at the exact moment the function is <em>invoked</em> (or called).</p>
<p>If a person throws a ball, <code>this</code> is the person. If a machine throws a ball, <code>this</code> is the machine. The action (the function) is the same, but the actor (<code>this</code>) changes depending on who triggers it.</p>
<hr />
<h2>2. The <code>this</code> Keyword Inside Normal Functions</h2>
<p>Let's look at what happens when you use <code>this</code> inside a standard, standalone function that isn't attached to any specific object.</p>
<pre><code class="language-javascript">function greet() {
    console.log(this);
}

greet(); 
</code></pre>
<p><strong>What happens here?</strong></p>
<p>Because <code>greet()</code> is called on its own - without a specific object calling it - JavaScript defaults to the global object.</p>
<ul>
<li><p>In a web browser environment, the global object is the <code>window</code> object.</p>
</li>
<li><p>If you are running JavaScript in "Strict Mode" (<code>"use strict";</code>), the browser stops this default fallback to the window object to prevent accidental global variable modifications. In Strict Mode, <code>this</code> inside a normal function will simply be <code>undefined</code>.</p>
</li>
</ul>
<hr />
<h2>3. The <code>this</code> Keyword Inside Objects</h2>
<p>Things start making a lot more sense when we look at methods - functions that live inside objects. When a function is called as a method of an object, <code>this</code> refers to the object that contains the method. Remember our golden rule: <em>Who is calling the function?</em></p>
<pre><code class="language-javascript">const user = {
    username: "Alex",
    role: "Developer",
    introduce: function() {
        console.log(`Hi, my name is \({this.username} and I am a \){this.role}.`);
    }
};

user.introduce(); 
// Output: Hi, my name is Alex and I am a Developer.
</code></pre>
<p>In the example above, <code>user</code> is calling the <code>introduce()</code> function. Therefore, inside that function, <code>this</code> equals the <code>user</code> object. <code>this.username</code> is evaluated as <code>user.username</code>.</p>
<hr />
<h2>The "Lost Context" Trap</h2>
<p>A common trap occurs when you extract a method from its object and assign it to a standalone variable.</p>
<pre><code class="language-javascript">const sayHello = user.introduce;
sayHello(); 
// Output: Hi, my name is undefined and I am a undefined.
</code></pre>
<p>Why did this break? Because by extracting it into <code>sayHello</code>, we are now calling it as a normal, standalone function. The <code>user</code> object is no longer calling it. The context has been lost.</p>
<p>To fix lost context, or to intentionally change the context of a function, JavaScript provides three incredibly powerful tools: <code>call()</code>, <code>apply()</code>, and <code>bind()</code>.</p>
<hr />
<h2>4. Taking Control: What <code>call()</code> Does</h2>
<p>The <code>call()</code> method allows you to execute a function immediately and explicitly dictate what <code>this</code> should point to. It effectively allows an object to "borrow" a method from another object.</p>
<p>Let's say we have two user objects, but only one of them has a greeting method:</p>
<pre><code class="language-javascript">const user1 = {
    username: "Sarah",
    greet: function(greeting) {
        console.log(`\({greeting}! I am \){this.username}.`);
    }
};

const user2 = {
    username: "Michael"
};

// user1 calls its own method naturally
user1.greet("Hello"); // Output: Hello! I am Sarah.

// user2 borrows the method using call()
user1.greet.call(user2, "Welcome"); // Output: Welcome! I am Michael.
</code></pre>
<p><strong>How</strong> <code>call()</code> <strong>works:</strong></p>
<ol>
<li><p>The first argument you pass to <code>call()</code> becomes the new <code>this</code> context (in this case, <code>user2</code>).</p>
</li>
<li><p>Any subsequent arguments are passed individually to the function (in this case, <code>"Welcome"</code> is passed as the <code>greeting</code> parameter).</p>
</li>
</ol>
<hr />
<h2>5. Taking Control: What <code>apply()</code> Does</h2>
<p>The <code>apply()</code> method is almost completely identical to <code>call()</code>. It executes the function immediately and assigns the <code>this</code> context.</p>
<p>The <em>only</em> difference between the two is how they handle additional arguments. While <code>call()</code> expects arguments to be passed one by one (comma-separated), <code>apply()</code> expects all additional arguments to be bundled inside a single <strong>Array</strong>.</p>
<pre><code class="language-javascript">const flight = {
    airline: "SkyAir",
    book: function(flightNum, passengerName) {
        console.log(`\({passengerName} booked a seat on \){this.airline} flight ${flightNum}.`);
    }
};

const partnerAirline = {
    airline: "Oceanic Express"
};

// Using apply() to borrow the book method
flight.book.apply(partnerAirline, ["OB-415", "John Doe"]);
// Output: John Doe booked a seat on Oceanic Express flight OB-415.
</code></pre>
<p><strong>Pro Tip to remember the difference:</strong> * <strong>C</strong>all takes <strong>C</strong>omma-separated arguments.</p>
<ul>
<li><strong>A</strong>pply takes an <strong>A</strong>rray of arguments.</li>
</ul>
<hr />
<h2>6. Taking Control: What <code>bind()</code> Does</h2>
<p>Both <code>call()</code> and <code>apply()</code> execute the function <em>immediately</em>. But what if you don't want to run the function right now? What if you are setting up an event listener, or a timer, and you want the function to remember its context for later?</p>
<p>This is where <code>bind()</code> shines. The <code>bind()</code> method does <strong>not</strong> execute the function immediately. Instead, it creates and returns a brand new function, permanently locking in the <code>this</code> context you provide.</p>
<pre><code class="language-javascript">const game = {
    title: "Space Invaders",
    start: function() {
        console.log(`Starting ${this.title}...`);
    }
};

const newGame = {
    title: "Pac-Man"
};

// bind() creates a new function and stores it in a variable
const startPacMan = game.start.bind(newGame);

// We can now call this new function whenever we want
startPacMan(); 
// Output: Starting Pac-Man...
</code></pre>
<p>You can also pre-set arguments using <code>bind()</code>, a concept known as <em>partial application</em>, but its primary use case is simply ensuring your methods don't lose their context when passed around in your code.</p>
<h2>Example:</h2>
<pre><code class="language-javascript">const person = {
  name: "Alice",
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  },
  farewell: () =&gt; {
    // Arrow function, 'this' is inherited from global context (or outer scope)
    // If run in browser, this.name would be undefined.
    // If run in Node.js, this would be empty object {}, so this.name is undefined.
    console.log(`Goodbye from ${this.name}`);
  }
};

person.greet();    // Output: Hello, my name is Alice ('this' is person)

person.farewell(); // Output: Goodbye from undefined (if in global scope)

// Example with call/apply/bind
function introduce(age, city) {
  console.log(`Hi, I'm \({this.name}, \){age} years old from ${city}.`);
}

const anotherPerson = { name: "Bob" };

introduce.call(anotherPerson, 30, "New York"); // Hi, I'm Bob, 30 years old from New York.
introduce.apply(anotherPerson, [25, "London"]); // Hi, I'm Bob, 25 years old from London.

const bobIntro = introduce.bind(anotherPerson, 40);
bobIntro("Paris"); // Hi, I'm Bob, 40 years old from Paris.
</code></pre>
<hr />
<h2>7. Summary Table: <code>call</code> vs <code>apply</code> vs <code>bind</code></h2>
<p>Keep this quick reference chart handy when deciding which method to use:</p>
<table style="min-width:125px"><colgroup><col style="min-width:25px"></col><col style="min-width:25px"></col><col style="min-width:25px"></col><col style="min-width:25px"></col><col style="min-width:25px"></col></colgroup><tbody><tr><td><p><strong>Method</strong></p></td><td><p><strong>Does it execute immediately?</strong></p></td><td><p><strong>How are arguments passed?</strong></p></td><td><p><strong>Returns</strong></p></td><td><p><strong>Primary Use Case</strong></p></td></tr><tr><td><p><code>call()</code></p></td><td><p>Yes</p></td><td><p>Comma-separated (<code>arg1, arg2</code>)</p></td><td><p>Function result</p></td><td><p>Borrowing a method and running it right away with specific arguments.</p></td></tr><tr><td><p><code>apply()</code></p></td><td><p>Yes</p></td><td><p>As an Array (<code>[arg1, arg2]</code>)</p></td><td><p>Function result</p></td><td><p>Borrowing a method when your arguments are already stored in an array.</p></td></tr><tr><td><p><code>bind()</code></p></td><td><p>No</p></td><td><p>Comma-separated (<code>arg1, arg2</code>)</p></td><td><p>A New Function</p></td><td><p>Storing a function with a locked-in context to be executed later (e.g., event listeners).</p></td></tr></tbody></table>

<hr />
<h2>8. Your Assignment / Challenge</h2>
<p>To truly solidify this knowledge, the best approach is to write the code yourself. Open up your favorite code editor or a browser console and complete the following steps:</p>
<ol>
<li><p><strong>Create an object with a method:</strong> Create a <code>developer</code> object with properties like <code>name</code> and <code>primaryLanguage</code>. Add a method called <code>writeCode()</code> that prints a string using <code>this</code> (e.g., "Alice is writing HTML").</p>
</li>
<li><p><strong>Borrow that method using</strong> <code>call()</code><strong>:</strong> Create a second object called <code>designer</code> with different properties. Use <code>call()</code> to make the designer object borrow the <code>writeCode()</code> method from the developer.</p>
</li>
<li><p><strong>Use</strong> <code>apply()</code> <strong>with array arguments:</strong> Modify the <code>writeCode</code> method to accept a few arguments (like <code>projectType</code> and <code>deadline</code>). Borrow the method again for another object, but this time use <code>apply()</code> and pass those arguments as an array.</p>
</li>
<li><p><strong>Use</strong> <code>bind()</code> <strong>and store the function:</strong> Create a completely detached variable. Use <code>bind()</code> to lock the <code>designer</code> context to the <code>writeCode</code> method, store it in the variable, and then invoke that variable a few lines later.</p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Arrow Functions in JavaScript: A Simpler Way to Write Functions]]></title><description><![CDATA[If you have been working with JavaScript for a little while, you have probably noticed a strange-looking syntax using an equals sign and a greater-than sign (=>). This is the arrow function, a feature]]></description><link>https://understanding-javascript-101.hashnode.dev/arrow-functions-in-javascript-a-simpler-way-to-write-functions</link><guid isPermaLink="true">https://understanding-javascript-101.hashnode.dev/arrow-functions-in-javascript-a-simpler-way-to-write-functions</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Divyanshu]]></dc:creator><pubDate>Sun, 15 Mar 2026 14:08:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69516d562b3718c1163a85f2/3bc99fe0-f507-4fd9-9e3d-b04260a32713.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you have been working with JavaScript for a little while, you have probably noticed a strange-looking syntax using an equals sign and a greater-than sign (<code>=&gt;</code>). This is the arrow function, a feature introduced in ES6 (ECMAScript 2015) that completely changed how developers write JavaScript.</p>
<p>Arrow functions provide a more concise way to write function expressions. They strip away the boilerplate, making your code cleaner and much easier to read. Let's break down exactly how they work, how to write them, and why they are a staple of modern web development.</p>
<hr />
<h2>What Are Arrow Functions?</h2>
<p>At their core, arrow functions are just a shorter syntax for writing function expressions. Before ES6, creating a function always required the <code>function</code> keyword. Arrow functions allow you to drop that keyword entirely, relying on the "fat arrow" (<code>=&gt;</code>) to separate the function parameters from the function body.</p>
<p>Here is a quick comparison showing how arrow functions reduce boilerplate:</p>
<p><strong>The Old Way (Normal Function)</strong></p>
<pre><code class="language-javascript">const greet = function() {
  return "Hello, World!";
};
</code></pre>
<p><strong>The Modern Way (Arrow Function)</strong></p>
<pre><code class="language-javascript">const greet = () =&gt; {
  return "Hello, World!";
};
</code></pre>
<hr />
<h2>Basic Arrow Function Syntax</h2>
<p>Writing an arrow function is straightforward. You define your parameters in parentheses, add the arrow <code>=&gt;</code>, and then open your curly braces for the function body.</p>
<h4>Arrow Functions with Multiple Parameters</h4>
<p>When you have two or more parameters, you must wrap them in parentheses.</p>
<pre><code class="language-javascript">const addNumbers = (a, b) =&gt; {
  return a + b;
};

console.log(addNumbers(5, 10)); // Output: 15
</code></pre>
<h4>Arrow Functions with One Parameter</h4>
<p>If your function only takes exactly <strong>one</strong> parameter, JavaScript allows you to drop the parentheses entirely. This makes the code incredibly sleek.</p>
<pre><code class="language-javascript">const square = x =&gt; {
  return x * x;
};

console.log(square(4)); // Output: 16
</code></pre>
<hr />
<h2>Implicit Return vs. Explicit Return</h2>
<p>One of the most powerful features of arrow functions is the <strong>implicit return</strong>.</p>
<p>In a standard function, if you want to send a value back, you must explicitly use the <code>return</code> keyword. This is called an <strong>explicit return</strong>.</p>
<pre><code class="language-javascript">// Explicit return
const multiply = (a, b) =&gt; {
  return a * b; 
};
</code></pre>
<p>However, if your function body consists of only a <em>single expression</em>, you can remove the curly braces <code>{}</code> and the <code>return</code> keyword. JavaScript will automatically return the result of that expression. This is an <strong>implicit return</strong>.</p>
<pre><code class="language-javascript">// Implicit return
const multiply = (a, b) =&gt; a * b;

console.log(multiply(3, 4)); // Output: 12
</code></pre>
<p>Implicit returns are fantastic for keeping your code readable, especially when doing simple math operations or manipulating arrays.</p>
<hr />
<h2>Basic Difference Between Arrow Functions and Normal Functions</h2>
<p>While arrow functions are great for shortening your code, they are not just "syntactic sugar" - they do have some functional differences compared to traditional functions.</p>
<ul>
<li><p><strong>Syntax:</strong> Arrow functions are noticeably shorter and omit the <code>function</code> keyword.</p>
</li>
<li><p><strong>Implicit Returns:</strong> Normal functions always require the <code>return</code> keyword to pass a value back. Arrow functions can return values implicitly on a single line.</p>
</li>
<li><p><strong>The</strong> <code>this</code> <strong>Keyword:</strong> Without getting too deep into execution contexts, normal functions create their own <code>this</code> binding based on how they are called. Arrow functions do <em>not</em> have their own <code>this</code>. They inherit <code>this</code> from the surrounding code where they were defined. For basic math operations and greetings, this won't affect you, but it becomes important later when building complex web applications.</p>
</li>
</ul>
<hr />
<h2>Your Assignment</h2>
<p>Ready to practice? Try completing the following steps in your code editor or console:</p>
<ol>
<li><p>Write a normal function (using the <code>function</code> keyword) that calculates the square of a number.</p>
</li>
<li><p>Rewrite that exact same logic using an arrow function.</p>
</li>
<li><p>Create an arrow function that accepts a number and returns a string stating whether the number is <code>"even"</code> or <code>"odd"</code>.</p>
</li>
<li><p>Use an arrow function inside the <code>map()</code> method to double every number in this array: <code>const numbers = [1, 2, 3, 4];</code></p>
</li>
</ol>
<p>The END :).</p>
]]></content:encoded></item><item><title><![CDATA[Function Declaration vs Function Expression: What’s the Difference?]]></title><description><![CDATA[If you are diving deep into JavaScript to eventually master libraries like React, understanding how functions work under the hood is an absolute must. Functions are the building blocks of JavaScript, ]]></description><link>https://understanding-javascript-101.hashnode.dev/function-declaration-vs-function-expression-what-s-the-difference</link><guid isPermaLink="true">https://understanding-javascript-101.hashnode.dev/function-declaration-vs-function-expression-what-s-the-difference</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Divyanshu]]></dc:creator><pubDate>Sun, 15 Mar 2026 14:05:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69516d562b3718c1163a85f2/c29a35a9-513c-438f-8e23-7ca5bd34e5d6.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you are diving deep into JavaScript to eventually master libraries like React, understanding how functions work under the hood is an absolute must. Functions are the building blocks of JavaScript, but there is more than one way to write them.</p>
<p>Today, we are going to break down the two most common ways to create functions: <strong>Function Declarations</strong> and <strong>Function Expressions</strong>. We'll look at their syntax, explore a quirky JavaScript behavior called "hoisting," and figure out when you should use each one.</p>
<hr />
<h2>What Are Functions and Why Do We Need Them?</h2>
<p>Imagine you are building a house. Instead of building every single door from scratch using raw wood and nails right at the doorframe, you build a door-making machine. You feed the machine wood, and it gives you a finished door anywhere you need it.</p>
<p>In programming, a <strong>function</strong> is exactly that: a reusable block of code designed to perform a specific task. We need them because they keep our code clean, organized, and DRY (Don't Repeat Yourself). Instead of writing the same logic over and over, you write it once inside a function and call it whenever you need it.</p>
<p>Let's look at the two main ways to build these "machines" in JavaScript.</p>
<h3>1. Function Declaration Syntax</h3>
<p>A function declaration is the standard, traditional way of writing a function. You start with the <code>function</code> keyword, followed by the name you want to give it, parentheses <code>()</code>, and curly braces <code>{}</code> holding your logic.</p>
<p>Here is a simple example that adds two numbers:</p>
<pre><code class="language-javascript">function addNumbers(a, b) {
  return a + b;
}

console.log(addNumbers(5, 10)); // Output: 15
</code></pre>
<p>It is straightforward and clearly announces, "Hey, I am creating a function named <code>addNumbers</code>!"</p>
<h3>2. Function Expression Syntax</h3>
<p>A function expression takes a different approach. Instead of declaring a standalone function, you create a function and store it inside a variable.</p>
<p>Here is the exact same addition logic, written as an expression:</p>
<pre><code class="language-javascript">const addNumbers = function(a, b) {
  return a + b;
};

console.log(addNumbers(5, 10)); // Output: 15
</code></pre>
<p>Notice that the function itself doesn't have a name between the <code>function</code> keyword and the parentheses (this is often called an <em>anonymous function</em>). The function is simply treated as a value that gets assigned to the <code>addNumbers</code> variable.</p>
<hr />
<h2>The Key Difference: A High-Level Look at Hoisting</h2>
<p>At first glance, declarations and expressions look like two different flavors of the exact same thing. But there is a crucial difference in how JavaScript handles them behind the scenes, and it comes down to a concept called <strong>hoisting</strong>.</p>
<p><strong>Hoisting</strong> is JavaScript's default behavior of moving declarations to the top of their scope before the code actually runs.</p>
<ul>
<li><p><strong>Function Declarations are fully hoisted.</strong> You can successfully call the function <em>before</em> you even define it in your code. JavaScript reads the whole file, pulls the declaration to the top, and then executes your code.</p>
</li>
<li><p><strong>Function Expressions are NOT hoisted.</strong> (More accurately, the variable might be hoisted, but its assignment as a function is not). If you try to call a function expression before you define it, JavaScript will throw an error.</p>
</li>
</ul>
<p>Let's see this in action.</p>
<hr />
<h2>The Declaration Way (Works!)</h2>
<pre><code class="language-javascript">// Calling the function before defining it
console.log(greet("Divyanshu")); // Output: "Hello, Divyanshu!"

function greet(name) {
  return `Hello, ${name}!`;
}
</code></pre>
<h2>The Expression Way (Breaks!)</h2>
<pre><code class="language-javascript">// Calling the function before defining it
console.log(greet("Divyanshu")); // ReferenceError: Cannot access 'greet' before initialization

const greet = function(name) {
  return `Hello, ${name}!`;
};
</code></pre>
<h2>Side-by-Side Comparison</h2>
<table style="min-width:75px"><colgroup><col style="min-width:25px"></col><col style="min-width:25px"></col><col style="min-width:25px"></col></colgroup><tbody><tr><td><p><strong>Feature</strong></p></td><td><p><strong>Function Declaration</strong></p></td><td><p><strong>Function Expression</strong></p></td></tr><tr><td><p><strong>Syntax</strong></p></td><td><p><code>function name() {}</code></p></td><td><p><code>const name = function() {}</code></p></td></tr><tr><td><p><strong>Hoisting</strong></p></td><td><p>Yes (can call before defining)</p></td><td><p>No (must define before calling)</p></td></tr><tr><td><p><strong>Use Case</strong></p></td><td><p>General, globally accessible helper functions</p></td><td><p>Passing functions as arguments (callbacks), strict top-to-bottom flow</p></td></tr></tbody></table>

<h2>When to Use Each Type</h2>
<ul>
<li><p><strong>Use Function Declarations</strong> when you want to create utility or helper functions that you want to be available anywhere in your file, regardless of where they are written. It can make your code easier to read if you want to put your main logic at the top of the file and hide the helper functions at the bottom.</p>
</li>
<li><p><strong>Use Function Expressions</strong> when you want to enforce a strict, predictable top-to-bottom execution flow. Because they aren't hoisted, you are forced to define your functions before you use them, which can prevent confusing bugs. They are also heavily used in modern JavaScript frameworks and when writing callbacks.</p>
</li>
</ul>
<hr />
<h2>Practice Assignment</h2>
<p>The best way to solidify this concept is to write it out yourself! Open up your code editor and try this:</p>
<ol>
<li><p>Write a <strong>Function Declaration</strong> that takes two numbers and multiplies them.</p>
</li>
<li><p>Write the exact same logic using a <strong>Function Expression</strong>.</p>
</li>
<li><p>Call both functions with some test numbers and <code>console.log</code> the results to make sure they work.</p>
</li>
<li><p><strong>The Hoisting Test:</strong> Try calling both functions <em>above</em> the lines where you defined them. Observe the behavior and the error messages in your console.</p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Understanding Object-Oriented Programming in JavaScript]]></title><description><![CDATA[If you are on the journey to becoming a full-stack engineer, you are going to encounter a lot of different ways to write and organize code. One of the most important concepts you will run into is Obje]]></description><link>https://understanding-javascript-101.hashnode.dev/understanding-object-oriented-programming-in-javascript</link><guid isPermaLink="true">https://understanding-javascript-101.hashnode.dev/understanding-object-oriented-programming-in-javascript</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Divyanshu]]></dc:creator><pubDate>Sun, 15 Mar 2026 13:50:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69516d562b3718c1163a85f2/4a8da31b-df85-4454-a7ca-d7abc191b571.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you are on the journey to becoming a full-stack engineer, you are going to encounter a lot of different ways to write and organize code. One of the most important concepts you will run into is <strong>Object-Oriented Programming (OOP)</strong>.</p>
<p>When you first get into tech, writing code often feels like just trying to get the computer to do what you want, line by line. But as your projects grow from simple scripts to full-fledged applications, having a massive file full of disconnected variables and standalone functions quickly becomes a nightmare to manage.</p>
<p>This is where OOP comes in to save the day. Object-Oriented Programming is a programming paradigm - a specific style of writing code - that organizes software design around <strong>data</strong> (objects) rather than just functions and logic. It allows us to group related data and behaviors together into neat, manageable packages.</p>
<p>Let’s break down exactly what that means in JavaScript and how you can start using it today.</p>
<hr />
<h2>The Blueprint Analogy: Classes vs. Objects</h2>
<p>To truly grasp OOP, you need to understand the relationship between a <strong>Class</strong> and an <strong>Object</strong>. The easiest way to visualize this is by looking at how a car is manufactured.</p>
<p>Imagine a team of engineers designing a brand-new car. Before a single piece of metal is cut, they draw up a highly detailed <strong>blueprint</strong>. This blueprint dictates that the car will have a specific brand, a certain color, four wheels, and behaviors like driving and braking.</p>
<p>However, you cannot hop into a blueprint and drive it to the grocery store. It is just a concept- a set of rules. To actually drive, the factory needs to build physical cars <em>based</em> on that exact blueprint.</p>
<ul>
<li><p><strong>The Class is the blueprint:</strong> It defines the structure, properties, and abilities that the final product will have.</p>
</li>
<li><p><strong>The Object is the physical car:</strong> It is the actual, tangible thing built from the blueprint.</p>
</li>
</ul>
<p>From one single blueprint (the Class), a factory can produce millions of distinct cars (the Objects). One might be a red Honda, while another is a black Ford, but they both share the exact same underlying architecture.</p>
<hr />
<h2>What is a Class in JavaScript?</h2>
<p>In modern JavaScript, we create our blueprints using the <code>class</code> keyword. A class acts as a template for creating objects.</p>
<p>Let's look at how we might write the basic structure for our <code>Car</code> blueprint:</p>
<pre><code class="language-javascript">class Car {
  // The blueprint logic goes here
}
</code></pre>
<p>Right now, our blueprint is empty. To make it useful, we need to define what properties a car should have when it is built. We do this using a special method called the constructor.</p>
<hr />
<h2>The Constructor Method: Setting Up Your Object</h2>
<p>The <code>constructor</code> is a special function that lives inside your class. It runs <strong>automatically</strong> the exact moment you create a new object from your class. Its entire job is to set up the initial state of your new object.</p>
<pre><code class="language-javascript">class Car {
  constructor(brand, color) {
    this.brand = brand;
    this.color = color;
  }
}
</code></pre>
<p>Let's break down the magic happening here:</p>
<ol>
<li><p>When we eventually build a car, we will pass in a specific <code>brand</code> and <code>color</code>.</p>
</li>
<li><p>The <code>this</code> keyword is incredibly important. It refers to the <strong>specific instance</strong> of the object being created right at that moment.</p>
</li>
<li><p><code>this.brand = brand;</code> basically translates to: "Take the brand name provided, and attach it to <em>this specific car</em> that is rolling off the assembly line right now."</p>
</li>
</ol>
<hr />
<h2>Adding Behaviors: Methods Inside a Class</h2>
<p>A car isn't very useful if it just sits there. It needs to <em>do</em> things. In OOP, the functions that belong to an object are called <strong>methods</strong>.</p>
<p>Let's give our car the ability to drive and announce itself:</p>
<pre><code class="language-javascript">class Car {
  constructor(brand, color) {
    this.brand = brand;
    this.color = color;
  }

  // This is a method 
  drive() {
    console.log(`Vroom! The \({this.color} \){this.brand} is hitting the road!`);
  }

  // Another method
  brake() {
    console.log(`The ${this.brand} is safely slowing down.`);
  }
}
</code></pre>
<p>Notice that we don't need to write the word <code>function</code> before <code>drive()</code> or <code>brake()</code>. When functions live inside a class, we just write their name. Additionally, because the method is inside the class, it has full access to <code>this.brand</code> and <code>this.color</code>.</p>
<h2>Creating Objects (Instantiation)</h2>
<p>Now that our factory blueprint is complete, let's build some actual cars. We do this using the <code>new</code> keyword, which tells JavaScript to execute the class constructor and create a fresh object.</p>
<pre><code class="language-javascript">// Building our first car object
const dailyCommuter = new Car("Honda", "Red");

// Building a completely separate car object
const weekendCruiser = new Car("Ford", "Black");

// Making them perform their behaviors
dailyCommuter.drive();      
// Output: Vroom! The Red Honda is hitting the road!

weekendCruiser.drive();     
// Output: Vroom! The Black Ford is hitting the road!

dailyCommuter.brake();
// Output: The Honda is safely slowing down.
</code></pre>
<p>By using the <code>new</code> keyword, we have "instantiated" two separate objects. They share the same methods, but they hold their own unique data.</p>
<hr />
<h2>The Power of Code Reusability</h2>
<p>This is where you start to see the incredible value of OOP. Imagine you are building a racing game and need 50 different cars on the track.</p>
<p>If you were writing standard, procedural code, you might have to write 50 different variables for brand, 50 variables for color, and 50 different functions to make them drive. It would be hundreds of lines of repetitive code.</p>
<p>With our <code>Car</code> class, the complex logic is written exactly <strong>once</strong>. We can then stamp out as many <code>new Car()</code> objects as we want in a single line of code each. This makes your code highly reusable, incredibly easy to read, and much simpler to fix. If you find a bug in the way cars drive, you only have to fix it in one place: inside the class blueprint.</p>
<hr />
<h2>A Basic Idea of Encapsulation</h2>
<p>As you dive deeper into web development cohorts and advanced tutorials, you will hear the term <strong>Encapsulation</strong>.</p>
<p>Encapsulation is one of the core pillars of OOP. At its most basic level, it simply means bundling the data (the properties like <code>brand</code> and <code>color</code>) and the methods that operate on that data (<code>drive()</code>) together inside a single unit (the class).</p>
<p>Think of encapsulation like a medicinal pill capsule. All the active ingredients (data and methods) are safely packed inside the shell. This keeps everything highly organized and prevents the internal workings of the car from spilling out and getting tangled up with the rest of the code in your application. It acts as a protective shield around your object's logic.</p>
<hr />
<h2>Your Assignment: Put it into Practice</h2>
<p>Reading about OOP is one thing, but writing it is where the concepts truly click. Fire up your code editor and try this challenge:</p>
<ol>
<li><p>Create a class called <code>Student</code>.</p>
</li>
<li><p>Set up a <code>constructor</code> method that accepts <code>name</code> and <code>age</code> as parameters and assigns them to the object using the <code>this</code> keyword.</p>
</li>
<li><p>Add a method inside the class called <code>printDetails()</code> that logs a formatted string to the console. For example: <em>"Student Name: Divyanshu, Age: 25"</em>.</p>
</li>
<li><p>Below your class, use the <code>new</code> keyword to create at least three distinct student objects.</p>
</li>
<li><p>Call the <code>printDetails()</code> method on each of your new student objects to verify they work correctly.</p>
</li>
</ol>
<p>Object-Oriented Programming might require a shift in how you think about structuring your code, but once you master classes and objects, you'll be well on your way to writing professional, scalable JavaScript.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Operators: The Basics You Need to Know]]></title><description><![CDATA[When you are starting your journey to become a full-stack engineer, JavaScript can sometimes feel like learning a completely new language. But just like any spoken language has verbs to make things ha]]></description><link>https://understanding-javascript-101.hashnode.dev/javascript-operators-the-basics-you-need-to-know</link><guid isPermaLink="true">https://understanding-javascript-101.hashnode.dev/javascript-operators-the-basics-you-need-to-know</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Divyanshu]]></dc:creator><pubDate>Sun, 15 Mar 2026 13:39:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69516d562b3718c1163a85f2/b7a9611e-6ba8-40af-9366-b9aa5386bdd8.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When you are starting your journey to become a full-stack engineer, JavaScript can sometimes feel like learning a completely new language. But just like any spoken language has verbs to make things happen, programming languages have <strong>operators</strong>.</p>
<p>Simply put, operators are symbols that tell the computer to perform specific mathematical, relational, or logical tasks. They are the engines that make your code actually <em>do</em> things.</p>
<p>Let's break down the everyday operators you will use constantly in your JavaScript projects.</p>
<hr />
<h2>1. Arithmetic Operators</h2>
<p>These are the most familiar ones. They perform basic math operations just like the calculator on your phone.</p>
<ul>
<li><p><code>+</code> (Addition)</p>
</li>
<li><p><code>-</code> (Subtraction)</p>
</li>
<li><p><code>*</code> (Multiplication)</p>
</li>
<li><p><code>/</code> (Division)</p>
</li>
<li><p><code>%</code> (Modulo / Remainder)</p>
</li>
</ul>
<p>The modulo operator (<code>%</code>) might look new, but it just gives you the remainder of a division. For example, <code>10 % 3</code> is <code>1</code> because 3 goes into 10 three times, leaving 1 leftover.</p>
<p><strong>Let's see them in action:</strong></p>
<pre><code class="language-javascript">let a = 10;
let b = 3;

console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.333...
console.log(a % b); // 1
</code></pre>
<h2>2. Comparison Operators</h2>
<p>We use comparison operators to compare two values. They always return a boolean: either <code>true</code> or <code>false</code>.</p>
<ul>
<li><p><code>&gt;</code> (Greater than)</p>
</li>
<li><p><code>&lt;</code> (Less than)</p>
</li>
<li><p><code>!=</code> (Not equal to)</p>
</li>
</ul>
<p><strong>The Big Debate:</strong> <code>==</code> <strong>vs</strong> <code>===</code></p>
<p>This is a classic "gotcha" in JavaScript. Both check for equality, but they do it differently:</p>
<ul>
<li><p><code>==</code> (Loose equality): Checks if the <em>values</em> are the same, even if the data types are different. JavaScript will try to convert the types behind the scenes to make a match.</p>
</li>
<li><p><code>===</code> (Strict equality): Checks if <em>both the value AND the data type</em> are exactly the same.</p>
</li>
</ul>
<p><strong>Always use</strong> <code>===</code> <strong>in your everyday coding.</strong> It prevents weird bugs.</p>
<pre><code class="language-javascript">let num = 5;
let str = "5";

console.log(num &gt; 3);  // true
console.log(num &lt; 10); // true

// The difference between loose and strict equality
console.log(num == str);  // true (JavaScript converts the string to a number)
console.log(num === str); // false (Number is not the same type as String)
</code></pre>
<h2>3. Logical Operators</h2>
<p>Logical operators are used to connect multiple conditions together. Think of them as the bouncers at a club checking multiple requirements before letting you in.</p>
<ul>
<li><p><code>&amp;&amp;</code> (AND): Returns <code>true</code> <em>only</em> if both conditions are true.</p>
</li>
<li><p><code>||</code> (OR): Returns <code>true</code> if <em>at least one</em> condition is true.</p>
</li>
<li><p><code>!</code> (NOT): Flips the boolean value. <code>true</code> becomes <code>false</code>, and vice versa.</p>
</li>
</ul>
<pre><code class="language-javascript">let hasTicket = true;
let isVip = false;

// AND (Requires both to be true)
console.log(hasTicket &amp;&amp; isVip); // false

// OR (Requires only one to be true)
console.log(hasTicket || isVip); // true

// NOT (Reverses the value)
console.log(!hasTicket); // false
</code></pre>
<h2>4. Assignment Operators</h2>
<p>You use these to assign values to variables. The most basic one is <code>=</code>, but there are handy shortcuts for updating a variable's value based on its current value.</p>
<ul>
<li><p><code>=</code> (Assign)</p>
</li>
<li><p><code>+=</code> (Add and assign)</p>
</li>
<li><p><code>-=</code> (Subtract and assign)</p>
</li>
</ul>
<pre><code class="language-javascript">let score = 10; // Basic assignment

score += 5; // Shortcut for: score = score + 5
console.log(score); // 15

score -= 2; // Shortcut for: score = score - 2
console.log(score); // 13
</code></pre>
<hr />
<h2>💻 Practice Assignment</h2>
<p>Ready to try it out yourself? Open up your console or a new file and complete these steps:</p>
<ol>
<li><p><strong>Arithmetic:</strong> Create two variables with numbers. Perform addition, subtraction, multiplication, and division, and <code>console.log</code> the results.</p>
</li>
<li><p><strong>Comparison:</strong> Create a number variable and a string variable holding the same digit (e.g., <code>9</code> and <code>"9"</code>). Compare them using both <code>==</code> and <code>===</code> and log the output.</p>
</li>
<li><p><strong>Logical:</strong> Write a small scenario with two boolean variables (e.g., <code>isWeekend</code> and <code>hasHomework</code>). Write a condition using <code>&amp;&amp;</code> or <code>||</code> to determine if you have free time, and log the result.</p>
</li>
</ol>
<hr />
<h2>Wrap Up</h2>
<p>You don't need to memorize complex operator precedence charts right now. In everyday usage, JavaScript reads pretty much left-to-right, and if you ever need a specific calculation to happen first, just wrap it in parentheses <code>()</code> exactly like you would in regular math!</p>
]]></content:encoded></item><item><title><![CDATA[Array Methods You Must Know]]></title><description><![CDATA[If you've been working with JavaScript for more than a few days, you've probably realized that arrays are everywhere. But storing data in a list is only half the battle; knowing how to manipulate, tra]]></description><link>https://understanding-javascript-101.hashnode.dev/array-methods-you-must-know</link><guid isPermaLink="true">https://understanding-javascript-101.hashnode.dev/array-methods-you-must-know</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[webdev]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[webdevelopment]]></category><dc:creator><![CDATA[Divyanshu]]></dc:creator><pubDate>Sun, 15 Mar 2026 13:33:40 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69516d562b3718c1163a85f2/97b13fd9-2d0c-415e-83c9-51d4feab828e.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you've been working with JavaScript for more than a few days, you've probably realized that arrays are everywhere. But storing data in a list is only half the battle; knowing how to manipulate, transform, and extract that data is where the real magic happens.</p>
<p>Before we dive in, do yourself a favor:<br /><strong>open up your browser's developer console right now (F12 or Right-Click -&gt; Inspect -&gt; Console).</strong>  </p>
<p>The best way to learn these methods is to type the examples out and see the results for yourself!</p>
<p>Let's break down the most important array methods you need to know.</p>
<hr />
<h3>Adding and Removing Elements: The Basics</h3>
<p>Think of these four methods as the doors to your array. They let items in and out of the front and back.</p>
<h4>1. <code>push()</code> and <code>pop()</code> (The Back Door)</h4>
<ul>
<li><p><code>push()</code> adds one or more elements to the <em>end</em> of an array.</p>
</li>
<li><p><code>pop()</code> removes the <em>last</em> element from an array.</p>
</li>
</ul>
<pre><code class="language-javascript">let fruits = ['apple', 'banana'];
console.log('Before push:', fruits); // ['apple', 'banana']

fruits.push('orange');
console.log('After push:', fruits); // ['apple', 'banana', 'orange']

fruits.pop();
console.log('After pop:', fruits); // ['apple', 'banana']
</code></pre>
<h4>2. <code>unshift()</code> and <code>shift()</code> (The Front Door)</h4>
<ul>
<li><p><code>unshift()</code> adds one or more elements to the <em>beginning</em> of an array.</p>
</li>
<li><p><code>shift()</code> removes the <em>first</em> element from an array.</p>
</li>
</ul>
<pre><code class="language-javascript">let queue = ['Alice', 'Bob'];
console.log('Before unshift:', queue); // ['Alice', 'Bob']

queue.unshift('Charlie');
console.log('After unshift:', queue); // ['Charlie', 'Alice', 'Bob']

queue.shift();
console.log('After shift:', queue); // ['Alice', 'Bob']
</code></pre>
<hr />
<h3>The Modern Way: Array Iteration Methods</h3>
<p>In the past, if you wanted to do something to every item in an array, you had to write a traditional <code>for</code> loop. It looked like this:</p>
<pre><code class="language-javascript">// Traditional For Loop Example
let numbers = [1, 2, 3];
let doubledNumbers = [];

for (let i = 0; i &lt; numbers.length; i++) {
  doubledNumbers.push(numbers[i] * 2);
}
console.log(doubledNumbers); // [2, 4, 6]
</code></pre>
<p>While this works, it's clunky. You have to manage the counter (<code>i</code>), the length condition, and manually push to a new array. Modern JavaScript gives us cleaner, more readable methods. Let's look at them.</p>
<h4>3. <code>map()</code>: The Transformer</h4>
<p><code>map()</code> goes through your array, applies a function to every single item, and gives you a brand <strong>new</strong> array with the modified items. It's perfect for when you want to change data without messing up the original array.</p>
<pre><code class="language-javascript">let prices = [10, 20, 30];
console.log('Before map:', prices); // [10, 20, 30]

let discountedPrices = prices.map(price =&gt; price - 5);

console.log('After map (new array):', discountedPrices); // [5, 15, 25]
console.log('Original array is untouched:', prices); // [10, 20, 30]
</code></pre>
<h4>4. <code>filter()</code>: The Bouncer</h4>
<p><code>filter()</code> looks at every item in your array and checks it against a condition you set. If the item passes the test (returns <code>true</code>), it gets put into a new array. If it fails, it gets left behind.</p>
<pre><code class="language-javascript">let scores = [45, 80, 32, 95, 100];
console.log('Before filter:', scores); // [45, 80, 32, 95, 100]

let passingScores = scores.filter(score =&gt; score &gt;= 50);

console.log('After filter (new array):', passingScores); // [80, 95, 100]
</code></pre>
<h4>5. <code>forEach()</code>: The Action Taker</h4>
<p><code>forEach()</code> is the closest cousin to the traditional <code>for</code> loop. It just runs a function for every item in the array. <strong>Important note:</strong> unlike <code>map()</code> or <code>filter()</code>, <code>forEach()</code> does <em>not</em> return a new array. It just executes code.</p>
<pre><code class="language-javascript">let users = ['Divyanshu', 'Sarah', 'Mike'];

// This just prints to the console, it doesn't create a new array
users.forEach(user =&gt; {
  console.log('Hello, ' + user + '!');
});
// Output: Hello, Divyanshu! 
// Output: Hello, Sarah! 
// Output: Hello, Mike!
</code></pre>
<h4>6. <code>reduce()</code>: The Accumulator (Beginner Friendly)</h4>
<p><code>reduce()</code> can look intimidating, but its core job is simple: it takes an array with many items and squishes them down into a <strong>single value</strong>.</p>
<p>Think of a snowball rolling down a hill, gathering more snow.</p>
<ul>
<li><p>The snowball is your <strong>accumulator</strong> (the running total).</p>
</li>
<li><p>The fresh snow it picks up is your <strong>current value</strong>.</p>
</li>
</ul>
<p>Here is how you use it to find the total sum of an array:</p>
<pre><code class="language-javascript">let expenses = [10, 20, 30];

// The '0' at the end is our starting point (an empty snowball).
let totalExpense = expenses.reduce((accumulator, currentValue) =&gt; {
  return accumulator + currentValue;
}, 0); 

console.log('Total Expense:', totalExpense); // 60
</code></pre>
<p><em>Note: As you get more advanced, you can chain these methods together (e.g.,</em> <code>array.map().filter()</code><em>). However, while you are learning, I highly recommend keeping them separate so you can</em> <code>console.log</code> <em>the intermediate steps and see exactly what your data is doing!</em></p>
<hr />
<h3>Your Assignment 📝</h3>
<p>Ready to test your knowledge? Try this exercise in your console:</p>
<ol>
<li><p>Create an array of numbers: <code>[2, 5, 8, 12, 15]</code>.</p>
</li>
<li><p>Use <code>map()</code> to create a new array where each number is doubled.</p>
</li>
<li><p>Take that new array and use <code>filter()</code> to extract only the numbers that are strictly greater than <code>10</code>.</p>
</li>
<li><p>Finally, use <code>reduce()</code> on the filtered array to calculate the total sum of those remaining numbers.</p>
</li>
</ol>
<p>Let me know in the comments what total sum you ended up with!</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Arrays 101]]></title><description><![CDATA[Have you ever tried to keep track of a list of items in your code? Let’s say you’re building a simple application to track your daily tasks or manage a list of your favorite fruits.
If you try to stor]]></description><link>https://understanding-javascript-101.hashnode.dev/javascript-arrays-101</link><guid isPermaLink="true">https://understanding-javascript-101.hashnode.dev/javascript-arrays-101</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[webdev]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Divyanshu]]></dc:creator><pubDate>Sun, 15 Mar 2026 13:28:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69516d562b3718c1163a85f2/7b9caa18-88b8-4c42-938b-d8ffff60cad6.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Have you ever tried to keep track of a list of items in your code? Let’s say you’re building a simple application to track your daily tasks or manage a list of your favorite fruits.</p>
<p>If you try to store these values individually, your code might look something like this:</p>
<pre><code class="language-javascript">let fruit1 = "Apple";
let fruit2 = "Banana";
let fruit3 = "Mango";
let fruit4 = "Orange";
</code></pre>
<p>Imagine if you had 100 fruits! Creating a separate variable for every single item quickly becomes messy, hard to manage, and nearly impossible to scale.</p>
<p>This is where <strong>arrays</strong> come to the rescue.</p>
<hr />
<h2>What Are Arrays and Why Do We Need Them?</h2>
<p>An array is a single variable that can store a <strong>collection of values in a specific order</strong>. Think of an array like a neatly organized pill organizer or a bookshelf. Instead of throwing all your books into a pile (or creating 100 individual variables), you line them up sequentially on one shelf where you can easily find exactly what you need.</p>
<p>Arrays allow us to group related data together, keeping our code clean and giving us powerful ways to interact with that data all at once.</p>
<hr />
<h2>How to Create an Array</h2>
<p>Creating an array in JavaScript is straightforward. You just use square brackets <code>[]</code> and separate your items with commas.</p>
<pre><code class="language-javascript">const fruits = ["Apple", "Banana", "Mango", "Orange"];
</code></pre>
<p>Just like that, we’ve replaced four separate variables with one clean, organized array.</p>
<hr />
<h2>Accessing Elements Using the Index</h2>
<p>Every item inside an array has a specific numbered position called an <strong>index</strong>.</p>
<p>Here is the most important rule to remember about arrays in JavaScript (and most programming languages): <strong>indexing starts at 0, not 1.</strong> * The first item is at index <code>0</code></p>
<ul>
<li><p>The second item is at index <code>1</code></p>
</li>
<li><p>The third item is at index <code>2</code></p>
</li>
</ul>
<p>To access an element, you write the name of the array followed by square brackets containing the index number.</p>
<pre><code class="language-javascript">const fruits = ["Apple", "Banana", "Mango", "Orange"];

console.log(fruits[0]); // Output: "Apple"
console.log(fruits[2]); // Output: "Mango"
</code></pre>
<h2>Updating Elements</h2>
<p>Arrays are dynamic, meaning you can change the data inside them after they are created. You can update an element by accessing its index and assigning a new value to it, exactly like you would with a standard variable.</p>
<p>Let's say we want to replace "Banana" with "Strawberry":</p>
<pre><code class="language-javascript">const fruits = ["Apple", "Banana", "Mango", "Orange"];

// "Banana" is at index 1
fruits[1] = "Strawberry"; 

console.log(fruits); 
// Output: ["Apple", "Strawberry", "Mango", "Orange"]
</code></pre>
<h2>The Array Length Property</h2>
<p>Often, you need to know exactly how many items are inside your array. JavaScript provides a built-in property called <code>.length</code> that does the counting for you.</p>
<p><em>Note: While indexes start at 0, the length property counts items normally, starting from 1.</em></p>
<pre><code class="language-javascript">const fruits = ["Apple", "Banana", "Mango", "Orange"];

console.log(fruits.length); // Output: 4
</code></pre>
<h2>Basic Looping Over Arrays</h2>
<p>The real power of an array is the ability to run code for every single item inside it without repeating yourself. We can use a basic <code>for</code> loop alongside the <code>.length</code> property to iterate through our collection.</p>
<p>Here is how you can print every fruit in the list:</p>
<pre><code class="language-javascript">const fruits = ["Apple", "Banana", "Mango", "Orange"];

for (let i = 0; i &lt; fruits.length; i++) {
  console.log("I love eating " + fruits[i]);
}

// Output:
// I love eating Apple
// I love eating Banana
// I love eating Mango
// I love eating Orange
</code></pre>
<p>In this loop, the variable <code>i</code> starts at <code>0</code> and increases by <code>1</code> each loop, stopping right before it hits the total length of the array. This perfectly matches our 0-based index system!</p>
<hr />
<h2>Your Turn: Try This Assignment!</h2>
<p>Ready to write some code yourself? Open up your code editor or console and try this quick assignment to test your knowledge:</p>
<ol>
<li><p>Create an array containing 5 of your favorite movies.</p>
</li>
<li><p>Print the very first and the very last element to the console.</p>
</li>
<li><p>Change the value of the 3rd movie in your list to something else, then print the updated array.</p>
</li>
<li><p>Write a basic <code>for</code> loop to go through the array and print all the movie names one by one.</p>
</li>
</ol>
<p>Drop your code in the comments below if you want feedback.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Objects in JavaScript]]></title><description><![CDATA[If you've been working with JavaScript, you've probably used arrays to store lists of data. But what happens when you need to store more complex, structured information? That's where objects come in.
]]></description><link>https://understanding-javascript-101.hashnode.dev/understanding-objects-in-javascript</link><guid isPermaLink="true">https://understanding-javascript-101.hashnode.dev/understanding-objects-in-javascript</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[webdev]]></category><category><![CDATA[webdevelopment]]></category><dc:creator><![CDATA[Divyanshu]]></dc:creator><pubDate>Sun, 15 Mar 2026 13:23:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69516d562b3718c1163a85f2/5acaff3f-9f3d-48ce-b79d-1ac066f16963.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you've been working with JavaScript, you've probably used arrays to store lists of data. But what happens when you need to store more complex, structured information? That's where <strong>objects</strong> come in.</p>
<p>As we continue our journey through the Web Dev Cohort 2026, understanding objects is a massive stepping stone toward becoming a full-stack engineer. Let's break down what objects are, why we need them, and how to use them.</p>
<hr />
<h2>What Are Objects and Why Do We Need Them?</h2>
<p>Imagine you want to store information about a person. You <em>could</em> use an array:</p>
<pre><code class="language-javascript">let personArray = ["Divyanshu", 25, "Delhi"];
</code></pre>
<p>But there's a problem here. Looking at <code>personArray[1]</code>, how do you know if <code>25</code> is the person's age, their lucky number, or their house number? An array is just an ordered list; it doesn't give us context.</p>
<p>An <strong>object</strong> solves this by storing data in a <strong>key-value pair</strong> structure. Instead of relying on an index (like 0, 1, or 2), you use a descriptive "key" (a label) to store and access a "value".</p>
<hr />
<h2>Creating Objects</h2>
<p>Creating an object is straightforward. We use curly braces <code>{}</code> and define our key-value pairs inside them.</p>
<pre><code class="language-javascript">let person = {
  name: "Divyanshu",
  age: 25,
  city: "Delhi"
};
</code></pre>
<p>Now, the data is perfectly clear. The key is <code>name</code> and the value is <code>"Divyanshu"</code>.</p>
<hr />
<h2>Accessing Properties</h2>
<p>To get information out of an object, you have two main options: <strong>Dot Notation</strong> and <strong>Bracket Notation</strong>.</p>
<h3>1. Dot Notation</h3>
<p>This is the most common and easiest way to access a property. You just write the object name, a dot, and the key.</p>
<pre><code class="language-javascript">console.log(person.name); // Output: Divyanshu
console.log(person.city); // Output: Delhi
</code></pre>
<h3>2. Bracket Notation</h3>
<p>Sometimes you need to use bracket notation. This is especially useful if your key has a space in it (which isn't recommended, but possible) or if you are using a variable to access the key.</p>
<pre><code class="language-javascript">console.log(person["age"]); // Output: 25

let propertyToCheck = "city";
console.log(person[propertyToCheck]); // Output: Delhi
</code></pre>
<hr />
<h2>Modifying Objects: Updating, Adding, and Deleting</h2>
<p>Objects are dynamic. You can change them even after you've created them.</p>
<h3>Updating Properties</h3>
<p>To change an existing value, just reassign it using dot or bracket notation.</p>
<pre><code class="language-javascript">person.age = 26;
console.log(person.age); // Output: 26
</code></pre>
<h3>Adding New Properties</h3>
<p>Adding a new property is just as easy. Simply assign a value to a key that doesn't exist yet.</p>
<pre><code class="language-javascript">person.profession = "Developer";
console.log(person); 
// Output: { name: 'Divyanshu', age: 26, city: 'Delhi', profession: 'Developer' }
</code></pre>
<h3>Deleting Properties</h3>
<p>If you need to remove a property completely, use the <code>delete</code> keyword.</p>
<pre><code class="language-javascript">delete person.city;
console.log(person); 
// Output: { name: 'Divyanshu', age: 26, profession: 'Developer' }
</code></pre>
<hr />
<h2>Looping Through Object Keys</h2>
<p>Sometimes you want to go through all the properties in an object, maybe to print them out to the console. Because objects aren't ordered like arrays, we use a special loop called the <code>for...in</code> loop.</p>
<pre><code class="language-javascript">let car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2022
};

for (let key in car) {
  console.log(key + ": " + car[key]);
}

// Output:
// brand: Toyota
// model: Corolla
// year: 2022
</code></pre>
<p><em>Notice how we used</em> <code>car[key]</code> <em>(bracket notation) inside the loop. If we used</em> <code>car.key</code><em>, JavaScript would look for a property literally named "key", which doesn't exist!</em></p>
<hr />
<h2>📝 Try It Yourself! (Assignment Idea)</h2>
<p>Ready to put this into practice? Here is a quick challenge to test your skills:</p>
<ol>
<li><p>Create an object representing a <code>student</code>.</p>
</li>
<li><p>Add the following properties: <code>name</code>, <code>age</code>, and <code>course</code>.</p>
</li>
<li><p>Update the <code>course</code> property to a new subject.</p>
</li>
<li><p>Print all the keys and their corresponding values using a <code>for...in</code> loop.</p>
</li>
</ol>
<p>The END :).</p>
]]></content:encoded></item><item><title><![CDATA[Control Flow in JavaScript: If, Else, and Switch Explained]]></title><description><![CDATA[Think about how you make decisions every day. If it's raining, you grab an umbrella. Otherwise, you wear sunglasses. If you have enough money, you buy a coffee; else, you make one at home.
Your code n]]></description><link>https://understanding-javascript-101.hashnode.dev/control-flow-in-javascript-if-else-and-switch-explained</link><guid isPermaLink="true">https://understanding-javascript-101.hashnode.dev/control-flow-in-javascript-if-else-and-switch-explained</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Divyanshu]]></dc:creator><pubDate>Sun, 15 Mar 2026 13:15:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69516d562b3718c1163a85f2/9f42ed0a-46b4-4d1b-a072-83d626bd3db2.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Think about how you make decisions every day. <em>If</em> it's raining, you grab an umbrella. <em>Otherwise</em>, you wear sunglasses. <em>If</em> you have enough money, you buy a coffee; <em>else</em>, you make one at home.</p>
<p>Your code needs to make decisions the exact same way. In programming, this is called <strong>control flow</strong>.</p>
<p>Control flow is the order in which a computer executes instructions. By default, code runs in a straight line, from top to bottom. But with control flow statements, you can tell your code to skip certain lines, run different blocks of code based on specific conditions, or repeat actions.</p>
<p>Let's break down how to give your JavaScript code a brain of its own!</p>
<hr />
<h2>The <code>if</code> Statement</h2>
<p>The <code>if</code> statement is the simplest form of decision-making in code. It evaluates a condition, and if that condition is true, the code inside its block runs.</p>
<p><strong>Example: Checking Age</strong></p>
<pre><code class="language-javascript">let age = 20;

if (age &gt;= 18) {
  console.log("You are old enough to vote!");
}
</code></pre>
<p><strong>Step-by-Step:</strong></p>
<ol>
<li><p>The program checks the condition: <em>Is the variable</em> <code>age</code> <em>greater than or equal to 18?</em></p>
</li>
<li><p>Since 20 is greater than 18, the condition is <code>true</code>.</p>
</li>
<li><p>The program enters the curly braces <code>{}</code> and prints the message to the console.</p>
</li>
</ol>
<h2>The <code>if-else</code> Statement</h2>
<p>What if the condition is false and you want to do something else? That’s where <code>else</code> comes in. It provides a fallback option.</p>
<pre><code class="language-javascript">let age = 15;

if (age &gt;= 18) {
  console.log("You are old enough to vote!");
} else {
  console.log("Sorry, you are too young to vote.");
}
</code></pre>
<p><strong>Step-by-Step:</strong></p>
<ol>
<li><p>The program checks if <code>age</code> (15) is greater than or equal to 18.</p>
</li>
<li><p>The condition is <code>false</code>.</p>
</li>
<li><p>The program skips the first block and immediately runs the code inside the <code>else</code> block.</p>
</li>
</ol>
<h2>The <code>else if</code> Ladder</h2>
<p>Sometimes, you have more than two possibilities. The <code>else if</code> ladder lets you chain multiple conditions together.</p>
<p><strong>Example: Checking Marks</strong></p>
<pre><code class="language-javascript">let studentMarks = 85;

if (studentMarks &gt;= 90) {
  console.log("Grade: A");
} else if (studentMarks &gt;= 80) {
  console.log("Grade: B");
} else if (studentMarks &gt;= 70) {
  console.log("Grade: C");
} else {
  console.log("Study harder!");
}
</code></pre>
<p>The program checks each condition from top to bottom. As soon as it finds a <code>true</code> condition (in this case, <code>studentMarks &gt;= 80</code>), it runs that block and skips the rest of the ladder entirely.</p>
<h2>The <code>switch</code> Statement</h2>
<p>When you have a single value that you want to check against many specific cases, writing a long <code>else if</code> ladder gets messy. The <code>switch</code> statement is a much cleaner alternative.</p>
<pre><code class="language-javascript">let trafficLight = "Green";

switch (trafficLight) {
  case "Red":
    console.log("Stop!");
    break;
  case "Yellow":
    console.log("Slow down.");
    break;
  case "Green":
    console.log("Go!");
    break;
  default:
    console.log("Invalid light color.");
}
</code></pre>
<h2>Why the <code>break</code> keyword is crucial!</h2>
<p>Notice the <code>break</code> at the end of each case. When the program finds the matching case ("Green"), it runs the code. The <code>break</code> tells the program, "We found what we needed, exit the switch statement now."</p>
<p>If you forget to include <code>break</code>, the program will keep running the code for <em>all the following cases</em>, even if they don't match! This is called "fall-through," and it is a very common beginner bug.</p>
<hr />
<h2>When to use <code>switch</code> vs <code>if-else</code></h2>
<ul>
<li><p><strong>Use</strong> <code>if-else</code> when you are checking ranges (like <code>age &gt; 18</code>), multiple different variables, or complex true/false conditions.</p>
</li>
<li><p><strong>Use</strong> <code>switch</code> when you are checking a single variable against a list of specific, exact values (like days of the week, menu options, or specific strings).</p>
</li>
</ul>
<hr />
<h2>Your Assignment</h2>
<p>Ready to put this into practice? Try writing the following programs in your code editor:</p>
<ol>
<li><p><strong>Number Checker:</strong> Write a program using <code>if-else</code> that takes a number and prints whether it is positive, negative, or zero.</p>
</li>
<li><p><strong>Day of the Week:</strong> Write a program using <code>switch</code> that takes a number from 1 to 7 and prints the corresponding day of the week (1 = Monday, 2 = Tuesday, etc.).</p>
</li>
<li><p><strong>Reflect:</strong> Explain in your own words why you chose <code>if-else</code> for the first program and <code>switch</code> for the second.</p>
</li>
</ol>
<hr />
]]></content:encoded></item><item><title><![CDATA[Understanding Variables and Data Types in JavaScript]]></title><description><![CDATA[Whether you're just starting your tech journey or, like me, you're currently grinding through the online courses to become a full-stack engineer, you have to start with the absolute fundamentals. In J]]></description><link>https://understanding-javascript-101.hashnode.dev/understanding-variables-and-data-types-in-javascript</link><guid isPermaLink="true">https://understanding-javascript-101.hashnode.dev/understanding-variables-and-data-types-in-javascript</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><dc:creator><![CDATA[Divyanshu]]></dc:creator><pubDate>Sun, 15 Mar 2026 13:08:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/69516d562b3718c1163a85f2/08f6d203-9dd6-43b5-83e9-2d6e4f03f401.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Whether you're just starting your tech journey or, like me, you're currently grinding through the online courses to become a full-stack engineer, you have to start with the absolute fundamentals. In JavaScript, that means understanding how we store and manage information.</p>
<p>Today, we are going to look at variables, the different types of data they can hold, and how to use them effectively.</p>
<hr />
<h2>What is a Variable?</h2>
<p>Think of a variable as a labeled storage box. When you are moving to a new house, you pack your books in a box and write "Books" on the outside. In JavaScript, you do the exact same thing with information. You create a "box" (the variable), give it a descriptive name (the label), and put data inside it (the value).</p>
<p>Whenever you need that data later, you just call the variable by its name.</p>
<hr />
<h2>Declaring Variables: <code>var</code>, <code>let</code>, and <code>const</code></h2>
<p>In JavaScript, we have three keywords to create (or "declare") these storage boxes: <code>var</code>, <code>let</code>, and <code>const</code>.</p>
<p><strong>1.</strong> <code>let</code> <strong>(The Flexible Box)</strong></p>
<p>Use <code>let</code> when you know the value inside the box will change later on.</p>
<pre><code class="language-javascript">let currentAge = 25;
console.log(currentAge); // Output: 25

// Fast forward a year...
currentAge = 26; 
console.log(currentAge); // Output: 26
</code></pre>
<p><strong>2.</strong> <code>const</code> <strong>(The Locked Box)</strong></p>
<p>Use <code>const</code> (short for constant) when the value should <em>never</em> change. Once you put something in a <code>const</code> box and seal it, you cannot swap it out.</p>
<pre><code class="language-javascript">const birthYear = 1998;
// birthYear = 1999; // ❌ This will throw an error!
</code></pre>
<p><strong>3.</strong> <code>var</code> <strong>(The Old School Box)</strong></p>
<p>Before <code>let</code> and <code>const</code> were introduced, <code>var</code> was the only way to declare variables. You will see it in older codebases, but it behaves a bit unpredictably compared to modern standards. For now, stick to <code>let</code> and <code>const</code> for your projects!</p>
<hr />
<h2>Understanding Scope (A Beginner-Friendly View)</h2>
<p>The main difference between how variables behave usually comes down to <strong>scope</strong>. Scope simply defines <em>where</em> your variable is allowed to be used.</p>
<p>Imagine your house:</p>
<ul>
<li><p><strong>Global Scope:</strong> A variable declared out in the open. It’s like the WiFi router in your living room - anyone in the house can access it.</p>
</li>
<li><p><strong>Block Scope (</strong><code>let</code> <strong>and</strong> <code>const</code><strong>):</strong> Variables declared inside a block of code (like inside <code>{ }</code> brackets) are stuck in that specific room. If you leave a book in your bedroom, someone in the kitchen can't read it.</p>
</li>
</ul>
<hr />
<h2>Primitive Data Types</h2>
<p>Now that we have our boxes, what kind of data can we put inside them? JavaScript has a few basic building blocks called primitive data types:</p>
<ul>
<li><p><strong>String:</strong> Text wrapped in quotes.</p>
<p><code>let name = "Divyanshu";</code></p>
</li>
<li><p><strong>Number:</strong> Exactly what it sounds like. No quotes needed!</p>
<p><code>let age = 25;</code></p>
</li>
<li><p><strong>Boolean:</strong> A simple True or False switch. Great for checking states.</p>
<p><code>let isLearningReact = true;</code></p>
</li>
<li><p><strong>Undefined:</strong> A box that has been created but doesn't have anything inside it yet.</p>
<p><code>let futureJob;</code> (Value is undefined)</p>
</li>
<li><p><strong>Null:</strong> An intentionally empty box. You are deliberately saying "there is nothing here."</p>
<p><code>let emptyValue = null;</code></p>
</li>
</ul>
<hr />
<h2>Let's Practice! (Your Assignment)</h2>
<p>The best way to learn is by doing. Open up your browser console or your favorite code editor and try this:</p>
<ol>
<li><p>Declare a <code>const</code> variable for your <strong>Name</strong>.</p>
</li>
<li><p>Declare a <code>let</code> variable for your <strong>Age</strong>.</p>
</li>
<li><p>Declare a boolean variable called <strong>IsStudent</strong> indicating if you are currently studying.</p>
</li>
<li><p>Print them all using <code>console.log()</code>.</p>
</li>
<li><p><em>Challenge:</em> Try to change the value of your Age, and then try to change your Name. Observe what happens!</p>
</li>
</ol>
<pre><code class="language-javascript">// Give it a try before checking the answer below!
const myName = "Divyanshu";
let myAge = 25;
let isStudent = true;

console.log(myName, myAge, isStudent);

myAge = 26; // This works!
// myName = "Alex"; // This will crash!
</code></pre>
<hr />
<h2>Core comparison</h2>
<p>Here is a comparison table outlining the key differences between <code>var</code>, <code>let</code>, and <code>const</code> in JavaScript. This comparison is primarily based on scope, redeclaration, and reassignment capabilities.</p>
<h2>Comparison Table: <code>var</code>, <code>let</code>, and <code>const</code></h2>
<table style="min-width:100px"><colgroup><col style="min-width:25px"></col><col style="min-width:25px"></col><col style="min-width:25px"></col><col style="min-width:25px"></col></colgroup><tbody><tr><td><p><strong>Feature</strong></p></td><td><p><strong>var</strong></p></td><td><p><strong>let</strong></p></td><td><p><strong>const</strong></p></td></tr><tr><td><p><strong>Scope</strong></p></td><td><p>Function scope. (If declared outside a function, it's globally scoped).</p></td><td><p>Block scope.</p></td><td><p>Block scope.</p></td></tr><tr><td><p><strong>Redeclaration</strong></p></td><td><p>Yes, you can redeclare within its scope.</p></td><td><p>No, you cannot redeclare in the same scope.</p></td><td><p>No, you cannot redeclare in the same scope.</p></td></tr><tr><td><p><strong>Reassignment</strong></p></td><td><p>Yes, you can reassign a new value.</p></td><td><p>Yes, you can reassign a new value.</p></td><td><p>No, you cannot reassign to the same variable identifier.</p></td></tr><tr><td><p><strong>Hoisting</strong></p></td><td><p>Yes, both declaration and initialization (to <code>undefined</code>) are hoisted.</p></td><td><p>Yes, only declaration is hoisted (but not initialized, leading to Temporal Dead Zone).</p></td><td><p>Yes, only declaration is hoisted (but not initialized, leading to Temporal Dead Zone).</p></td></tr><tr><td><p><strong>Window Object Property</strong></p></td><td><p>Creates a property on the global object (e.g., <code>window</code> in browsers).</p></td><td><p>Does not create a property on the global object.</p></td><td><p>Does not create a property on the global object.</p></td></tr></tbody></table>

<hr />
<p>Understanding how to store and manage data is your first major step into JavaScript.</p>
]]></content:encoded></item></channel></rss>