<?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: Adnan Ahmed</title>
    <description>The latest articles on DEV Community by Adnan Ahmed (@idnan).</description>
    <link>https://dev.to/idnan</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%2F72324%2Fae5225a5-d54d-464b-8d48-f87bc2126bd0.jpeg</url>
      <title>DEV Community: Adnan Ahmed</title>
      <link>https://dev.to/idnan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/idnan"/>
    <language>en</language>
    <item>
      <title>HTTP Caching</title>
      <dc:creator>Adnan Ahmed</dc:creator>
      <pubDate>Sun, 27 Dec 2020 20:13:23 +0000</pubDate>
      <link>https://dev.to/idnan/http-caching-hk7</link>
      <guid>https://dev.to/idnan/http-caching-hk7</guid>
      <description>&lt;p&gt;When we build websites we want them to load fast. One of the best ways is to use caching.&lt;/p&gt;

&lt;p&gt;Caching is just keeping a copy closer to the user so you don't have to get it from someplace farther away. HTTP cache is part of HTTP specs. It gives us a way to keep a copy of the response in the user browser's memory.&lt;/p&gt;

&lt;p&gt;So let's start talking about what HTTP caching is.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is HTTP Caching?
&lt;/h2&gt;

&lt;p&gt;Let's say user visits "&lt;a href="https://example.com/docs"&gt;https://example.com/docs&lt;/a&gt;". Here's what happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Browser

&lt;ul&gt;
&lt;li&gt;Sends request&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Server

&lt;ul&gt;
&lt;li&gt;Receives the request&lt;/li&gt;
&lt;li&gt;Renders the page into HTML&lt;/li&gt;
&lt;li&gt;Send a response with the HTML in the body and a status code of 200&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Browser

&lt;ul&gt;
&lt;li&gt;Downloads the body of the response&lt;/li&gt;
&lt;li&gt;Browser renders the page &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2URpoG2g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://adnanahmed.info/img/20201221/http-cache-part-1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2URpoG2g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://adnanahmed.info/img/20201221/http-cache-part-1.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next time user visits the page all of that has to happen all over again unless the server sends some specific header that tells the browser to cache it: "Cache-Control".&lt;/p&gt;

&lt;p&gt;That's right, your browser already knows how to cache responses so it doesn't have to download them more than once.&lt;/p&gt;

&lt;p&gt;So if your server was sending a response like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;headers&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="s2"&gt;Content-Type&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="s2"&gt;text/html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You just add the Cache Control header:&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;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;headers&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="s2"&gt;Content-Type&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="s2"&gt;text/html&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="s2"&gt;Cache-Control&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="s2"&gt;max-age=0, must-revalidate&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="s2"&gt;Etag&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;md5&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two lines change quite a bit for the user when he next visits the page. Let's check it out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Request with Caching
&lt;/h2&gt;

&lt;p&gt;Here's what happens when visiting the page for the first time.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Browser

&lt;ul&gt;
&lt;li&gt;Sends request&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Server

&lt;ul&gt;
&lt;li&gt;Receives the request&lt;/li&gt;
&lt;li&gt;Renders the page into HTML&lt;/li&gt;
&lt;li&gt;Send a response with the HTML in the body and a status code of 200

&lt;ul&gt;
&lt;li&gt;Sends &lt;strong&gt;"Cache-Control"&lt;/strong&gt; and &lt;strong&gt;"Etag"&lt;/strong&gt; headers &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Browser

&lt;ul&gt;
&lt;li&gt;Downloads the body of the response&lt;/li&gt;
&lt;li&gt;Browser renders the page &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Browser caches the page on disk or in memory&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--06GI6zeS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://adnanahmed.info/img/20201221/http-cache-part-2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--06GI6zeS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://adnanahmed.info/img/20201221/http-cache-part-2.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now here's what happens when the user visits the page second time.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Browser

&lt;ul&gt;
&lt;li&gt;Sends request

&lt;ul&gt;
&lt;li&gt;Also sends request header &lt;strong&gt;If-None-Match with the Etag from last request&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Server

&lt;ul&gt;
&lt;li&gt;Receives the request&lt;/li&gt;
&lt;li&gt;Renders the page into HTML&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compares the Etag headers to its new Etag&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Etag matches, sends an empty response and a status code of 304&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Browser

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sees 304, downloads nothing, reads from cache&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Renders the page&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sJuztL0x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://adnanahmed.info/img/20201221/http-cache-part-3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sJuztL0x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://adnanahmed.info/img/20201221/http-cache-part-3.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So how is this faster? As the browser doesn't have to download the page again. You will also notice, that our server still had to generate the content to compare the Etag to know if it could send a 304 or not.&lt;/p&gt;

&lt;p&gt;Now let's see how can we fix this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing max-age
&lt;/h2&gt;

&lt;p&gt;We used "max-age=0, must-revalidate" last time. What happens if we use "max-age=3600". This tells the browser to cache this for an hour.&lt;/p&gt;

&lt;p&gt;So this is what happens when the user sends a request a second time.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Browser

&lt;ul&gt;
&lt;li&gt;Looks at max-age from last request&lt;/li&gt;
&lt;li&gt;Sees it's within an hour, doesn't even make a request, reads from cache&lt;/li&gt;
&lt;li&gt;Renders the page&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the next hour, the user is on your website, they won't be downloading any of the same pages twice.&lt;/p&gt;

&lt;h2&gt;
  
  
  CDNs
&lt;/h2&gt;

&lt;p&gt;So far we've optimized the second visit to a page for a single user. But when another visitor shows up, we're going to have to do the whole request, the server builds the page, sends the response cycle again.&lt;/p&gt;

&lt;p&gt;That's where a CDN can come in. Think of a CDN as a shared cache among your visitors.&lt;/p&gt;

&lt;p&gt;A CDN is a "content delivery network" that puts servers closer to users. There's your "origin server", that's the one you deployed somewhere, and then a bunch of CDN servers that your visitors browsers actually talk to. &lt;/p&gt;

&lt;p&gt;Not only does the CDN put your website closer geographically to the user, but it also lets you skip hitting your server to build dynamic pages for as long as you specify in max-age.&lt;/p&gt;

&lt;p&gt;And that wraps it up for this article. Feel free to leave your feedback or questions in the comments section.&lt;/p&gt;

</description>
      <category>httpcaching</category>
      <category>http</category>
      <category>programming</category>
    </item>
    <item>
      <title>Freeze and Seal Objects in JavaScript</title>
      <dc:creator>Adnan Ahmed</dc:creator>
      <pubDate>Fri, 25 Dec 2020 18:44:46 +0000</pubDate>
      <link>https://dev.to/idnan/freeze-and-seal-objects-in-javascript-d7b</link>
      <guid>https://dev.to/idnan/freeze-and-seal-objects-in-javascript-d7b</guid>
      <description>&lt;p&gt;Variables we declare in javascript with the help of &lt;code&gt;const&lt;/code&gt;, are not purely constant. Let's say that if we have a variable called config with a bunch of properties and if we print it to the console you will see it has a name and a database 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;config&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;module-account&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;127.0.0.1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2020&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;r@@t&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// {"name":"module-account","database":{"host":"127.0.0.1","port":"2020","username":"admin","password":"r@@t"}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if we update the value of let's say &lt;code&gt;name&lt;/code&gt; to be &lt;code&gt;xyz&lt;/code&gt;, you will see you can do that. Although it's a constant.&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="nx"&gt;config&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;xyz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// xyz &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To prevent this javascript comes with a bunch of methods, such as &lt;code&gt;Object.freeze&lt;/code&gt;, &lt;code&gt;Object.seal&lt;/code&gt; and &lt;code&gt;Object.preventExtensions&lt;/code&gt;. Which we can use to make them immutable. Let's look at the examples to understand how they work and how we can use them in our codebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object.freeze
&lt;/h2&gt;

&lt;p&gt;If we freeze an object, let's say &lt;code&gt;Object.freeze(config)&lt;/code&gt; and print the &lt;code&gt;name&lt;/code&gt; you will see that we are still able to read the value from the config.&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;freeze&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// xyz&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if we try to update any of the existing values, let's say &lt;code&gt;config.name&lt;/code&gt; is &lt;code&gt;abc&lt;/code&gt;, we will get the error that we cannot assign the value to a read-only property.&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="nx"&gt;config&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;abc&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// error &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, if we try to delete a property, let's say delete &lt;code&gt;config.name&lt;/code&gt;, we will not be able to do that, and also if we try to add a new property, let's say &lt;code&gt;config.timeout&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt;, we will still get the error because the object is not extensible.&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;delete&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// error&lt;/span&gt;
&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timeout&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;// error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only thing we can do is reading the properties from the existing object. One important thing to remember about the freeze is that it works only at the top level. So now, in this case, we have a database object, which is nested inside the config object.&lt;/p&gt;

&lt;p&gt;If we try to update the value for, let's say &lt;code&gt;config.database.host&lt;/code&gt; is &lt;code&gt;10.10.10.20&lt;/code&gt; and if we print the config, you will see that the database host has been updated.&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="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;host&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10.10.10.20&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;      &lt;span class="c1"&gt;// 10.10.10.20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want the object to be fully frozen, with all the objects inside, we have to recursively freeze all the objects. So in this case now if we want the database to be frozen also, we will have to do&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;freeze&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we'll get the error while updating the &lt;code&gt;host&lt;/code&gt; that the database host cannot be updated because &lt;code&gt;config.database&lt;/code&gt; is frozen&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="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;host&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10.10.10.20&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Object.seal
&lt;/h2&gt;

&lt;p&gt;Next, we have &lt;code&gt;Object.seal&lt;/code&gt; which is similar to &lt;code&gt;Object.freeze&lt;/code&gt; in a way that you cannot add or remove properties from an object but you can update the values of the existing properties. Let's say that we seal our config object so &lt;code&gt;Object.seal(config)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And now, if we do &lt;code&gt;config.name&lt;/code&gt; to be &lt;code&gt;XYZ&lt;/code&gt;, you will see that the &lt;code&gt;name&lt;/code&gt; has been updated.&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="nx"&gt;config&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;XYZ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if we try to delete the property from the config object. Let's say delete &lt;code&gt;config.database&lt;/code&gt;, we will not be able to do that because, with seal, you cannot delete the properties from the object. And also, if we try to add a new property, let's say &lt;code&gt;config.timeout&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt; we will get the error, that you cannot add a new property to the 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="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// error&lt;/span&gt;
&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timeout&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;// error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And similar to &lt;code&gt;object.freeze&lt;/code&gt;, &lt;code&gt;object.seal&lt;/code&gt; also works on the top-level only. So, the seal will not be applied to the database object here and if we try to delete a property from the database object, let's say delete &lt;code&gt;config.database.host&lt;/code&gt;, we will see that the database host has been deleted from here.&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;delete&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// success&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, if we want to prevent this also, we will have to seal the nested objects.&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;seal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we will get the error that we cannot delete a property from a sealed object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object.preventExtensions
&lt;/h2&gt;

&lt;p&gt;The last one we have is the &lt;code&gt;Object.preventExtensions&lt;/code&gt;, which allows us to update the values and delete the properties from an existing object but it does not allow us to add new properties to the object.&lt;/p&gt;

&lt;p&gt;So now, if we call &lt;code&gt;Object.preventExtensions&lt;/code&gt; on our &lt;code&gt;config&lt;/code&gt; object, and try to update the value for one of the properties, let's say &lt;code&gt;name&lt;/code&gt;, you will see that the name has been updated, and also if we try to delete one of the properties, let's say delete &lt;code&gt;config.database&lt;/code&gt;, we can also delete the properties but if we try to extend our object or let's say add new properties, for example, &lt;code&gt;config.timeout&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt; we can't do that because our object is not extensible.&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="nx"&gt;config&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;XYZ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// success&lt;/span&gt;
&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// success&lt;/span&gt;

&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timeout&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;// error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One more thing to know about the &lt;code&gt;preventExtensions&lt;/code&gt; is that if you delete a property from an object, you cannot add the same property back again and the reason for that is because adding a new property is considered extension. So if I do &lt;code&gt;config.database&lt;/code&gt; again with something, it will give me the error that you cannot add a new property to the 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="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;10.20.20.10&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;        &lt;span class="c1"&gt;// error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similar to &lt;code&gt;freeze&lt;/code&gt; and &lt;code&gt;seal&lt;/code&gt;, &lt;code&gt;preventExtensions&lt;/code&gt; also applies only to the top-level properties.&lt;/p&gt;

&lt;p&gt;There are three more methods that can be used to check if the objects are &lt;code&gt;frozen&lt;/code&gt;, &lt;code&gt;sealed&lt;/code&gt;, or &lt;code&gt;extensible&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Helper Methods
&lt;/h2&gt;

&lt;p&gt;So &lt;code&gt;Object.freeze&lt;/code&gt; is to freeze the objects and &lt;code&gt;Object.isFrozen&lt;/code&gt; can be used to check if the object is frozen or not.&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;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;firstName&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;"&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;freeze&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user1&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="nx"&gt;log&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;isFrozen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;        &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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;isFrozen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;        &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Object.seal&lt;/code&gt; is to seal and &lt;code&gt;Object.isSealed&lt;/code&gt; is to check if the object is sealed or not. And for the &lt;code&gt;Object.preventExtensions&lt;/code&gt;, we have &lt;code&gt;Object.isExtensible&lt;/code&gt; which can be used to check if the new properties can be added to the object or not.&lt;/p&gt;

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

&lt;p&gt;We can conclude this topic using a CRUD table.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;CREATE&lt;/th&gt;
&lt;th&gt;READ&lt;/th&gt;
&lt;th&gt;UPDATE&lt;/th&gt;
&lt;th&gt;DELETE&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Object.freeze&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✗&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;✗&lt;/td&gt;
&lt;td&gt;✗&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Object.seal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✗&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;✗&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Object.preventExtensions&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✗&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;td&gt;✓&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And that wraps it up for this article. Feel free to leave your feedback or questions in the comments section.&lt;/p&gt;

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