<?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: Priyanka Sarode</title>
    <description>The latest articles on DEV Community by Priyanka Sarode (@priyanka_sarode_694e69474).</description>
    <link>https://dev.to/priyanka_sarode_694e69474</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%2F3065230%2F241332e2-3eac-4fa6-a3e8-0a53d39446be.png</url>
      <title>DEV Community: Priyanka Sarode</title>
      <link>https://dev.to/priyanka_sarode_694e69474</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/priyanka_sarode_694e69474"/>
    <language>en</language>
    <item>
      <title>SQL vs NoSQL</title>
      <dc:creator>Priyanka Sarode</dc:creator>
      <pubDate>Fri, 23 May 2025 20:29:12 +0000</pubDate>
      <link>https://dev.to/priyanka_sarode_694e69474/sql-vs-nosql-4h8m</link>
      <guid>https://dev.to/priyanka_sarode_694e69474/sql-vs-nosql-4h8m</guid>
      <description>&lt;p&gt;In today’s data world, there are two databases mostly used in the market to perform operations ( add, retrieve, update and delete) on data :- SQL database and NoSQL database&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;SQL&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; SQL database  stores data in &lt;strong&gt;structured&lt;/strong&gt; way means in the form of rows and columns (tabular format like Excel). And that data doesn’t change frequently such as bank transaction. &lt;/li&gt;
&lt;li&gt;It &lt;strong&gt;uses SQL&lt;/strong&gt; (structured query language).
e.g. to retrieve products from products table where price is greater than 1000 Rs, SQL query would be
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select * from products where price &amp;gt; 1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;It follows a &lt;strong&gt;fixed schema&lt;/strong&gt;⁣⁣ - need to define all fields first. It ensures consistency and predictability.&lt;/li&gt;
&lt;li&gt;SQL databases &lt;strong&gt;scales vertically&lt;/strong&gt; meaning you can increase the load on a single server by adding more CPU, RAM, or SSD capacity i.e. you have to increase hardware.. &lt;/li&gt;
&lt;li&gt;Also SQL queries &lt;strong&gt;helps in deriving insights&lt;/strong&gt; from data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;NoSQL&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NoSQL stores data in &lt;strong&gt;unstructured&lt;/strong&gt;  way means it is stored in JSON-like documents, key-value pairs, etc.⁣ and data changes frequently such as social media posts. &lt;/li&gt;
&lt;li&gt;NoSQL databases &lt;strong&gt;don’t have a single standardized query language&lt;/strong&gt;. Their queries depending upon database type.
e.g. to retrieve products from products table where price is greater than 1000 Rs, MongoDB query would be
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.products.find({ price: { $gt: 1000 } })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;It follows &lt;strong&gt;flexible schema&lt;/strong&gt; - no need to define everything upfront⁣. You can add fields later on as needed.&lt;/li&gt;
&lt;li&gt;NoSQL databases &lt;strong&gt;scales horizontally&lt;/strong&gt;, meaning they use multiple servers to handle increased workloads&lt;/li&gt;
&lt;li&gt;In NoSQL, you can’t derive insights effectively as data changes dynamically.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If you go to Flipkart site and in one tab select men’s t-shirt and in second tab select any brand of mobile, you will get to see different fields in Product Details section for both products even if it comes under product database table.  So in this case, Flipkart uses NoSQL database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Netflix uses hybrid model. For user and billing data, it uses SQL because for every user fields/ schema won’t change like username, password, email. And for recommendation data it uses NoSQL because recommendation movie cards that display on each user page are different as per user’s interest or likings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Uber also uses NoSQL because if company introduces new feature later on, so need to add that additional fields in database too. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;While &lt;strong&gt;SQL follows ACID&lt;/strong&gt; properties, &lt;strong&gt;NoSQL follows the CAP theorem&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CAP theorem states that you can &lt;strong&gt;only guarantee two out of three&lt;/strong&gt; properties in a distributed system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consistency&lt;/strong&gt; - with every request I am going to fetch latest response for you&lt;br&gt;
&lt;strong&gt;Availability&lt;/strong&gt; - with every request I am going to fetch some result even if it is not recent one.&lt;br&gt;
&lt;strong&gt;Partition tolerance&lt;/strong&gt;: The system keeps working even if network issues occur&lt;/p&gt;

&lt;p&gt;e.g. &lt;br&gt;
1) Banking follows CP - User need to enter correct account data. If some data is wrong, cancel that transaction.&lt;br&gt;
2) Uber follows AP - If there is delay in locating vehicle's exact position, show some nearby location. Means it works on availability basis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;SQL&lt;/strong&gt; is best for banking, inventory systems, CRMs⁣&lt;br&gt;
Examples of  SQL database are Oracle, MySQL, PostgreSQL, SQLite, SQL Server⁣&lt;br&gt;
Choose SQL when you need strict consistency.⁣&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NoSQL&lt;/strong&gt; is best for real-time apps, social media, IoT⁣ etc.&lt;br&gt;
Examples: MongoDB, Cassandra, DynamoDB⁣, Redis,  HBase, Neo4j &lt;br&gt;
Choose NoSQL when you need flexibility.&lt;/p&gt;

</description>
      <category>database</category>
      <category>webdev</category>
      <category>interview</category>
    </item>
    <item>
      <title>Web Storage API (localStorage, sessionStorage) &amp; cookie</title>
      <dc:creator>Priyanka Sarode</dc:creator>
      <pubDate>Tue, 22 Apr 2025 07:57:02 +0000</pubDate>
      <link>https://dev.to/priyanka_sarode_694e69474/web-storage-api-localstorage-sessionstorage-cookie-2i8p</link>
      <guid>https://dev.to/priyanka_sarode_694e69474/web-storage-api-localstorage-sessionstorage-cookie-2i8p</guid>
      <description>&lt;p&gt;We all know that for client and server communication &lt;strong&gt;HTTP protocol&lt;/strong&gt; is used. But this protocol is &lt;strong&gt;stateless&lt;/strong&gt; means it doesn't track any response by the server or request by the client. To resolve this problem, cookies, localStorage &amp;amp; sessionStorage come into picture to manage and track data between client-server requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;localStorage and sessionStorage&lt;/strong&gt; both are &lt;strong&gt;part of Web Storage API&lt;/strong&gt; provided by browser and introduced in HTML5 to store the data in &lt;strong&gt;client-side&lt;/strong&gt; computer. &lt;br&gt;
&lt;strong&gt;Cookie&lt;/strong&gt; &lt;strong&gt;are not a part of web storage API&lt;/strong&gt; as they were introduced way before web storage API. They are used in &lt;strong&gt;both client and server side&lt;/strong&gt; communication.&lt;/p&gt;

&lt;p&gt;We will see each one of behaviors, when to use with example.&lt;/p&gt;

&lt;p&gt;1) &lt;strong&gt;Cookie&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cookies are small piece of plain text data to remember/ persist user's information across sessions on the web.&lt;/li&gt;
&lt;li&gt;They are &lt;strong&gt;sent to server with each request&lt;/strong&gt; so that server can identify particular user is authenticated user or not and provide personalized content to him/ her.&lt;/li&gt;
&lt;li&gt;They can &lt;strong&gt;storage 4KB&lt;/strong&gt; of data per cookie means can store small amount of data.&lt;/li&gt;
&lt;li&gt;They are typically used for authentication, session management, storing login status, user preferences and tracking user's behavior on the site.&lt;/li&gt;
&lt;li&gt;They can be set to expire at a specific period of time or can be deleted manually.&lt;/li&gt;
&lt;li&gt;They can be &lt;strong&gt;accessed by other domains&lt;/strong&gt; that user visits without user consent. This is the main concern by cookie. We can't store sensitive data in the cookie. Yes we can use HttpOnly, Secure, and Expires/Max-Age attributes that cookies provide to prevent/ leakage of the user's information in the web. &lt;/li&gt;
&lt;li&gt;Cookies &lt;strong&gt;doesn't expire on closing the browser&lt;/strong&gt; but &lt;strong&gt;will expire&lt;/strong&gt; at a specific date (&lt;strong&gt;Expires&lt;/strong&gt;) or length of time (&lt;strong&gt;Max-Age&lt;/strong&gt;) .&lt;/li&gt;
&lt;li&gt;Mainly suitable for client-server communication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example - When user login for the first time, the server can create a cookie to remember their username, unique session_ID etc. If the user visits the same website again in future, the browser sends the cookie back to the server with every request. The server can then use the information in the cookie to identify the user and personalize their experience on the site. But here whenever user sends request to server, api call is made and it costs a lot. Some user will experience a significant delay if network is slow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//setting cookie
document.cookie = "username=John; expires=Fri, 31 Dec 2025 12:00:00 UTC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) &lt;strong&gt;localStorage&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;stores data in &lt;strong&gt;key-value pairs as string&lt;/strong&gt; in user's browser.&lt;/li&gt;
&lt;li&gt;here &lt;strong&gt;data persists&lt;/strong&gt; even after user &lt;strong&gt;closes their browser tab/ window&lt;/strong&gt; or on page reloades/ refreshes/ reopend. Means it doesn't have expiry date. It remains until user &lt;strong&gt;explicitly remove&lt;/strong&gt; the data through code or clearing by double clicking on that data in Application tab of browser developer tool or by clearing browser's cache.
&lt;/li&gt;
&lt;li&gt;We can access localstorage data in &lt;strong&gt;multiple tabs, different windows&lt;/strong&gt; if it has the &lt;strong&gt;same url&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It can &lt;strong&gt;store data upto 5MB or 10MB&lt;/strong&gt; depending on the browser.&lt;/li&gt;
&lt;li&gt;Its data is never transferred to the server but you can manually include its value in fetch request.&lt;/li&gt;
&lt;li&gt;It is &lt;strong&gt;synchronous&lt;/strong&gt;. If I perform operations on localStorage, It will block the execution of other javascript code until the current operation will get completed.&lt;/li&gt;
&lt;li&gt;It follows &lt;strong&gt;same origin policy&lt;/strong&gt; (per-origin) means &lt;a href="http://abc.com%E2%80%99s%C2%A0localStorage%C2%A0object" rel="noopener noreferrer"&gt;http://abc.com’s localStorage object&lt;/a&gt; is different from &lt;a href="https://blog.abc.com%E2%80%99s%C2%A0localStorage%C2%A0object" rel="noopener noreferrer"&gt;https://blog.abc.com’s localStorage object&lt;/a&gt; because the two origins use different protocols (http and https), different hosts (abc and blog.abc). So data will be different for these two sites.&lt;/li&gt;
&lt;li&gt;localStorage is a &lt;strong&gt;property of the global Window object&lt;/strong&gt;. Therefore, localStorage.setItem() is equivalent to window.localStorage.setItem().&lt;/li&gt;
&lt;li&gt;Use localStorage when you need to store simple, persistent data on a user's device for a longer period of time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Methods of localStorage&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Save data
localStorage.setItem("key", "value");

localStorage.setItem("username", "Alice");

// Retrieve data
localStorage.getItem("key");

localStorage.getItem("username")
console.log(username)

// Remove data
localStorage.removeItem("key");

localStorage.removeItem("username");

// Clear all data from localStorage
localStorage.clear();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If I store &lt;strong&gt;same key with different value&lt;/strong&gt;, then data will be &lt;strong&gt;override&lt;/strong&gt; with the previous ones and get new value as result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localStorage.setItem("username", "Alice");
localStorage.setItem("username", "John");

localStorage.getItem("username")
//output - John
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;removeItem()&lt;/strong&gt; method will do nothing if its &lt;strong&gt;key does not exist&lt;/strong&gt; in the local storage. You will get output as &lt;strong&gt;undefind&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localStorage.removeItem("username");
//output - undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And &lt;strong&gt;getItem()&lt;/strong&gt; method will return null if its &lt;strong&gt;key doesn't exist&lt;/strong&gt; and I try to access that key, it will return &lt;strong&gt;null&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localStorage.getItem("username");
//output - null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can store data as string in localStorage, but what if I want to &lt;strong&gt;store object&lt;/strong&gt; in it. If I do below like this, it will return "[object Object]".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localStorage.setItem("user", {"name": "Alice"});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to convert object into string before storing it into localStorage by using &lt;strong&gt;JSON.stringify()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user = {"name": "Alice"}
localStorage.setItem("User", JSON.stringify(user));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to &lt;strong&gt;get the object&lt;/strong&gt; data from localStorage, again we need to convert string to its original form by using &lt;strong&gt;JSON.parse()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JSON.parse(localStorage.getItem("User")))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can use it to create and store notes in user's computer&lt;/li&gt;
&lt;li&gt;most suitable for user preferences data like theme, layout or language settings that doesn't need to be sent to server&lt;/li&gt;
&lt;li&gt;to store form inputs so users don’t lose data when they refresh the page.&lt;/li&gt;
&lt;li&gt;to keep user authentication tokens for faster login whenever visit the website next time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:- When login in social media application or e-commerce website, your login credentials like username, password (same for longer period of time) gets stored in localStorage. So every time you come back, application doesn't require to make api call to get account details &amp;amp; retrieve data from it. It helps to save api call.&lt;/p&gt;

&lt;p&gt;3) &lt;strong&gt;sessionStorage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It does almost the &lt;strong&gt;same work&lt;/strong&gt;, has the &lt;strong&gt;same methods like localStorage&lt;/strong&gt; but with a few minor differences mentioned below: -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sessionStorage &lt;strong&gt;stores&lt;/strong&gt; the data &lt;strong&gt;temporarily&lt;/strong&gt; as string in key-value pairs in client side but &lt;strong&gt;for a single/ current session&lt;/strong&gt;/ when a tab is open.&lt;/li&gt;
&lt;li&gt;It's data gets &lt;strong&gt;cleared when user closes the tab&lt;/strong&gt;/ window/ page session ends/ refreshes that page. i.e. it follows &lt;strong&gt;per-instance policy&lt;/strong&gt; plus &lt;strong&gt;same origin policy&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Here browser &lt;strong&gt;creates unique page session with each tab&lt;/strong&gt;/ window. Hence it creates new instance/ object of sessionStorage for an app without interfering other instances of the same app. &lt;/li&gt;
&lt;li&gt;It can &lt;strong&gt;store 5MB of data&lt;/strong&gt; means it has a higher storage capacity compared to cookies.&lt;/li&gt;
&lt;li&gt;It's useful for storing temporary data that is needed when user is on that website like session-based authentication details that shouldn't persist beyond the current session.&lt;/li&gt;
&lt;li&gt;To store large amount of data, use database instead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Methods of sessionStorage&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Save data
sessionStorage.setItem("username", "John");

// Retrieve data
let username = sessionStorage.getItem("username");

// Remove data
sessionStorage.removeItem("username");

// Clear all data
sessionStorage.clear();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;to store authentication tokens for a single session&lt;/li&gt;
&lt;li&gt;to store user's input in multi-step form. In this case user need that input data for that session only and don't want to lose it when switching between pages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tricky Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Q) Is there any way you can persist sessionStorage data for a longer period of time?&lt;br&gt;
Ans - Yes. We can set manual expiration time in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let now = new Date().getTime();
let expiry = now + 1000 * 60 * 30; // Expires in 30 minutes
sessionStorage.setItem("expiryTime", expiry);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Differences and similarity between cookie, localStorage and sessionStorage&lt;/strong&gt;&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;cookie&lt;/th&gt;
&lt;th&gt;localStorage&lt;/th&gt;
&lt;th&gt;sessionStorage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;data accessible&lt;/td&gt;
&lt;td&gt;any window&lt;/td&gt;
&lt;td&gt;any window&lt;/td&gt;
&lt;td&gt;same tab&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;data persistence&lt;/td&gt;
&lt;td&gt;depends on expires/ max-age attribute&lt;/td&gt;
&lt;td&gt;data persists even after closing the session&lt;/td&gt;
&lt;td&gt;data is lost when session is over&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;storage capacity&lt;/td&gt;
&lt;td&gt;4KB of storage&lt;/td&gt;
&lt;td&gt;5-10MB per origin depends on browser&lt;/td&gt;
&lt;td&gt;5MB per origin&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;expire&lt;/td&gt;
&lt;td&gt;manually set using expires attribute&lt;/td&gt;
&lt;td&gt;till explicitly delete by code/ user&lt;/td&gt;
&lt;td&gt;when session ends/ explicitly delete by code/ user&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;data transfer to server&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;can read and write data&lt;/td&gt;
&lt;td&gt;client and server&lt;/td&gt;
&lt;td&gt;client&lt;/td&gt;
&lt;td&gt;client&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;accessible in domain/ origin&lt;/td&gt;
&lt;td&gt;different&lt;/td&gt;
&lt;td&gt;same&lt;/td&gt;
&lt;td&gt;same&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;supported by all browsers including older ones&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;web storage api (introduced in html5)&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;synchronous&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;property of window object&lt;/td&gt;
&lt;td&gt;indirectly (window.document.cookie)&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;usage&lt;/td&gt;
&lt;td&gt;for client-server communication&lt;/td&gt;
&lt;td&gt;for data that doesn't need to be  sent to server&lt;/td&gt;
&lt;td&gt;for storing session-relevant data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>interview</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>async and defer in &lt;script&gt; tag</title>
      <dc:creator>Priyanka Sarode</dc:creator>
      <pubDate>Mon, 21 Apr 2025 08:25:33 +0000</pubDate>
      <link>https://dev.to/priyanka_sarode_694e69474/async-and-defer-in-tag-2h9e</link>
      <guid>https://dev.to/priyanka_sarode_694e69474/async-and-defer-in-tag-2h9e</guid>
      <description>&lt;p&gt;This question asked in the interview to test whether you know different JavaScript files loading strategies or not and their behavior.&lt;/p&gt;

&lt;p&gt;Let's understand its behavior and when to use.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basically both async and defer are &lt;strong&gt;boolean&lt;/strong&gt; attributes which are used in script tag to &lt;strong&gt;load external&lt;/strong&gt; (e.g. bootstrap.js) JavaScript files in to our web page.&lt;/li&gt;
&lt;li&gt;When we load the page, two things happened: &lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;html parsing&lt;/li&gt;
&lt;li&gt;loading of JavaScript files - also contains 2 parts&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;a) fetching/ downloading script file from the network/ server&lt;br&gt;
   b) execution of that file line by line&lt;/p&gt;

&lt;p&gt;Now we will look into different scenarios where both the keywords used in script tag&lt;/p&gt;

&lt;p&gt;1) &lt;strong&gt;script tag in Head&lt;/strong&gt; element where &lt;strong&gt;no async and defer&lt;/strong&gt; used (&lt;strong&gt;Normal flow&lt;/strong&gt;)&lt;br&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%2Fg9nfgduw4zyvgso2nqkl.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%2Fg9nfgduw4zyvgso2nqkl.PNG" alt="script tag in Head" width="800" height="114"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;script src="./script.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Here when the html page loads in the browser, it starts html parsing from top to bottom line by line. If it finds the script tag, html parsing is paused, a request is sent to the server to get the script and script file starts downloading. Once download completes, script file starts executing.  After this, browser starts parsing the rest of the html page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limitation of this approach:&lt;/strong&gt;&lt;br&gt;
Sometimes size of the script file is larger, in  this case processing time will be longer. And this increases the load time of the page and restricts the user to navigate through other pages of the website. To solve this problem, async and defer attributes come into play.&lt;/p&gt;

&lt;p&gt;2) &lt;strong&gt;script tag&lt;/strong&gt; in bottom of &lt;strong&gt;the body&lt;/strong&gt; and &lt;strong&gt;no async and defer&lt;/strong&gt; is used&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%2Fdrup8bt84ah7a45hr7gl.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%2Fdrup8bt84ah7a45hr7gl.PNG" alt="script tag in body" width="800" height="122"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;script src="./script.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here browser starts html parsing from top and once html parsing finished, then only script fetching starts. After completing fetching, script execution starts till end. &lt;/p&gt;

&lt;p&gt;This is far better approach than the previous ones. But for long HTML documents, there may be a noticeable delay for people who are using slow internet. &lt;/p&gt;

&lt;p&gt;3) &lt;strong&gt;script tag in head&lt;/strong&gt; and &lt;strong&gt;async&lt;/strong&gt; is used &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%2Fsyxrnynnlddvbi26j32z.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%2Fsyxrnynnlddvbi26j32z.PNG" alt="script in head with async" width="800" height="119"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;script async src="./script.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here html parsing and script fetching continue in parallel. Once script fetching done, html parsing paused and script execution starts till end. After this html parsing resumes and loads the rest of the page. &lt;/p&gt;

&lt;p&gt;This approach is useful when there are multiple third party scripts (google ads, analytics etc.) which are independent of each other and doesn't have direct relationship with DOM. &lt;br&gt;
And they will execute as soon as they finish downloading, regardless of their order in the code means browser doesn't guarantee the order of execution of scripts.&lt;/p&gt;

&lt;p&gt;4) &lt;strong&gt;script tag in head&lt;/strong&gt; and &lt;strong&gt;defer&lt;/strong&gt; is used&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%2Fiu4920ij6c07g9dfu2gu.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%2Fiu4920ij6c07g9dfu2gu.PNG" alt="script in head with defer" width="800" height="121"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;script defer src="./script.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like async, here also html parsing and script fetching continue in parallel. After fetching of script completes, browser keep that file aside means doesn't execute instantly. Once the html parsing finished, then only script execution starts till end.&lt;/p&gt;

&lt;p&gt;This approach is useful when you want html dom should be load first and then other dependent scripts. Also here scripts are executed in the order the way they are defined in the code. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Similarity&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both async and defer are &lt;strong&gt;boolean&lt;/strong&gt; attributes &lt;/li&gt;
&lt;li&gt;If script tag has &lt;strong&gt;no src&lt;/strong&gt; property, then defer and async attributes will be &lt;strong&gt;ignored by browser&lt;/strong&gt; hence no effect on the page loading&lt;/li&gt;
&lt;li&gt;Both allow the browser to &lt;strong&gt;continue html parsing and script fetching&lt;/strong&gt; to be done &lt;strong&gt;in parallel&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Difference&lt;/strong&gt;&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;async&lt;/th&gt;
&lt;th&gt;defer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;execution&lt;/td&gt;
&lt;td&gt;when loaded, script files executes immediately&lt;/td&gt;
&lt;td&gt;it doesn't execute immediately after loading&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;execution order&lt;/td&gt;
&lt;td&gt;execution in no particular order&lt;/td&gt;
&lt;td&gt;execution in order&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;dependency&lt;/td&gt;
&lt;td&gt;used when script files are not dependent on each other&lt;/td&gt;
&lt;td&gt;used when scripts are dependent on each other&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;blocking&lt;/td&gt;
&lt;td&gt;before executing, browser blocks html parsing&lt;/td&gt;
&lt;td&gt;it doesn't block html page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DOM interaction&lt;/td&gt;
&lt;td&gt;use when don't need to interact with the DOM&lt;/td&gt;
&lt;td&gt;use when interact with the DOM&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Tricky Question&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What will happen if I put script tag in the bottom of the body with defer attribute?&lt;br&gt;
Ans - As we know, defer attribute delaying the execution of the script. &lt;br&gt;
When we use defer at the bottom of the body, entire html parsing is already completed, so any way that script will execute later on. So it doesn't have any effect on the page. It is similar to no defer tag. &lt;br&gt;
defer attribute is useful when we use in the script tag in the head section. And we want execution to be happened later.&lt;/p&gt;

</description>
      <category>interview</category>
      <category>javascript</category>
    </item>
    <item>
      <title>What is Javascript?</title>
      <dc:creator>Priyanka Sarode</dc:creator>
      <pubDate>Sat, 19 Apr 2025 10:59:25 +0000</pubDate>
      <link>https://dev.to/priyanka_sarode_694e69474/what-is-javascript-23gi</link>
      <guid>https://dev.to/priyanka_sarode_694e69474/what-is-javascript-23gi</guid>
      <description>&lt;p&gt;As we all know that JavaScript is one of the widely used programming languages across all domains, such as web, mobile, game development etc. till date now.&lt;/p&gt;

&lt;h2&gt;
  
  
  But why it becomes so much popular among all the existing languages?
&lt;/h2&gt;

&lt;p&gt;Because it is easy to learn, &lt;strong&gt;dynamic, high-level, interpreted, light-weight scripting programming language&lt;/strong&gt; that allows to add interactivity on the web page.&lt;/p&gt;

&lt;p&gt;Let's break it down what all these keywords mean.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dynamic means we can run JavaScript code in user's local computer as well as on the server.&lt;/li&gt;
&lt;li&gt;a) We can dynamically generate or update any html element like p, ul, li, div etc. tags inside client's browser using DOM's API (which is browser's API resides in the browser).&lt;/li&gt;
&lt;li&gt;b) Also we can dynamically generate new content on the server side by writing application logic e.g., pulling data from a database.&lt;/li&gt;
&lt;li&gt;High-level means it is human readable language.&lt;/li&gt;
&lt;li&gt;Interpreted means the code starts running from top to bottom and when we run that code, we get immediate human readable result. 
We don't need to translate the code into machine code before running like in C, C++ etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to use JavaScript in HTML?
&lt;/h2&gt;

&lt;p&gt;There are 3 ways, we can place JavaScript code in html page depending upon use cases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. &lt;strong&gt;Inline JavaScript&lt;/strong&gt; - We can use it inside HTML tags by using JavaScript attributes. E.g.,
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onclick="alert('You clicked me')"&amp;gt;Click me!&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;2. &lt;strong&gt;Internal JavaScript&lt;/strong&gt; - with Script tag&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can add script tag at the bottom of your body means just before your closing body tag or at the top before closing head tag&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    function display(){
       console.log("I am in the script tag")
    }
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;3 &lt;strong&gt;External JavaScript&lt;/strong&gt; - Just like we use link tag for linking stylesheet to html, use script tag for attaching JavaScript file to html. For this, create new file with extension .js in the same or different directory path. Add that file path in the html page using src (source) attribute. This is important because it tells the browser to fetch data from "xyz.js" file. Otherwise js file code won't run.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- index.html --&amp;gt;
&amp;lt;script src="./script.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Benefits:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Platform independent&lt;/strong&gt; - We can run JavaScript code in any machine. It is supported by all major browsers and enabled by default. &lt;/li&gt;
&lt;li&gt;Many &lt;strong&gt;JavaScript open source libraries and frameworks&lt;/strong&gt; are available in the market. e.g.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;a) We can use Chart.js library to display static or dynamic data in bar, line, pie charts or maps.&lt;br&gt;
 b) Reactjs, Vuejs or Angularjs web frameworks simplifies the web development  and makes developer's life ease. &lt;br&gt;
 c) React Native is another framework is used for building Android, iOS mobile apps.&lt;br&gt;
 d) Using Nodejs framework, we can run JavaScript code outside of the browser.&lt;/p&gt;

&lt;p&gt;3.&lt;strong&gt;Client-side validations&lt;/strong&gt; - Before submitting the form and send data to server, JavaScript quickly checks whether user have entered a 10-digit number for the mobile phone field or not. Because of it server load reduces by performing logical operations in the client side itself.&lt;br&gt;
4.&lt;strong&gt;Single threaded&lt;/strong&gt; - JavaScript executes one task at a time. However with the help of event loop concept, it can handle multiple tasks simultaneously.&lt;br&gt;
5.JavaScript is &lt;strong&gt;synchronous&lt;/strong&gt; by default, meaning it executes code line by line in a sequential manner. However, it can handle asynchronous tasks like fetching data from database using features like callbacks, Promises, and async/await.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Weakly typed language&lt;/strong&gt; - JavaScript does not allow the programmer to define the variable type. As a result, you can get unexpected results, such as
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 + 1 + '1' = '21'
'1' + 1 + 1 = '111'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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