<?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[Ashish Shah Blog.]]></title><description><![CDATA[Ashish Shah Blog.]]></description><link>https://blog.ashishshah.me</link><generator>RSS for Node</generator><lastBuildDate>Sat, 02 May 2026 15:47:37 GMT</lastBuildDate><atom:link href="https://blog.ashishshah.me/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[What is a web browser?]]></title><description><![CDATA[A web browser is like an empty canvas; the browser asks the server(anything can be who can give the required file), the server gives you some files, and the browser paints that file in front of you. In the same way, when you open a text file saved on...]]></description><link>https://blog.ashishshah.me/what-is-a-web-browser</link><guid isPermaLink="true">https://blog.ashishshah.me/what-is-a-web-browser</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Chaiaurcode]]></category><category><![CDATA[ChaiCohort]]></category><category><![CDATA[@hiteshchoudharylco]]></category><category><![CDATA[#HiteshChaudhary ]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[webdev]]></category><dc:creator><![CDATA[Ashish Shah]]></dc:creator><pubDate>Tue, 25 Feb 2025 13:43:25 GMT</pubDate><content:encoded><![CDATA[<p>A web browser is like an empty canvas; the browser asks the server(anything can be who can give the required file), the server gives you some files, and the browser paints that file in front of you. In the same way, when you open a text file saved on your PC, laptop or phone, the OS reads that file and paints it to the text editor canvas(software).</p>
<p>as the OS does some mechanism to perform this task, as the same browser has some internal mechanism to do this task(loading, unloading, deleting, POST, DELETE etc)</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Object Prototypes]]></title><description><![CDATA[Hey! This is Ashish Shah, writing this Article on 19 Fab 2025, explaining the prototype property of JavaScript and its superpower. I’m listening to the song Kill Me Slowly By Sickick


Have you ever wondered why most things in JavaScript are either o...]]></description><link>https://blog.ashishshah.me/javascript-object-prototypes</link><guid isPermaLink="true">https://blog.ashishshah.me/javascript-object-prototypes</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Chaiaurcode]]></category><category><![CDATA[ChaiCohort]]></category><category><![CDATA[chai]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[React]]></category><category><![CDATA[@hiteshchoudharylco]]></category><category><![CDATA[#HiteshChaudhary ]]></category><category><![CDATA[javascript framework]]></category><dc:creator><![CDATA[Ashish Shah]]></dc:creator><pubDate>Wed, 19 Feb 2025 07:42:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/gTs2w7bu3Qo/upload/03e70688c213810bee4d636bb98f52b9.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey! This is Ashish Shah, writing this Article on 19 Fab 2025, explaining the prototype property of JavaScript and its superpower. I’m listening to the song <a target="_blank" href="https://open.spotify.com/track/3ZgC8QbdvmjFSzAbz7DWFl?si=1f2dc9fdfb74414b">Kill Me Slowly By Sickick</a></p>
<iframe style="border-radius:12px" src="https://open.spotify.com/embed/track/3ZgC8QbdvmjFSzAbz7DWFl?utm_source=generator" width="100%" height="152"></iframe>

<p>Have you ever wondered why most things in JavaScript are either <code>objects</code> or behave like <code>objects</code>? or why are you able to access <code>.toString()</code> method on any object without defining it at the time of object creation? This is because JavaScript has a property known as <code>prototype</code>. Let’s dive deep into what a <code>prototype</code> is and how it works internally to make JavaScript seem almost entirely object-oriented.</p>
<p>In JavaScript, <code>Object class</code> or <code>Object type</code> is the base class for almost all <code>objects</code>.<br />It is the fundamental part of the language itself, that provides some <code>properties</code> and <code>methods</code> to all <code>objects</code> either directly or via inheritance.</p>
<p>So, the <code>Object class</code> has a property known as <code>prototype</code>, and we can access it using <code>.</code> operator. If you console <code>Object.prototype</code> you’ll get something like this in the image:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739938686584/e15b7f41-b9fa-4be9-aa58-1f231c4ba33c.png" alt class="image--center mx-auto" /></p>
<p>So, when you create any <code>function</code>, <code>class</code> or <code>data type</code> like below, the JavaScript engine injects all the above <code>properties</code> and <code>methods</code> of base class into that data type.<br />e.g: class</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span>{
    <span class="hljs-keyword">constructor</span>(fname, lname,) {
        <span class="hljs-built_in">this</span>.firstname = fname;
        <span class="hljs-built_in">this</span>.lastname = lname;
    }
    getFullName() {
        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.lastname !== <span class="hljs-literal">undefined</span>) 
            <span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">this</span>.firstname}</span> <span class="hljs-subst">${<span class="hljs-built_in">this</span>.lastname}</span>`</span>
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.firstname;
    }
}
<span class="hljs-keyword">const</span> objectOne = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Ashish"</span>, <span class="hljs-string">"Shah"</span>);
</code></pre>
<p>and If we do console <code>Person.prototype</code> we’ll get something like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739939620300/a14e0c6b-769f-46c3-b0a3-acccac0848fc.png" alt class="image--center mx-auto" /></p>
<p>So, what is happening under the hood is: that JavaScript is creating a property <code>prototype</code> to <code>class Person</code>, and injecting all the <code>methods</code> that are defined inside the class along with <code>base Object</code> class’s properties and method via <code>Person.prototype = Object.prototype</code>. That’s why at the start we’re able to see our <code>user-defined</code> <code>method</code> and then the base <code>Object class</code> methods. JavaScript does the same to all types of classes either <code>predefined</code> (<code>String</code>, <code>Array</code>, <code>Number</code>, <code>Boolean</code>, <code>Function</code>, <code>Date</code> etc) or <code>user-defined</code> as above (That’s the reason almost everything in JS seems an object because in some way they all have <code>Object class’s</code> methods and properties). It creates a prototype property and injects it into all base class methods or parent class methods into the object’s prototype property.</p>
<p>So even on a string, we’re able to call a method <code>.toUpperCase()</code> like the below without defining it and also on the object we’re able to call <code>.toString()</code> method.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"Ashish shah"</span>
name.toUpperCase();
<span class="hljs-comment">//Output: ASHISH SHAH</span>
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> one = {
    <span class="hljs-attr">fname</span>: <span class="hljs-string">"Ashish"</span>,
    <span class="hljs-attr">lname</span>: <span class="hljs-string">"Shah"</span>
}
one.toString()
<span class="hljs-comment">// output: [object Object]</span>
</code></pre>
<h2 id="heading-so-what-prototype-is">So what prototype is?</h2>
<p>prototype is a way of accessing the <code>properties</code> and <code>methods</code> of other <code>objects</code> (as we were accessing methods of the base <code>Object class</code> on <code>string</code> class above). But the <strong><em>superpower</em></strong> of <code>prototype</code> comes here: Let’s take the example of below code snippet</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> objOne = {
    <span class="hljs-attr">fname</span>: <span class="hljs-string">"Ashish"</span>,
    <span class="hljs-attr">lname</span>: <span class="hljs-string">"Shah"</span>,
    <span class="hljs-attr">getFullName</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.lname !== <span class="hljs-literal">undefined</span>) <span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">this</span>.fname}</span> <span class="hljs-subst">${<span class="hljs-built_in">this</span>.lname}</span>`</span>
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.fname;
    }
}

<span class="hljs-keyword">const</span> objTwo = {
    <span class="hljs-attr">fname</span>: <span class="hljs-string">"Abhay"</span>,
    <span class="hljs-attr">lname</span>: <span class="hljs-string">"Sharma"</span>,
}

<span class="hljs-built_in">console</span>.log(objOne.getFullName())
<span class="hljs-built_in">console</span>.log(objTwo.getFullName())
</code></pre>
<p>Can you guess the output for both the <code>console.log()</code> ? (try on). For the first <code>console.log()</code> the output will be <code>Ashish Shah</code> and for the second <code>console.log()</code> we’ll get an error saying: <code>Uncaught TypeError: objTwo.getFullName is not a function</code>. Why is that so? Simply because we don’t have a method named <code>getFullName</code> in the <code>objTwo</code> object, right? Absolutely right. But what if we can make a way so that we can access the <code>getFullName</code> method on <code>objTwo</code> as well? (I’m not gonna write again the <code>getFullName</code> method in <code>objTwo</code> because it will violet <a target="_blank" href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> concept in programming).</p>
<p>So when you run this line: <code>console.log(objTwo.getFullName())</code>, the interpreter’s flow will go like the below image:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739946471676/dddf4a0f-463b-42ca-90aa-055bef452510.png" alt class="image--center mx-auto" /></p>
<p>First will check into object definition methods and if it doesn’t find then goes into <code>.__proto__</code> property’s methods.</p>
<pre><code class="lang-javascript">objTwo.__proto__ 
<span class="hljs-comment">/* .prototype this is the way of accessing prototype values on class like
   String, Array, Date, Person but .__proto__ is the way of accesing 
   prototype on the object/instance of classes.
*/</span>
</code></pre>
<p>and internally JavaScript injects all the properties and methods of its parent class. Here JavaScript did something like this:</p>
<pre><code class="lang-javascript">objTwo.__proto__ = <span class="hljs-built_in">Object</span>.prototype
</code></pre>
<p>and here comes the superpower of the <code>prototype</code> property. If JS engine doing like this <code>objTwo.__proto__ = Object.prototype</code> then can’t we modify it and inject the <code>objOne</code> methods and properties into <code>objTwo.__proto__</code> then we’ll have access to the <code>getFullName</code> method into <code>objTwo</code> as well. Yessssss ofc we can do.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> objTwo = {
    <span class="hljs-attr">fname</span>: <span class="hljs-string">"Abhay"</span>,
    <span class="hljs-attr">lname</span>: <span class="hljs-string">"Sharma"</span>,
}
objTwo.__proto__ = objOne
</code></pre>
<p>Now if you print <code>objTwo</code>, you’ll able to see the <code>getFullName</code> method inside of <code>objTwo.__proto__</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739947876509/95baff2b-b155-476c-88bd-bd607aabd7a4.png" alt class="image--center mx-auto" /></p>
<p>and the third box’s <code>methods</code> are coming from <code>objOne.__proto__</code>, and the second box <code>methods</code> are coming from the <code>objOne</code> object’s definition (or <code>objTwo.__proto__</code>) and the first box’s methods are coming from <code>objTwo</code> object’s definition and this goes on until the interpreter reaches <code>null</code> and this is known as <strong>prototype chaining</strong>.</p>
<h2 id="heading-prototype-chaining">Prototype chaining:</h2>
<p>Keep on checking the methods or properties of the objects by JS interpreter into the object’s prototype until it reaches to null value, is known as prototype chaining.<br />For the above image, the JS interpreter will go like this:</p>
<pre><code class="lang-javascript">objTwo -&gt; objTwo.__proto__ -&gt; objTwo.__proto__.__proto__ -&gt; .... until
objTwo.__proto__.__proto__....__proto__ = <span class="hljs-literal">null</span>
</code></pre>
<h2 id="heading-inheritance">Inheritance:</h2>
<p>Inheriting properties and methods from different objects either directly(using extends keyword) or indirectly (via prototype chaining) is known as inheritance.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// direct inheritance:</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span></span>{
    funInsideA() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello from A class."</span>)
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">B</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">A</span></span>{
    funInsideB() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello from B class."</span>)
    }
}
<span class="hljs-built_in">console</span>.log(B.prototype)
<span class="hljs-comment">// Now class B will also have funInsideA method</span>


<span class="hljs-comment">// indirectly: via prototype chaining as we were doing earlier</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span></span>{
    funInsideA() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello from A class."</span>)
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">B</span></span>{
    funInsideB() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello from B class."</span>)
    }
}
B.prototype = A.prototype
</code></pre>
<h2 id="heading-prototype-inheritance">Prototype Inheritance:</h2>
<p>Inheriting properties and methods via prototype chaining is known as prototype inheritance. below, you can go as nested as you want.</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span></span>{
    funInsideA() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello from A class."</span>)
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">B</span></span>{
    funInsideB() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello from B class."</span>)
    }
}
B.prototype = A.prototype
</code></pre>
<p>Now, can you get the job done of <code>extends</code> keyword without using it? if yes then return something or else read from different sources and come again on this article, you’ll get to know how easy I’ve explained without video :)</p>
<p>Thanks for the reading guys, keep reading and keep writing articles.</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript: Array Methods in JS]]></title><description><![CDATA[💡
TL;DR:


The Goal is to Share: Some important methods of array that are most used in Full Stack Development.
I’m Ashish Shah, writing this blog on ‘Wed 05 Fab 2025 at 21:42:59 PM IST’, because I think these methods should be known by each develope...]]></description><link>https://blog.ashishshah.me/javascript-array-methods-in-js</link><guid isPermaLink="true">https://blog.ashishshah.me/javascript-array-methods-in-js</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Chaiaurcode]]></category><category><![CDATA[ChaiCohort]]></category><category><![CDATA[chai]]></category><category><![CDATA[Chainlink]]></category><category><![CDATA[cohort]]></category><category><![CDATA[@hiteshchoudharylco]]></category><category><![CDATA[#HiteshChaudhary ]]></category><category><![CDATA[#hiteshchoudhary]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[js]]></category><category><![CDATA[Hashnode]]></category><dc:creator><![CDATA[Ashish Shah]]></dc:creator><pubDate>Wed, 05 Feb 2025 20:09:06 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1738785125399/391e2e61-8936-4550-bbdb-312313fc2d3f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">TL;DR:</div>
</div>

<h2 id="heading-the-goal-is-to-share-some-important-methods-of-array-that-are-most-used-in-full-stack-development">The Goal is to Share: Some important methods of array that are most used in Full Stack Development.</h2>
<p>I’m Ashish Shah, writing this blog on ‘Wed 05 Fab 2025 at 21:42:59 PM IST’, because I think these methods should be known by each developer. I’m listening to the song <a target="_blank" href="https://open.spotify.com/track/14GUcf9gXjZpvEVDs4Zztv?si=d80e921899834003">Synergy by Tash Sultana</a></p>
<iframe style="border-radius:12px" src="https://open.spotify.com/embed/track/14GUcf9gXjZpvEVDs4Zztv?utm_source=generator" width="100%" height="152"></iframe>

<h3 id="heading-1-arrayfilter">1. <code>Array.filter()</code>:</h3>
<p>Suppose, you’ve database of Aadhar Card of Indian citizens, and Modi Government ask you to filter out those people who are Eligible for voting so that they can give freebies, How can you do that? using array’s <code>filter()</code> because database (might) contain people’s details in the form of array like this: <code>citizens = [ { name: “Ashish”, age: 21, city: “Bangalore”}, … ]</code></p>
<p><code>Syntax:</code> <code>arrayName.filter(callbackfun, argThis(optional))</code> and <em>it will return an array</em> that passes the certain condition implemented by callbackfun. Let’s see example and you’ll understand better:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> citizens = [
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Ashish"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">21</span>, <span class="hljs-attr">city</span>: <span class="hljs-string">"Bangalore"</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Priya"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">9</span>, <span class="hljs-attr">city</span>: <span class="hljs-string">"Mumbai"</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Ravi"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">18</span>, <span class="hljs-attr">city</span>: <span class="hljs-string">"Delhi"</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Neha"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">16</span>, <span class="hljs-attr">city</span>: <span class="hljs-string">"Chennai"</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Sandeep"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">23</span>, <span class="hljs-attr">city</span>: <span class="hljs-string">"Hyderabad"</span> }
];
<span class="hljs-keyword">let</span> electorate = citizens.filter(<span class="hljs-function">(<span class="hljs-params">citizen</span>) =&gt;</span> citizen.age &gt;= <span class="hljs-number">18</span>)
<span class="hljs-built_in">console</span>.log(electorate);

<span class="hljs-comment">// output</span>
[
  { <span class="hljs-attr">name</span>: <span class="hljs-string">'Ashish'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">21</span>, <span class="hljs-attr">city</span>: <span class="hljs-string">'Bangalore'</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">'Ravi'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">18</span>, <span class="hljs-attr">city</span>: <span class="hljs-string">'Delhi'</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">'Sandeep'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">23</span>, <span class="hljs-attr">city</span>: <span class="hljs-string">'Hyderabad'</span> }
]
<span class="hljs-comment">// as you can see this is also an array.</span>
</code></pre>
<p><code>(citizen) =&gt; citizen.age &gt;= 18</code> this part is function (to be precise <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">arrow function</a>, you can directly pass name of the function there as well.</p>
<p><strong>In brief:</strong> you can filter out elements based on certain conditions, and it will give you array itself that passes the condition or gives <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Glossary/Truthy">Truthy values</a>.</p>
<h3 id="heading-2-arraymap">2. <code>Array.map()</code> :</h3>
<p>Lets say Modi Government wants to give ₹1000 to each woman as freebies in order to win 2029 election. How can they do it? They can use <code>map()</code> function on the array of citizen data, do some manipulation to the data, and returns the array of details of Indian Citizens with updated bank balance and win hell the election.</p>
<p><code>Syntax:</code> <code>array.map(callbackFun, thisArg(optional));</code> and it will return new created array, that will hold the updated value only not all elements. (<code>Hint💡</code>: if you want to hold all value use <code>spread operator</code> first.)</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> womenCitizens = [
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Priya"</span>, <span class="hljs-attr">state</span>: <span class="hljs-string">"Maharashtra"</span>, <span class="hljs-attr">balance</span>: <span class="hljs-number">500</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">30</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Neha"</span>, <span class="hljs-attr">state</span>: <span class="hljs-string">"Tamil Nadu"</span>, <span class="hljs-attr">balance</span>: <span class="hljs-number">1200</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Sita"</span>, <span class="hljs-attr">state</span>: <span class="hljs-string">"Uttar Pradesh"</span>, <span class="hljs-attr">balance</span>: <span class="hljs-number">700</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">35</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Anjali"</span>, <span class="hljs-attr">state</span>: <span class="hljs-string">"Karnataka"</span>, <span class="hljs-attr">balance</span>: <span class="hljs-number">800</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">28</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Rekha"</span>, <span class="hljs-attr">state</span>: <span class="hljs-string">"West Bengal"</span>, <span class="hljs-attr">balance</span>: <span class="hljs-number">600</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">40</span> }
];

womenCitizens.map(<span class="hljs-function">(<span class="hljs-params">women</span>) =&gt;</span> women.balance += <span class="hljs-number">1000</span>)
<span class="hljs-built_in">console</span>.log(womenCitizens);
<span class="hljs-comment">// output: </span>
<span class="hljs-comment">/*[
  { name: 'Priya', state: 'Maharashtra', balance: 1500, age: 30 },
  { name: 'Neha', state: 'Tamil Nadu', balance: 2200, age: 25 },
  { name: 'Sita', state: 'Uttar Pradesh', balance: 1700, age: 35 },
  { name: 'Anjali', state: 'Karnataka', balance: 1800, age: 28 },
  { name: 'Rekha', state: 'West Bengal', balance: 1600, age: 40 }
]
*/</span>
<span class="hljs-built_in">console</span>.log(womenCitizens.map(<span class="hljs-function">(<span class="hljs-params">women</span>) =&gt;</span> women.balance += <span class="hljs-number">1000</span>))
<span class="hljs-comment">// guess the output of above line</span>
</code></pre>
<p><strong>In brief:</strong> want to do some manipulation on the existing array, use <code>map()</code> function of array.</p>
<h3 id="heading-3-arrayforeach">3. <code>Array.forEach()</code>:</h3>
<p>Now, Modi Government decides to meet each one of them who they gave ₹1000 (in programming print or visit each element of array) so that they can whisper <em>“Vote for Modi”</em> nahi to mai paisa wapas le lunga. (otherwise I’ll get back ₹1000 if you don’t vote for Modi).</p>
<p><code>Syntax:</code> <code>array.forEach(callbackFun, thisArg(optional))</code> callback function executes for each element of array once, and inside the callback function you can do whatever you want to do with the element of array. And it returns nothing or <code>undefined</code></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> womenCitizens = [
  { <span class="hljs-attr">name</span>: <span class="hljs-string">'Priya'</span>, <span class="hljs-attr">state</span>: <span class="hljs-string">'Maharashtra'</span>, <span class="hljs-attr">balance</span>: <span class="hljs-number">1500</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">30</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">'Neha'</span>, <span class="hljs-attr">state</span>: <span class="hljs-string">'Tamil Nadu'</span>, <span class="hljs-attr">balance</span>: <span class="hljs-number">2200</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">'Sita'</span>, <span class="hljs-attr">state</span>: <span class="hljs-string">'Uttar Pradesh'</span>, <span class="hljs-attr">balance</span>: <span class="hljs-number">1700</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">35</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">'Anjali'</span>, <span class="hljs-attr">state</span>: <span class="hljs-string">'Karnataka'</span>, <span class="hljs-attr">balance</span>: <span class="hljs-number">1800</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">28</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">'Rekha'</span>, <span class="hljs-attr">state</span>: <span class="hljs-string">'West Bengal'</span>, <span class="hljs-attr">balance</span>: <span class="hljs-number">1600</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">40</span> }
]
<span class="hljs-comment">// lets say government is interested only in names</span>
<span class="hljs-keyword">let</span> val = womenCitizens.forEach(<span class="hljs-function">(<span class="hljs-params">citizen</span>) =&gt;</span> <span class="hljs-built_in">console</span>.log(citizen.name))
<span class="hljs-comment">/* output: 
Priya
Neha
Sita
Anjali
Rekha
*/</span>
<span class="hljs-built_in">console</span>.log(val)
<span class="hljs-comment">// guess the output</span>

<span class="hljs-comment">//output: undefined</span>
</code></pre>
<p><strong>In brief:</strong> If you want to visit each element of an array use <code>forEach()</code> function.</p>
<h3 id="heading-4-arrayfind">4. <code>Array.find()</code>:</h3>
<p>The government has identified their target <a target="_blank" href="https://www.google.com/search?q=electorate&amp;sca_esv=88727e9b8f42f71d&amp;rlz=1C1ONGR_enIN1080IN1080&amp;ei=s6ajZ5TzFrKW4-EP-J7joAU&amp;ved=0ahUKEwjUmJrzja2LAxUyyzgGHXjPGFQQ4dUDCBA&amp;uact=5&amp;oq=electorate&amp;gs_lp=Egxnd3Mtd2l6LXNlcnAiCmVsZWN0b3JhdGUyDhAAGIAEGJECGLEDGIoFMhEQABiABBiRAhixAxiDARiKBTILEAAYgAQYkQIYigUyBRAAGIAEMgUQABiABDIFEAAYgAQyBRAAGIAEMgUQABiABDIFEAAYgAQyBRAAGIAESNEGUNMDWPMEcAF4AZABAJgBkgGgAY0CqgEDMC4yuAEDyAEA-AEBmAICoAKXApgDAIgGAZIHAzAuMqAH3ws&amp;sclient=gws-wiz-serp">electorate</a> and now needs to decide which candidate should represent their side in the <a target="_blank" href="https://www.google.com/search?q=bangalore&amp;rlz=1C1ONGR_enIN1080IN1080&amp;oq=bangalore&amp;gs_lcrp=EgZjaHJvbWUyBggAEEUYOdIBCDE4MTNqMGo5qAIAsAIA&amp;sourceid=chrome&amp;ie=UTF-8">Bangalore</a> region, and they have 5 members for the Bangalore region. They can decide by getting to know who won the most number of times in Bangalore region, and the catch over here is <strong>First Come, First Serve,</strong> meaning who get first in the array will be selected (if their criterion is lets say wins &gt; 5).</p>
<p><code>Syntax:</code> <code>array.find(callbackFun, thisArg(optional))</code> It returns the first element of the array which satisfy the condition, and in the case of no-one, <code>undefined</code> will be returned.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> candidates = [
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Ashish"</span>, <span class="hljs-attr">region</span>: <span class="hljs-string">"Bangalore"</span>, <span class="hljs-attr">wins</span>: <span class="hljs-number">3</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Priya"</span>, <span class="hljs-attr">region</span>: <span class="hljs-string">"Bangalore"</span>, <span class="hljs-attr">wins</span>: <span class="hljs-number">7</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Ravi"</span>, <span class="hljs-attr">region</span>: <span class="hljs-string">"Bangalore"</span>, <span class="hljs-attr">wins</span>: <span class="hljs-number">5</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Neha"</span>, <span class="hljs-attr">region</span>: <span class="hljs-string">"Bangalore"</span>, <span class="hljs-attr">wins</span>: <span class="hljs-number">6</span> },
    { <span class="hljs-attr">name</span>: <span class="hljs-string">"Sandeep"</span>, <span class="hljs-attr">region</span>: <span class="hljs-string">"Bangalore"</span>, <span class="hljs-attr">wins</span>: <span class="hljs-number">8</span> }
];

<span class="hljs-keyword">const</span> candidate = candidates.find(<span class="hljs-function">(<span class="hljs-params">candidate</span>) =&gt;</span> candidate.wins &gt; <span class="hljs-number">5</span>)
<span class="hljs-built_in">console</span>.log(candidate);
<span class="hljs-comment">/* output:
{ name: 'Priya', region: 'Bangalore', wins: 7 }
*/</span>

<span class="hljs-built_in">console</span>.log(candidates.find(<span class="hljs-function">(<span class="hljs-params">candidate</span>) =&gt;</span> candidate.wins &gt; <span class="hljs-number">5</span>));
<span class="hljs-comment">// can you guess the output of the above line?</span>

<span class="hljs-comment">// { name: 'Priya', region: 'Bangalore', wins: 7 }</span>
</code></pre>
<p>Although, there is <code>sandeep</code> who won 8 times but didn’t get select because he comes last in the array, and the <code>find()</code> function works on first-come, first serve.</p>
<p><strong>In brief:</strong> Want to find the first element from L-To-R in the array who satisfies conditions, use <code>find()</code> function.</p>
<h3 id="heading-5-arrayconcat">5. <code>Array.concat()</code>:</h3>
<p>lets say <code>priya</code> didn’t get the winning number of seats in Karnataka but got majority of seats and Modi Government wants to build their Government in Karnataka, they can do alliance with other party, lets say with AAP and Congress as well (that ain’t gonna happen in real life for sure). So they will combine the seats of BJP, AAP and Congress and will get the total seats.</p>
<p><code>Syntax:</code> <code>array.concat(value1, value2, /* …, */ valueN)</code> It returns a new array with the combined values or populated values of all arrays without modifying their individual’s actual values.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Numbers in the array is representing seats in particular region by party</span>
<span class="hljs-keyword">const</span> BJP = [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">2</span>];
<span class="hljs-keyword">const</span> AAP = [<span class="hljs-number">6</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>];
<span class="hljs-keyword">const</span> Congress = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">7</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>];

<span class="hljs-keyword">const</span> total = BJP.concat(AAP, Congress); 
<span class="hljs-comment">// or </span>
<span class="hljs-keyword">const</span> halfhalf = BJP.concat(AAP);
<span class="hljs-keyword">const</span> total = halfhalf.concat(Congress);
<span class="hljs-built_in">console</span>.log(total);
<span class="hljs-comment">/* output: 
[ 3, 4, 5, 2, 6, 3, 4, 1, 2, 7, 3, 5 ]
*/</span>

<span class="hljs-built_in">console</span>.log(AAP);
<span class="hljs-built_in">console</span>.log(BJP);
<span class="hljs-built_in">console</span>.log(Congress);
<span class="hljs-comment">// can you guess the output of the above snippet</span>
</code></pre>
<p><strong>In brief:</strong> All the elements of arrays will be populated into a new array without modifying the original one when you use <code>concat()</code> function.</p>
<h3 id="heading-6-arrayevery">6. <code>Array.every()</code>:</h3>
<p>Now they want to check whether the number of seats from the alliance of a particular region is <code>greater than 2</code> or not? if the seats in particular region is greater than 2 then they will form the alliance otherwise not. To check this they will use <code>every()</code> function of array because if each element I said eachhhhh passes the condition then it will return <code>true</code> only otherwise <code>false</code> .</p>
<p><code>Syntax:</code> <code>array.every(callbackFun, thisArg)</code> It returns <code>boolean</code> value and it will return <code>true</code> if and only if all the elements pass the condition or else <code>false</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Seats from alliance as well</span>
<span class="hljs-keyword">const</span> total = [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">2</span>, <span class="hljs-number">6</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">7</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>]
<span class="hljs-keyword">const</span> willAllianceForm = total.every(<span class="hljs-function">(<span class="hljs-params">seat</span>) =&gt;</span> seat &gt;=<span class="hljs-number">2</span>)

<span class="hljs-built_in">console</span>.log(willAllianceForm);
<span class="hljs-comment">// output: false cuz not all elements are greater than or equal to 2</span>
</code></pre>
<p><strong>In brief:</strong> To check condition on all the elements of an array use <code>every()</code> function.</p>
<h3 id="heading-7-arrayincludes">7. <code>Array.includes()</code>:</h3>
<p>Now enough about Politics and Government for today, lets talk about our most favorite place our wardrobe. You want to check if you have black color shirt or not, and you can represent your shirts in an array (you can right? can’t you🤔). We can check if we have black color shirt or not (I do, wau? you have let me know in comment or mail) using <code>includes()</code> function or array.</p>
<p><code>Syntax:</code> <code>array.includes(toBeSearchElement, fromIndex)</code>: It returns <code>boolean</code> value and it will return <code>true</code> if the element is found from the specified <code>index</code> of the array otherwise it will return <code>false</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> shirts = [<span class="hljs-string">"Red Shirt"</span>, <span class="hljs-string">"Blue Oxford Shirt"</span>, <span class="hljs-string">"Green Polo Shirt"</span>, 
                <span class="hljs-string">"Black linen Shirt"</span>, <span class="hljs-string">"White Linen Shirt"</span>, 
                 <span class="hljs-string">"Gray Checkered Shirt"</span>]
<span class="hljs-keyword">const</span> haveShirt = shirts.includes(<span class="hljs-string">"Black linen Shirt"</span>, <span class="hljs-number">1</span>)
<span class="hljs-built_in">console</span>.log(haveShirt);
<span class="hljs-comment">// output: true</span>

<span class="hljs-keyword">const</span> haveShirt = shirts.includes(<span class="hljs-string">"Black linen Shirt"</span>, <span class="hljs-number">3</span>)
<span class="hljs-built_in">console</span>.log(haveShirt);
<span class="hljs-comment">// output: true</span>

<span class="hljs-keyword">const</span> haveShirt = shirts.includes(<span class="hljs-string">"Black linen Shirt"</span>, <span class="hljs-number">4</span>)
<span class="hljs-built_in">console</span>.log(haveShirt);
<span class="hljs-comment">// output: false</span>
</code></pre>
<p><strong>In brief:</strong> It checks from the specified <code>searchIndex</code> if mentioned otherwise from <code>0</code>, and if finds out then returns <code>true</code> or else <code>false</code>.</p>
<h3 id="heading-8-arraypush">8. <code>Array.push()</code>:</h3>
<p>Now lets suppose you don’t have Black linen Shirt in your wardrobe and you saw me wearing it, and wants to add it because its fucking cool. You can do by using push() function of array(wardrobe).</p>
<p><code>Syntax:</code> <code>array.push(element1, element2, /* …, */ elementN)</code> It returns the <code>length</code> of the new array, and the <code>array</code> will be updated with the values of element1, element2 … (in the case of multiple push elements at the same time) at the end of the array.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> shirts = [<span class="hljs-string">"Red Shirt"</span>, <span class="hljs-string">"Blue Oxford Shirt"</span>, <span class="hljs-string">"Green Polo Shirt"</span>, 
                <span class="hljs-string">"White Linen Shirt"</span>, <span class="hljs-string">"Gray Checkered Shirt"</span>]

shirts.push(<span class="hljs-string">"Black linen Shirt"</span>)
<span class="hljs-built_in">console</span>.log(shirts.push(<span class="hljs-string">"Black linen Shirt"</span>));
<span class="hljs-comment">// output: 7</span>

<span class="hljs-comment">// now guess the output of below line: its little bit of trickey💡</span>
<span class="hljs-built_in">console</span>.log(shirts);


<span class="hljs-comment">/* output:
[
  'Red Shirt',
  'Blue Oxford Shirt',
  'Green Polo Shirt',
  'White Linen Shirt',
  'Gray Checkered Shirt',
  'Black linen Shirt',
  'Black linen Shirt'
]
two times black shirt is added because two times we called push() fn
*/</span>
</code></pre>
<p><strong>In brief:</strong> If you want to add any elements in the last of the array use <code>push()</code> fn.</p>
<h3 id="heading-9-arraypop">9. <code>Array.pop()</code>:</h3>
<p>Now, your mom doesn’t like black color (<code>Note:</code> in your case not mine😎), you’ve to remove it from your wardrobe and return back to zara. You can do this by pop() fn.</p>
<p><code>Syntax:</code> <code>array.pop()</code> It returns the last element from the array and in the case of no element in array, returns <code>undefined</code></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> shirts = [<span class="hljs-string">"Red Shirt"</span>, <span class="hljs-string">"Blue Oxford Shirt"</span>, <span class="hljs-string">"Green Polo Shirt"</span>, 
                <span class="hljs-string">"White Linen Shirt"</span>, <span class="hljs-string">"Gray Checkered Shirt"</span>, 
                <span class="hljs-string">"Black linen Shirt"</span>]

<span class="hljs-keyword">const</span> returnShirt = shirts.pop()
<span class="hljs-built_in">console</span>.log(shirts);
<span class="hljs-comment">/* output: 
[
  'Red Shirt',
  'Blue Oxford Shirt',
  'Green Polo Shirt',
  'White Linen Shirt',
  'Gray Checkered Shirt'
] */</span>
<span class="hljs-built_in">console</span>.log(returnShirt);
<span class="hljs-comment">// output: Black linen Shirt</span>
</code></pre>
<h3 id="heading-10-arrayshift">10. <code>Array.shift()</code>:</h3>
<p>Ok you removed your shirt from the last but what if you want to remove your shirt from the first or left side. Oh! here we are JS developer provided you a method knows as <code>shift()</code> .</p>
<p><code>Syntax:</code> <code>array.shift()</code> It removes the element of the array from the left side or beginning side (<code>index 0</code>) and <code>returns removed element</code>, and in the case of no element in array, it returns <code>undefined</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> shirts = [<span class="hljs-string">"Red Shirt"</span>, <span class="hljs-string">"Blue Oxford Shirt"</span>, <span class="hljs-string">"Green Polo Shirt"</span>, 
                <span class="hljs-string">"White Linen Shirt"</span>, <span class="hljs-string">"Gray Checkered Shirt"</span>, 
                <span class="hljs-string">"Black linen Shirt"</span>]

<span class="hljs-keyword">const</span> returnShirt = shirts.shift()
<span class="hljs-built_in">console</span>.log(shirts);
<span class="hljs-comment">/* output: 
[
  'Blue Oxford Shirt',
  'Green Polo Shirt',
  'White Linen Shirt',
  'Gray Checkered Shirt',
  'Black linen Shirt'
] */</span>
<span class="hljs-built_in">console</span>.log(returnShirt);
<span class="hljs-comment">// output: Red Shirt</span>
</code></pre>
<p><code>In brief:</code> Okay want to remove element from the beginning of the array use <code>shift()</code> fn.</p>
<p>Thank you for reading.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span>(you read all) <span class="hljs-keyword">return</span> “you’re gonna be a good developer”
<span class="hljs-keyword">else</span> <span class="hljs-keyword">return</span> <span class="hljs-string">"au revoir"</span>
</code></pre>
]]></content:encoded></item><item><title><![CDATA[What the heck is "Preprocessor directive" in C++?]]></title><description><![CDATA[💡
tl;dr - certain action is done by pre-processor before actual complilation of source code.


The goal is to share: what is Preprocessor Directive in C & C++ and what are the different ways we use it.
I’m writing this ‘blog’ today 2 February 2025 b...]]></description><link>https://blog.ashishshah.me/what-the-heck-is-preprocessor-directive-in-c</link><guid isPermaLink="true">https://blog.ashishshah.me/what-the-heck-is-preprocessor-directive-in-c</guid><category><![CDATA[#chaiaurcpp]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[ChaiCohort]]></category><category><![CDATA[Chaiaurcode]]></category><category><![CDATA[chai]]></category><category><![CDATA[C++]]></category><category><![CDATA[C]]></category><category><![CDATA[C#]]></category><category><![CDATA[C++]]></category><category><![CDATA[c#]]></category><category><![CDATA[Blogging]]></category><category><![CDATA[blog]]></category><category><![CDATA[Hashnode]]></category><category><![CDATA[HashnodeCommunity]]></category><dc:creator><![CDATA[Ashish Shah]]></dc:creator><pubDate>Sun, 02 Feb 2025 10:10:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1738490504949/f1736590-1544-4453-be29-47b4b8f72257.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">tl;dr - certain action is done by pre-processor before actual complilation of source code.</div>
</div>

<h3 id="heading-the-goal-is-to-share-what-is-preprocessor-directive-in-c-amp-c-and-what-are-the-different-ways-we-use-it">The goal is to share: what is <mark>Preprocessor Directive</mark> in C &amp; C++ and what are the different ways we use it.</h3>
<p>I’m writing this ‘blog’ today 2 February 2025 because I found ‘preprocessor directive’ fascinating way in C++ to do before compilation of code. I’m listening to the song <a target="_blank" href="https://open.spotify.com/track/56Jx2JqTWgzt4QI2B6Ebtj?si=df2e987f2d1d42ff">The Journey by Tom Misch</a></p>
<iframe style="border-radius:12px" src="https://open.spotify.com/embed/track/56Jx2JqTWgzt4QI2B6Ebtj?utm_source=generator" width="100%" height="152"></iframe>

<p>You’ve written this below code snippet, whenever you had chance to write C or C++ program in your school, high-school or uni.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>{
    <span class="hljs-built_in">cout</span>&lt;&lt; <span class="hljs-string">"Welcome to Ashish's blog"</span> &lt;&lt;<span class="hljs-built_in">endl</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>On 29 Jan 2025 I started my C++ journey till then I was learning Java and loved Java so much but had to leave, and when I wrote my very first line code “#include&lt;iostream&gt;”, then I was like what the hell is this and why there is #, can’t it be other symbol than #? at that time my curiosity was at its peak.</p>
<p>I Googled, did ChatGPT, then got to know that there is no single thing # only, it comes with #include and this “#include” is called ‘preprocessor directive’ in C and C++ and there are many types of it. Like to define micro, you write: #define MAX(a,b) ( a &gt; b ? a : b); here #define is also a pre-processor directive.</p>
<h3 id="heading-so-why-is-it-written-in-the-very-first-line-of-code-and-what-does-it-do-to-my-code-and-what-will-happen-if-i-dont-write-this-so-many-questions-right">So why is it written in the very first line of code, and what does it do to my code? and what will happen if I don’t write this? So many questions, right?</h3>
<p>These were the questions in my mind back then. So first look at the diagram below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738487214367/463235bc-e1ac-4ff1-9549-cbfe1d9d6426.png" alt class="image--center mx-auto" /></p>
<p>Do you get anything? a little bit IMO. This is how our code go from a High-level to low level.</p>
<p>So when you write “#include …”, you just told to pre-processor to perform certain action, in this case you’re saying to pre-processor bhai kuchh to lele apne sath me, may be kam aa jaye future me (bro as pre-processor include something that may be we require in the future). Basically we’re giving some instructions to pre-processor to do some action before compile our source file, it can of many types:</p>
<ol>
<li><p>To include some files or headers. (user-defined headers as well)</p>
</li>
<li><p>To run a particular part of code of our program. (Conditional compilation of code)</p>
</li>
<li><p>To guard our file from multiple inclusion of same headers.</p>
</li>
<li><p>To add micro in our program.</p>
</li>
</ol>
<p>So, got the answer why is it written? To perform certain task before actual compilation of code because may be some part of our code is dependent on other files or standard C or C++ library, and to include them we give instructions to pre-processor.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>
</code></pre>
<p>In this case we’re using standard C++ way of taking input and displaying output, but we can also define how are we going to take input eg: from CSV file, spreadsheet, web application etc. and can display output on Refrigerator’s screen, washing machine screen etc. and then we’ll use our header like this:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ioashish"</span></span>
</code></pre>
<p>and you’ll get output on your washing machine’s screen.😎</p>
<p>and if we don’t write this, from where are we going to take input and where to display output? to do so, we include C++ standard I/O header file.</p>
<p>Why there is “#” to start with? because whoever is created the C and C++ language, they set the way to write pre-processor directive, if you don’t like it that’s fine, you can also add any sysmbol as your liking but you’ve to write C++ lang again, and you can do it I’m sure. but why want to do this, ask this question before doing it? (or before anything doing).</p>
<h3 id="heading-suppose-you-want-to-write-a-program-in-c-that-clears-screen-on-different-os-here-you-can-achive-by-single-program-using-pre-processor-directive">Suppose you want to write a program in C++ that clears screen on different OS, here you can achive by single program using “Pre-processor directive”:</h3>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">ifdef</span> __WIN32</span>
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;window.h&gt;</span></span>
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">clearScreen</span><span class="hljs-params">()</span></span>{
        system(<span class="hljs-string">"cls"</span>);
    }
<span class="hljs-meta">#<span class="hljs-meta-keyword">elif</span> __linux__ </span>
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;cstdlib&gt;</span></span>
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">clearScreen</span><span class="hljs-params">()</span></span>{
        system(<span class="hljs-string">"clear"</span>);
    }
<span class="hljs-meta">#<span class="hljs-meta-keyword">else</span></span>
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">clearScreen</span><span class="hljs-params">()</span></span>{
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt; <span class="hljs-string">"Screen can't be clear on this OS. Thank you!"</span>&lt;&lt;<span class="hljs-built_in">std</span>::ndl;
    }
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>{
    clearScreen();
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>That’s from myside guys. au revoir 👋</p>
<p>Hero image credit: <a target="_blank" href="https://cdn.prod.website-files.com/5f9072399b2640f14d6a2bf4/675b68bade331881c764f6c0_image2.png">Discord blog</a></p>
<p>Gonna sooooon write on pre-processor.</p>
]]></content:encoded></item></channel></rss>