<?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: nishchal singh</title>
    <description>The latest articles on DEV Community by nishchal singh (@nishchaldev).</description>
    <link>https://dev.to/nishchaldev</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%2F1231797%2F0c243537-17d9-431b-b323-f81531f96741.jpg</url>
      <title>DEV Community: nishchal singh</title>
      <link>https://dev.to/nishchaldev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nishchaldev"/>
    <language>en</language>
    <item>
      <title>Why Date and Time Bugs Happen in JavaScript (And How Apps Actually Get It Right)</title>
      <dc:creator>nishchal singh</dc:creator>
      <pubDate>Tue, 10 Feb 2026 11:46:37 +0000</pubDate>
      <link>https://dev.to/nishchaldev/why-date-and-time-bugs-happen-in-javascript-and-how-apps-actually-get-it-right-hjh</link>
      <guid>https://dev.to/nishchaldev/why-date-and-time-bugs-happen-in-javascript-and-how-apps-actually-get-it-right-hjh</guid>
      <description>&lt;h2&gt;
  
  
  If you’ve ever seen reminders fire early, late, or on the wrong day,
&lt;/h2&gt;

&lt;p&gt;this post is for you.&lt;/p&gt;

&lt;p&gt;I used to think date and time bugs were just edge cases.&lt;/p&gt;

&lt;p&gt;They’re not.&lt;/p&gt;

&lt;p&gt;Most of the time, they come from a slightly wrong mental model of how&lt;br&gt;
time actually works in JavaScript. The problem only becomes visible&lt;br&gt;
when an app starts dealing with real users, real schedules, and real&lt;br&gt;
expectations.&lt;/p&gt;

&lt;p&gt;This post explains why these bugs happen so often, how JavaScript really&lt;br&gt;
handles date and time, and how production apps avoid them.&lt;/p&gt;

&lt;p&gt;No heavy theory.&lt;br&gt;
No library comparisons.&lt;br&gt;
Just the parts that matter.&lt;/p&gt;


&lt;h2&gt;
  
  
  The illusion: “I used &lt;code&gt;Date()&lt;/code&gt;, so time should be correct”
&lt;/h2&gt;

&lt;p&gt;Most developers start with 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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It prints a date and time that looks correct, so we assume everything&lt;br&gt;
underneath is local and intuitive.&lt;/p&gt;

&lt;p&gt;That assumption is where many bugs begin.&lt;/p&gt;

&lt;p&gt;JavaScript dates are not stored as local time.&lt;/p&gt;

&lt;p&gt;Internally, they are stored as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Milliseconds since January 1, 1970 (UTC)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Local time, timezone, and formatting are applied only when the date is&lt;br&gt;
read or displayed.&lt;/p&gt;

&lt;p&gt;This sounds like a small detail.&lt;br&gt;
In real apps, it isn’t.&lt;/p&gt;


&lt;h2&gt;
  
  
  What JavaScript is actually doing
&lt;/h2&gt;

&lt;p&gt;A useful mental model is this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript stores one number&lt;/li&gt;
&lt;li&gt;That number represents a moment in UTC&lt;/li&gt;
&lt;li&gt;Local time is calculated only at display or formatting time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For 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;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2026-02-10T09:00:00&lt;/span&gt;&lt;span class="dl"&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 does not mean “9am in my timezone”.&lt;/p&gt;

&lt;p&gt;It means “9am UTC, converted to the user’s local timezone”.&lt;/p&gt;

&lt;p&gt;If your app does not control that context explicitly, time slowly drifts.&lt;/p&gt;




&lt;h2&gt;
  
  
  The three most common time mistakes
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw02rwrm9tjh42b8drtyy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw02rwrm9tjh42b8drtyy.png" alt="The three most common time mistakes in JavaScript" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Assuming &lt;code&gt;Date()&lt;/code&gt; is local internally
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Date&lt;/code&gt; behaves like a local clock, but it is not one.&lt;/p&gt;

&lt;p&gt;It is a UTC timestamp with a local view layered on top.&lt;/p&gt;

&lt;p&gt;This becomes a problem when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;users are in different timezones&lt;/li&gt;
&lt;li&gt;servers run in UTC&lt;/li&gt;
&lt;li&gt;background jobs don’t share locale information&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2. Mixing server time and client time
&lt;/h3&gt;

&lt;p&gt;A very common pattern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;server schedules something&lt;/li&gt;
&lt;li&gt;client displays or adjusts it&lt;/li&gt;
&lt;li&gt;background job runs later&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If these layers don’t agree on what “now” means, reminders drift.&lt;/p&gt;

&lt;p&gt;This is especially dangerous for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;recurring schedules&lt;/li&gt;
&lt;li&gt;weekly reminders&lt;/li&gt;
&lt;li&gt;“tomorrow at 9am” logic&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  3. Generating dates without context
&lt;/h3&gt;

&lt;p&gt;Any logic that generates dates needs to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;today’s date&lt;/li&gt;
&lt;li&gt;the user’s timezone&lt;/li&gt;
&lt;li&gt;daylight saving rules&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without this context, it’s easy to generate reminders:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in the past&lt;/li&gt;
&lt;li&gt;on the wrong day&lt;/li&gt;
&lt;li&gt;in the wrong year&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why this gets worse in PWAs
&lt;/h2&gt;

&lt;p&gt;These problems become much more visible in Progressive Web Apps.&lt;/p&gt;

&lt;p&gt;Browsers are not designed to guarantee precise scheduling.&lt;br&gt;
They optimize for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;battery life&lt;/li&gt;
&lt;li&gt;performance&lt;/li&gt;
&lt;li&gt;security&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;background execution is delayed&lt;/li&gt;
&lt;li&gt;service workers wake unpredictably&lt;/li&gt;
&lt;li&gt;scheduled logic runs in time windows, not exact moments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6qwxvaiflf3hnw9tkcup.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6qwxvaiflf3hnw9tkcup.png" alt="Why scheduling is unreliable in PWAs" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I ran into this while building a productivity app where reminders and&lt;br&gt;
check-ins were core features.&lt;/p&gt;

&lt;p&gt;Notifications worked.&lt;br&gt;
Nothing crashed.&lt;br&gt;
But timing was unreliable.&lt;/p&gt;

&lt;p&gt;I wrote about that experience here:&lt;/p&gt;

&lt;p&gt;When Notifications Matter, PWAs Start to Show Their Limits&lt;br&gt;&lt;br&gt;
&lt;a href="https://dev.to/nishchaldev/when-notifications-matter-pwas-start-to-show-their-limits-5ebl"&gt;https://dev.to/nishchaldev/when-notifications-matter-pwas-start-to-show-their-limits-5ebl&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That post explains where things broke.&lt;br&gt;
This post explains why.&lt;/p&gt;




&lt;h2&gt;
  
  
  How mature apps avoid time bugs
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdey4v3z6iv841j5fjpja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdey4v3z6iv841j5fjpja.png" alt="How mature apps avoid time bugs" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once I looked at how established reminder apps work, a clear pattern&lt;br&gt;
emerged.&lt;/p&gt;

&lt;h3&gt;
  
  
  One clear definition of “now”
&lt;/h3&gt;

&lt;p&gt;There is exactly one source of truth for current time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;based on the device timezone&lt;/li&gt;
&lt;li&gt;or an explicit user setting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No guessing.&lt;br&gt;
No hidden defaults.&lt;/p&gt;




&lt;h3&gt;
  
  
  Store in UTC, schedule in local time
&lt;/h3&gt;

&lt;p&gt;This is the most important rule.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Store timestamps in UTC&lt;/li&gt;
&lt;li&gt;Convert to local time for display&lt;/li&gt;
&lt;li&gt;Schedule alarms using local context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This avoids:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;daylight saving bugs&lt;/li&gt;
&lt;li&gt;timezone drift&lt;/li&gt;
&lt;li&gt;unexpected early or late triggers&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Let the OS handle scheduling
&lt;/h3&gt;

&lt;p&gt;On native platforms, reminders are scheduled using system services.&lt;/p&gt;

&lt;p&gt;The operating system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;reschedules after reboots&lt;/li&gt;
&lt;li&gt;adjusts for timezone changes&lt;/li&gt;
&lt;li&gt;guarantees delivery timing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript logic does not need to stay alive.&lt;/p&gt;




&lt;h2&gt;
  
  
  The shift that fixed my app
&lt;/h2&gt;

&lt;p&gt;After understanding these patterns, I changed the architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the web app handles dashboards and planning&lt;/li&gt;
&lt;li&gt;the native layer handles scheduling and notifications&lt;/li&gt;
&lt;li&gt;the backend stores everything in UTC&lt;/li&gt;
&lt;li&gt;time context is always explicit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same product logic runs everywhere, but time-sensitive&lt;br&gt;
responsibilities live where they belong.&lt;/p&gt;

&lt;p&gt;The app is still evolving, but the difference is noticeable.&lt;/p&gt;

&lt;p&gt;Live app:&lt;br&gt;&lt;br&gt;
&lt;a href="https://wio-dev.vercel.app" rel="noopener noreferrer"&gt;https://wio-dev.vercel.app&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  A simple rule that helps
&lt;/h2&gt;

&lt;p&gt;If you remember only one thing, remember this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Time is not a utility. It’s a product feature.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If your app depends on time being correct:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;model it deliberately&lt;/li&gt;
&lt;li&gt;pass context explicitly&lt;/li&gt;
&lt;li&gt;let the platform handle scheduling&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Closing thought
&lt;/h2&gt;

&lt;p&gt;JavaScript dates are not broken.&lt;br&gt;
Browsers are not broken.&lt;/p&gt;

&lt;p&gt;Most time bugs come from assumptions that feel reasonable until real&lt;br&gt;
users and real schedules expose them.&lt;/p&gt;

&lt;p&gt;Once your mental model matches how time actually works, things become&lt;br&gt;
much simpler.&lt;/p&gt;




&lt;h2&gt;
  
  
  A question for you
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Have you run into confusing date or timezone bugs?&lt;/li&gt;
&lt;li&gt;How are you handling reminders or schedules today?&lt;/li&gt;
&lt;li&gt;Did this explanation match what you’ve seen in practice?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’d genuinely love to hear how others are dealing with this.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>When notifications matter, PWAs start to show their limits</title>
      <dc:creator>nishchal singh</dc:creator>
      <pubDate>Sat, 07 Feb 2026 08:37:57 +0000</pubDate>
      <link>https://dev.to/nishchaldev/when-notifications-matter-pwas-start-to-show-their-limits-5ebl</link>
      <guid>https://dev.to/nishchaldev/when-notifications-matter-pwas-start-to-show-their-limits-5ebl</guid>
      <description>&lt;h2&gt;
  
  
  I was getting notifications at &lt;strong&gt;3am&lt;/strong&gt;.
&lt;/h2&gt;

&lt;p&gt;Users were confused.&lt;br&gt;&lt;br&gt;
I hadn’t changed anything.&lt;/p&gt;

&lt;p&gt;That’s when I realized something uncomfortable:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PWAs don’t really understand time the way we expect them to.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nothing was “broken” in the obvious sense.&lt;br&gt;&lt;br&gt;
Reminders worked. Notifications appeared. Logs looked fine.&lt;/p&gt;

&lt;p&gt;But for a productivity app — where reminders and check-ins &lt;em&gt;are the product&lt;/em&gt; —&lt;br&gt;&lt;br&gt;
“mostly works” isn’t good enough.&lt;/p&gt;

&lt;p&gt;This post is about what went wrong, why it wasn’t actually a bug, and why I eventually stopped relying on a pure PWA setup for time-critical notifications.&lt;/p&gt;




&lt;h2&gt;
  
  
  When PWAs work great (and when they don’t)
&lt;/h2&gt;

&lt;p&gt;If your app needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in-app banners
&lt;/li&gt;
&lt;li&gt;occasional push notifications
&lt;/li&gt;
&lt;li&gt;background sync &lt;em&gt;when possible&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PWAs are an excellent choice.&lt;/p&gt;

&lt;p&gt;They’re fast to build, easy to ship, and good enough for many products.&lt;/p&gt;

&lt;p&gt;But reminders are different.&lt;/p&gt;

&lt;p&gt;A reminder isn’t just a notification.&lt;br&gt;&lt;br&gt;
It’s a &lt;strong&gt;promise about time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And time is where PWAs start to show their limits.&lt;/p&gt;




&lt;h2&gt;
  
  
  How PWA notifications really behave
&lt;/h2&gt;

&lt;p&gt;When a PWA is open:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript is running
&lt;/li&gt;
&lt;li&gt;the user’s local time is known
&lt;/li&gt;
&lt;li&gt;scheduling logic is predictable
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Things feel solid.&lt;/p&gt;

&lt;p&gt;The problems start once the app is closed or backgrounded.&lt;/p&gt;

&lt;p&gt;At that point:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the browser decides when background work runs
&lt;/li&gt;
&lt;li&gt;service workers wake up only under certain conditions
&lt;/li&gt;
&lt;li&gt;push delivery is best-effort
&lt;/li&gt;
&lt;li&gt;timing happens in &lt;em&gt;windows&lt;/em&gt;, not exact moments
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A “scheduled” notification often looks like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a backend job checks reminder time
&lt;/li&gt;
&lt;li&gt;timezone logic runs on the server
&lt;/li&gt;
&lt;li&gt;a web push is sent
&lt;/li&gt;
&lt;li&gt;the browser decides when (or if) to wake the service worker
&lt;/li&gt;
&lt;li&gt;the OS eventually shows the notification
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each step makes sense on its own.&lt;/p&gt;

&lt;p&gt;Together, they introduce uncertainty.&lt;/p&gt;

&lt;p&gt;This isn’t bad engineering.&lt;br&gt;&lt;br&gt;
It’s how browsers are designed.&lt;/p&gt;

&lt;p&gt;Browsers optimize for battery life, performance, and security —&lt;br&gt;&lt;br&gt;
&lt;strong&gt;not precise scheduling guarantees.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmeruqf7l5u97d4oq0rz9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmeruqf7l5u97d4oq0rz9.png" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where things actually broke for me
&lt;/h2&gt;

&lt;p&gt;At first, I assumed this was just a PWA limitation.&lt;/p&gt;

&lt;p&gt;Then I wrapped the app with a native Android container and refactored the notification flow.&lt;/p&gt;

&lt;p&gt;Some issues still showed up.&lt;/p&gt;

&lt;p&gt;That’s when it became clear:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Notification reliability isn’t just about platform choice.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
It’s also about how your app understands &lt;em&gt;time itself&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Looking at how mature reminder apps work made the gaps obvious.&lt;/p&gt;




&lt;h2&gt;
  
  
  What real reminder apps get right
&lt;/h2&gt;

&lt;p&gt;Apps like &lt;strong&gt;Google Calendar&lt;/strong&gt;, &lt;strong&gt;TickTick&lt;/strong&gt;, and &lt;strong&gt;Any.do&lt;/strong&gt; all follow the same principles.&lt;/p&gt;




&lt;h3&gt;
  
  
  One clear definition of “now”
&lt;/h3&gt;

&lt;p&gt;There is a single authoritative source of current time —&lt;br&gt;&lt;br&gt;
based on device timezone or an explicit user setting.&lt;/p&gt;

&lt;p&gt;No guessing.&lt;br&gt;&lt;br&gt;
No hidden defaults.&lt;/p&gt;




&lt;h3&gt;
  
  
  Store in UTC, schedule in local time
&lt;/h3&gt;

&lt;p&gt;Events are stored in UTC.&lt;br&gt;&lt;br&gt;
They’re converted to local time only for display and scheduling.&lt;/p&gt;

&lt;p&gt;This avoids slow, painful bugs around daylight saving and timezone changes.&lt;/p&gt;




&lt;h3&gt;
  
  
  OS-level scheduling
&lt;/h3&gt;

&lt;p&gt;Reminders are scheduled using system services (for example, &lt;code&gt;AlarmManager&lt;/code&gt; on Android).&lt;/p&gt;

&lt;p&gt;They’re automatically rescheduled when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the device restarts
&lt;/li&gt;
&lt;li&gt;the timezone changes
&lt;/li&gt;
&lt;li&gt;daylight saving rules shift
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The OS owns delivery — not JavaScript.&lt;/p&gt;




&lt;h3&gt;
  
  
  No dates without context
&lt;/h3&gt;

&lt;p&gt;Any AI planner or automation that generates dates is explicitly told:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Today is YYYY-MM-DD in this timezone.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Without this, it’s surprisingly easy to generate reminders in the past or wrong year without noticing.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fomy1i56msmyni8viw3vi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fomy1i56msmyni8viw3vi.png" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The real issues in my app
&lt;/h2&gt;

&lt;p&gt;Once I stepped back, the problems were surprisingly basic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;timezone defaulting to UTC instead of user locale
&lt;/li&gt;
&lt;li&gt;AI planner not receiving current date and timezone explicitly
&lt;/li&gt;
&lt;li&gt;weekly reminders drifting due to timezone conversion
&lt;/li&gt;
&lt;li&gt;limited control over notification channels and sounds
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These aren’t exotic bugs.&lt;/p&gt;

&lt;p&gt;They’re the kind that appear only after real users and real time collide.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Android behaves more predictably
&lt;/h2&gt;

&lt;p&gt;On Android, the mental model is simpler:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the app schedules the reminder locally
&lt;/li&gt;
&lt;li&gt;the OS owns the timing
&lt;/li&gt;
&lt;li&gt;the notification fires when expected
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the app is closed
&lt;/li&gt;
&lt;li&gt;the process is killed
&lt;/li&gt;
&lt;li&gt;the device restarts
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notifications are first-class system features.&lt;/p&gt;

&lt;p&gt;That’s why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sound and vibration are consistent
&lt;/li&gt;
&lt;li&gt;notification channels behave correctly
&lt;/li&gt;
&lt;li&gt;widgets are possible
&lt;/li&gt;
&lt;li&gt;background behavior is predictable
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This isn’t about Android being “better”.&lt;/p&gt;

&lt;p&gt;It’s about using the platform for what it was designed to do.&lt;/p&gt;




&lt;h2&gt;
  
  
  The setup that finally made sense
&lt;/h2&gt;

&lt;p&gt;For this app, reminders and check-ins aren’t optional.&lt;/p&gt;

&lt;p&gt;They &lt;strong&gt;are&lt;/strong&gt; the product.&lt;/p&gt;

&lt;p&gt;So the architecture that aligned best was:&lt;/p&gt;

&lt;h3&gt;
  
  
  Web app (desktop)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;dashboards
&lt;/li&gt;
&lt;li&gt;insights
&lt;/li&gt;
&lt;li&gt;planning and review
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Android app (Capacitor wrapper)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;native scheduling
&lt;/li&gt;
&lt;li&gt;OS-level notifications
&lt;/li&gt;
&lt;li&gt;background reliability
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Same backend.&lt;br&gt;&lt;br&gt;
Same database.&lt;br&gt;&lt;br&gt;
Same product logic.&lt;/p&gt;

&lt;p&gt;Different delivery mechanisms — matched to platform strengths.&lt;/p&gt;




&lt;h2&gt;
  
  
  A simple rule of thumb
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If notifications are a nice-to-have → PWAs are usually fine
&lt;/li&gt;
&lt;li&gt;If notifications are a core promise → let the OS handle timing
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That distinction would have saved me weeks.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final thought
&lt;/h2&gt;

&lt;p&gt;PWAs aren’t unreliable.&lt;/p&gt;

&lt;p&gt;They’re just not designed to guarantee time.&lt;/p&gt;

&lt;p&gt;Native platforms are.&lt;/p&gt;

&lt;p&gt;Once you align product expectations with what a platform can realistically promise, a lot of confusion — and frustration — disappears.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick question for you
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Have you faced random notification timings in PWAs?
&lt;/li&gt;
&lt;li&gt;How are you handling scheduled reminders today?
&lt;/li&gt;
&lt;li&gt;Would you still choose a PWA if notifications were critical?
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’m genuinely curious how others are solving this.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Live app (still evolving):&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
👉 &lt;a href="https://wio-dev.vercel.app" rel="noopener noreferrer"&gt;https://wio-dev.vercel.app&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>mobile</category>
      <category>productivity</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Object Cloning in Javascript</title>
      <dc:creator>nishchal singh</dc:creator>
      <pubDate>Fri, 01 Mar 2024 05:43:13 +0000</pubDate>
      <link>https://dev.to/nishchaldev/object-cloning-in-javascript-636</link>
      <guid>https://dev.to/nishchaldev/object-cloning-in-javascript-636</guid>
      <description>&lt;p&gt;While coding in Javascript have you ever encountered a situation where you needed to copy an object? and when you try to copy an object, you face some issues like all of the properties are not cloned, or when changing the copied object somehow the original object is changed, leaving you without the desired results. Well, In this article we'll explore some simple solutions to these issues.&lt;/p&gt;

&lt;p&gt;First thing that came to your mind is that why not simply use 🟰 to copy objects, but it'll not work b'coz Javascript objects are reference type values. worry not, there are 3 basic ways of object cloning:&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;originalObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&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="c1"&gt;// Using Spread Operator (...)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;spread_Cloned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;originalObject&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Using Object.assign() Method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;assign_Cloned&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;originalObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Using JSON.stringify() and JSON.parse() &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cloned_JSON&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="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalObject&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;spread_Cloned&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'John', age: 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;assign_Cloned&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'John', age: 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;cloned_JSON&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'John', age: 30 }&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you're dealing with tricky data setups, making exact copies, or just want to keep things from changing unexpectedly, copying objects can be super helpful in JavaScript. This article is all about diving into object cloning in JavaScript. We'll check out different ways to do it and figure out when and why it's important. By the time you finish reading, you'll totally get how to clone objects and be ready to use it in your own coding projects.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Shallow Clone vs Deep Clone
&lt;/h2&gt;

&lt;p&gt;When you shallow clone something in JavaScript, you're basically making a copy that only goes skin-deep. It means you get a new object with the same basic stuff as the original, but if those basic things include other objects, those aren't copied – they're just referenced. Now, deep cloning is like making a photocopy of everything. You get a brand-new object where every little thing, even the stuff inside other stuff, gets copied over completely. So, you end up with a totally separate copy of everything from the original.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Techniques for Object Cloning
&lt;/h1&gt;




&lt;h2&gt;
  
  
  Using Spread Operator (...)
&lt;/h2&gt;

&lt;p&gt;The spread operator in JavaScript is a concise and efficient way to clone objects. It allows us to create a copy of an object, including all its enumerable properties, into a new object.&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;originalObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clonedObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;originalObject&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;clonedObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'John', age: 30 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Very simple syntax&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports cloning both Shallow and moderately nested objects&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cannot handle deeply nested objects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Does not clone non-enumerable properties or methods&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using Object.assign() Method
&lt;/h2&gt;

&lt;p&gt;The Object.assign() method is another powerful technique to clone objects. It copies the values of all enumerable properties from one or more source objects into a target object, returning the target object.&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;originalObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clonedObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;originalObject&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;clonedObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'John', age: 30 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handles cloning of non-enumerable properties and methods&lt;/li&gt;
&lt;li&gt;Supports cloning moderately nested objects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cannot clone deeply nested objects directly&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Overwrites properties if they already exist in the target object&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using JSON.stringify() and JSON.parse()
&lt;/h2&gt;

&lt;p&gt;The combination of JSON.stringify() and JSON.parse() methods can be used as a workaround for cloning complex objects. By converting the object to a JSON string and then parsing it back into an object, we can achieve a Deep Clone of the original object.&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;originalObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clonedObject&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="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalObject&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;clonedObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'John', age: 30 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Supports cloning deeply nested objects&lt;/li&gt;
&lt;li&gt;Can be used to clone objects with circular references&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Does not preserve non-enumerable properties or methods&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;May not preserve certain data types (e.g., dates, regular expressions) during cloning&lt;br&gt;
.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using Third-party Library
&lt;/h2&gt;

&lt;p&gt;Lodash is a popular JavaScript utility library that provides a powerful method called cloneDeep() for object cloning. It performs a deep copy of the entire object hierarchy, ensuring accurate replication of the original object's structure.&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;originalObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clonedObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cloneDeep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalObject&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;clonedObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'John', age: 30 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can handle cloning of any level of object nesting&lt;/li&gt;
&lt;li&gt;Preserves non-enumerable properties and methods&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adds a dependency on an external library (Lodash)&lt;/li&gt;
&lt;li&gt;May have performance implications for large objects due to deep copying&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you don't want to use any third-party library, there is an alternate solution: using a custom deep cloning function we'll discuss it further in this article. Both have their advantages and limitations.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Shallow Cloning
&lt;/h2&gt;

&lt;p&gt;Shallow cloning is the simplest form of object cloning. It creates a new object with the same top-level properties as the original object. However, if those properties are themselves objects, they are still references to the original objects. Shallow cloning is a quick and easy way to duplicate objects when there's no need to clone nested objects.&lt;/p&gt;

&lt;p&gt;Shallow Clone 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;const&lt;/span&gt; &lt;span class="nx"&gt;originalObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;hobbies&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;reading&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;swimming&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clonedObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;originalObject&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the spread operator (...) creates a shallow clone of originalObject. Although clonedObject appears to be an identical copy, modifying the hobbies property in either object will affect both, as they still share a reference to the same array.&lt;/p&gt;

&lt;p&gt;We can fix this issue by Deep Cloning.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Deep Cloning
&lt;/h2&gt;

&lt;p&gt;Deep cloning is a more thorough form of object cloning. It creates a new object and recursively clones all nested properties and their descendants, ensuring each object is an independent copy. Deep cloning is indispensable when working with complex data structures and avoiding unintended side effects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using JSON.stringify() and JSON.parse():
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Deep Clone Using JSON.stringify() and JSON.parse()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;originalObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&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="c1"&gt;// Nested object&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;New York&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;NY&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="c1"&gt;// Method&lt;/span&gt;
  &lt;span class="na"&gt;doAction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;function &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;log&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; is walking in &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&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;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clonedObject&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="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalObject&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;clonedObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="c1"&gt;// { name: 'John', age: 30, address: { city: 'New York', state: 'NY' } }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see in the above example we are losing our data, the method "doAction" is not cloned b'coz it does not preserve non-enumerable properties or methods. The solution for this issue is given below.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Custom Recursive Function:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Deep Clone Using Custom Recursive Function&lt;/span&gt;
&lt;span class="c1"&gt;// Custom Recursive Function &lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;deepClone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;visited&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WeakMap&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 non-object types and null&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;obj&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&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;return&lt;/span&gt; &lt;span class="nx"&gt;obj&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 circular references&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;visited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&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 dates&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;obj&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getTime&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 regular expressions&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;obj&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;RegExp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RegExp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&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 arrays&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;))&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;cloneArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="nx"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cloneArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;cloneArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deepClone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cloneArray&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 objects&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cloneObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="nx"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cloneObj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;for &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;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;obj&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;cloneObj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deepClone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;visited&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;return&lt;/span&gt; &lt;span class="nx"&gt;cloneObj&lt;/span&gt;&lt;span class="p"&gt;;&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;clonedObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deepClone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalObject&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;clonedObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="cm"&gt;/*{
  name: 'John',
  age: 30,
  address: { city: 'New York', state: 'NY' },
  doAction: [Function: doAction]
}*/&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;clonedObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doAction&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// John is walking in New York&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, deepClone is a recursive function that clones all properties and nested objects of originalObject. The resulting clonedObject is a deep copy, ensuring no changes made to original one and no object will impact the other.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We use a WeakMap to keep track of visited objects to handle circular references.&lt;/li&gt;
&lt;li&gt;We add support for cloning Date objects and RegExp objects.&lt;/li&gt;
&lt;li&gt;We handle cloning of arrays and objects separately to ensure correct cloning and handling of circular references.&lt;/li&gt;
&lt;li&gt;We use Object.prototype.hasOwnProperty.call(obj, key) to ensure that properties from the object's prototype chain are not mistakenly cloned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This deepClone function can handle a wider range of data types and scenarios.&lt;/p&gt;

&lt;p&gt;Let's see another example of 'deepClone' function with different data 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="c1"&gt;// Define an object with various data types including arrays, objects, dates, and regular expressions&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;originalObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;string&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;array&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;object&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="na"&gt;regexp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/test/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Add circular reference&lt;/span&gt;
&lt;span class="nx"&gt;originalObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;circular&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;originalObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Clone the original object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clonedObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deepClone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Modify the cloned object&lt;/span&gt;
&lt;span class="nx"&gt;clonedObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Modified&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;clonedObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;456&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;clonedObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;clonedObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;c&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;// Log both original and cloned objects&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;Original Object:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;originalObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/*Original Object: &amp;lt;ref *1&amp;gt; {
  string: 'Hello',
  number: 123,
  boolean: true,
  array: [ 1, 2, 3 ],
  object: { a: 1, b: 2 },
  date: 2024-02-23T13:18:38.521Z,
  regexp: /test/g,
  circular: [Circular *1]
}*/&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;Cloned Object:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;clonedObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/*Cloned Object: &amp;lt;ref *1&amp;gt; {
  string: 'Modified',
  number: 456,
  boolean: true,
  array: [ 1, 2, 3, 4 ],
  object: { a: 1, b: 2, c: 3 },
  date: 2024-02-23T13:18:38.521Z,
  regexp: /test/g,
  circular: [Circular *1]
}*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We define an originalObject containing various data types including arrays, objects, dates, and regular expressions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We add a circular reference to the originalObject.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We then clone the originalObject using the deepClone function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We modify some properties of the cloned object to demonstrate &lt;br&gt;
that the original object remains unaffected by the modifications to the clone.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we log both the original and cloned objects to see the differences.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The deepClone function has several advantages and limitations:
&lt;/h2&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deep Cloning: The function can recursively clone nested objects and arrays, ensuring a deep copy of the original object.&lt;/li&gt;
&lt;li&gt;Support for Various Data Types: It supports cloning of various data types including objects, arrays, dates, and regular expressions, making it versatile for different use cases.&lt;/li&gt;
&lt;li&gt;Handling Circular References: It can handle circular references gracefully by keeping track of visited objects using a WeakMap, preventing infinite recursion.&lt;/li&gt;
&lt;li&gt;Maintains Object Structure: It maintains the structure of the original object, including nested objects and arrays, ensuring the cloned object has the same structure.&lt;/li&gt;
&lt;li&gt;Flexible and Customizable: It can be extended to support additional data types or custom cloning logic based on specific requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance: Deep cloning can be computationally expensive, especially for large and deeply nested objects, potentially leading to performance issues.&lt;/li&gt;
&lt;li&gt;Prototype Chain: It does not preserve the prototype chain of objects. Cloned objects will lose their original prototype chain and inherit from Object.prototype.&lt;/li&gt;
&lt;li&gt;Non-Clonable Properties: Certain properties like non-enumerable properties or properties with getters/setters may not be cloned accurately.&lt;/li&gt;
&lt;li&gt;External References: If the original object contains references to external resources or mutable shared state, the cloned object will still reference the same external resources, potentially leading to unintended side effects.&lt;/li&gt;
&lt;li&gt;Memory Consumption: The use of a WeakMap for handling circular references may consume additional memory, especially for large datasets with many circular references.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, while the deepClone function provides a robust solution for deep copying objects, it's important to be aware of its limitations and potential impact on performance and memory usage, especially in scenarios involving large or complex data structures.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Remember, when cloning objects, it's crucial to choose the appropriate method based on your specific needs. Whether you opt for native JavaScript techniques or leverage external libraries like Lodash or Immer, object cloning is a valuable tool for maintaining data integrity and preventing unintended side effects.&lt;/p&gt;

&lt;p&gt;So, the next time you find yourself needing to duplicate an object in JavaScript, don't go down the path of manual copying and mutation. Instead, harness the power of object cloning and unlock a world of possibilities.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Object cloning in JavaScript is the key to maintaining data integrity and avoiding unintended side effects."&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>object</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Overview of ReactJs</title>
      <dc:creator>nishchal singh</dc:creator>
      <pubDate>Tue, 12 Dec 2023 14:14:59 +0000</pubDate>
      <link>https://dev.to/nishchaldev/overview-of-reactjs-3cik</link>
      <guid>https://dev.to/nishchaldev/overview-of-reactjs-3cik</guid>
      <description>&lt;p&gt;Whenever the development community talks about modern web development stacks, the word "ReactJS" is often mentioned. It's one of the widely used tech stacks for web development, In this article, we'll take an overview of ReactJS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is ReactJs?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ReactJS, or simply React, is like a superhero or supertool for building websites and web applications. It's a special set of tools and rules that helps people create websites that are not only really cool but also super fast and easy to work with.&lt;/p&gt;

&lt;p&gt;React is a JavaScript-based front-end library used for UI(user interface) development. It's used to create modular user interfaces and promotes the development of reusable UI components that display dynamic data. React is often used to build single-page applications (SPAs).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Concept of ReactJS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frd4i3yhyw1ocj40kghz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frd4i3yhyw1ocj40kghz3.png" alt=" " width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;his is Hashnode's homepage as we can see here a react application is made from independent components that together make a whole web application. The search bar in the top header is a reusable component. the same search bar component is used in the other sections of this app.&lt;/p&gt;

&lt;p&gt;Key concepts and principles of ReactJS include:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Component-Based Architecture:&lt;/strong&gt; React is built around the idea of breaking down user interfaces into reusable, self-contained components. These components can range from simple elements like buttons or form inputs to complex parts of the user interface like navigation bars or entire pages. By building UIs as components, development becomes modular, maintainable, and scalable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Virtual DOM (Document Object Model):&lt;/strong&gt; React introduced the concept of a Virtual DOM which is like a lightweight copy of the actual DOM. virtual DOM is an abstract representation of the actual DOM in the browser. When data in a React application changes, React doesn't directly manipulate the real DOM. Instead, it creates a virtual representation of the changes and efficiently updates only the parts of the real DOM that need to change. This approach minimizes browser reflows and enhances performance.&lt;/p&gt;

&lt;p&gt;The image below is a visual representation of the concept of the virtual DOM:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg3kc3r71fahhf7ozhfu6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg3kc3r71fahhf7ozhfu6.png" alt=" " width="800" height="603"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unidirectional Data Flow:&lt;/strong&gt; React follows a unidirectional data flow, where data moves in one direction, usually from parent components to child components. Child components cannot send data back to their parent components. This design minimizes errors and gives us greater control over our code.&lt;/p&gt;

&lt;p&gt;As shown in the diagram below, data can only flow from top to bottom:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flzstclqdqv73m8brl3t8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flzstclqdqv73m8brl3t8.png" alt=" " width="800" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Component Lifecycle:&lt;/strong&gt; React components have a lifecycle that includes various methods such as componentDidMount, componentDidUpdate, and componentWillUnmount. These lifecycle methods allow developers to hook into different stages of a component's existence and perform actions like data fetching, DOM manipulation, or cleanup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State in React:&lt;/strong&gt; In ReactJS, "state" refers to a built-in feature that allows you to manage and store data within a component. State represents the internal data of a component that can change during the component's lifecycle, and when it does change, React will automatically re-render the component to reflect the updated state.&lt;/p&gt;

&lt;p&gt;State acts as a storage space for data essential for a component's functioning. It's versatile, capable of holding different data types like numbers, strings, arrays, or objects. Unlike props, which are unchangeable and flow from parent to child components, state is changeable. Components have the power to modify their own state through React's setState method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reactivity:&lt;/strong&gt; React encourages a reactive approach to UI development. When the state or props of a component change, React automatically re-renders the component to reflect those changes. This reactivity simplifies UI updates and ensures that the UI always matches the application's data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State Management:&lt;/strong&gt; React components can effectively handle their internal state using either the useState hook for functional components or class-based state for class components. These approaches allow components to store and update data within themselves.&lt;/p&gt;

&lt;p&gt;For more intricate applications, developers often turn to state management libraries like Redux or Mobx. These libraries centralize and manage state across the entire application, making it easier to handle complex data interactions.&lt;/p&gt;

&lt;p&gt;For instance, consider a web application with dynamic notification functionality. State management ensures that notifications can be added, removed, and displayed in real-time without the need for complex manual updates. Similarly, in an online shopping cart, managing the dynamic count of items and their state helps ensure a seamless and responsive user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSX (JavaScript XML):&lt;/strong&gt; JSX, which stands for JavaScript XML, is a powerful feature in React. It's like a special language that React developers use to describe how a component should look. Imagine it as a blend of HTML and JavaScript that makes building user interfaces more intuitive.&lt;/p&gt;

&lt;p&gt;With JSX, you can write your user interface code in a way that closely resembles HTML. This not only makes it easier to understand but also helps you visualize your UI components. However, under the hood, JSX gets converted into regular JavaScript using tools like Babel before it's shown in the browser.&lt;/p&gt;

&lt;p&gt;One of the cool things about JSX is that you can embed JavaScript expressions within curly braces {}. This means you can include dynamic content, variables, and calculations right alongside your JSX code. It's a flexible way to create dynamic and interactive user interfaces in React.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';

function Greeting(props) {
  const name = props.name;
  const isLoggedIn = props.isLoggedIn;

  // Define a JavaScript function to conditionally render a greeting message
  function getGreeting(name) {
    if (isLoggedIn) {
      return &amp;lt;p&amp;gt;Hello, {name}!&amp;lt;/p&amp;gt;;
    } else {
      return &amp;lt;p&amp;gt;Please log in.&amp;lt;/p&amp;gt;;
    }
  }
  return (
    &amp;lt;div&amp;gt;
      {/* Embed JavaScript expression to render the greeting message */}
      {getGreeting(name)}
    &amp;lt;/div&amp;gt;
  );
}
export default Greeting;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Community and Ecosystem:&lt;/strong&gt; React has a vast and active community, resulting in a rich ecosystem of third-party libraries, tools, and extensions. These resources make it easier for developers to build feature-rich and performant applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Does ReactJS Work?&lt;/strong&gt;&lt;br&gt;
ReactJS works by utilizing a component-based architecture to build user interfaces efficiently. It uses a Virtual DOM to optimize updates, enforces a unidirectional data flow for predictable behavior, and allows developers to define UI components using JSX, which is transpiled into JavaScript. React updates the actual DOM efficiently, making it ideal for creating dynamic and interactive web applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
ReactJS stands as a powerful and pivotal force in modern web development. Its component-based architecture, Virtual DOM, and unidirectional data flow provide developers with a robust toolkit to create dynamic and responsive user interfaces. React's flexibility, combined with a thriving community and extensive ecosystem, makes it a top choice for building web applications that are not only efficient and maintainable but also user-friendly. As we've explored in this overview, ReactJS empowers developers to bring their web projects to life with elegance and precision, setting new standards for web development in the digital age. Whether you're a seasoned developer or just beginning your journey, ReactJS remains a technology worth exploring and mastering.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>dom</category>
      <category>props</category>
    </item>
  </channel>
</rss>
