<?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>DEV Community: Akash Singh</title>
    <description>The latest articles on DEV Community by Akash Singh (@akashsingh-dev).</description>
    <link>https://dev.to/akashsingh-dev</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3981716%2F722b57c0-2068-444a-a6ca-3c1fd78cc830.jpg</url>
      <title>DEV Community: Akash Singh</title>
      <link>https://dev.to/akashsingh-dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akashsingh-dev"/>
    <language>en</language>
    <item>
      <title>A Cleaner Way to Access the Last Element of an Array in JavaScript</title>
      <dc:creator>Akash Singh</dc:creator>
      <pubDate>Sun, 26 Jul 2026 15:47:14 +0000</pubDate>
      <link>https://dev.to/akashsingh-dev/a-cleaner-way-to-access-the-last-element-of-an-array-in-javascript-42p7</link>
      <guid>https://dev.to/akashsingh-dev/a-cleaner-way-to-access-the-last-element-of-an-array-in-javascript-42p7</guid>
      <description>&lt;p&gt;Most JavaScript developers access the last element of an array like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 40&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works perfectly and is still a common approach.&lt;/p&gt;

&lt;p&gt;However, modern JavaScript provides a cleaner alternative:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 40&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why use &lt;code&gt;.at()&lt;/code&gt;?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;✅ Cleaner and easier to read.&lt;/li&gt;
&lt;li&gt;✅ Supports negative indexing.&lt;/li&gt;
&lt;li&gt;✅ No need to calculate &lt;code&gt;length - 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;✅ Great for accessing elements from the end of an array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  More examples
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 40&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 30&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of calculating indexes manually, you can directly access elements from the end of an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Both approaches are correct.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;arr[arr.length - 1]&lt;/code&gt; is the classic and widely used approach.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;arr.at(-1)&lt;/code&gt; is a modern, more expressive alternative that improves readability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Small JavaScript features like this can make your code cleaner over time.&lt;/p&gt;

&lt;p&gt;💬 **Do you use &lt;code&gt;.at()&lt;/code&gt; in your projects, or do you still prefer &lt;code&gt;arr[arr.length - 1]&lt;/code&gt;? Let me know in the comments!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Can You Predict the Output? 🤔 A JavaScript Equality Puzzle That Tricks Developers</title>
      <dc:creator>Akash Singh</dc:creator>
      <pubDate>Tue, 21 Jul 2026 09:18:43 +0000</pubDate>
      <link>https://dev.to/akashsingh-dev/can-you-predict-the-output-a-javascript-equality-puzzle-that-tricks-developers-1b23</link>
      <guid>https://dev.to/akashsingh-dev/can-you-predict-the-output-a-javascript-equality-puzzle-that-tricks-developers-1b23</guid>
      <description>&lt;p&gt;JavaScript's loose equality (==) can produce some surprising results because of type coercion. Here's a classic interview question that confuses even experienced developers.&lt;/p&gt;

&lt;p&gt;If you think you know JavaScript, try answering this without running the code.&lt;/p&gt;

&lt;p&gt;console.log([] == ![]);&lt;br&gt;
🤔 What will be the output?&lt;br&gt;
A. true&lt;br&gt;
B. false&lt;br&gt;
C. undefined&lt;br&gt;
D. TypeError&lt;/p&gt;

&lt;p&gt;Take a moment to guess before reading further.&lt;/p&gt;

&lt;p&gt;✅ Answer&lt;/p&gt;

&lt;p&gt;The output is:&lt;/p&gt;

&lt;p&gt;true&lt;/p&gt;

&lt;p&gt;Surprised?&lt;/p&gt;

&lt;p&gt;Let's break it down step by step.&lt;/p&gt;

&lt;p&gt;Step 1: Evaluate ![]&lt;/p&gt;

&lt;p&gt;In JavaScript, every object (including an empty array) is truthy.&lt;/p&gt;

&lt;p&gt;Boolean([]); // true&lt;/p&gt;

&lt;p&gt;Therefore,&lt;/p&gt;

&lt;p&gt;![] // false&lt;/p&gt;

&lt;p&gt;Now the expression becomes:&lt;/p&gt;

&lt;p&gt;[] == false&lt;br&gt;
Step 2: Loose Equality (==)&lt;/p&gt;

&lt;p&gt;The == operator performs type coercion.&lt;/p&gt;

&lt;p&gt;Since one side is a boolean, JavaScript converts it to a number.&lt;/p&gt;

&lt;p&gt;false // becomes 0&lt;/p&gt;

&lt;p&gt;Now the comparison is:&lt;/p&gt;

&lt;p&gt;[] == 0&lt;br&gt;
Step 3: Convert the Array&lt;/p&gt;

&lt;p&gt;When an array is compared with a primitive, JavaScript converts the array into a primitive value.&lt;/p&gt;

&lt;p&gt;[].toString(); // ""&lt;/p&gt;

&lt;p&gt;So the comparison becomes:&lt;/p&gt;

&lt;p&gt;"" == 0&lt;br&gt;
Step 4: Convert the String to a Number&lt;/p&gt;

&lt;p&gt;An empty string converts to the number 0.&lt;/p&gt;

&lt;p&gt;Number(""); // 0&lt;/p&gt;

&lt;p&gt;Now JavaScript compares:&lt;/p&gt;

&lt;p&gt;0 == 0&lt;/p&gt;

&lt;p&gt;Which evaluates to:&lt;/p&gt;

&lt;p&gt;true&lt;br&gt;
🔄 Conversion Chain&lt;br&gt;
[] == ![]&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;![] → false&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;[] == false&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;false → 0&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;[] == 0&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;[] → ""&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;"" → 0&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;0 == 0&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;true ✅&lt;br&gt;
💡 Why Does This Happen?&lt;/p&gt;

&lt;p&gt;The == operator follows JavaScript's Abstract Equality Comparison rules.&lt;/p&gt;

&lt;p&gt;Instead of comparing values directly, JavaScript automatically converts operands into compatible types before making the comparison.&lt;/p&gt;

&lt;p&gt;That's why many developers recommend using:&lt;/p&gt;

&lt;p&gt;===&lt;/p&gt;

&lt;p&gt;The strict equality operator (===) compares both value and type, avoiding unexpected type coercion.&lt;/p&gt;

&lt;p&gt;⚡ Key Takeaways&lt;br&gt;
[] is an object, and all objects are truthy.&lt;br&gt;
![] evaluates to false.&lt;br&gt;
false is converted to 0.&lt;br&gt;
[] becomes an empty string ("").&lt;br&gt;
"" converts to 0.&lt;br&gt;
Finally, JavaScript compares 0 == 0, which is true.&lt;br&gt;
🧩 Your Turn&lt;/p&gt;

&lt;p&gt;Without running the code, what do you think this prints?&lt;/p&gt;

&lt;p&gt;console.log([] + []);&lt;/p&gt;

&lt;p&gt;Drop your answer in the comments before testing it.&lt;/p&gt;

&lt;p&gt;If you enjoyed this puzzle, follow me for more JavaScript interview questions, tricky outputs, and deep dives into how JavaScript actually works.&lt;/p&gt;

&lt;p&gt;Happy Coding! 🚀&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How I Merged Two MongoDB Collections Using $unionWith</title>
      <dc:creator>Akash Singh</dc:creator>
      <pubDate>Sat, 13 Jun 2026 06:04:16 +0000</pubDate>
      <link>https://dev.to/akashsingh-dev/how-i-merged-two-mongodb-collections-using-unionwith-2mj9</link>
      <guid>https://dev.to/akashsingh-dev/how-i-merged-two-mongodb-collections-using-unionwith-2mj9</guid>
      <description>&lt;h1&gt;
  
  
  How I Merged Two MongoDB Collections Using &lt;code&gt;$unionWith&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;While working on a doctor listing feature, I encountered an issue where some doctors were missing from the All Doctors page.&lt;/p&gt;

&lt;p&gt;After investigating the issue, I found that data was being fetched from two separate collections, and pagination was being applied independently. This resulted in inconsistent results and unnecessary complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;I had two collections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ReferralUser&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;user_managements&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fetching data separately caused:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple API calls&lt;/li&gt;
&lt;li&gt;Missing records&lt;/li&gt;
&lt;li&gt;Inconsistent pagination&lt;/li&gt;
&lt;li&gt;Additional frontend logic&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;MongoDB's &lt;code&gt;$unionWith&lt;/code&gt; aggregation stage allowed me to combine both collections into a single pipeline.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
js
`db.ReferralUser.aggregate([
  {
    $unionWith: {
      coll: "user_managements"
    }
  }
]);`
---

Benefits
Reduced multiple API calls into one
Improved pagination consistency
Simplified frontend logic
Easier to maintain
Key Takeaways

When working with similar data across multiple collections, $unionWith can help simplify the API and provide more consistent results.

Thanks for reading! 🚀
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>mongodb</category>
      <category>node</category>
      <category>backend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
