<?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: Muhammad Wasif</title>
    <description>The latest articles on DEV Community by Muhammad Wasif (@wasifkhan111).</description>
    <link>https://dev.to/wasifkhan111</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4070942%2F38542df1-7cec-4d69-a4f4-7ee211d47c94.jpg</url>
      <title>DEV Community: Muhammad Wasif</title>
      <link>https://dev.to/wasifkhan111</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wasifkhan111"/>
    <language>en</language>
    <item>
      <title>JavaScript Array Loops: for vs forEach vs map</title>
      <dc:creator>Muhammad Wasif</dc:creator>
      <pubDate>Wed, 26 Aug 2026 12:54:49 +0000</pubDate>
      <link>https://dev.to/wasifkhan111/javascript-array-loops-for-vs-foreach-vs-map-h6n</link>
      <guid>https://dev.to/wasifkhan111/javascript-array-loops-for-vs-foreach-vs-map-h6n</guid>
      <description>&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;map&lt;/code&gt; when you want a new array, &lt;code&gt;filter&lt;/code&gt; when you want fewer items, &lt;code&gt;for...of&lt;/code&gt; when you need to &lt;code&gt;break&lt;/code&gt; or &lt;code&gt;await&lt;/code&gt;, and a plain &lt;code&gt;for&lt;/code&gt; loop only when you are looping over millions of items and have measured that it matters.&lt;/strong&gt; &lt;code&gt;forEach&lt;/code&gt; sits in an awkward middle: it reads nicely, but it cannot &lt;code&gt;break&lt;/code&gt; and it silently ignores &lt;code&gt;await&lt;/code&gt; — which makes it the wrong default more often than people realise.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick decision table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You want to...&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Transform every item into something new&lt;/td&gt;
&lt;td&gt;&lt;code&gt;map&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns a new array, same length&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Keep only some items&lt;/td&gt;
&lt;td&gt;&lt;code&gt;filter&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns a new, shorter array&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Boil the array down to one value&lt;/td&gt;
&lt;td&gt;&lt;code&gt;reduce&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Sum, total, grouped object&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop early / &lt;code&gt;break&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for...of&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;forEach&lt;/code&gt; cannot be stopped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;await&lt;/code&gt; something each time round&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for...of&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;forEach&lt;/code&gt; does not wait&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Just do a side effect (log, push, update DOM)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;for...of&lt;/code&gt; or &lt;code&gt;forEach&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Both fine; &lt;code&gt;for...of&lt;/code&gt; is safer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Squeeze out speed on a huge array&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Measurably faster, but see the benchmark&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  forEach vs map
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;map&lt;/code&gt; builds and returns a &lt;strong&gt;new array&lt;/strong&gt;. &lt;code&gt;forEach&lt;/code&gt; returns &lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt; — it exists purely for side effects.&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;nums&lt;/span&gt; &lt;span class="o"&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&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;doubled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="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;doubled&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// [2, 4, 6, 8, 10]&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;nums&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// [1, 2, 3, 4, 5]  ← original untouched&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;nums&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;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The practical rule: &lt;strong&gt;if you're not using the returned array, you shouldn't be using &lt;code&gt;map&lt;/code&gt;.&lt;/strong&gt; Calling &lt;code&gt;map&lt;/code&gt; purely for side effects builds a whole array you then throw away, and it misleads the next reader into hunting for a result that never gets used.&lt;/p&gt;

&lt;h2&gt;
  
  
  You can't break out of forEach
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;return&lt;/code&gt; inside the callback only ends &lt;strong&gt;that one call&lt;/strong&gt; — the loop keeps going:&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;nums&lt;/span&gt; &lt;span class="o"&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&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;visited&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;nums&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;n&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;visited&lt;/span&gt;&lt;span class="o"&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// looks like a break, isn't one&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;visited&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 5 — every item was still visited&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;for...of&lt;/code&gt;, &lt;code&gt;break&lt;/code&gt; does what you expect:&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;let&lt;/span&gt; &lt;span class="nx"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;const&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;nums&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;break&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="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;found&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you only need to know &lt;em&gt;whether&lt;/em&gt; something matches, &lt;code&gt;find&lt;/code&gt;, &lt;code&gt;some&lt;/code&gt;, &lt;code&gt;every&lt;/code&gt; and &lt;code&gt;includes&lt;/code&gt; all stop as soon as they have their answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The async bug
&lt;/h2&gt;

&lt;p&gt;This is the one that costs real debugging hours:&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;withForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ids&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;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="nx"&gt;ids&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="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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;results&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="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchThing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;   &lt;span class="c1"&gt;// runs later, too late&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;results&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// []  ← empty, every time&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It returns an empty array. No error, no warning — &lt;code&gt;forEach&lt;/code&gt; fires off the promises, ignores all of them, and returns. Use &lt;code&gt;for...of&lt;/code&gt;, which genuinely pauses at each &lt;code&gt;await&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;withForOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ids&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;results&lt;/span&gt; &lt;span class="o"&gt;=&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;const&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;results&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="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchThing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;   &lt;span class="c1"&gt;// waits properly&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;results&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That runs the requests &lt;strong&gt;one after another&lt;/strong&gt;. If they're independent and you want them concurrent, don't loop at all:&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;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fetchThing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;for...of&lt;/code&gt; with &lt;code&gt;await&lt;/code&gt; is sequential — use it when each step depends on the last, or when you're being gentle on a rate-limited API. &lt;code&gt;Promise.all&lt;/code&gt; is concurrent and finishes as fast as the slowest request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting the index
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&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="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// 0 1 / 1 2 / 2 3 ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;nums&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;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;i&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;n&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Is a for loop actually faster?
&lt;/h2&gt;

&lt;p&gt;Yes — and far less often than people assume. Summing an array of &lt;strong&gt;10 million&lt;/strong&gt; numbers on Node v24:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Loop&lt;/th&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;for&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;88 ms&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;for...of&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;577 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;forEach&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;750 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So a classic &lt;code&gt;for&lt;/code&gt; loop is roughly &lt;strong&gt;8× faster&lt;/strong&gt; than &lt;code&gt;forEach&lt;/code&gt; at that size. That sounds dramatic until you scale it down: for an array of 1,000 items — which covers the overwhelming majority of real code — the same gap is well under a millisecond.&lt;/p&gt;

&lt;p&gt;Treat these as a rough shape rather than a law; results shift with the engine, the version and what the callback does. &lt;strong&gt;Write the clearest loop first.&lt;/strong&gt; Reach for a plain &lt;code&gt;for&lt;/code&gt; when you've measured a real bottleneck, not because a benchmark article said it was faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  The sparse array gotcha
&lt;/h2&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;sparse&lt;/span&gt; &lt;span class="o"&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="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="c1"&gt;// note the missing middle item&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;sparse&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;++&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;const&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;sparse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="o"&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;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// 2 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;forEach&lt;/code&gt; (and &lt;code&gt;map&lt;/code&gt;, and &lt;code&gt;filter&lt;/code&gt;) &lt;strong&gt;skip the hole&lt;/strong&gt;; &lt;code&gt;for...of&lt;/code&gt; visits it as &lt;code&gt;undefined&lt;/code&gt;. Rare, but genuinely confusing when it bites — two loops over the same array producing different counts.&lt;/p&gt;

&lt;h2&gt;
  
  
  A few things people ask
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Should I use forEach or for...of?&lt;/strong&gt;&lt;br&gt;
Prefer &lt;code&gt;for...of&lt;/code&gt; as your default for side effects. It supports &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt;, works correctly with &lt;code&gt;await&lt;/code&gt;, and handles sparse arrays predictably. &lt;code&gt;forEach&lt;/code&gt; is fine for short synchronous callbacks — just never when async is involved.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does map change the original array?&lt;/strong&gt;&lt;br&gt;
No. It returns a new array and leaves the original alone, which is why it fits well with React state. Be aware the &lt;em&gt;items&lt;/em&gt; aren't deep-copied though: if your array holds objects, both arrays point at the same objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about for...in?&lt;/strong&gt;&lt;br&gt;
Avoid it for arrays. &lt;code&gt;for...in&lt;/code&gt; loops over &lt;em&gt;keys&lt;/em&gt;, gives you strings (&lt;code&gt;"0"&lt;/code&gt;, &lt;code&gt;"1"&lt;/code&gt;) rather than numbers, and includes inherited enumerable properties. It's meant for plain objects.&lt;/p&gt;




&lt;p&gt;The short version: &lt;strong&gt;pick the loop that says what you mean.&lt;/strong&gt; &lt;code&gt;map&lt;/code&gt; announces "I'm building a new array", &lt;code&gt;filter&lt;/code&gt; announces "I'm narrowing this down", and &lt;code&gt;for...of&lt;/code&gt; announces "I'm doing something for each item, and I might stop or wait." Speed almost never decides it — clarity does.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://cybercodelab.online/blog/javascript-array-loops-for-foreach-map" rel="noopener noreferrer"&gt;cybercodelab.online&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Convert a Unix Timestamp to a Date in JavaScript</title>
      <dc:creator>Muhammad Wasif</dc:creator>
      <pubDate>Mon, 24 Aug 2026 16:34:21 +0000</pubDate>
      <link>https://dev.to/wasifkhan111/how-to-convert-a-unix-timestamp-to-a-date-in-javascript-59bj</link>
      <guid>https://dev.to/wasifkhan111/how-to-convert-a-unix-timestamp-to-a-date-in-javascript-59bj</guid>
      <description>&lt;p&gt;&lt;strong&gt;To convert a Unix timestamp to a date in JavaScript, multiply it by 1000 and pass it to the &lt;code&gt;Date&lt;/code&gt; constructor: &lt;code&gt;new Date(timestamp * 1000)&lt;/code&gt;.&lt;/strong&gt; The multiplication is the whole trick — Unix timestamps count &lt;em&gt;seconds&lt;/em&gt; since 1 January 1970, but JavaScript's &lt;code&gt;Date&lt;/code&gt; counts &lt;em&gt;milliseconds&lt;/em&gt;. Skip the &lt;code&gt;* 1000&lt;/code&gt; and your 2026 date silently becomes January 1970.&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;timestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1786000000&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;date&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;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&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;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// "2026-08-06T07:06:40.000Z"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the answer. The rest of this post covers the parts that actually bite people: telling seconds from milliseconds, formatting the result for humans, and converting back the other way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does my timestamp show 1970?
&lt;/h2&gt;

&lt;p&gt;Because the value is in seconds and &lt;code&gt;new Date()&lt;/code&gt; expects milliseconds. This is the single most common bug in timestamp code, and it fails &lt;em&gt;quietly&lt;/em&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;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1786000000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// "1970-01-21T16:06:40.000Z"  ← wrong, forgot the * 1000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Twenty-one days after the epoch instead of the year 2026. No error, no warning — just a wrong date sitting in your database.&lt;/p&gt;

&lt;p&gt;The reason is that the two worlds disagree on units. Unix time — used by C, Python, PHP, MySQL, most REST APIs and every server log file — counts &lt;strong&gt;seconds&lt;/strong&gt;. The ECMAScript specification defines a JavaScript &lt;code&gt;Date&lt;/code&gt; as a number of &lt;strong&gt;milliseconds&lt;/strong&gt; since the same 1970 epoch, which is why &lt;code&gt;Date.now()&lt;/code&gt; returns a 13-digit number. Anything crossing that boundary needs a factor of 1000.&lt;/p&gt;

&lt;h2&gt;
  
  
  Seconds or milliseconds? Count the digits
&lt;/h2&gt;

&lt;p&gt;For any date in the current era the answer is unambiguous:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Digits&lt;/th&gt;
&lt;th&gt;Unit&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;th&gt;Means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;Seconds&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1786000000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;6 Aug 2026&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;Milliseconds&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1786000000000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;6 Aug 2026&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;Microseconds&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1786000000000000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;6 Aug 2026&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Ten digits means seconds and needs the &lt;code&gt;* 1000&lt;/code&gt;. Thirteen digits is already milliseconds — pass it straight to &lt;code&gt;new Date()&lt;/code&gt;. If you're handling both (say, from two different APIs), normalise defensively:&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;toDate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// 13+ digits is already milliseconds&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;e11&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;n&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;The &lt;code&gt;1e11&lt;/code&gt; threshold works because 100,000,000,000 seconds lands in the year 5138 — far beyond anything a real seconds-based timestamp will contain, and far below any millisecond timestamp from the last 50 years.&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting it for humans
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;new Date()&lt;/code&gt; returns a &lt;code&gt;Date&lt;/code&gt; object, not text. The default output is machine-shaped:&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;date&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;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1786000000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// "2026-08-06T07:06:40.000Z"   ← always UTC, good for storage and APIs&lt;/span&gt;

&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// A long local-time string that varies by machine — avoid for output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For anything a person reads, use &lt;code&gt;toLocaleString()&lt;/code&gt; with an explicit locale and time zone. Being explicit is what makes the output predictable instead of "whatever the server happens to be set to":&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;date&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;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1786000000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLocaleString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en-GB&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="na"&gt;timeZone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UTC&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;dateStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;full&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;timeStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;short&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;// "Thursday, 6 August 2026 at 07:06"&lt;/span&gt;

&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLocaleString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en-GB&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="na"&gt;timeZone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Asia/Karachi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;dateStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;full&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;timeStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;short&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;// "Thursday, 6 August 2026 at 12:06"&lt;/span&gt;

&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLocaleString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en-US&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="na"&gt;timeZone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;America/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;dateStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;medium&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;timeStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;short&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;// "Aug 6, 2026, 3:06 AM"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One timestamp, three different wall-clock readings — because a timestamp is a single instant, and the time zone is applied only at display time. That's a feature, not a bug: &lt;strong&gt;store the number, format at the edge.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Going the other way
&lt;/h2&gt;

&lt;p&gt;To produce a timestamp instead of consuming one, divide by 1000 and floor it:&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;// Current time as a Unix timestamp (seconds)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// A specific date as a Unix timestamp (seconds)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2026-08-06T07:06:40Z&lt;/span&gt;&lt;span class="dl"&gt;'&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="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// 1786000000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details worth getting right:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;Math.floor()&lt;/code&gt;, not &lt;code&gt;Math.round()&lt;/code&gt;.&lt;/strong&gt; Rounding can push you a full second into the future, which breaks "not valid before" comparisons and JWT expiry checks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Include the &lt;code&gt;Z&lt;/code&gt;&lt;/strong&gt; (or a &lt;code&gt;+05:00&lt;/code&gt; style offset) when parsing a date string. &lt;code&gt;new Date('2026-08-06T07:06:40Z')&lt;/code&gt; is unambiguously UTC; &lt;code&gt;new Date('2026-08-06 07:06:40')&lt;/code&gt; is interpreted in the &lt;em&gt;browser's&lt;/em&gt; local zone, so identical code produces different timestamps on different machines.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common mistakes, and the fixes
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mistake&lt;/th&gt;
&lt;th&gt;What happens&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;new Date(seconds)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Date lands in Jan 1970&lt;/td&gt;
&lt;td&gt;&lt;code&gt;new Date(seconds * 1000)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Date.now()&lt;/code&gt; sent to a seconds-based API&lt;/td&gt;
&lt;td&gt;Timestamp ~1000× too large&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Math.floor(Date.now() / 1000)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Parsing &lt;code&gt;"2026-08-06 07:06:40"&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Result depends on the machine's zone&lt;/td&gt;
&lt;td&gt;Use ISO format with &lt;code&gt;Z&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Math.round()&lt;/code&gt; when converting to seconds&lt;/td&gt;
&lt;td&gt;Occasionally one second ahead&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Math.floor()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Formatting with &lt;code&gt;toString()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Output differs per server and browser&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;toLocaleString()&lt;/code&gt; with explicit &lt;code&gt;timeZone&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storing formatted text in the database&lt;/td&gt;
&lt;td&gt;Zone information is lost forever&lt;/td&gt;
&lt;td&gt;Store the timestamp or a UTC ISO string&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That last row is the one that costs the most later. Store the instant; format on the way out.&lt;/p&gt;

&lt;h2&gt;
  
  
  A live clock, same conversion
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setInterval&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&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;label&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;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en-GB&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="na"&gt;timeZone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UTC&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;hour12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;clock&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A few things people ask
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Does &lt;code&gt;new Date(timestamp * 1000)&lt;/code&gt; apply my local time zone?&lt;/strong&gt;&lt;br&gt;
No. The &lt;code&gt;Date&lt;/code&gt; object stores a single UTC instant; the time zone appears only when you format it. &lt;code&gt;toISOString()&lt;/code&gt; always prints UTC, while &lt;code&gt;toLocaleString()&lt;/code&gt; uses the browser's zone unless you pass an explicit &lt;code&gt;timeZone&lt;/code&gt; option.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can JavaScript handle timestamps from before 1970?&lt;/strong&gt;&lt;br&gt;
Yes. Negative timestamps work normally: &lt;code&gt;new Date(-86400 * 1000)&lt;/code&gt; returns 31 December 1969. JavaScript's &lt;code&gt;Date&lt;/code&gt; spans roughly ±8.64 × 10¹⁵ milliseconds around 1970 — about 273,790 years in each direction. Anything outside that returns &lt;code&gt;Invalid Date&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is JavaScript affected by the Year 2038 problem?&lt;/strong&gt;&lt;br&gt;
No. The 2038 problem hits systems storing timestamps in a signed 32-bit integer, which overflows at &lt;code&gt;2147483647&lt;/code&gt; seconds — 19 January 2038, 03:14:07 UTC. JavaScript stores time as a 64-bit float of milliseconds, so it sails past 2038. You can still receive a broken value &lt;em&gt;from&lt;/em&gt; a 32-bit backend, so the fix belongs on that side.&lt;/p&gt;




&lt;p&gt;The rule is short enough to memorise: &lt;strong&gt;seconds in Unix, milliseconds in JavaScript, &lt;code&gt;* 1000&lt;/code&gt; in between.&lt;/strong&gt; Get that one factor right and the rest of the &lt;code&gt;Date&lt;/code&gt; API stops being mysterious.&lt;/p&gt;

&lt;p&gt;If you want to sanity-check a raw number from a log file or a JWT payload without opening a console, I keep a free &lt;a href="https://cybercodelab.online/tools/category/web-dev/timestamp-converter" rel="noopener noreferrer"&gt;Unix Timestamp Converter&lt;/a&gt; that shows the real date instantly.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://cybercodelab.online/blog/convert-unix-timestamp-javascript" rel="noopener noreferrer"&gt;cybercodelab.online&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why Does Unix Time Start on January 1, 1970?</title>
      <dc:creator>Muhammad Wasif</dc:creator>
      <pubDate>Mon, 10 Aug 2026 09:05:34 +0000</pubDate>
      <link>https://dev.to/wasifkhan111/why-does-unix-time-start-on-january-1-1970-47g8</link>
      <guid>https://dev.to/wasifkhan111/why-does-unix-time-start-on-january-1-1970-47g8</guid>
      <description>&lt;p&gt;&lt;strong&gt;Unix time starts on 1 January 1970 because that date was chosen as the "Unix epoch" — the zero point from which seconds are counted — by the engineers building Unix at Bell Labs in the early 1970s. It has no cosmic meaning; it was simply a convenient, recent round date, and counting seconds from a single fixed moment makes time math simple and free of time zones.&lt;/strong&gt; Every timestamp you see, like &lt;code&gt;1786000000&lt;/code&gt;, is just "this many seconds after that 1970 zero point."&lt;/p&gt;

&lt;p&gt;If you've ever wondered why computers seem obsessed with 1970 — why a broken date defaults to "1 Jan 1970" and why timestamps count from there — this is the story. You can see the current count ticking live, or convert any timestamp to a real date, with our free &lt;a href="https://cybercodelab.online/tools/category/web-dev/timestamp-converter" rel="noopener noreferrer"&gt;Unix Timestamp Converter&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short history: Bell Labs, early 1970s
&lt;/h2&gt;

&lt;p&gt;Unix was created at Bell Labs starting around 1969 by Ken Thompson, Dennis Ritchie and their colleagues. Early versions needed a way to store time as a simple number, so they picked a starting point — an &lt;em&gt;epoch&lt;/em&gt; — and counted forward from it.&lt;/p&gt;

&lt;p&gt;The first choice wasn't even 1970. Early Unix measured time in sixtieths of a second (matching the electrical grid's 60 Hz) and used an epoch of &lt;strong&gt;1 January 1971&lt;/strong&gt;. The problem: at 60 ticks per second, a 32-bit counter overflows in only about &lt;strong&gt;2.5 years&lt;/strong&gt; — the clock would run out almost immediately. So the team made two changes: they switched to counting whole &lt;strong&gt;seconds&lt;/strong&gt; instead of sixtieths, and they moved the epoch back to the round date of &lt;strong&gt;1 January 1970, 00:00:00 UTC&lt;/strong&gt;. Counting seconds instead of sixtieths stretched the same 32-bit counter to last until 2038 instead of 1973.&lt;/p&gt;

&lt;p&gt;That's the entire reason. 1970 was recent, round, and convenient in the early 1970s — nothing more profound than that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why have a fixed "zero point" at all?
&lt;/h2&gt;

&lt;p&gt;Storing time as one number counted from a fixed moment has three big advantages over storing "5 July 2026, 3:00 PM" as text:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Easy math.&lt;/strong&gt; "Which event happened first?" becomes a simple number comparison. "How long between them?" is a subtraction. No wrestling with months of different lengths or leap years.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No time zones.&lt;/strong&gt; The count is always in UTC. A server in Karachi and one in London write the &lt;em&gt;same number&lt;/em&gt; for the same instant — the conversion to &lt;a href="https://cybercodelab.online/tools/category/date-time/timezone-converter" rel="noopener noreferrer"&gt;local time&lt;/a&gt; happens only when a human needs to read it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compact and fast.&lt;/strong&gt; One integer is smaller and quicker to sort and index than a formatted date string.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is why virtually every operating system, database and programming language stores time this way internally. (For a fuller explanation of what a timestamp is and how to read one, see &lt;a href="https://cybercodelab.online/blog/what-is-a-unix-timestamp" rel="noopener noreferrer"&gt;what is a Unix timestamp&lt;/a&gt;.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Why 1970 and not the year 0, or 1900?
&lt;/h2&gt;

&lt;p&gt;A few practical reasons ruled out older starting points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The year 0 doesn't really exist&lt;/strong&gt; in the common calendar (it goes 1 BC → 1 AD), and counting billions of seconds across 2,000 years of messy calendar reforms would be a nightmare.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A recent date wastes fewer numbers.&lt;/strong&gt; Computers in the 1970s had tiny amounts of memory. Starting near the present meant small, efficient numbers for current dates rather than huge ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1970 was "now-ish."&lt;/strong&gt; A round year just before the work was done was the pragmatic engineer's choice — easy to reason about and document.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The date was later locked in by the &lt;strong&gt;POSIX standard&lt;/strong&gt;, which is why it's identical across Linux, macOS, Android, iOS and almost everything else today.&lt;/p&gt;

&lt;h2&gt;
  
  
  What about dates before 1970?
&lt;/h2&gt;

&lt;p&gt;They work fine — the count simply goes &lt;strong&gt;negative&lt;/strong&gt;. A timestamp of &lt;code&gt;-86400&lt;/code&gt; is exactly one day before the epoch: 31 December 1969. So the "1970 wall" isn't a limit on what dates you can represent; it's just where the counting starts from. Historical dates are perfectly valid negative numbers.&lt;/p&gt;

&lt;p&gt;This is also why a device with a dead clock or a software bug often shows &lt;strong&gt;"1 January 1970"&lt;/strong&gt; — when the stored timestamp resets to &lt;code&gt;0&lt;/code&gt;, that's the date it lands on.&lt;/p&gt;

&lt;h2&gt;
  
  
  The flip side: the Year 2038 problem
&lt;/h2&gt;

&lt;p&gt;If 1970 is the start, is there an end? For older systems, yes. A signed &lt;strong&gt;32-bit&lt;/strong&gt; integer maxes out at &lt;code&gt;2,147,483,647&lt;/code&gt; seconds, which lands on &lt;strong&gt;19 January 2038, 03:14:07 UTC&lt;/strong&gt;. One second later, the counter overflows and wraps around to 1901 — the same family of bug as Y2K.&lt;/p&gt;

&lt;p&gt;The fix is already standard: modern systems use &lt;strong&gt;64-bit&lt;/strong&gt; time values, which won't overflow for roughly 292 billion years. Try pasting &lt;code&gt;2147483647&lt;/code&gt; into the &lt;a href="https://cybercodelab.online/tools/category/web-dev/timestamp-converter" rel="noopener noreferrer"&gt;Unix Timestamp Converter&lt;/a&gt; to see that exact 2038 moment for yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Can a Unix timestamp be negative?&lt;/strong&gt;&lt;br&gt;
Yes. Negative timestamps represent moments before the 1970 epoch — &lt;code&gt;-1&lt;/code&gt; is 31 December 1969, 23:59:59 UTC. The epoch is just the zero point of the number line, not the earliest representable date, so historical dates are stored as negative seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is the 1970 epoch the same in every time zone?&lt;/strong&gt;&lt;br&gt;
Yes. The Unix epoch is defined as 1 January 1970 at 00:00:00 &lt;strong&gt;UTC&lt;/strong&gt;, and the count is always in UTC. That's the whole point — one universal number for an instant, converted to local time only for display. In Karachi that same instant reads as 5:00 AM on 1 January 1970, but the timestamp is identical.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why not start counting from the year 0?&lt;/strong&gt;&lt;br&gt;
Because the year 0 doesn't exist in the standard calendar, older dates involve centuries of calendar reforms, and 1970s computers had little memory — a recent starting point kept the numbers small and the math simple. 1970 was chosen for convenience, not meaning.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do broken devices show 1 January 1970?&lt;/strong&gt;&lt;br&gt;
When a device loses its clock setting or a program reads an empty/zero timestamp, the value defaults to &lt;code&gt;0&lt;/code&gt; — and 0 seconds after the epoch is exactly 1 January 1970, 00:00 UTC. That's why the "1970 glitch" appears on phones and gadgets with drained batteries.&lt;/p&gt;

&lt;p&gt;Next time you see a stray "1970" on a screen, you'll know it's not a random glitch — it's the zero mark of the clock every computer counts from. Decode any real timestamp back to a human date with the &lt;a href="https://cybercodelab.online/tools/category/web-dev/timestamp-converter" rel="noopener noreferrer"&gt;Unix Timestamp Converter&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://cybercodelab.online/blog/why-does-unix-time-start-in-1970" rel="noopener noreferrer"&gt;cybercodelab.online&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
