<?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: Zakir</title>
    <description>The latest articles on DEV Community by Zakir (@amdzakir).</description>
    <link>https://dev.to/amdzakir</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%2F1449206%2Fabbec293-13f0-48da-8325-c50a6b908200.png</url>
      <title>DEV Community: Zakir</title>
      <link>https://dev.to/amdzakir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amdzakir"/>
    <language>en</language>
    <item>
      <title>10 cool habits that helps frontend developer to code faster (React + TS Edition)</title>
      <dc:creator>Zakir</dc:creator>
      <pubDate>Thu, 22 May 2025 06:12:16 +0000</pubDate>
      <link>https://dev.to/amdzakir/10-cool-habits-that-helps-frontend-developer-to-code-faster-react-ts-edition-31m7</link>
      <guid>https://dev.to/amdzakir/10-cool-habits-that-helps-frontend-developer-to-code-faster-react-ts-edition-31m7</guid>
      <description>&lt;p&gt;Hey folks,&lt;/p&gt;

&lt;p&gt;I've been working with &lt;strong&gt;React, Redux, and TypeScript&lt;/strong&gt; for years now, and if you're anything like me, you've probably hit that point where it's less about learning &lt;em&gt;what&lt;/em&gt; to do — and more about &lt;em&gt;how to do it faster and better&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So here are &lt;strong&gt;10 habits/tools/processes&lt;/strong&gt; I’ve picked up over the years that have genuinely helped me &lt;strong&gt;code faster and more confidently&lt;/strong&gt; as a frontend dev.&lt;/p&gt;

&lt;p&gt;Let’s dive in.&lt;/p&gt;




&lt;h3&gt;
  
  
  1. &lt;strong&gt;Set up proper snippet shortcuts (and actually use them)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;VSCode snippets are like second brains. I have shortcuts for common stuff like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;rfce&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="nx"&gt;Functional&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="k"&gt;export&lt;/span&gt;  
&lt;span class="nx"&gt;rtsx&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="nx"&gt;TS&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;  
&lt;span class="nx"&gt;dux&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="nx"&gt;Redux&lt;/span&gt; &lt;span class="nx"&gt;slice&lt;/span&gt; &lt;span class="nx"&gt;boilerplate&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using these religiously saves &lt;em&gt;hours&lt;/em&gt; every week.&lt;br&gt;
&lt;em&gt;Use Popular Extensions&lt;/em&gt;&lt;br&gt;
ES7+ React/Redux/React-Native snippets&lt;br&gt;
👉 Extension ID: dsznajder.es7-react-js-snippets&lt;br&gt;
🔗 Install from Marketplace&lt;/p&gt;


&lt;h3&gt;
  
  
  2. &lt;strong&gt;Use a component library (your own or one like shadcn/ui)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As a senior, you’re either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Building components again and again 😩&lt;/li&gt;
&lt;li&gt;Or reusing tested ones 🙌&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pick the second one. Personally, I love &lt;a href="https://ui.shadcn.com/" rel="noopener noreferrer"&gt;shadcn/ui&lt;/a&gt; for React/TS projects. If you’re working in a team, push for a design system + Storybook combo early.&lt;/p&gt;


&lt;h3&gt;
  
  
  3. &lt;strong&gt;Automate imports with &lt;code&gt;@&lt;/code&gt; paths and aliases&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This tiny &lt;code&gt;tsconfig.json&lt;/code&gt; tweak avoids spaghetti imports:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"paths"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"@components/*"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"src/components/*"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"@hooks/*"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"src/hooks/*"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you import with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@components/ui&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cleaner. Faster. Less hunting in folders.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. &lt;strong&gt;Have your “starter repo” ready&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Have a private GitHub template with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vite + React + TS&lt;/li&gt;
&lt;li&gt;Prettier + ESLint + Husky&lt;/li&gt;
&lt;li&gt;Tailwind (optional)&lt;/li&gt;
&lt;li&gt;Zod + Zustand / Redux Toolkit setup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One click, and I’m 30 mins ahead in any new project. Big time-saver.&lt;/p&gt;




&lt;h3&gt;
  
  
  5. &lt;strong&gt;Use AI coding tools — but don’t outsource your brain&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Yes, I use GitHub Copilot. Yes, it sometimes guesses better prop names than I would 😅.&lt;br&gt;
But I never accept a block without reading it.&lt;/p&gt;

&lt;p&gt;It’s a speed &lt;em&gt;assist&lt;/em&gt;, not an auto-pilot.&lt;/p&gt;


&lt;h3&gt;
  
  
  6. &lt;strong&gt;Master your debugger, not just &lt;code&gt;console.log&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Seriously — put breakpoints, inspect call stacks, step through logic.&lt;/p&gt;

&lt;p&gt;It took me way too long to realize &lt;strong&gt;debuggers are made for humans like us&lt;/strong&gt;, not backend-only devs.&lt;/p&gt;


&lt;h3&gt;
  
  
  7. &lt;strong&gt;TypeScript is your best friend. Make types strict early.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I used to avoid &lt;code&gt;as const&lt;/code&gt; and &lt;code&gt;Record&amp;lt;string, string&amp;gt;&lt;/code&gt; like the plague. Now I use them to &lt;em&gt;lock in assumptions&lt;/em&gt; early, so bugs don’t creep in later.&lt;/p&gt;

&lt;p&gt;Start strict, not lazy.&lt;/p&gt;


&lt;h3&gt;
  
  
  8. &lt;strong&gt;Get fast at Git (CLI + GUI hybrid)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I use the CLI for speed but also love a good GUI like GitHub Desktop or LazyGit when resolving conflicts.&lt;/p&gt;

&lt;p&gt;Pro tip: &lt;code&gt;git commit -S -m "feat: add payment button"&lt;/code&gt; with commit signing = 🔒 + ✅&lt;/p&gt;


&lt;h3&gt;
  
  
  9. &lt;strong&gt;Use Chrome DevTools like a pro&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cmd + shift + p&lt;/code&gt; → Power commands&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$$("selector")&lt;/code&gt; → Run jQuery-style selection in console&lt;/li&gt;
&lt;li&gt;Device emulation, network throttling, performance profiling = gold for frontend polish.&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  10. &lt;strong&gt;Write TODOs like a breadcrumb trail&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Instead of just:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// TODO: refactor this later&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I now write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// TODO [Auth Refactor Q3]: This login check should move to middleware&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s so much easier for &lt;em&gt;Future Me&lt;/em&gt; (and teammates) to understand what's going on.&lt;/p&gt;

&lt;p&gt;That’s it! These aren’t magic tricks — just small changes with big impact over time. I hope a few of these resonate with you.&lt;/p&gt;

&lt;p&gt;Got a tip I should add to my workflow? Drop it in the comments — I’m always tweaking my setup.&lt;/p&gt;

&lt;p&gt;Happy coding! 👨‍💻⚡&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>css</category>
      <category>git</category>
    </item>
    <item>
      <title>Level Up Your JavaScript Error Handling: From `try...catch` to Custom Errors</title>
      <dc:creator>Zakir</dc:creator>
      <pubDate>Mon, 23 Sep 2024 06:09:11 +0000</pubDate>
      <link>https://dev.to/amdzakir/level-up-your-javascript-error-handling-from-trycatch-to-custom-errors-jge</link>
      <guid>https://dev.to/amdzakir/level-up-your-javascript-error-handling-from-trycatch-to-custom-errors-jge</guid>
      <description>&lt;p&gt;Error handling is one of those things every JavaScript developer encounters, but not everyone dives deep into mastering it. If you’ve been relying on simple &lt;code&gt;try...catch&lt;/code&gt; statements, it's time to elevate your game. This post will take you through the journey from basic error handling to crafting custom errors, making your code more resilient and easier to debug.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Basics: &lt;code&gt;try...catch&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Let's start with the classic &lt;code&gt;try...catch&lt;/code&gt;:&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="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{"name": "John"}&lt;/span&gt;&lt;span class="dl"&gt;'&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;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// This will cause an error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Something went wrong:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is simple and effective. The &lt;code&gt;try&lt;/code&gt; block lets you run code that might throw an error, and the &lt;code&gt;catch&lt;/code&gt; block captures it, allowing you to handle it gracefully.&lt;/p&gt;

&lt;p&gt;However, as your application grows, relying solely on this can lead to less informative error messages, which makes debugging a nightmare. This is where custom error handling shines.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Understanding Native Error Types
&lt;/h2&gt;

&lt;p&gt;JavaScript comes with built-in error types, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Error&lt;/code&gt;: The generic error object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TypeError&lt;/code&gt;: Thrown when a variable is not of the expected type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SyntaxError&lt;/code&gt;: Thrown when code contains a syntax mistake.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ReferenceError&lt;/code&gt;: Thrown when referencing a non-existent variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&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="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;myUndefinedFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;ReferenceError&lt;/span&gt;&lt;span class="p"&gt;)&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ReferenceError detected:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Unexpected error:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By utilizing these types, you can start building more descriptive error handling paths. But what if you want to define errors specific to your application logic? That's where custom errors come in.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Creating Custom Errors
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why Custom Errors?
&lt;/h3&gt;

&lt;p&gt;Custom errors help in identifying specific issues within your code, making it much easier to debug. For example, if you're building an API, you might want to differentiate between &lt;code&gt;ValidationError&lt;/code&gt;, &lt;code&gt;AuthenticationError&lt;/code&gt;, or &lt;code&gt;DatabaseError&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Create Custom Errors
&lt;/h3&gt;

&lt;p&gt;Let’s create a &lt;code&gt;ValidationError&lt;/code&gt; as an example:&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;class&lt;/span&gt; &lt;span class="nc"&gt;ValidationError&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ValidationError&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ValidationError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Age cannot be negative!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;ValidationError&lt;/span&gt;&lt;span class="p"&gt;)&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Validation Error:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Unexpected error:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By extending the &lt;code&gt;Error&lt;/code&gt; class, we're able to create a more meaningful &lt;code&gt;ValidationError&lt;/code&gt;. This approach gives clarity about the nature of the problem, making debugging easier and your code cleaner.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Enriching Custom Errors with More Information
&lt;/h2&gt;

&lt;p&gt;Why stop at just a custom message? Let’s enrich our custom error with additional properties like &lt;code&gt;errorCode&lt;/code&gt; or &lt;code&gt;statusCode&lt;/code&gt;.&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;class&lt;/span&gt; &lt;span class="nc"&gt;HttpError&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;HttpError&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HttpError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Resource not found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;HttpError&lt;/span&gt;&lt;span class="p"&gt;)&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Error &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Unexpected error:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, you can capture more context about the error, making it easier to act upon or log effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Building a Global Error Handling Strategy
&lt;/h2&gt;

&lt;p&gt;As your application scales, you’ll want a centralized way to handle errors. One approach is to create an error-handling utility that handles different error types.&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;function&lt;/span&gt; &lt;span class="nf"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;ValidationError&lt;/span&gt;&lt;span class="p"&gt;)&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Validation failed:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;HttpError&lt;/span&gt;&lt;span class="p"&gt;)&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`HTTP error (&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;):`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;An unexpected error occurred:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HttpError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Internal Server Error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This centralized error handling helps keep your code DRY (Don't Repeat Yourself) and ensures consistent error processing across your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Leveraging &lt;code&gt;finally&lt;/code&gt; for Cleanup
&lt;/h2&gt;

&lt;p&gt;Often, you might need to perform cleanup tasks like closing a database connection or clearing timeouts. That’s where &lt;code&gt;finally&lt;/code&gt; comes in handy:&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="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Attempt to execute code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Handle errors&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This will always execute, whether an error occurred or not.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;finally&lt;/code&gt; ensures that this block executes regardless of whether an error was thrown or not, making it ideal for cleanup operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Integrating with Logging Services
&lt;/h2&gt;

&lt;p&gt;For production applications, it’s essential to log errors. Services like Sentry, LogRocket, or Datadog can capture errors with full stack traces, making troubleshooting much easier.&lt;/p&gt;

&lt;p&gt;Example:&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="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Sentry&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@sentry/browser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;Sentry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YOUR_SENTRY_DSN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Your code that might throw an error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Sentry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;captureException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This integration provides better visibility into issues and helps you monitor the health of your application in real-time.&lt;/p&gt;

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

&lt;p&gt;By moving beyond the basics of &lt;code&gt;try...catch&lt;/code&gt; and incorporating custom errors, you create more maintainable, readable, and robust JavaScript code. Implementing these practices will not only make debugging a breeze but also improve the overall reliability of your application.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s Next?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Experiment with custom errors in your current project.&lt;/li&gt;
&lt;li&gt;Implement a centralized error handler.&lt;/li&gt;
&lt;li&gt;Integrate error logging services in your app.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let me know how you handle errors in your projects, or if you have any cool tips and tricks to share!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Enjoyed this post? Follow me for more JavaScript tips and tricks!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>trycatch</category>
      <category>errors</category>
      <category>customerror</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Spread and Rest Operators in JavaScript (...)</title>
      <dc:creator>Zakir</dc:creator>
      <pubDate>Fri, 13 Sep 2024 09:50:51 +0000</pubDate>
      <link>https://dev.to/amdzakir/spread-and-rest-operators-in-javascript-57j9</link>
      <guid>https://dev.to/amdzakir/spread-and-rest-operators-in-javascript-57j9</guid>
      <description>&lt;p&gt;&lt;strong&gt;Snack story:&lt;/strong&gt;&lt;br&gt;
Let’s say you have a basket of snacks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const snacks = ['apple', 'banana', 'chocolate'];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you want to share these snacks with your friends. But instead of giving them the whole basket, you take out each snack and give it to them one by one:&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(...snacks);  // Output: apple banana chocolate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ... (spread) operator is like taking the snacks out of the basket and spreading them on the table. Your friends can now pick them up individually!&lt;/p&gt;

&lt;p&gt;On the other hand, if your friends bring even more snacks, you can gather them all into one big basket using the ... (rest) operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function collectSnacks(...moreSnacks) {
  console.log(moreSnacks);
}

collectSnacks('cookie', 'chips', 'juice');  // Output: ['cookie', 'chips', 'juice']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the ... operator collects all the snacks into a new basket. This is how the rest operator works!&lt;/p&gt;

&lt;p&gt;If you're new to JavaScript, you might have come across two special syntax elements: the spread and rest operators. They both look the same—three dots (...)—but they are used in different ways. Let’s break them down with simple examples!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. What is the Spread Operator?&lt;/strong&gt;&lt;br&gt;
The spread operator (...) is used to expand an array or an object into individual elements. It’s like unpacking an array or object into its components.&lt;/p&gt;

&lt;p&gt;Example: Expanding an Array&lt;br&gt;
Let’s say you have an array of numbers, and you want to pass them individually into a 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 numbers = [1, 2, 3];
console.log(...numbers);  // Output: 1 2 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the spread operator to spread the array elements out!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Combining Arrays&lt;/strong&gt;&lt;br&gt;
If you have two arrays and want to merge them, the spread operator makes it easy:&lt;br&gt;
&lt;/p&gt;

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

const mergedArray = [...array1, ...array2];
console.log(mergedArray);  // Output: [1, 2, 3, 4, 5, 6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s like laying out all the elements from array1 and array2 into one new array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Copying an Object&lt;/strong&gt;&lt;br&gt;
You can also use the spread operator with objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user = { name: "Ahmed", age: 25 };
const updatedUser = { ...user, location: "Bengaluru" };

console.log(updatedUser);
// Output: { name: "Ahmed", age: 25, location: "Bengaluru" }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we copied the properties from the &lt;code&gt;user&lt;/code&gt; object and added a new property &lt;code&gt;location&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is the Rest Operator?
The rest operator is the opposite of the spread operator. Instead of expanding an array or object, it collects multiple elements into an array or object.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example: Function with Rest Parameters&lt;/strong&gt;&lt;br&gt;
Imagine you’re writing a function that takes any number of arguments:&lt;br&gt;
&lt;/p&gt;

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

console.log(addNumbers(1, 2, 3, 4));  // Output: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the rest operator ...nums collects all the arguments into an array, making it easier to work with multiple values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Destructuring with Rest&lt;/strong&gt;&lt;br&gt;
You can also use the rest operator when destructuring arrays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [first, ...rest] = [1, 2, 3, 4];
console.log(first);  // Output: 1
console.log(rest);   // Output: [2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, first gets the first element of the array, and rest collects the remaining elements into a new array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;spread&lt;/code&gt; and &lt;code&gt;rest&lt;/code&gt; operators are powerful and versatile tools in JavaScript. To recap:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spread (...) expands an array or object into individual elements.&lt;/li&gt;
&lt;li&gt;Rest (...) collects multiple elements into an array or object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope you understood these 2 concepts spread and rest, it will make your JavaScript code more concise and easier to read!&lt;/p&gt;

&lt;p&gt;Happy coding :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>spread</category>
      <category>rest</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
