<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Param Bedi</title>
    <description>The latest articles on Forem by Param Bedi (@pbedi14).</description>
    <link>https://forem.com/pbedi14</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2484939%2F28f3a091-605d-4a13-a6f1-0cb077bf67c1.png</url>
      <title>Forem: Param Bedi</title>
      <link>https://forem.com/pbedi14</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pbedi14"/>
    <language>en</language>
    <item>
      <title>JS Array Methods - Map, Filter and Reduce</title>
      <dc:creator>Param Bedi</dc:creator>
      <pubDate>Sun, 20 Jul 2025 10:56:35 +0000</pubDate>
      <link>https://forem.com/pbedi14/js-array-methods-map-filter-and-reduce-4id0</link>
      <guid>https://forem.com/pbedi14/js-array-methods-map-filter-and-reduce-4id0</guid>
      <description>&lt;p&gt;So this is day 2 for me on covering all the basics required to prepare for js interview. Today I am going to cover Array methods - Map, Filter and Reduce. &lt;/p&gt;

&lt;h2&gt;
  
  
  Map
&lt;/h2&gt;

&lt;p&gt;So, Map is an array method used to create a new array by applying a function to each element of the input array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nums = [1, 2, 3, 4];

const multipliedTwo = nums.map((val, index, arr) =&amp;gt; {
    return val * 2;
})

console.log(multipliedTwo); // [ 2, 4, 6, 8 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Filter
&lt;/h2&gt;

&lt;p&gt;Filter is an array method used to apply condition on each element of the input array and keep the ones that satisfies the condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const evenNums = nums.filter((val) =&amp;gt; {
    return val % 2 == 0;
});

console.log(evenNums); // [ 2, 4 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Reduce
&lt;/h2&gt;

&lt;p&gt;Reduce is an array method that Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sum = nums.reduce((acc, val) =&amp;gt; {
    return acc + val;
}, 0);

console.log(sum); // 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before diving into some interview questions, lets understand prototype feature provided by js. &lt;/p&gt;

&lt;p&gt;Array.prototype is a core js feature that refers to the object from which all js arrays inherit. &lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; Array inherits from array.prototype which inherits from Object.prototype.&lt;/p&gt;
&lt;h2&gt;
  
  
  Interview Questions based on prototype, map, filter and reduce
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Write a polyfill for map function?
&lt;/h3&gt;

&lt;p&gt;Note: Polyfill means a function manually written for a functionality that is not supported on older web browsers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.myMap = function (callBackFn) {
    let result = [];
    for (let i = 0; i &amp;lt; this.length; i++) {
        result.push(callBackFn(this[i], i, this));
    }
    return result;
};

const multipliedThree = nums.myMap((val) =&amp;gt; {
    return val * 3;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  write a polyfill for filter function?
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.myFilter = function (callBackFn) {
    let result = [];
    for (let i = 0; i &amp;lt; this.length; i++) {
        if (callBackFn(this[i], i, this)) result.push(this[i]);
    }
    return result;
};

const oddNums = nums.myFilter((val) =&amp;gt; {
    return val % 2 !== 0;
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  write a polyfill for reduce function?
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.myReduce = function (callBackFn, initialValue) {
    let acc = initialValue;
    for (let i = 0; i &amp;lt; this.length; i++) {
        acc = acc ? callBackFn(acc, this[i], i, this) : this[i];
    }
    return acc;
}

const sumNums = nums.myReduce((acc, val) =&amp;gt; {
    return acc + val;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Differences btw map and forEach?
&lt;/h2&gt;

&lt;p&gt;Map is used to return a new array where as forEach is used to iterate through array and doesn't return anything.&lt;br&gt;
We can chain array functions on map whereas not possible through forEach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const isMoreThan3Multiple = nums.map((val) =&amp;gt; {
    return val * 2;
}).filter((val) =&amp;gt; val &amp;gt; 3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Write a function to return total of students marks after 20 marks are added to the students less than 60.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
let students = [
    { name: "param", rollNumber: 25, marks: 80 },
    { name: "srijan", rollNumber: 37, marks: 73 },
    { name: "shitiz", rollNumber: 38, marks: 15 },
    { name: "arun", rollNumber: 1, marks: 11 },
]

console.log(students.reduce((acc, stu) =&amp;gt; {
    let newVal = stu.marks &amp;lt; 60 ? stu.marks + 20 : stu.marks;
    return acc + newVal;
}, 0));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>interview</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Difference between let, var and const</title>
      <dc:creator>Param Bedi</dc:creator>
      <pubDate>Sat, 19 Jul 2025 08:42:46 +0000</pubDate>
      <link>https://forem.com/pbedi14/difference-between-let-var-and-const-1jdo</link>
      <guid>https://forem.com/pbedi14/difference-between-let-var-and-const-1jdo</guid>
      <description>&lt;p&gt;So after a long while, I finally decided to switch companies. And it is the perfect time to polish on the basics before I deep dive in interviews. As a Frontend developer, basics matters a lot. In javascript, I think day 1 should be for, how &lt;em&gt;let, var and const&lt;/em&gt; works and what are their differences. So this is my way of writing and learning once again the basics. lets start...&lt;/p&gt;




&lt;h2&gt;
  
  
  Scoping
&lt;/h2&gt;

&lt;p&gt;It is worth noting how the variables are scoped. When declaring variables, if its done with &lt;em&gt;var&lt;/em&gt;, its in global/functional scope where as if the variables are declared with &lt;em&gt;let/const&lt;/em&gt;, its always block scoped. Lets take an example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function varScope() {
  if (true) {
    var x = 10;
  }
  console.log(x); // 10 - accessible due to function scope
}

function letScope() {
  if (true) {
    let y = 20;
  }
  console.log(y); // ReferenceError - y is block scoped
}

{
   const a = 10;
}
console.log(a); // ReferenceError - a is block scoped

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Shadowing
&lt;/h2&gt;

&lt;p&gt;A variable declared in inner scope of a block with the same name as in outer scope shadows (overlaps) the outer one. lets take an example -&lt;/p&gt;

&lt;p&gt;In case of let and const, shadowing happens...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 100;
{
  let a = 200;
  console.log(a); // 200 - inner 'a' shadows the outer one
}

console.log(a); // 100 - outer scope unchanged

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

&lt;/div&gt;



&lt;p&gt;but if you use var in block scope with a let in outer, it will give error as var is global scope which means there will be 2 declarations in global scope at the same time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let b = 10;
{
  var b = 20; // SyntaxError: Identifier 'b' has already been declared
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Redeclaration
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;var _allows redeclaration where as _let, const&lt;/em&gt; doesn't allow redeclaration in the same scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name = 'Alice';
var name = 'Bob'; // Allowed

let city = 'Paris';
let city = 'London'; // SyntaxError - cannot redeclare block-scope variable

const PI = 3.14;
const PI = 3.14159; // SyntaxError - cannot redeclare block-scope variable

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Declaration without initialization and Reassigning
&lt;/h2&gt;

&lt;p&gt;var and let variables can be declared without initialization and reassigned where as const needs to be initialized at the time of declaration and cannot be reassigned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a; // Correct
let b;  // Correct
const c; // SyntaxError: Missing initializer in const declaration

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 10;
x = 15; // correct

let y = 20;
y = 25; // correct

const z = 30;
z = 35; // TypeError: Assignment to constant variable

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Hoisting
&lt;/h2&gt;

&lt;p&gt;A JS program consists of 2 phases - creation phase and execution phase. &lt;/p&gt;

&lt;p&gt;During Creation phase, JS engine creates global/window object and allocates heap memory for declaration of variables.&lt;/p&gt;

&lt;p&gt;During this phase, all variables and functions are moved to top of the code and declared. variables are initialized _undefined _whereas whole function is declared. This is called hoisting.&lt;/p&gt;

&lt;p&gt;Remember, var is hoisted but let, const are not (they are hoisted technically but in temporal deadzone which means they are in the scope but not declared yet).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(price); // undefined (hoisted)
var price = 100;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(message); // ReferenceError (not initialized)
let message = "Hello";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The temporal dead zone is the time between entering scope and the variable's actual declaration where let and const exist, but accessing them throws an error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Notes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid using var in modern JS code unless absolutely necessary.&lt;/li&gt;
&lt;li&gt;Prefer let for variables that need reassignment.&lt;/li&gt;
&lt;li&gt;Use const by default for immutable references.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>interview</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
