<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-4611029784033708994</atom:id><lastBuildDate>Mon, 27 Apr 2026 16:54:39 +0000</lastBuildDate><category>java</category><category>javaGUI</category><category>javaBasic</category><category>asp.net</category><category>javascript</category><category>dsa with java</category><category>asp dot net</category><category>blogging</category><category>c#</category><category>data structure and algorithm</category><category>dsa</category><category>Network</category><category>bim 4th semester</category><category>asp</category><category>javaSwing</category><category>notes</category><category>algorithm</category><category>algorithm analysis</category><category>analysis</category><category>sql</category><category>tech</category><category>tutorials</category><category>angular</category><category>difference between stack and queue</category><category>mvc design pattern</category><category>programming</category><category>angular 8</category><category>angular forms</category><category>angular project</category><category>angular project setup steps</category><category>angular reactive forms</category><category>angular tutorial</category><category>angular tutorial for beginners</category><category>asp. dot net</category><category>b tree and b+ tree in data structure</category><category>b tree in data structure</category><category>b tree in dbms</category><category>bim</category><category>bim 1st sem syllabus in nepali</category><category>bim 5th sem syllabus in nepali</category><category>bim 7th sem syllabus in nepali</category><category>bim course</category><category>bim course syllabus</category><category>bim syllabus in nepali</category><category>binary trees</category><category>blog</category><category>combo box</category><category>create angular project in visual studio code</category><category>database</category><category>difference between template driven form and reactive form</category><category>dot net</category><category>dot net tutorial</category><category>download udemy courses</category><category>example</category><category>how to</category><category>how to create angular project</category><category>implement queue using stack</category><category>industry</category><category>it</category><category>jcombobox</category><category>learn angular</category><category>mssql</category><category>mvc</category><category>mysql</category><category>oop</category><category>oop concept</category><category>queue</category><category>reactive forms</category><category>remove footer from blogger</category><category>rest api</category><category>rotations in avl trees</category><category>soap  api</category><category>stack</category><category>stack and queue</category><category>stack vs queue</category><category>tech news</category><category>template driven forms</category><category>tree in data structure</category><category>trees</category><category>trees in data structures</category><category>tutorial</category><title>Code Craft</title><description>Welcome to Code Central, your go-to resource for all things networking and coding! Whether you&#39;re a beginner looking to learn the basics or an expert seeking advanced techniques, this blog provides insightful articles, tutorials, and tips to help you navigate the world of technology. Stay updated on the latest trends in networking, coding practices, and industry best practices to elevate your skills and keep ahead of the curve.</description><link>https://www.ankitchamlagain.com.np/</link><managingEditor>noreply@blogger.com (Unknown)</managingEditor><generator>Blogger</generator><openSearch:totalResults>93</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-8015428132306678168</guid><pubDate>Mon, 27 Apr 2026 15:01:00 +0000</pubDate><atom:updated>2026-04-27T20:46:12.844+05:45</atom:updated><title>The 2026 IT Playbook: Agentic AI, Cyber Resilience, and Sustainable Innovation</title><description>&lt;div style=&quot;max-width: 800px; margin: 0 auto; padding: 20px; font-family: &#39;Segoe UI&#39;, Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: #333;&quot;&gt;

&lt;div style=&quot;background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 40px 30px; border-radius: 12px; margin-bottom: 30px; color: white;&quot;&gt;
&lt;div style=&quot;font-size: 14px; opacity: 0.9; margin-bottom: 10px;&quot;&gt;IT Trends • April 2026&lt;/div&gt;
&lt;div style=&quot;font-size: 32px; font-weight: bold; margin-bottom: 15px;&quot;&gt;The IT Revolution of 2026: Agentic AI, Quantum Readiness, and the New Digital Frontier&lt;/div&gt;
&lt;div style=&quot;font-size: 16px; opacity: 0.95;&quot;&gt;Exploring the transformative technologies reshaping enterprise IT and what they mean for your organization&lt;/div&gt;
&lt;/div&gt;

&lt;div style=&quot;background: #f8f9fa; padding: 20px; border-left: 4px solid #667eea; margin-bottom: 25px; border-radius: 0 8px 8px 0;&quot;&gt;
&lt;div style=&quot;font-weight: 600; margin-bottom: 8px;&quot;&gt;🔍 Quick Takeaway&lt;/div&gt;
&lt;div&gt;2026 marks a pivotal shift from AI experimentation to enterprise-scale implementation. Organizations are moving beyond chatbots to autonomous agents, prioritizing governed AI deployment, cybersecurity expansion, and sustainable tech practices. [[1]]&lt;/div&gt;
&lt;/div&gt;

&lt;div style=&quot;margin-bottom: 30px;&quot;&gt;
&lt;div style=&quot;font-size: 24px; font-weight: bold; margin-bottom: 15px; color: #2c3e50;&quot;&gt;The Dawn of Agentic AI: Beyond Chatbots&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
The most significant trend dominating IT conversations in 2026 is the emergence of Agentic AI—autonomous systems capable of planning, decision-making, and executing complex workflows without constant human oversight. [[2]] Unlike traditional AI assistants that wait for prompts, agentic systems break down objectives into sub-tasks, leverage multiple tools, and adapt in real-time to achieve goals. This represents a fundamental evolution from reactive AI to proactive digital collaborators.
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
Organizations are increasingly exploring agentic patterns for IT operations, sales enablement, and customer support workflows. However, experts caution that 2026 is not the year for fully autonomous business operations. Instead, it&#39;s a preparation phase: testing controlled agents in low-risk domains, stress-testing governance policies, and establishing clear escalation paths before the anticipated agent ramp-up in 2027-2028. [[7]]
&lt;/div&gt;
&lt;/div&gt;

&lt;div style=&quot;margin-bottom: 30px;&quot;&gt;
&lt;div style=&quot;font-size: 24px; font-weight: bold; margin-bottom: 15px; color: #2c3e50;&quot;&gt;Generative AI at Scale: From Hype to Business Value&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
While generative AI captured headlines in previous years, 2026 is the year of maturation. Enterprises are shifting from &quot;testing everything&quot; to strategic implementation focused on measurable ROI. The key challenge now isn&#39;t model capability—it&#39;s data quality, governance frameworks, and seamless integration into existing workflows. [[5]]
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
A notable shift is the rebalancing between large language models and smaller, specialized alternatives. For many use cases like internal search, document summarization, or content cleanup, lightweight models deliver comparable results at lower cost, with better latency and more predictable behavior. Smart organizations are adopting a &quot;right model for the job&quot; approach rather than defaulting to the largest available LLM. [[2]]
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
Software development is also being transformed. AI-powered coding assistants are now standard in developer toolchains, accelerating prototyping and reducing repetitive tasks. The focus for tech leaders is establishing clear guidelines for AI-generated code review, vulnerability scanning, and licensing compliance while measuring genuine productivity gains. [[7]]
&lt;/div&gt;
&lt;/div&gt;

&lt;div style=&quot;margin-bottom: 30px;&quot;&gt;
&lt;div style=&quot;font-size: 24px; font-weight: bold; margin-bottom: 15px; color: #2c3e50;&quot;&gt;Cybersecurity: Defending the Expanded Attack Surface&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
As AI adoption accelerates, so do AI-enabled threats. Cybersecurity in 2026 extends far beyond traditional perimeter defense. The attack surface has migrated to SaaS platforms, third-party integrations, and business-led AI initiatives—creating new blind spots that attackers exploit. [[2]] Recent incident reports show that 56% of attacks against large companies originated through subsidiaries or partners, highlighting the critical need for extended security visibility.
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
Forward-thinking organizations are leveraging AI defensively: using machine learning to pre-classify sensitive data, automate threat detection, and accelerate incident response. The goal isn&#39;t to replace human analysts but to eliminate bottlenecks so security teams can focus on high-impact decisions. [[7]] Additionally, securing business-deployed AI tools has become a priority—establishing simple declaration processes and minimum guardrails for data access, logging, and human oversight.
&lt;/div&gt;
&lt;/div&gt;

&lt;div style=&quot;margin-bottom: 30px;&quot;&gt;
&lt;div style=&quot;font-size: 24px; font-weight: bold; margin-bottom: 15px; color: #2c3e50;&quot;&gt;Sustainable-by-Design IT: The Carbon-Aware Technology Stack&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
Environmental responsibility is no longer optional. With nearly 80% of organizations reporting that CSR now plays a larger role in corporate governance, IT leaders are integrating carbon considerations into technology decisions. [[7]] This is especially critical for AI workloads, where compute intensity directly impacts energy consumption.
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
The most advanced enterprises are adopting &quot;carbon budgeting&quot;—treating environmental impact with the same rigor as financial planning. Every major initiative now includes measurable footprint estimates, enabling conscious trade-offs between innovation value and sustainability goals. [[2]] This approach doesn&#39;t block ambitious projects; it ensures they&#39;re pursued with full awareness of their resource implications and mitigation strategies.
&lt;/div&gt;
&lt;/div&gt;

&lt;div style=&quot;margin-bottom: 30px;&quot;&gt;
&lt;div style=&quot;font-size: 24px; font-weight: bold; margin-bottom: 15px; color: #2c3e50;&quot;&gt;Preparing for the Quantum Horizon&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
While large-scale quantum computers remain years away, the cryptographic implications demand action today. Security agencies warn of &quot;harvest now, decrypt later&quot; attacks, where adversaries capture encrypted data today to decrypt once quantum capabilities mature. [[7]] Organizations handling long-lived sensitive data—financial records, intellectual property, healthcare information—must begin post-quantum cryptography planning now.
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 15px;&quot;&gt;
The practical 2026 approach isn&#39;t panic-driven overhaul but strategic roadmap development: inventorying cryptographic dependencies, identifying high-value secrets requiring priority migration, and running controlled pilots with emerging quantum-safe algorithms. Starting this work now prevents rushed, risky migrations later. [[2]]
&lt;/div&gt;
&lt;/div&gt;

&lt;div style=&quot;background: #e8f4fd; padding: 25px; border-radius: 10px; margin: 30px 0;&quot;&gt;
&lt;div style=&quot;font-weight: bold; font-size: 18px; margin-bottom: 12px; color: #1a5276;&quot;&gt;Key Actions for IT Leaders in 2026&lt;/div&gt;
&lt;ul style=&quot;padding-left: 20px; margin: 0;&quot;&gt;
&lt;li style=&quot;margin-bottom: 8px;&quot;&gt;✅ Prioritize 3-5 high-value AI use cases with clear metrics; retire low-impact experiments&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 8px;&quot;&gt;✅ Establish cross-functional AI governance involving CIO, CISO, CDO, and business units&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 8px;&quot;&gt;✅ Map cryptographic dependencies and begin post-quantum readiness planning&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 8px;&quot;&gt;✅ Integrate carbon impact assessments into technology procurement and project approval&lt;/li&gt;
&lt;li style=&quot;margin-bottom: 8px;&quot;&gt;✅ Extend security monitoring to SaaS, third-party integrations, and business-led AI tools&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;div style=&quot;margin-top: 30px; padding-top: 20px; border-top: 2px solid #eee; font-size: 14px; color: #666;&quot;&gt;
&lt;div style=&quot;margin-bottom: 10px;&quot;&gt;&lt;strong&gt;About This Analysis:&lt;/strong&gt; This blog synthesizes insights from leading technology research including CompTIA&#39;s IT Industry Outlook 2026 [[5]], Wavestone&#39;s Technology Trends Report [[2]], and emerging industry perspectives on AI governance and sustainable IT practices.&lt;/div&gt;
&lt;div&gt;💡 &lt;em&gt;The organizations that thrive in 2026 won&#39;t be those chasing every new technology, but those strategically implementing governed, value-driven solutions while preparing for tomorrow&#39;s challenges today.&lt;/em&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;</description><link>https://www.ankitchamlagain.com.np/2026/04/-it-playbook-agentic-ai.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-1390133175511504101</guid><pubDate>Fri, 23 Jan 2026 05:05:00 +0000</pubDate><atom:updated>2026-01-23T11:21:47.367+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">blog</category><category domain="http://www.blogger.com/atom/ns#">industry</category><category domain="http://www.blogger.com/atom/ns#">it</category><category domain="http://www.blogger.com/atom/ns#">tech</category><category domain="http://www.blogger.com/atom/ns#">tech news</category><title>Top IT Industry Trends 2026: AI, Cybersecurity, Cloud Computing &amp; Nepal Opportunities</title><description>&lt;div class=&quot;blog-post-container&quot;&gt; &lt;article class=&quot;post-article&quot;&gt; &lt;header class=&quot;post-header&quot;&gt; &lt;h1 class=&quot;post-title&quot;&gt;Top IT Industry Trends 2026: AI, Cybersecurity, Cloud Computing &amp;amp; Nepal Opportunities&lt;/h1&gt; &lt;div class=&quot;post-meta&quot;&gt; &lt;span class=&quot;author&quot;&gt;By Ankit Chamlagain&lt;/span&gt; &lt;span class=&quot;date&quot;&gt;January 23, 2026&lt;/span&gt; &lt;span class=&quot;read-time&quot;&gt;15 min read&lt;/span&gt; &lt;/div&gt; &lt;/header&gt;
&lt;section class=&quot;post-content&quot;&gt;
  &lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEjbdHoaIeQ710BUP_xyzgfBzVRC2tqbfqXWPwOsLjVpZXjuwIKruURVAy5DVO9YjLwkekGcRYD2Y28sor7VohYXVQ-7q70vfeFLgTRDcVSAa73T3JmkGAH87V2oRa3uL9WE5ubpL_JLh-NkG0PeP1awIYHJOGc7QwekVgCGOaifkYbhlcmlkFy6K7oIe2c&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;top it industry nepal&quot; data-original-height=&quot;768&quot; data-original-width=&quot;1408&quot; height=&quot;219&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEjbdHoaIeQ710BUP_xyzgfBzVRC2tqbfqXWPwOsLjVpZXjuwIKruURVAy5DVO9YjLwkekGcRYD2Y28sor7VohYXVQ-7q70vfeFLgTRDcVSAa73T3JmkGAH87V2oRa3uL9WE5ubpL_JLh-NkG0PeP1awIYHJOGc7QwekVgCGOaifkYbhlcmlkFy6K7oIe2c=w400-h219&quot; title=&quot;top it industry nepal&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;The IT industry in 2026 surges forward with transformative trends like AI governance, advanced cybersecurity, and cloud dominance, reshaping global and local landscapes. In Nepal, these shifts open doors for developers and businesses amid digital transformation. This guide explores high-search keywords such as &quot;IT trends 2026,&quot; &quot;AI cybersecurity,&quot; and &quot;cloud computing Nepal&quot; to deliver actionable insights.&lt;p&gt;&lt;/p&gt;[1][2][3][4]
  
  &lt;h2&gt;AI Revolution: Governance and Real-World Applications&lt;/h2&gt;
  &lt;p&gt;Artificial Intelligence leads 2026 IT trends, with governance emerging as a priority to ensure ethical deployment across sectors. Organizations focus on frameworks balancing innovation and risk, particularly in large language models powering applications from chatbots to predictive analytics. In Nepal, AI adoption accelerates in fintech, education, and healthcare, driving demand for AI/ML engineers.&lt;/p&gt;[3][4][5][6][1]
  &lt;p&gt;Governance involves compliance with evolving regulations, data privacy, and bias mitigation, crucial as AI integrates into daily operations. Businesses adopting AI report 40% efficiency gains, but require skilled talent to implement responsibly. Nepali firms leverage tools like ChatGPT for content and development, aligning with global standards.&lt;/p&gt;[2][7][1]
  
  &lt;h2&gt;Cybersecurity Evolution: AI-Driven Threats and Defenses&lt;/h2&gt;
  &lt;p&gt;Cybersecurity trends in 2026 highlight AI-accelerated attacks, making proactive defenses essential amid rising cloud breaches. Non-human identities in cloud environments become prime targets, demanding strict permissions and automated remediation. High-intent searches like &quot;AI cybersecurity trends 2026&quot; reflect urgency as attacks grow cheaper and faster.&lt;/p&gt;[8][9][10]
  &lt;ul&gt;
    &lt;li&gt;AI detects cloud misconfigurations and suspicious access in real-time.&lt;/li&gt;[8]
    &lt;li&gt;Machine identities require governance to prevent breaches.&lt;/li&gt;[10]
    &lt;li&gt;In Nepal, cybersecurity specialists top in-demand jobs due to fintech growth.&lt;/li&gt;[11][3]
  &lt;/ul&gt;
  &lt;p&gt;Preemptive exposure management overtakes runtime detection, with custom AI tools tailored for organizational needs. Nepali developers integrate these into Laravel/Node.js apps for secure API banking and fraud detection.&lt;/p&gt;[3][10]
  
  &lt;h2&gt;Cloud Computing Boom: Hybrid and Multi-Cloud Strategies&lt;/h2&gt;
  &lt;p&gt;Cloud 3.0 defines 2026, blending public, private, and edge computing for intelligent operations. Trends favor AWS, DigitalOcean, and multi-cloud setups, vital for Nepal&#39;s outsourcing surge. Searches for &quot;cloud computing trends 2026&quot; spike as workloads migrate, emphasizing security.&lt;/p&gt;[2][3][8]
  &lt;p&gt;Key advancements include serverless architectures and AI-optimized infrastructure, reducing costs by 30% for adopters. In Nepal, cloud engineers command premium salaries, supporting e-commerce and telemedicine platforms.&lt;/p&gt;[4][1][11]
  [3]
      [3]
      [3]
      &lt;table class=&quot;trend-comparison&quot;&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Cloud Provider&lt;/th&gt;
        &lt;th&gt;Key Strength 2026&lt;/th&gt;
        &lt;th&gt;Nepal Adoption&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;AWS&lt;/td&gt;
        &lt;td&gt;AI Integration&lt;/td&gt;
        &lt;td&gt;High in Fintech&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;DigitalOcean&lt;/td&gt;
        &lt;td&gt;Affordable Scaling&lt;/td&gt;
        &lt;td&gt;Popular for Startups&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;GCP&lt;/td&gt;
        &lt;td&gt;Data Analytics&lt;/td&gt;
        &lt;td&gt;Growing in Education&lt;/td&gt;&lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;h2&gt;Nepal&#39;s IT Landscape: Opportunities and Challenges&lt;/h2&gt;
  &lt;p&gt;Nepal&#39;s IT sector thrives under Digital Nepal Framework, targeting a digital hub by 2030 with robust infrastructure. In-demand skills include full-stack development (Laravel/React), DevOps, and Flutter for mobile. Government e-governance and private digital transformation fuel 20% annual growth.&lt;/p&gt;[12][4][3]
  &lt;p&gt;Challenges like skill gaps and brain drain persist, but international collaborations bring investment. Top careers: software devs (NPR 1-2L/month), cloud engineers, cybersecurity pros. Freelancing on Upwork surges with AI/blockchain expertise.&lt;/p&gt;[4][11]
  
  &lt;h2&gt;Emerging Tech: Blockchain, IoT, and Edge Computing&lt;/h2&gt;
  &lt;p&gt;Beyond AI/cloud, blockchain secures transactions in Nepal&#39;s fintech, while IoT powers smart cities. Edge computing complements 5G for low-latency apps in logistics. Quantum threats loom, pushing post-quantum cryptography research.&lt;/p&gt;[6][4]
  &lt;ul&gt;
    &lt;li&gt;Headless CMS for multi-language sites.&lt;/li&gt;[3]
    &lt;li&gt;Microservices for scalable FinTech.&lt;/li&gt;[3]
    &lt;li&gt;Redis for caching in high-traffic apps.&lt;/li&gt;[3]
  &lt;/ul&gt;
  
  &lt;h2&gt;SEO Strategies for IT Content in 2026&lt;/h2&gt;
  &lt;p&gt;Optimize for &quot;IT trends Nepal 2026&quot; with mobile-first, voice search, and AI tools like SEMrush. EEAT signals boost rankings; focus on local SEO for Kathmandu businesses. Branded searches and YouTube integration drive traffic.&lt;/p&gt;[7][13]
  &lt;p&gt;Keyword clusters around high-intent queries like pricing, locations yield conversions. Track with Google Search Console for Nepal-specific insights.&lt;/p&gt;[5][7]
  
  &lt;h2&gt;Future Outlook: Skills and Investments&lt;/h2&gt;
  &lt;p&gt;2026 demands upskilling in TypeScript, Docker, and AI workflows. Nepal&#39;s vision includes AI diagnostics, digital KYC, and legal tech. Global M&amp;amp;A heats competition, favoring agile firms.&lt;/p&gt;[1][3]
  &lt;p&gt;Talent gaps widen, but training institutes bridge with certifications. Invest in cybersecurity hygiene to counter AI attacks.&lt;/p&gt;[10][4]
  
  &lt;blockquote class=&quot;post-quote&quot;&gt;
    &quot;Nepal shifts to a digital-first economy, creating unprecedented opportunities for IT professionals.&quot;[3]
  &lt;/blockquote&gt;
&lt;/section&gt;

&lt;footer class=&quot;post-footer&quot;&gt;
  &lt;div class=&quot;tags&quot;&gt;
    &lt;a class=&quot;tag&quot; href=&quot;#&quot;&gt;IT Trends 2026&lt;/a&gt;
    &lt;a class=&quot;tag&quot; href=&quot;#&quot;&gt;AI Cybersecurity&lt;/a&gt;
    &lt;a class=&quot;tag&quot; href=&quot;#&quot;&gt;Cloud Computing&lt;/a&gt;
    &lt;a class=&quot;tag&quot; href=&quot;#&quot;&gt;Nepal IT Jobs&lt;/a&gt;
  &lt;/div&gt;
  &lt;div class=&quot;share-buttons&quot;&gt;
    &lt;button class=&quot;share-btn&quot; onclick=&quot;sharePost(&#39;twitter&#39;)&quot;&gt;Twitter&lt;/button&gt;
    &lt;button class=&quot;share-btn&quot; onclick=&quot;sharePost(&#39;facebook&#39;)&quot;&gt;Facebook&lt;/button&gt;
    &lt;button class=&quot;share-btn&quot; onclick=&quot;sharePost(&#39;linkedin&#39;)&quot;&gt;LinkedIn&lt;/button&gt;
  &lt;/div&gt;
&lt;/footer&gt;
&lt;style&gt; /* Custom CSS mimicking ankitchamlagain.com.np clean tech blog style */ .blog-post-container { max-width: 800px; margin: 0 auto; padding: 20px; font-family: &#39;Segoe UI&#39;, Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: #333; background: #fff; } .post-article { box-shadow: 0 4px 12px rgba(0,0,0,0.1); border-radius: 12px; overflow: hidden; } .post-header { background: linear-gradient(135deg, #007bff, #0056b3); color: white; padding: 40px 30px; text-align: center; } .post-title { font-size: 2.2em; margin: 0 0 15px; font-weight: 700; } .post-meta { display: flex; justify-content: center; gap: 20px; font-size: 0.95em; opacity: 0.95; } .post-featured-image { width: 100%; height: 300px; overflow: hidden; } .post-featured-image img { width: 100%; height: 100%; object-fit: cover; } .post-content { padding: 40px 30px; } .post-content h2 { color: #007bff; font-size: 1.8em; margin-top: 35px; border-bottom: 3px solid #e9ecef; padding-bottom: 10px; } .post-content p { margin-bottom: 20px; text-align: justify; } .post-content ul { list-style: none; padding-left: 0; } .post-content ul li { padding: 10px 0; position: relative; padding-left: 25px; } .post-content ul li::before { content: &quot;→&quot;; position: absolute; left: 0; color: #007bff; font-weight: bold; } .trend-comparison { width: 100%; border-collapse: collapse; margin: 25px 0; background: #f8f9fa; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 8px rgba(0,0,0,0.1); } .trend-comparison th, .trend-comparison td { padding: 15px; text-align: left; border-bottom: 1px solid #dee2e6; } .trend-comparison th { background: #007bff; color: white; font-weight: 600; } .trend-comparison tr:hover { background: #e3f2fd; } .post-quote { background: #e3f2fd; border-left: 5px solid #007bff; padding: 25px; font-style: italic; font-size: 1.1em; margin: 30px 0; } .post-footer { padding: 30px; background: #f8f9fa; border-top: 1px solid #dee2e6; } .tags { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 20px; } .tag { background: #007bff; color: white; padding: 8px 16px; text-decoration: none; border-radius: 20px; font-size: 0.9em; transition: background 0.3s; } .tag:hover { background: #0056b3; } .share-buttons { display: flex; gap: 10px; } .share-btn { background: #28a745; color: white; border: none; padding: 10px 20px; border-radius: 6px; cursor: pointer; transition: background 0.3s; } .share-btn:hover { background: #218838; } @media (max-width: 768px) { .post-meta { flex-direction: column; gap: 10px; } .post-content { padding: 25px 20px; } .trend-comparison th, .trend-comparison td { padding: 10px; font-size: 0.9em; } } &lt;/style&gt; &lt;script&gt; function sharePost(platform) { const url = window.location.href; const title = &quot;Top IT Industry Trends 2026: AI, Cybersecurity, Cloud &amp; Nepal Opportunities&quot;; let shareUrl; if (platform === &#39;twitter&#39;) { shareUrl = `https://twitter.com/intent/tweet?url=${encodeURIComponent(url)}&amp;text=${encodeURIComponent(title)}`; } else if (platform === &#39;facebook&#39;) { shareUrl = `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`; } else if (platform === &#39;linkedin&#39;) { shareUrl = `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(url)}`; } window.open(shareUrl, &#39;_blank&#39;, &#39;width=600,height=400&#39;); } &lt;/script&gt;
&lt;/article&gt;&lt;/div&gt;</description><link>https://www.ankitchamlagain.com.np/2026/01/top-it-industry-trends-2026-ai.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/a/AVvXsEjbdHoaIeQ710BUP_xyzgfBzVRC2tqbfqXWPwOsLjVpZXjuwIKruURVAy5DVO9YjLwkekGcRYD2Y28sor7VohYXVQ-7q70vfeFLgTRDcVSAa73T3JmkGAH87V2oRa3uL9WE5ubpL_JLh-NkG0PeP1awIYHJOGc7QwekVgCGOaifkYbhlcmlkFy6K7oIe2c=s72-w400-h219-c" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-4438692834927344343</guid><pubDate>Sun, 06 Apr 2025 05:38:00 +0000</pubDate><atom:updated>2026-01-23T11:20:02.203+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">asp dot net</category><category domain="http://www.blogger.com/atom/ns#">asp.net</category><category domain="http://www.blogger.com/atom/ns#">c#</category><title>Unit testing in C#</title><description>&lt;h2 data-end=&quot;288&quot; data-start=&quot;215&quot;&gt;&lt;br /&gt;Unit Testing for .NET Developers: Boost Your Skills and Squash Bugs&lt;/h2&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi1dyxP3LHdj-x9k2rxpqc6XiyRnkhPaQHckxw3CbepX_-oHkVqdrvVMH4rCdiQglksnTbUWdUkahbXeBJO_nJUVZo7vV20b1OkSRlPBZE_zXJqoQ5sdKr_GBvQ9gyKJyAtacnobF6qB9FCrYk0y3us6YWt-3xVTuX6mZz5psWKA1i5ZbEWd_yffb0HHs4&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;unit testing c# image&quot; data-original-height=&quot;768&quot; data-original-width=&quot;1408&quot; height=&quot;218&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi1dyxP3LHdj-x9k2rxpqc6XiyRnkhPaQHckxw3CbepX_-oHkVqdrvVMH4rCdiQglksnTbUWdUkahbXeBJO_nJUVZo7vV20b1OkSRlPBZE_zXJqoQ5sdKr_GBvQ9gyKJyAtacnobF6qB9FCrYk0y3us6YWt-3xVTuX6mZz5psWKA1i5ZbEWd_yffb0HHs4=w400-h218&quot; title=&quot;unit testing c# image&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;
&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Are you tired of shipping bugs to production? Want to feel confident when changing &lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=define+legacy+code&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;legacy code&lt;/a&gt;? &lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=define+unit+testing&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;Unit testing&lt;/a&gt; is your secret weapon. This guide walks you through the basics with real examples in .NET using &lt;strong data-end=&quot;504&quot; data-start=&quot;495&quot;&gt;XUnit&lt;/strong&gt;, so you can write reliable code faster.&lt;/h3&gt;&lt;h2 data-end=&quot;602&quot; data-start=&quot;551&quot;&gt;🧠 What is Unit Testing and Why Should You Care?&lt;/h2&gt;&lt;p data-end=&quot;544&quot; data-start=&quot;290&quot;&gt;
&lt;/p&gt;&lt;p data-end=&quot;727&quot; data-start=&quot;604&quot;&gt;Unit testing is about testing small parts of your code in isolation. Think of it like testing an engine one part at a time.&lt;/p&gt;&lt;h3 data-end=&quot;764&quot; data-start=&quot;734&quot;&gt;✅ Benefits of Unit Testing&lt;/h3&gt;&lt;p data-end=&quot;727&quot; data-start=&quot;604&quot;&gt;
&lt;/p&gt;&lt;ul data-end=&quot;880&quot; data-start=&quot;765&quot;&gt;
&lt;li data-end=&quot;790&quot; data-start=&quot;765&quot;&gt;
&lt;strong data-end=&quot;790&quot; data-start=&quot;767&quot;&gt;Prevents bugs early&lt;/strong&gt;
&lt;/li&gt;
&lt;li data-end=&quot;818&quot; data-start=&quot;791&quot;&gt;
&lt;strong data-end=&quot;818&quot; data-start=&quot;793&quot;&gt;Improves code quality&lt;/strong&gt;
&lt;/li&gt;
&lt;li data-end=&quot;846&quot; data-start=&quot;819&quot;&gt;
&lt;strong data-end=&quot;846&quot; data-start=&quot;821&quot;&gt;Acts as documentation&lt;/strong&gt;
&lt;/li&gt;
&lt;li data-end=&quot;880&quot; data-start=&quot;847&quot;&gt;
&lt;strong data-end=&quot;880&quot; data-start=&quot;849&quot;&gt;Supports refactoring safely&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;blockquote data-end=&quot;926&quot; data-start=&quot;882&quot;&gt;&lt;p data-end=&quot;926&quot; data-start=&quot;884&quot;&gt;✨ &lt;em data-end=&quot;926&quot; data-start=&quot;886&quot;&gt;Example: A math error caught by a test&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;1114&quot; data-start=&quot;927&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;[&lt;span class=&quot;hljs-meta&quot;&gt;Fact&lt;/span&gt;]
&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Divide_ByZero_ShouldThrowException&lt;/span&gt;()
{
    &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; calculator = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; Calculator();

    Assert.Throws&amp;lt;DivideByZeroException&amp;gt;(() =&amp;gt; calculator.Divide(&lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;));
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;hr data-end=&quot;1119&quot; data-start=&quot;1116&quot; /&gt;
&lt;h2 data-end=&quot;1173&quot; data-start=&quot;1121&quot;&gt;🛠️ Setting Up Your .NET Unit Testing Environment&lt;/h2&gt;&lt;/div&gt;&lt;div&gt;&lt;ol data-end=&quot;1330&quot; data-start=&quot;1175&quot;&gt;&lt;li data-end=&quot;1203&quot; data-start=&quot;1175&quot;&gt;&lt;p data-end=&quot;1203&quot; data-start=&quot;1178&quot;&gt;&lt;strong data-end=&quot;1203&quot; data-start=&quot;1178&quot;&gt;&lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=install+Visual+Studio&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;Install Visual Studio&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;1257&quot; data-start=&quot;1204&quot;&gt;
&lt;p data-end=&quot;1257&quot; data-start=&quot;1207&quot;&gt;&lt;strong data-end=&quot;1236&quot; data-start=&quot;1207&quot;&gt;&lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=create+new+test+project+Visual+Studio&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;Create a new Test Project&lt;/a&gt;&lt;/strong&gt;: &lt;code data-end=&quot;1257&quot; data-start=&quot;1238&quot;&gt;&lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=DemoLibrary.Tests+example&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;DemoLibrary.Tests&lt;/a&gt;&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;1330&quot; data-start=&quot;1258&quot;&gt;
&lt;p data-end=&quot;1284&quot; data-start=&quot;1261&quot;&gt;&lt;strong data-end=&quot;1283&quot; data-start=&quot;1261&quot;&gt;&lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=add+NuGet+packages+Visual+Studio&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;Add NuGet packages&lt;/a&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;/li&gt;&lt;ul data-end=&quot;1330&quot; data-start=&quot;1288&quot;&gt;
&lt;li data-end=&quot;1297&quot; data-start=&quot;1288&quot;&gt;
&lt;code data-end=&quot;1297&quot; data-start=&quot;1290&quot;&gt;&lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=xunit+unit+testing+framework&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;xunit&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li data-end=&quot;1330&quot; data-start=&quot;1301&quot;&gt;
&lt;code data-end=&quot;1330&quot; data-start=&quot;1303&quot;&gt;&lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=xunit.runner.visualstudio+NuGet+package&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;xunit.runner.visualstudio&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;/ol&gt;&lt;div&gt;&lt;h2 data-end=&quot;1393&quot; data-start=&quot;1337&quot;&gt;🤖 Writing Your First Unit Tests: A Practical Example&lt;/h2&gt;
&lt;p data-end=&quot;1432&quot; data-start=&quot;1395&quot;&gt;Suppose you have a simple calculator:&lt;/p&gt;
&lt;h3 data-end=&quot;1453&quot; data-start=&quot;1434&quot;&gt;&lt;code data-end=&quot;1453&quot; data-start=&quot;1438&quot;&gt;Calculator.cs&lt;/code&gt;&lt;/h3&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;1585&quot; data-start=&quot;1454&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Calculator&lt;/span&gt;
{
    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Add&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;&lt;/span&gt; a, &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; b) =&amp;gt; a + b;

    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Divide&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;&lt;/span&gt; a, &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; b) =&amp;gt; a / b;
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;h3 data-end=&quot;1611&quot; data-start=&quot;1587&quot;&gt;&lt;code data-end=&quot;1611&quot; data-start=&quot;1591&quot;&gt;CalculatorTests.cs&lt;/code&gt;&lt;/h3&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;1892&quot; data-start=&quot;1612&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;CalculatorTests&lt;/span&gt;
{
    [&lt;span class=&quot;hljs-meta&quot;&gt;Fact&lt;/span&gt;]
    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Add_SimpleValuesShouldCalculate&lt;/span&gt;()
    {
        &lt;span class=&quot;hljs-comment&quot;&gt;// Arrange&lt;/span&gt;
        &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; calc = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; Calculator();

        &lt;span class=&quot;hljs-comment&quot;&gt;// Act&lt;/span&gt;
        &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; result = calc.Add(&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;);

        &lt;span class=&quot;hljs-comment&quot;&gt;// Assert&lt;/span&gt;
        Assert.Equal(&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;, result);
    }
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;hr data-end=&quot;1897&quot; data-start=&quot;1894&quot; /&gt;
&lt;h2 data-end=&quot;1958&quot; data-start=&quot;1899&quot;&gt;&lt;br /&gt;&lt;/h2&gt;&lt;div&gt;&lt;h2 data-end=&quot;1958&quot; data-start=&quot;1899&quot;&gt;📊 Using Theories and InlineData for Parameterized Tests&lt;/h2&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;2321&quot; data-start=&quot;1960&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;CalculatorTheoryTests&lt;/span&gt;
{
    [&lt;span class=&quot;hljs-meta&quot;&gt;Theory&lt;/span&gt;]
    [&lt;span class=&quot;hljs-meta&quot;&gt;InlineData(2, 2, 4)&lt;/span&gt;]
    [&lt;span class=&quot;hljs-meta&quot;&gt;InlineData(0, 0, 0)&lt;/span&gt;]
    [&lt;span class=&quot;hljs-meta&quot;&gt;InlineData(-1, 1, 0)&lt;/span&gt;]
    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Add_VariousInputs_ShouldReturnExpectedResult&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;&lt;/span&gt; a, &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; b, &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; expected)
    {
        &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; calc = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; Calculator();

        &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; result = calc.Add(a, b);

        Assert.Equal(expected, result);
    }
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;hr data-end=&quot;2326&quot; data-start=&quot;2323&quot; /&gt;
&lt;h2 data-end=&quot;2353&quot; data-start=&quot;2328&quot;&gt;🚧 Handling Edge Cases&lt;/h2&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;2595&quot; data-start=&quot;2355&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;[&lt;span class=&quot;hljs-meta&quot;&gt;Fact&lt;/span&gt;]
&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Add_MaxIntValues_ShouldOverflow&lt;/span&gt;()
{
    &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; calc = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; Calculator();
    
    Assert.Throws&amp;lt;OverflowException&amp;gt;(() =&amp;gt;
    {
        checked
        {
            calc.Add(&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;.MaxValue, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);
        }
    });
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;hr data-end=&quot;2600&quot; data-start=&quot;2597&quot; /&gt;
&lt;h2 data-end=&quot;2630&quot; data-start=&quot;2602&quot;&gt;⚠️ Testing for Exceptions&lt;/h2&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;2890&quot; data-start=&quot;2632&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;[&lt;span class=&quot;hljs-meta&quot;&gt;Fact&lt;/span&gt;]
&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Divide_ByZero_ShouldThrowDivideByZeroException&lt;/span&gt;()
{
    &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; calc = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; Calculator();

    &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; ex = Assert.Throws&amp;lt;DivideByZeroException&amp;gt;(() =&amp;gt; calc.Divide(&lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;));
    Assert.Equal(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Attempted to divide by zero.&quot;&lt;/span&gt;, ex.Message);
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;hr data-end=&quot;2895&quot; data-start=&quot;2892&quot; /&gt;
&lt;h2 data-end=&quot;2944&quot; data-start=&quot;2897&quot;&gt;🧱 Testing Complex Code: Data Access Example&lt;/h2&gt;
&lt;h3 data-end=&quot;2971&quot; data-start=&quot;2946&quot;&gt;&lt;code data-end=&quot;2971&quot; data-start=&quot;2950&quot;&gt;PersonRepository.cs&lt;/code&gt;&lt;/h3&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;3311&quot; data-start=&quot;2972&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;&lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=PersonRepository+unit+testing+example&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;PersonRepository&lt;/a&gt;&lt;/span&gt;
{
    &lt;span class=&quot;hljs-keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;readonly&lt;/span&gt; &lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=IFileService+unit+testing+example&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;IFileService&lt;/a&gt; _fileService;

    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;PersonRepository&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;IFileService fileService&lt;/span&gt;)
    {
        _fileService = fileService;
    }

    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;SavePerson&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt;&lt;/span&gt; name)
    {
        &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; line = &lt;span class=&quot;hljs-string&quot;&gt;$&quot;Name: &lt;span class=&quot;hljs-subst&quot;&gt;{name}&lt;/span&gt;&lt;/span&gt;&quot;;
        _fileService.WriteLine(&lt;span class=&quot;hljs-string&quot;&gt;&quot;people.txt&quot;&lt;/span&gt;, line);
    }
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;h3 data-end=&quot;3334&quot; data-start=&quot;3313&quot;&gt;&lt;code data-end=&quot;3334&quot; data-start=&quot;3317&quot;&gt;IFileService.cs&lt;/code&gt;&lt;/h3&gt;&lt;/div&gt;&lt;div&gt;&lt;code data-end=&quot;3334&quot; data-start=&quot;3317&quot;&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;3432&quot; data-start=&quot;3335&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;IFileService&lt;/span&gt;
{
    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;WriteLine&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt;&lt;/span&gt; fileName, &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; line);
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;h3 data-end=&quot;3452&quot; data-start=&quot;3434&quot;&gt;Test using Moq&lt;/h3&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;3776&quot; data-start=&quot;3453&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;[&lt;span class=&quot;hljs-meta&quot;&gt;Fact&lt;/span&gt;]
&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;SavePerson_ShouldCallWriteLineWithCorrectArguments&lt;/span&gt;()
{
    &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; mockFileService = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; Mock&amp;lt;IFileService&amp;gt;();
    &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; repo = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; PersonRepository(mockFileService.Object);

    repo.SavePerson(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Ankit&quot;&lt;/span&gt;);

    mockFileService.Verify(fs =&amp;gt; fs.WriteLine(&lt;span class=&quot;hljs-string&quot;&gt;&quot;people.txt&quot;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;Name: Ankit&quot;&lt;/span&gt;), Times.Once);
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;hr data-end=&quot;3781&quot; data-start=&quot;3778&quot; /&gt;
&lt;h2 data-end=&quot;3829&quot; data-start=&quot;3783&quot;&gt;📐 Refactoring for Testability: SRP Example&lt;/h2&gt;
&lt;p data-end=&quot;3847&quot; data-start=&quot;3831&quot;&gt;Instead of this:&lt;/p&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;3936&quot; data-start=&quot;3848&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;AddNewPerson&lt;/span&gt;()
{
    &lt;span class=&quot;hljs-comment&quot;&gt;// Fetch, transform, save — all in one!&lt;/span&gt;
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;p data-end=&quot;3946&quot; data-start=&quot;3938&quot;&gt;Do this:&lt;/p&gt;&lt;pre class=&quot;overflow-visible!&quot; data-end=&quot;4049&quot; data-start=&quot;3947&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary&quot;&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;whitespace-pre! language-csharp&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;AddPersonToList&lt;/span&gt;() { &lt;span class=&quot;hljs-comment&quot;&gt;/* Logic */&lt;/span&gt; }
&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;ConvertToCSV&lt;/span&gt;() { &lt;span class=&quot;hljs-comment&quot;&gt;/* Logic */&lt;/span&gt; }
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;p data-end=&quot;4096&quot; data-start=&quot;4051&quot;&gt;Each method is now &lt;strong data-end=&quot;4095&quot; data-start=&quot;4070&quot;&gt;&lt;a data-preview=&quot;&quot; href=&quot;https://www.google.com/search?ved=1t:260882&amp;amp;q=define+testable+in+isolation&amp;amp;bbid=4611029784033708994&amp;amp;bpid=4438692834927344343&quot; target=&quot;_blank&quot;&gt;testable in isolation&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;hr data-end=&quot;4101&quot; data-start=&quot;4098&quot; /&gt;
&lt;h2 data-end=&quot;4121&quot; data-start=&quot;4103&quot;&gt;✅ Key Takeaways&lt;/h2&gt;&lt;h2 data-end=&quot;4121&quot; data-start=&quot;4103&quot;&gt;
&lt;ul data-end=&quot;4383&quot; data-start=&quot;4123&quot;&gt;
&lt;li data-end=&quot;4182&quot; data-start=&quot;4123&quot;&gt;
&lt;span style=&quot;font-family: arial; font-size: small; font-weight: normal;&quot;&gt;Unit testing = testing small, independent chunks of code.&lt;/span&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4235&quot; data-start=&quot;4183&quot;&gt;
&lt;span style=&quot;font-family: arial; font-size: small; font-weight: normal;&quot;&gt;Use &lt;code data-end=&quot;4196&quot; data-start=&quot;4189&quot;&gt;xUnit&lt;/code&gt; with &lt;code data-end=&quot;4222&quot; data-start=&quot;4202&quot;&gt;Arrange-Act-Assert&lt;/code&gt; for clarity.&lt;/span&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4269&quot; data-start=&quot;4236&quot;&gt;
&lt;span style=&quot;font-family: arial; font-size: small; font-weight: normal;&quot;&gt;Test edge cases and exceptions.&lt;/span&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4332&quot; data-start=&quot;4270&quot;&gt;
&lt;span style=&quot;font-family: arial; font-size: small; font-weight: normal;&quot;&gt;Refactor to apply &lt;span data-end=&quot;4331&quot; data-start=&quot;4290&quot;&gt;Single Responsibility Principle (SRP)&lt;/span&gt;.&lt;/span&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4383&quot; data-start=&quot;4333&quot;&gt;
&lt;span style=&quot;font-family: arial; font-size: small; font-weight: normal;&quot;&gt;Mock dependencies to test external interactions.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/h2&gt;&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description><link>https://www.ankitchamlagain.com.np/2025/04/unit-testing-in-c.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/a/AVvXsEi1dyxP3LHdj-x9k2rxpqc6XiyRnkhPaQHckxw3CbepX_-oHkVqdrvVMH4rCdiQglksnTbUWdUkahbXeBJO_nJUVZo7vV20b1OkSRlPBZE_zXJqoQ5sdKr_GBvQ9gyKJyAtacnobF6qB9FCrYk0y3us6YWt-3xVTuX6mZz5psWKA1i5ZbEWd_yffb0HHs4=s72-w400-h218-c" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-7770732341423940614</guid><pubDate>Sun, 06 Apr 2025 05:13:00 +0000</pubDate><atom:updated>2025-05-11T19:56:38.153+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">Network</category><title>Understanding Email Protocols: A Deep Dive into POP3, IMAP, and SMTP</title><description>&lt;h1&gt;Understanding POP3, IMAP, and SMTP: Your Complete Guide to Email Protocols&lt;/h1&gt;
&lt;p&gt;In the realm of email communication, the acronyms POP3, IMAP, and SMTP play a pivotal role in how we receive and send messages. While these terms may seem confusing at first, understanding them is crucial for anyone who interacts with email, whether for personal use or in a professional setting. In this guide, we will explore what each of these protocols means, how they function, and the best practices for using them effectively.&lt;/p&gt;
&lt;h2&gt;What is POP3?&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmnCCBToG6sYVcQ37sNhBN0_Lpu-8fqisy0mTtSbyiIMkjkVUfFxaPzbZ1D0K1a-mfgRbjzZw-ai4OJUn0ZxRwq-nc8_rBMnxZXXcjwzewLUSWWcmmTBx3oB_MvhE8Ee6SW9h1ZOfCA82CApyE9909Juwb1lx0-u0V7Y7QtDXNt5hpcEPKpokIJL9myA0/s1280/POP3.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;512&quot; data-original-width=&quot;1280&quot; height=&quot;128&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmnCCBToG6sYVcQ37sNhBN0_Lpu-8fqisy0mTtSbyiIMkjkVUfFxaPzbZ1D0K1a-mfgRbjzZw-ai4OJUn0ZxRwq-nc8_rBMnxZXXcjwzewLUSWWcmmTBx3oB_MvhE8Ee6SW9h1ZOfCA82CApyE9909Juwb1lx0-u0V7Y7QtDXNt5hpcEPKpokIJL9myA0/s320/POP3.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/h2&gt;
&lt;h3&gt;Definition and Functionality&lt;/h3&gt;
&lt;p&gt;POP3, or Post Office Protocol version 3, is one of the oldest protocols used for receiving email. It operates by allowing your email client — such as Microsoft Outlook, Mozilla Thunderbird, or Apple Mail — to communicate with your email server to download your messages. When you configure your email program to use POP3, these are the key features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Download and Store&lt;/strong&gt;: POP3 downloads emails from the server to your device and typically removes them from the server.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single-Device Access&lt;/strong&gt;: Once emails are downloaded, they reside on your local device. This means the master copy of your emails is on your personal computer, making it inaccessible from other devices.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;When to Use POP3&lt;/h3&gt;
&lt;p&gt;Although POP3 may not be as popular today, it&#39;s still beneficial in specific scenarios:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Limited Internet Access&lt;/strong&gt;: If you frequently find yourself without internet access and need to access your emails offline, POP3 is suitable as it stores your emails on your device.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single Device Use&lt;/strong&gt;: If you only use one computer to manage your email, POP3 can simplify the process since all emails will live locally.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;What is IMAP?&lt;/h2&gt;
&lt;h3&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjl0AjrOkKHTCGmf6C1q6KWaOsn29kd0d_WUSg19nmjoX4-nedlbaPpS9HxI3iF9RrzfaUAChG02WwwmxmcPrM_oPN11hdiEFMk3Rga4Nc1hyphenhyphene-6wvMLthIRQkVPpg__6ERMsQQJAANcR-1uoH2ZBZj2Uiiwl6VY2gUWiyRFkszEbH-Dz3OMeaDUtbCDqo/s2560/IMAP.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1440&quot; data-original-width=&quot;2560&quot; height=&quot;180&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjl0AjrOkKHTCGmf6C1q6KWaOsn29kd0d_WUSg19nmjoX4-nedlbaPpS9HxI3iF9RrzfaUAChG02WwwmxmcPrM_oPN11hdiEFMk3Rga4Nc1hyphenhyphene-6wvMLthIRQkVPpg__6ERMsQQJAANcR-1uoH2ZBZj2Uiiwl6VY2gUWiyRFkszEbH-Dz3OMeaDUtbCDqo/s320/IMAP.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Definition and Functionality&lt;/h3&gt;
&lt;p&gt;IMAP, or Internet Message Access Protocol, is often seen as the more modern alternative to POP3. Unlike POP3, IMAP allows users to access and manage their email directly on the server. Here are the key features of IMAP:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Master Copy on Server&lt;/strong&gt;: IMAP keeps a master copy of your emails on the server, enabling easy access from multiple devices simultaneously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Synchronized Management&lt;/strong&gt;: Any actions you perform — reading, deleting, or marking emails as read — are synchronized across all devices. This ensures that your inbox remains consistent, regardless of where you check it.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;When to Use IMAP&lt;/h3&gt;
&lt;p&gt;In the current digital landscape, IMAP is often the preferred choice for email management:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Multiple Devices&lt;/strong&gt;: If you access your email from a smartphone, tablet, and computer, IMAP ensures all devices reflect the same information.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Collaboration&lt;/strong&gt;: IMAP is ideal for teams or individuals who need to keep their communications in sync across devices, allowing for smooth collaboration.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;What is SMTP?&lt;/h2&gt;
&lt;h3&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuj2oMWQZdweL0iO8eLnJIBFNT9nrAkOy2PoFMyulyfUf5sRRerxhflK3FI0Ep3gckirVETWiKNk9fZMuOPCop5Mn12diXixFQKRt5osG6pTw5FSX8J5qKgWw96x4mDkJmuLXuSU2DU5YdaZy8ijdaDjJW-ZH7JD1q5dx_QCI3vt1A-hZeg7_rx1SXWLo/s1844/SMTP.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1221&quot; data-original-width=&quot;1844&quot; height=&quot;212&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuj2oMWQZdweL0iO8eLnJIBFNT9nrAkOy2PoFMyulyfUf5sRRerxhflK3FI0Ep3gckirVETWiKNk9fZMuOPCop5Mn12diXixFQKRt5osG6pTw5FSX8J5qKgWw96x4mDkJmuLXuSU2DU5YdaZy8ijdaDjJW-ZH7JD1q5dx_QCI3vt1A-hZeg7_rx1SXWLo/s320/SMTP.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Definition and Functionality&lt;/h3&gt;
&lt;p&gt;While POP3 and IMAP focus on the reception of emails, SMTP, or Simple Mail Transfer Protocol, is responsible for sending emails. Here are some important aspects of SMTP:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sending Emails&lt;/strong&gt;: SMTP is the go-to protocol for your email client to transfer outgoing emails to the server, which then forwards the message to the intended recipient.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-Hop Delivery&lt;/strong&gt;: SMTP doesn’t just send emails from your device to the email server; it also handles the transfer between server and server until the email reaches its final destination.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;The Importance of SMTP&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Foundation of Email&lt;/strong&gt;: SMTP is fundamental to the email ecosystem. Without it, the act of sending emails across the internet would not be possible.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Comparing POP3, IMAP, and SMTP&lt;/h2&gt;
&lt;p&gt;To further clarify how each of these protocols functions, here’s a quick comparison:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;POP3&lt;/th&gt;
&lt;th&gt;IMAP&lt;/th&gt;
&lt;th&gt;SMTP&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Function&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Retrieves email&lt;/td&gt;
&lt;td&gt;Accesses server-stored email&lt;/td&gt;
&lt;td&gt;Sends email&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Storage&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Local on device&lt;/td&gt;
&lt;td&gt;Server-stored&lt;/td&gt;
&lt;td&gt;N/A&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Multi-Device&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Limited (single device)&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;N/A&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Synchronization&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No (static post-download state)&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;N/A&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2&gt;Email Access through Web Mail&lt;/h2&gt;
&lt;p&gt;One notable point is that webmail services, such as Gmail or Outlook.com, typically use a different method for accessing email than traditional email clients:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Master Copy Access&lt;/strong&gt;: When you log in to webmail, you essentially access the master copy of your email on their servers, commonly using IMAP.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Behind-the-Scenes&lt;/strong&gt;: Users may not be aware, but webmail services leverage both IMAP and SMTP for retrieving and sending messages. For example, they might use POP3 to fetch email from other accounts, integrating them into your main webmail interface.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Understanding the differences between POP3, IMAP, and SMTP is essential for anyone looking to configure their email effectively. While POP3 can still serve specific needs, IMAP is generally the preferred method for managing email across multiple devices, especially in today&#39;s mobile-first world. Meanwhile, SMTP serves as the backbone for sending emails.&lt;/p&gt;
&lt;p&gt;With this knowledge, you can choose the right setup that best fits your email usage habits and ensures a seamless experience across all your devices. For more insights on technology and computing, be sure to visit AskLeo.com!&lt;/p&gt;
&lt;p&gt;Ready to make better decisions about your email setup? Consider the advantages of IMAP over POP3 for a more synchronized experience across all your devices!&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/04/understanding-email-protocols.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmnCCBToG6sYVcQ37sNhBN0_Lpu-8fqisy0mTtSbyiIMkjkVUfFxaPzbZ1D0K1a-mfgRbjzZw-ai4OJUn0ZxRwq-nc8_rBMnxZXXcjwzewLUSWWcmmTBx3oB_MvhE8Ee6SW9h1ZOfCA82CApyE9909Juwb1lx0-u0V7Y7QtDXNt5hpcEPKpokIJL9myA0/s72-c/POP3.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-171358931306260567</guid><pubDate>Sun, 06 Apr 2025 05:03:00 +0000</pubDate><atom:updated>2025-05-01T14:05:27.725+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">Network</category><title>How the Internet Works</title><description>&lt;h1&gt;&lt;br /&gt;Understanding the Internet: Fundamentals of How It Works&lt;/h1&gt;&lt;h1&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRqXyd6IvToQoz6EvZnchyJ0wa6AXpd733_y9ixnpo4Ev72fIHPGfwwksikpF9yTElkk3kHJqEmHkXt7UvSJSvy-X_fDNyNAvHQpcOtyjqjdhm8rtdRO5WwjlpSKr2MVhcpPZdSL0l85dNY0YG9iABKB611IyifPEjKBtv9ZxHxghRtD-RbC2RgFV3/s478/communication-technology-for-int.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;200&quot; data-original-width=&quot;478&quot; height=&quot;134&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRqXyd6IvToQoz6EvZnchyJ0wa6AXpd733_y9ixnpo4Ev72fIHPGfwwksikpF9yTElkk3kHJqEmHkXt7UvSJSvy-X_fDNyNAvHQpcOtyjqjdhm8rtdRO5WwjlpSKr2MVhcpPZdSL0l85dNY0YG9iABKB611IyifPEjKBtv9ZxHxghRtD-RbC2RgFV3/s320/communication-technology-for-int.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/h1&gt;
&lt;p&gt;In our hyper-connected world, the Internet plays a crucial role in our daily lives, enabling everything from social media exchanges to online streaming of live events. But have you ever wondered how a video travels from a server to your smartphone in seconds? Or how millions can watch a live sports event online simultaneously? This article dives into the intricacies of the Internet, explaining its essential components and how they work together to create a seamless digital experience.&lt;/p&gt;
&lt;h2&gt;The Evolution of the Internet&lt;/h2&gt;
&lt;p&gt;The Internet we know today is the result of decades of technological advancements and innovations. Here is a brief overview of its evolution:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;1960s:&lt;/strong&gt; The origin of the Internet began with ARPANET, a project funded by the U.S. Department of Defense.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;1970s:&lt;/strong&gt; The introduction of the TCP/IP protocol suite laid the foundation for modern Internet communication.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;1989:&lt;/strong&gt; The invention of the World Wide Web changed how we access and share information forever.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;2000s:&lt;/strong&gt; A surge in social media and mobile internet reshaped our interactions and connectivity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Present Day:&lt;/strong&gt; The rise of IoT (Internet of Things) and 5G technology is pushing the boundaries of connectivity and innovation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Key Components of the Internet&lt;/h2&gt;
&lt;p&gt;To fully understand how the Internet functions, we must examine its critical components, which can be divided into three main parts: &lt;strong&gt;the edge network, access networks, and the network core.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;1. Network Edge&lt;/h3&gt;
&lt;p&gt;The network edge consists of end systems or hosts—devices that connect to the Internet, such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Personal Devices:&lt;/strong&gt; Desktops, laptops, and smartphones.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Servers:&lt;/strong&gt; Powerful machines that host websites, stream videos, and deliver other Internet services.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These hosts are categorized into two types: &lt;strong&gt;clients&lt;/strong&gt; (devices used by individuals) and &lt;strong&gt;servers&lt;/strong&gt; (powerful machines handling data requests). Most servers today reside in large &lt;strong&gt;data centers&lt;/strong&gt; that contain thousands of interconnected servers, which serve as the backbone for various online services.&lt;/p&gt;
&lt;h3&gt;2. Access Networks&lt;/h3&gt;
&lt;p&gt;Access networks physically connect end systems to the first router on their path to other networks. There are three main types:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Home Access Networks:&lt;/strong&gt; Utilize technologies like DSL, cable, or fiber optic connections to connect households to the Internet.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Institutional Access Networks:&lt;/strong&gt; Designed for businesses and organizations, these networks use high-speed connections to handle large volumes of users and data-intensive applications.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mobile Access Networks:&lt;/strong&gt; These include cellular technologies such as &lt;strong&gt;4G&lt;/strong&gt; and &lt;strong&gt;5G&lt;/strong&gt;, which allow smartphones to connect to the Internet wirelessly, providing on-the-go access to online resources.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;3. Network Core&lt;/h3&gt;
&lt;p&gt;The network core is a mesh of packet routers and links that interconnect various systems across the Internet. &lt;strong&gt;Routers&lt;/strong&gt; play a crucial role by directing data packets from one network to another.&lt;/p&gt;
&lt;h3&gt;Packet Switching Explained&lt;/h3&gt;
&lt;p&gt;At the heart of the network core is a technology known as &lt;strong&gt;packet switching.&lt;/strong&gt; Unlike circuit switching, where a dedicated connection is maintained for the duration of a communication session, packet switching breaks data into smaller chunks called &lt;strong&gt;packets.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Each packet:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Contains a segment of the original data along with information about its source and destination.&lt;/li&gt;
&lt;li&gt;Is sent independently through the network, potentially taking different routes to reach its target.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once all packets arrive at their destination, they reassemble to form the complete message. This method has several advantages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It allows for efficient use of available bandwidth by handling multiple communications simultaneously.&lt;/li&gt;
&lt;li&gt;If one route is congested or fails, packets can be redirected through alternative paths.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Forwarding and Routing&lt;/h3&gt;
&lt;p&gt;Two essential functions within the network core are &lt;strong&gt;forwarding&lt;/strong&gt; and &lt;strong&gt;routing.&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Forwarding&lt;/strong&gt; involves moving an incoming packet from a router&#39;s input link to the correct output link based on a forwarding table.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Routing&lt;/strong&gt; is a more complex process that determines the path packets follow between source and destination across multiple networks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Routing Protocols&lt;/h3&gt;
&lt;p&gt;One of the most significant routing protocols is &lt;strong&gt;Border Gateway Protocol (BGP).&lt;/strong&gt; BGP enables different autonomous systems (large networks) to exchange routing information, helping routers dynamically choose the best paths for data transmission.&lt;/p&gt;
&lt;h2&gt;The Role of Protocols in Internet Communication&lt;/h2&gt;
&lt;p&gt;The efficient functioning of the Internet is governed by a set of standardized rules called &lt;strong&gt;protocols.&lt;/strong&gt; Protocols define how messages are formatted, the order of exchanges, and the expected responses between devices. Some common protocols include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;TCP (Transmission Control Protocol):&lt;/strong&gt; Ensures reliable, ordered delivery of data between applications. It handles packet segmentation, acknowledgments, and retransmissions of lost packets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IP (Internet Protocol):&lt;/strong&gt; Responsible for addressing and routing packets. Every device on the Internet has a unique IP address, which functions like a mailing address.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HTTP (Hypertext Transfer Protocol):&lt;/strong&gt; The foundation of the World Wide Web, defining how messages are formatted and transmitted between browsers and servers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;TCP/IP Stack: Organizing the Internet&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;TCP/IP stack&lt;/strong&gt;, a crucial framework for Internet communication, is often depicted as having four layers:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Application Layer:&lt;/strong&gt; Closest to the end user, it interacts with software applications (e.g., HTTP for web browsing).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transport Layer:&lt;/strong&gt; Ensures data is reliably transmitted between applications (e.g., TCP and UDP).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Network Layer:&lt;/strong&gt; Deals with addressing and routing packets (IPv4 and IPv6).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Link Layer:&lt;/strong&gt; Manages physical connections between devices on the same network segment.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Encapsulation and Decapsulation&lt;/h3&gt;
&lt;p&gt;Each layer in the TCP/IP model adds its own header information during the process called &lt;strong&gt;encapsulation&lt;/strong&gt; when data is sent, and removes it during &lt;strong&gt;decapsulation&lt;/strong&gt; when data is received. This layered approach makes the Internet robust and highly versatile, enabling varying technologies to work seamlessly together.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Understanding how the Internet works provides us with insight into the complex systems that power our digital experiences. From the evolution of networking technologies to the protocols that enable communication, each component plays a vital role in our interconnected world. With emerging technologies like 5G and the Internet of Things expanding the network capabilities, the future of Internet connectivity promises even greater innovation.&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/04/understaing-how-internet-works.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRqXyd6IvToQoz6EvZnchyJ0wa6AXpd733_y9ixnpo4Ev72fIHPGfwwksikpF9yTElkk3kHJqEmHkXt7UvSJSvy-X_fDNyNAvHQpcOtyjqjdhm8rtdRO5WwjlpSKr2MVhcpPZdSL0l85dNY0YG9iABKB611IyifPEjKBtv9ZxHxghRtD-RbC2RgFV3/s72-c/communication-technology-for-int.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-5757662129026398010</guid><pubDate>Sat, 05 Apr 2025 16:04:00 +0000</pubDate><atom:updated>2025-04-06T10:48:12.643+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">Network</category><title>The Request/Response Cycle of the Web</title><description>&lt;h1&gt;&lt;br /&gt;Understanding Web Server Functionality: The HTTP Request/Response Cycle&lt;/h1&gt;&lt;h1&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRGxri5ofm1QyKsaZSE16H_DO8YpESTk0u-6q7MU9tOgXtW2kw_9gVL0lmZEzxxpFgu_VlScA-oP-1To-av9iD2Z24ArzlN3TZIfXNdxkBFtXaW1-9IjbcbfOl41R5PkDWB0XTkuJq_9fY5QdVcNgHM_RcYDvOKaX1Ru-xia1ZNuOdIti18ON04OPq/s796/webserver.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;260&quot; data-original-width=&quot;796&quot; height=&quot;105&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRGxri5ofm1QyKsaZSE16H_DO8YpESTk0u-6q7MU9tOgXtW2kw_9gVL0lmZEzxxpFgu_VlScA-oP-1To-av9iD2Z24ArzlN3TZIfXNdxkBFtXaW1-9IjbcbfOl41R5PkDWB0XTkuJq_9fY5QdVcNgHM_RcYDvOKaX1Ru-xia1ZNuOdIti18ON04OPq/s320/webserver.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;/h1&gt;
&lt;p&gt;The world of web development can be complex and intimidating, especially if you&#39;re just beginning your journey. Understanding how web servers operate is pivotal for creating efficient websites and applications. A web server is more than just a piece of hardware; it is software designed to request and respond to users. This article delves into the fundamental concepts of web servers, particularly focusing on the HTTP request/response cycle and the distinction between static and dynamic routing.&lt;/p&gt;
&lt;h2&gt;What is a Web Server?&lt;/h2&gt;
&lt;p&gt;The term &quot;web server&quot; can be a bit misleading. While initially one might think of specific hardware, the reality is that almost any device with a network connection can function as a web server. Your personal laptop, a Raspberry Pi, or even a smartphone can serve this role. At its core, a web server is a software application that serves web content to clients, responding to requests sent via a transport protocol.&lt;/p&gt;
&lt;h2&gt;The HTTP Request/Response Cycle&lt;/h2&gt;
&lt;p&gt;To better understand how web servers function, let&#39;s explore the six primary actions they perform during the HTTP request/response cycle.&lt;/p&gt;
&lt;h3&gt;1. Listening for Requests&lt;/h3&gt;
&lt;p&gt;When a web server application is initiated, it enters a passive state, waiting for incoming requests from clients. This is akin to a customer service representative waiting for calls. Each web server listens on a specific port designated by the operating system, commonly either port 80 for HTTP or port 443 for HTTPS.&lt;/p&gt;
&lt;h3&gt;2. Handling the Incoming Request&lt;/h3&gt;
&lt;p&gt;Once a request arrives, the web server decodes the HTTP request. HTTP (Hypertext Transfer Protocol) serves as the language that web servers and web clients use to communicate. An HTTP request comprises several parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Start Line:&lt;/strong&gt; Specifies the request method (GET or POST), the target resource, and the HTTP version.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Headers:&lt;/strong&gt; Contain metadata about the request, such as the &lt;code&gt;User-Agent&lt;/code&gt; and &lt;code&gt;Host&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Body:&lt;/strong&gt; This is optional and typically included in POST requests, containing data submitted by the user.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;3. Processing the Request&lt;/h3&gt;
&lt;p&gt;Through routing, a web server determines which resource to send back in response to the request. This process can be divided into two categories:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Static Routing:&lt;/strong&gt; The web server serves files directly from the file system. For instance, if a request is made for an image located in a specific folder, the server retrieves and sends that file.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamic Routing:&lt;/strong&gt; For web applications that present personalized content (like user profiles or order statuses), the web server runs scripts (PHP, Python, etc.) that generate responses based on data from a database.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;4. Retrieving the Resource&lt;/h3&gt;
&lt;p&gt;After determining which content to respond with, the web server fetches the necessary files or data. For static resources, this could involve simply reading a file from its directory. For dynamic content, the server runs a script that queries a database for the requested information, processes it, and prepares for sending.&lt;/p&gt;
&lt;h3&gt;5. Sending the Response&lt;/h3&gt;
&lt;p&gt;The server doesn&#39;t just return files; it sends an HTTP response. This response mirrors the structure of an HTTP request but in reverse, containing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Status Line:&lt;/strong&gt; Indicates the status of the request (like 200 for success).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Headers:&lt;/strong&gt; Provide information about the response, such as the type of content being returned (HTML, JSON, etc.).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Body:&lt;/strong&gt; Contains the requested resource, whether that&#39;s HTML for a web page, an image, or API data.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;6. Closing the Connection&lt;/h3&gt;
&lt;p&gt;After delivering the response, the server either waits for another request or closes the connection, marking the end of the request/response cycle.&lt;/p&gt;
&lt;h2&gt;Static vs. Dynamic Routing&lt;/h2&gt;
&lt;p&gt;Understanding the difference between static and dynamic routing is crucial for developers.&lt;/p&gt;
&lt;h3&gt;Static Routing&lt;/h3&gt;
&lt;p&gt;Static routing is straightforward, and it&#39;s primarily about serving existing files. A simple HTML page can be hosted without additional overhead. This can make it economical and fast, especially for small static websites or landing pages. Services like GitHub Pages or Amazon S3 allow hosting static websites at minimal costs while handling high traffic volumes efficiently.&lt;/p&gt;
&lt;h3&gt;Dynamic Routing&lt;/h3&gt;
&lt;p&gt;Dynamic routing, on the other hand, involves generating content on the fly based on user input or other variables. For example, when checking the status of an order, a web application queries a database, retrieves the information, and constructs a response accordingly. The dynamic nature of these applications makes it essential to use a suitable programming structure like MVC (Model-View-Controller) which allows for organized routing and content generation.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;As the digital landscape continues to evolve, understanding the basic operations of web servers becomes ever more critical. Whether you are building static or dynamic websites, having a solid grasp of the HTTP request/response cycle and how various routing methods work is fundamental to your success as a developer.&lt;/p&gt;
&lt;p&gt;Curious to delve deeper into web server functionalities? Explore various web server technologies and frameworks to enhance your understanding further! Join communities, participate in discussions, and broaden your skills to stay ahead in this rapidly changing field.&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/04/understanding-web-server-functionality.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRGxri5ofm1QyKsaZSE16H_DO8YpESTk0u-6q7MU9tOgXtW2kw_9gVL0lmZEzxxpFgu_VlScA-oP-1To-av9iD2Z24ArzlN3TZIfXNdxkBFtXaW1-9IjbcbfOl41R5PkDWB0XTkuJq_9fY5QdVcNgHM_RcYDvOKaX1Ru-xia1ZNuOdIti18ON04OPq/s72-c/webserver.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-869912693669920088</guid><pubDate>Sun, 30 Mar 2025 14:08:00 +0000</pubDate><atom:updated>2025-04-06T10:48:22.462+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">Network</category><title>SSL and TLS</title><description>&lt;h1&gt;&lt;br /&gt;Understanding SSL and TLS: The Essential Guide to Data Security and Encryption&lt;/h1&gt;&lt;h1&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0-DC2bzH6kesNPJhXr3vmItR0YV3ezgM8aWtkEwr6c0YUKIc3nlMfFCqzTLv173zTT-pFxWAZQoxEVwT4t2arW3WkV6wI_cJBlyeNIdJEQ7rOvGsH2FWjDQiXd6-k8UHRK4r6ZtADo8EDhuyfpBzMBw4-5AwgClwxtQgSRGQXsXRMwWUgOS0-HpNr/s1200/SSL-TLC.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;600&quot; data-original-width=&quot;1200&quot; height=&quot;160&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0-DC2bzH6kesNPJhXr3vmItR0YV3ezgM8aWtkEwr6c0YUKIc3nlMfFCqzTLv173zTT-pFxWAZQoxEVwT4t2arW3WkV6wI_cJBlyeNIdJEQ7rOvGsH2FWjDQiXd6-k8UHRK4r6ZtADo8EDhuyfpBzMBw4-5AwgClwxtQgSRGQXsXRMwWUgOS0-HpNr/s320/SSL-TLC.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;/h1&gt;
&lt;p&gt;In today&#39;s digital landscape, data security is more critical than ever. With online transactions and sensitive information being shared frequently, understanding how SSL (Secure Sockets Layer) and TLS (Transport Layer Security) work is vital for everyone, especially web owners and users. This article breaks down SSL and TLS protocols, explaining their roles in secure online communications and how they protect sensitive data from hackers.&lt;/p&gt;
&lt;h2&gt;What is SSL and TLS?&lt;/h2&gt;
&lt;h3&gt;Overview of SSL and TLS&lt;/h3&gt;
&lt;p&gt;SSL and its successor TLS are cryptographic protocols designed to secure communications over a network. These protocols use encryption to protect data transmitted between clients (like web browsers) and servers, ensuring &lt;strong&gt;confidentiality&lt;/strong&gt;, &lt;strong&gt;integrity&lt;/strong&gt;, and &lt;strong&gt;authentication&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;The Transition from SSL to TLS&lt;/h3&gt;
&lt;p&gt;While SSL was the first protocol developed for these purposes, TLS is its modern version. Despite this adjustment, many still refer to SSL when discussing website security. The key should be understood as the general term that encompasses both protocols.&lt;/p&gt;
&lt;h2&gt;How SSL/TLS Works&lt;/h2&gt;
&lt;h3&gt;The Importance of Securing Sensitive Data&lt;/h3&gt;
&lt;p&gt;Consider your sensitive data, such as bank account details or login credentials. This information travels over the internet and may be intercepted if not protected adequately. To secure this data during transmission, SSL/TLS protocols work effectively.&lt;/p&gt;
&lt;h2&gt;Key Concepts in SSL/TLS&lt;/h2&gt;
&lt;h3&gt;1. &lt;strong&gt;Encryption&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What is Encryption?&lt;/strong&gt; Encryption is the process of converting plain text information into a coded format, which prevents unauthorized users from understanding it. SSL/TLS uses encryption to secure data during transmission.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Types of Encryption:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Symmetric Encryption:&lt;/strong&gt; The same key is used for both encryption and decryption. This method is faster and more efficient for large data transfers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Asymmetric Encryption:&lt;/strong&gt; Involves a pair of keys (public and private). The public key encrypts the data, and only the private key can decrypt it. This method is more secure but slower and is typically used for sharing the symmetric key.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;2. &lt;strong&gt;Hashing&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Hashing is crucial for maintaining data integrity during transmissions. This process converts data into a fixed-size string of characters through a hash function.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Message Authentication Code (MAC):&lt;/strong&gt; Hashing helps verify that the message remains unchanged during transmission by comparing the hash value at both the sending and receiving ends.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;3. &lt;strong&gt;SSL Certificates&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What are SSL Certificates?&lt;/strong&gt; These are digital certificates provided by Certificate Authorities (CAs) that verify the identity of a website. They ensure that users are communicating with a legitimate source.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Role of CAs:&lt;/strong&gt; CAs are trusted entities that issue SSL certificates and verify the information provided by the website owner.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Types of SSL Certificates:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Single Domain:&lt;/strong&gt; Covers one domain.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Wildcard:&lt;/strong&gt; Secures a primary domain and all its subdomains.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-Domain:&lt;/strong&gt; Covers multiple domains with one certificate.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;4. &lt;strong&gt;Public and Private Keys&lt;/strong&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;How They Work Together:&lt;/strong&gt; In the SSL handshake, the client uses the server&#39;s public key to encrypt information, which the server then decrypts using its private key. This process ensures secure communication between both parties.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;The SSL/TLS Handshake Process&lt;/h2&gt;
&lt;p&gt;Understanding the SSL/TLS handshake process is essential as it establishes a secure connection.&lt;/p&gt;
&lt;h3&gt;Steps in the Handshake:&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Client Hello:&lt;/strong&gt; The browser (client) sends a message to the server indicating its supported SSL/TLS versions and algorithms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Server Hello:&lt;/strong&gt; The server responds with its supported version and its identity certificate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Certificate Verification:&lt;/strong&gt; The client verifies the server&#39;s certificate against known CAs to ensure authenticity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Key Exchange:&lt;/strong&gt; A pre-master secret key is generated and securely communicated to the server.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Session Establishment:&lt;/strong&gt; Both parties generate session keys using the pre-master secret key, ensuring all data shared, post-handshake, is encrypted.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Real-World Applications of SSL/TLS&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Why SSL/TLS is Essential for Websites:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Secures sensitive transactions on websites, especially for e-commerce and banking.&lt;/li&gt;
&lt;li&gt;Builds trust with users; browsers display warnings on sites without SSL/TLS, discouraging visits.&lt;/li&gt;
&lt;li&gt;Helps protect sensitive information from potential breaches.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;SSL and TLS play an integral role in securing our online communications and protecting sensitive data. They ensure that every transaction, every login, and every data transfer is encrypted and secure from prying eyes. As the digital landscape continues to evolve, understanding these protocols becomes increasingly important for anyone who interacts with internet services.&lt;/p&gt;
&lt;p&gt;Stay informed and vigilant about your online security practices. Ensuring your website uses SSL/TLS protocols is crucial for protecting both your data and that of your users.&lt;/p&gt;
&lt;p&gt;For more information on how SSL/TLS can benefit your business and enhance your security measures, feel free to explore &lt;strong&gt;practical insights&lt;/strong&gt; and detailed guides tailored for you. Let&#39;s navigate the world of secure communications together!&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/ssl-and-tls.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0-DC2bzH6kesNPJhXr3vmItR0YV3ezgM8aWtkEwr6c0YUKIc3nlMfFCqzTLv173zTT-pFxWAZQoxEVwT4t2arW3WkV6wI_cJBlyeNIdJEQ7rOvGsH2FWjDQiXd6-k8UHRK4r6ZtADo8EDhuyfpBzMBw4-5AwgClwxtQgSRGQXsXRMwWUgOS0-HpNr/s72-c/SSL-TLC.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-7408838017424156144</guid><pubDate>Sun, 09 Mar 2025 16:45:00 +0000</pubDate><atom:updated>2025-04-06T10:47:54.524+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">Network</category><title>HTTP: The Journey from HTTP 1 to HTTP 3 Explained</title><description>&lt;h1&gt;Understanding the Evolution of HTTP: From HTTP 1 to HTTP 3&lt;/h1&gt;
&lt;img alt=&quot;Evolution of HTTP&quot; src=&quot;https://img.youtube.com/vi/UMwQjFzTQXw/maxresdefault.jpg&quot; /&gt;
&lt;p&gt;In the expansive realm of the internet, the Hypertext Transfer Protocol (HTTP) stands as a vital component of web communication. This article dives into the fascinating evolution of HTTP, tracking its journey from HTTP 1.0 to the latest HTTP 3. By understanding these advancements, we can appreciate how the web has become faster and more reliable, catering to our increasingly complex online needs.&lt;/p&gt;
&lt;h2&gt;What is HTTP?&lt;/h2&gt;
&lt;p&gt;HTTP, or Hypertext Transfer Protocol, is the protocol used for transferring hypertext requests and information on the World Wide Web. It was initially designed to facilitate the transfer of HTML files between web servers and browsers. Over the years, its functionality has expanded to support various web services including images, videos, APIs, and file transfers.&lt;/p&gt;
&lt;h2&gt;The Journey Through HTTP Versions&lt;/h2&gt;
&lt;h3&gt;HTTP 0.9 and HTTP 1.0: The Basics&lt;/h3&gt;
&lt;p&gt;Before delving into HTTP 1.1, it’s important to briefly mention HTTP 0.9, which was the first iteration introduced in 1991. HTTP 0.9 was simple and supported only the GET method to retrieve HTML documents without headers or status codes.&lt;/p&gt;
&lt;h4&gt;Key Features of HTTP 1.0:&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Introduction of Headers:&lt;/strong&gt; HTTP 1.0, launched in 1996, introduced HTTP headers and status codes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;New Methods:&lt;/strong&gt; Apart from GET, it included POST and HEAD methods, allowing users to send data to the server and check resource updates, respectively.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single Connection Per Request:&lt;/strong&gt; Each request required a new connection, leading to significant overhead.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;HTTP 1.1: Enhanced Efficiency&lt;/h3&gt;
&lt;p&gt;Released in 1997, HTTP 1.1 brought several improvements, which are still in use today:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Persistent Connections:&lt;/strong&gt; Reduced the overhead of establishing a new connection for each request, enhancing speed and efficiency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pipelining:&lt;/strong&gt; Browsers could send multiple requests without waiting for responses, significantly cutting down wait times.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Chunked Transfer Encoding:&lt;/strong&gt; Allowed servers to send data in smaller parts, thereby speeding up initial page rendering.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Improved Caching:&lt;/strong&gt; New headers such as Cache-Control enabled better cache management, reducing unnecessary transfers.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Challenges with HTTP 1.1&lt;/strong&gt;&lt;br /&gt;
Despite its advantages, HTTP 1.1 had drawbacks including head-of-line blocking, where a delayed response could hold up subsequent requests. This issue prompted developers to leverage workarounds, such as domain sharding and resource bundling, to enhance performance.&lt;/p&gt;
&lt;h3&gt;HTTP 2: A Paradigm Shift&lt;/h3&gt;
&lt;p&gt;In 2015, HTTP 2 was introduced to address the limitations of HTTP 1.1, focusing on optimization and performance:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Binary Framing Layer:&lt;/strong&gt; Unlike the text-based format of HTTP 1, HTTP 2 utilized a binary format, allowing messages to be divided into frames that could be handled more efficiently.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Request Multiplexing:&lt;/strong&gt; Multiple requests could be sent simultaneously over a single connection, completely alleviating head-of-line blocking issues.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stream Prioritization:&lt;/strong&gt; Developers could prioritize requests, ensuring that the most crucial resources were delivered faster.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Server Push:&lt;/strong&gt; This feature allowed servers to send multiple resources in anticipation of client requests, preparing web pages in a faster and more efficient manner.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While HTTP 2 significantly improved web performance and user experience, it still faced limitations characteristic of TCP, particularly under conditions of high latency.&lt;/p&gt;
&lt;h3&gt;HTTP 3: The Future of Web Communication&lt;/h3&gt;
&lt;p&gt;Standardized in 2022, HTTP 3 represents a significant leap forward by introducing QUIC (Quick UDP Internet Connections), which is fundamentally different from its predecessors:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Connectionless Protocol:&lt;/strong&gt; Built on UDP, QUIC eliminates the need for a connection establishment phase, reducing latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enhanced Multiplexing:&lt;/strong&gt; With HTTP 3, the traditional head-of-line blocking issue is eliminated, improving resource loading times on complex websites.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reduced Latency:&lt;/strong&gt; QUIC can support faster connection setups, which is particularly beneficial for mobile applications, enhancing user experience.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Seamless Network Changes:&lt;/strong&gt; HTTP 3 maintains connectivity even when switching between networks, such as from Wi-Fi to cellular, a crucial feature in today&#39;s mobile-dominated landscape.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Current HTTP Usage Trends&lt;/h3&gt;
&lt;p&gt;As of 2023, while HTTP 1.1 remains in widespread use (especially for simpler sites), HTTP 2 has seen considerable adoption, handling over 60% of web requests. Meanwhile, HTTP 3 is still emerging, with tech giants like Google and Cloudflare leading its adoption.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The evolution of HTTP from version 1.0 to the latest HTTP 3 is a testament to the web&#39;s ability to adapt to increasing demands for speed, reliability, and performance. Each iteration has built upon its predecessor, continually refining the way browsers and servers communicate. Understanding these developments not only informs best practices in web development but also guides future innovations in web technology.&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/http-journey-from-http-1-to-http-3.html</link><author>noreply@blogger.com (Pratik)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-76232729969558973</guid><pubDate>Sun, 09 Mar 2025 16:37:00 +0000</pubDate><atom:updated>2025-04-06T10:50:52.458+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">Network</category><title>How SSL Certificates Work</title><description>&lt;h1&gt;&lt;br /&gt;Understanding How SSL Certificates Work: A Guide to HTTPS Security&lt;/h1&gt;&lt;h1&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUMUBTfH_VND-dM2uyl0B_tnLd4ZDO3DTaZdEv8PJ99yMauuWB_h8zBgDZ4TZmWyJmAr_RJ9TL4vu-zOd9Y27zdW6x4b5dQx2bL4MGD6aBwGCRMrctgo0MtfwepXXTG-VBB7pHE-7mkhzJ0MtekHYRQbLRzg64M5lxdaz612ebuQef9gMBvVwUXLdj/s275/images.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;183&quot; data-original-width=&quot;275&quot; height=&quot;183&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUMUBTfH_VND-dM2uyl0B_tnLd4ZDO3DTaZdEv8PJ99yMauuWB_h8zBgDZ4TZmWyJmAr_RJ9TL4vu-zOd9Y27zdW6x4b5dQx2bL4MGD6aBwGCRMrctgo0MtfwepXXTG-VBB7pHE-7mkhzJ0MtekHYRQbLRzg64M5lxdaz612ebuQef9gMBvVwUXLdj/s1600/images.png&quot; width=&quot;275&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;/h1&gt;
&lt;p&gt;In today&#39;s digital world, security is paramount, especially when it comes to online communications. One of the key technologies that enable secure connections is the SSL (Secure Sockets Layer) certificate. This guide will walk you through the workings of SSL certificates and their significance in safeguarding data during transmission over the internet.&lt;/p&gt;
&lt;h2&gt;What is an SSL Certificate?&lt;/h2&gt;
&lt;p&gt;SSL certificates are digital certificates that authenticate the identity of a website and enable an encrypted connection. When you visit a website that uses HTTPS (HyperText Transfer Protocol Secure), the SSL certificate ensures that your connection to the server is secure, protecting any sensitive information you share.&lt;/p&gt;
&lt;h3&gt;Importance of SSL Certificates&lt;/h3&gt;
&lt;p&gt;SSL certificates serve several essential functions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Encryption&lt;/strong&gt;: They encrypt the data exchanged between your web browser and the server, preventing unauthorized access.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Authentication&lt;/strong&gt;: SSL certificates verify the legitimacy of the website, assuring users that they are communicating with the correct party.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Integrity&lt;/strong&gt;: They ensure that the data sent and received has not been tampered with during transmission.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;How Do SSL Certificates Work?&lt;/h2&gt;
&lt;p&gt;Understanding how SSL certificates work requires familiarity with a few basic concepts involving encryption and the networking process. Let&#39;s break it down step by step.&lt;/p&gt;
&lt;h3&gt;1. The Client and Server Interaction&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;When a user (client) tries to access a secure website, their browser sends a request to the server hosting that website.&lt;/li&gt;
&lt;li&gt;If the server is configured correctly, it sends back its SSL certificate along with the requested information.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;2. Establishing a Connection&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The client’s browser verifies the SSL certificate against a list of trusted certificate authorities (CAs). If the certificate is valid, it proceeds to establish a secure connection.&lt;/li&gt;
&lt;li&gt;If validation fails (e.g., the certificate is expired or self-signed), the browser will display a warning to the user.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;3. Encryption Process: Symmetric vs. Asymmetric&lt;/h3&gt;
&lt;p&gt;SSL uses a combination of symmetric and asymmetric encryption to securely exchange data.&lt;/p&gt;
&lt;h4&gt;Symmetric Encryption&lt;/h4&gt;
&lt;p&gt;In symmetric encryption, the same key is used for both encryption and decryption. However, securely sharing this key is a challenge as both the client and server need it.&lt;/p&gt;
&lt;h4&gt;Asymmetric Encryption&lt;/h4&gt;
&lt;p&gt;To address the issue of key exchange, SSL employs asymmetric encryption:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This involves two keys: a &lt;strong&gt;public key&lt;/strong&gt; that can be shared with anyone and a &lt;strong&gt;private key&lt;/strong&gt; that remains confidential to the server.&lt;/li&gt;
&lt;li&gt;When the client wants to send sensitive information, they first encrypt it using the server&#39;s public key. The server then decrypts it with its private key.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;4. The Role of Certificate Authorities&lt;/h3&gt;
&lt;p&gt;To ensure the authenticity of the public key, SSL certificates are issued by trusted third-party organizations known as Certificate Authorities (CAs). These authorities verify the identity of the certificate requestor before issuing a certificate. Here’s how it works:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The server generates a key pair (public and private) and requests a certificate from a CA.&lt;/li&gt;
&lt;li&gt;The CA verifies the server’s identity and issues an SSL certificate containing the server’s public key.&lt;/li&gt;
&lt;li&gt;When a user connects to the server, the CA&#39;s digital signature on the certificate helps the browser verify that the public key belongs to the legitimate server.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;5. Secure Communication with SSL&lt;/h3&gt;
&lt;p&gt;Once the secure connection is established:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The client can send encrypted data to the server using the public key.&lt;/li&gt;
&lt;li&gt;The server receives the encrypted data and decrypts it using its private key.&lt;/li&gt;
&lt;li&gt;This process ensures that even if a malicious actor intercepts the communication, they cannot decrypt the information without the private key.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;SSL certificates are crucial for secure online communications. They not only protect sensitive data through encryption but also provide authentication and ensure data integrity. As online threats grow more sophisticated, understanding and implementing SSL certificates becomes imperative for businesses and web developers alike.&lt;/p&gt;
&lt;p&gt;For anyone looking to secure their communications or the data of their clients, investing in a reputable SSL certificate and ensuring proper configuration is essential. Remember, when you browse the web, your online safety is only as strong as the connections you secure.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Stay informed about digital security!&lt;/strong&gt; If you found this article helpful or have further questions about SSL certificates and web security, feel free to leave your comments below. And don&#39;t forget to share this guide with your peers to help them understand the importance of SSL certificates in the digital landscape.&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/understanding-how-ssl-certificates-work.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUMUBTfH_VND-dM2uyl0B_tnLd4ZDO3DTaZdEv8PJ99yMauuWB_h8zBgDZ4TZmWyJmAr_RJ9TL4vu-zOd9Y27zdW6x4b5dQx2bL4MGD6aBwGCRMrctgo0MtfwepXXTG-VBB7pHE-7mkhzJ0MtekHYRQbLRzg64M5lxdaz612ebuQef9gMBvVwUXLdj/s72-c/images.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-4984060529962579368</guid><pubDate>Sun, 09 Mar 2025 16:22:00 +0000</pubDate><atom:updated>2025-04-06T10:52:16.506+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">Network</category><title>Understanding FTP: How File Transfers Power the Web</title><description>&lt;h1&gt;Understanding FTP: The Backbone of File Transfers&lt;/h1&gt;
&lt;img alt=&quot;Understanding FTP: The Backbone of File Transfers&quot; src=&quot;https://img.youtube.com/vi/kKxSiJPQ2kg/maxresdefault.jpg&quot; /&gt;
&lt;p&gt;In today&#39;s digital age, file transfers are integral to both personal and business operations. One method that has been a cornerstone in this process is the &lt;strong&gt;File Transfer Protocol (FTP)&lt;/strong&gt;. This article delves into how FTP works, its functionalities, and explores secure alternatives that can protect your data during transfer.&lt;/p&gt;
&lt;h2&gt;What is FTP?&lt;/h2&gt;
&lt;p&gt;FTP, or &lt;strong&gt;File Transfer Protocol&lt;/strong&gt;, is a standardized network protocol that facilitates the transfer of files between a client and a server over the internet. Operating at the &lt;strong&gt;application layer&lt;/strong&gt; of the OSI model, which is the seventh and topmost layer responsible for user interface and application-level functions, FTP provides essential rules and conventions to govern these connections.&lt;/p&gt;
&lt;h3&gt;How FTP Works&lt;/h3&gt;
&lt;p&gt;FTP relies on a client-server model. In this environment, the server acts as a &lt;strong&gt;repository&lt;/strong&gt; for files, while the client establishes connections to interact with it. Key operations, including downloading, uploading, deleting, and listing directories, can be performed using FTP.&lt;/p&gt;
&lt;h4&gt;Client and Server Connections&lt;/h4&gt;
&lt;p&gt;FTP operates exclusively over &lt;strong&gt;TCP&lt;/strong&gt; (Transmission Control Protocol), establishing two different types of connections between the client and server:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Control Connection&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The client initiates this connection on &lt;strong&gt;Port 21&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It&#39;s responsible for sending essential information such as user ID, password, and commands (like changing directories or requesting files).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Connection&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;After the control information is exchanged, the server opens a data connection to the client typically on &lt;strong&gt;Port 20&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;This connection is responsible for the actual transfer of files between the server and client.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Commands Used in FTP&lt;/h3&gt;
&lt;p&gt;To interact with the server effectively, FTP employs a set of commands. Some of the most common commands include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;GET&lt;/strong&gt;: Downloads files from the server.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PUT&lt;/strong&gt;: Uploads files to the server.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LS&lt;/strong&gt;: Lists directory contents.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CD&lt;/strong&gt;: Changes directories on the server.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;The Security Concerns of FTP&lt;/h2&gt;
&lt;p&gt;Despite its widespread usage, &lt;strong&gt;FTP is not secure&lt;/strong&gt;. One significant drawback is that it transmits data, including usernames and passwords, in &lt;strong&gt;plain text&lt;/strong&gt; over the Internet. This vulnerability opens the door for hackers to intercept sensitive information.&lt;/p&gt;
&lt;h3&gt;Secure Alternatives&lt;/h3&gt;
&lt;p&gt;To mitigate these security risks, it&#39;s crucial to consider encrypted alternatives to FTP. Two popular ones are &lt;strong&gt;SFTP&lt;/strong&gt; and &lt;strong&gt;FTPS&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;SFTP (SSH File Transfer Protocol)&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;SFTP&lt;/strong&gt; employs encryption to secure files during transfer.&lt;/li&gt;
&lt;li&gt;It generally uses one port, typically &lt;strong&gt;Port 22&lt;/strong&gt;, providing a more robust security framework than traditional FTP.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;FTPS (FTP Secure)&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;FTPS&lt;/strong&gt; enhances regular FTP by incorporating &lt;strong&gt;SSL (Secure Sockets Layer)&lt;/strong&gt; or &lt;strong&gt;TLS (Transport Layer Security)&lt;/strong&gt; encryption.&lt;/li&gt;
&lt;li&gt;It can utilize two ports—one for control and another for data—adding an extra layer of security to file transfers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;FTP has long been a reliable method for transferring files over the Internet, but its lack of security measures leaves data vulnerable to interception. For users who prioritize security, turning to alternatives like SFTP and FTPS is not just advised but essential. Understanding these protocols can help individuals and businesses securely manage their file transfers.&lt;/p&gt;
&lt;p&gt;If you found this overview informative, consider exploring more about secure file transfer protocols and enhancing your data protection strategies. Make sure to take steps towards safeguarding your digital information today!&lt;/p&gt;&lt;p&gt;We have discussed about SFTP and FTPS in our other article please check it out as well.&lt;/p&gt;
&lt;p&gt;Engage with us in the comments below! What are your thoughts on FTP vs. its secure alternatives? Have you had experience with secure transfers? Let’s discuss!&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/understanding-ftp-backbone-of-file.html</link><author>noreply@blogger.com (Pratik)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-5790393423281355633</guid><pubDate>Sun, 09 Mar 2025 16:20:00 +0000</pubDate><atom:updated>2025-04-06T10:53:04.075+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">Network</category><title>FTP, SFTP, and TFTP: Understanding the Core Protocols Behind File Transfers</title><description>&lt;h1&gt;Understanding FTP, SFTP, and TFTP: A Comprehensive Guide to File Transfer Protocols&lt;/h1&gt;
&lt;img src=&quot;https://img.youtube.com/vi/tOj8MSEIbfA/maxresdefault.jpg&quot; alt=&quot;Understanding FTP, SFTP, and TFTP: A Comprehensive Guide to File Transfer Protocols&quot;&gt;
&lt;p&gt;In our increasingly digital world, sharing files over networks is a common necessity for individuals and organizations. This is where file transfer protocols like FTP, SFTP, and TFTP come into play. Each of these protocols has distinct characteristics and use cases, making them suitable for different scenarios involving file transfers. This article dives into these protocols, explaining what they are, how they work, and when to use each one.&lt;/p&gt;
&lt;h2&gt;What is FTP?&lt;/h2&gt;
&lt;h3&gt;Definition and Purpose of FTP&lt;/h3&gt;
&lt;p&gt;FTP stands for File Transfer Protocol. It&#39;s a standard protocol utilized for the transfer of files between computers and servers over a network, most commonly the Internet. In essence, FTP serves as the language that computers rely on to communicate with one another to facilitate file transfers.&lt;/p&gt;
&lt;p&gt;For instance, individuals wanting to share files online can upload documents, images, or any other files to an FTP server. People from around the globe can then connect to this server via FTP and download the shared files effortlessly.&lt;/p&gt;
&lt;h3&gt;How to Use FTP&lt;/h3&gt;
&lt;p&gt;Transferring files using FTP can be accomplished in two ways:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Web Browser:&lt;/strong&gt; You can access an FTP server directly through a web browser. Just type in the server&#39;s address (for example, &lt;code&gt;ftp://example.com&lt;/code&gt;) into the URL bar just like you would with a standard website. This allows you to browse the server’s accessible folders and files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;FTP Client:&lt;/strong&gt; Alternatively, you can use an FTP client, which provides a more user-friendly graphical interface. Popular examples include FileZilla, which is a widely-used free FTP client. With an FTP client, you can easily manage your local files and server files by dragging and dropping.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Limitations of FTP&lt;/h3&gt;
&lt;p&gt;Despite its ease of use, FTP has a significant drawback: it is not a secure protocol. Data transferred over FTP is sent in clear text, exposing sensitive information to potential interception. Therefore, FTP should be used cautiously, ideally for non-sensitive data or within trusted networks.&lt;/p&gt;
&lt;h2&gt;What is SFTP?&lt;/h2&gt;
&lt;h3&gt;Definition and Enhanced Security of SFTP&lt;/h3&gt;
&lt;p&gt;SFTP stands for Secure File Transfer Protocol. As the name suggests, SFTP is built on the foundations of FTP but adds a crucial layer of security. Using secure shell (SSH) encryption during data transfer, SFTP ensures that no data is sent unencrypted, protecting sensitive information from eavesdroppers.&lt;/p&gt;
&lt;h3&gt;Authenticating with SFTP&lt;/h3&gt;
&lt;p&gt;Like FTP, SFTP facilitates both user and server authentication, but it operates over a different port, specifically port 22. It retains FTP’s connection-oriented feature that guarantees file delivery through TCP, ensuring reliable file transfers.&lt;/p&gt;
&lt;h3&gt;When to Use SFTP&lt;/h3&gt;
&lt;p&gt;If you&#39;re transferring confidential data or working in environments where data privacy is paramount, SFTP is the recommended protocol to use. Its secure nature makes it suitable for transactions involving sensitive information, like financial records or personal data.&lt;/p&gt;
&lt;h2&gt;What is TFTP?&lt;/h2&gt;
&lt;h3&gt;Definition and Purpose of TFTP&lt;/h3&gt;
&lt;p&gt;TFTP, or Trivial File Transfer Protocol, is a much simpler protocol compared to FTP and SFTP. While FTP and SFTP are designed for secure file transfers across networks, TFTP is primarily used for transferring files within a local area network (LAN). Its simplicity makes it suitable for transferring small files, especially configuration files and firmware images to network devices like routers or firewalls.&lt;/p&gt;
&lt;h3&gt;Characteristics of TFTP&lt;/h3&gt;
&lt;p&gt;Unlike FTP and SFTP, TFTP does not operate over TCP. Instead, it uses a connectionless protocol based on UDP (User Datagram Protocol), which makes it less reliable for large transfers but faster for small-sized file transactions. You won’t usually find security features in TFTP since it’s tailored for use within a safe local network rather than the global internet.&lt;/p&gt;
&lt;h3&gt;Drawbacks of TFTP&lt;/h3&gt;
&lt;p&gt;While TFTP can be adequately posited for specific circumstances, its unreliability and lack of security features make it unsuitable for sensitive data transfers. Its use is streamlined for local applications, which most casual users will likely not interact with.&lt;/p&gt;
&lt;h2&gt;Comparing FTP, SFTP, and TFTP&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Protocol&lt;/th&gt;
&lt;th&gt;Security&lt;/th&gt;
&lt;th&gt;Port&lt;/th&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;FTP&lt;/td&gt;
&lt;td&gt;No security&lt;/td&gt;
&lt;td&gt;21&lt;/td&gt;
&lt;td&gt;General file transfer over the internet&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SFTP&lt;/td&gt;
&lt;td&gt;Encrypted&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;Secure file transfer for sensitive data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TFTP&lt;/td&gt;
&lt;td&gt;No security&lt;/td&gt;
&lt;td&gt;69&lt;/td&gt;
&lt;td&gt;Simple file transfer within local networks&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;FTP, SFTP, and TFTP each play a significant role in file transfers across networks, and understanding their differences is crucial for using them effectively. When handling sensitive information, always prefer SFTP for its robust security measures. For general file transfers, FTP remains a viable option, while TFTP can be applied for simple local network transactions. Choose the appropriate protocol based on the specific needs of your data transfer scenario to ensure efficiency and security.&lt;/p&gt;
&lt;p&gt;To learn more about enhancing your file transfer experiences or if you have specific questions, feel free to explore additional resources or reach out!&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/understanding-ftp-sftp-and-tftp.html</link><author>noreply@blogger.com (Pratik)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-2549556363307729529</guid><pubDate>Sun, 09 Mar 2025 16:05:00 +0000</pubDate><atom:updated>2025-03-30T19:53:20.876+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">data structure and algorithm</category><title>Essential 8 Data Structures</title><description>&lt;h1&gt;&lt;br /&gt;The Essential 8 Data Structures Every Programmer Should Know&lt;/h1&gt;&lt;h1&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJV17Spbr230Yw6IqBNBMFrVnh12nd9SmCVRvW99srg_afI_nz_pRKuhoFJBPW16YjRob3wnaT6ZMJi4dk_DcaxdBPOqjAYFO2DNtPoH6X0GlEWZh8kdviEVuNXic-Y1BiioaX2I8utM1Zmr79igqoUrjJo51EV0BhiQNR9rvS_odg26bhsqC05frT/s1280/maxresdefault.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;720&quot; data-original-width=&quot;1280&quot; height=&quot;180&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJV17Spbr230Yw6IqBNBMFrVnh12nd9SmCVRvW99srg_afI_nz_pRKuhoFJBPW16YjRob3wnaT6ZMJi4dk_DcaxdBPOqjAYFO2DNtPoH6X0GlEWZh8kdviEVuNXic-Y1BiioaX2I8utM1Zmr79igqoUrjJo51EV0BhiQNR9rvS_odg26bhsqC05frT/s320/maxresdefault.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;/h1&gt;
&lt;p&gt;Understanding data structures is foundational for every programmer, enhancing not only code efficiency but also problem-solving capabilities. Whether you are a novice or an experienced coder, knowing these eight essential data structures will significantly bolster your programming arsenal. Here we will delve into each data structure, their definitions, properties, and real-world applications, inviting you to explore how these structures can be leveraged for optimal performance in various scenarios.&lt;/p&gt;
&lt;h2&gt;1. Arrays&lt;/h2&gt;
&lt;p&gt;Arrays are the simplest and most widely used data structure, serving as a foundational type for data storage. An array holds a fixed number of values of a single data type, stored in contiguous memory locations. Here are some defining features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fixed Length&lt;/strong&gt;: The array size is defined at creation and cannot be changed dynamically.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single Data Type&lt;/strong&gt;: Each array can only contain one type of data (e.g., integers or strings).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Random Access&lt;/strong&gt;: Elements can be accessed quickly via their index, making operations like value retrieval efficient (O(1) time complexity).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Real-World Use Case&lt;/h3&gt;
&lt;p&gt;Arrays are ideal for storing data that does not change in size, such as historical stock prices for financial analysis. However, inserting or deleting elements in the middle of an array can be inefficient (O(n)), as it may require shifting elements.&lt;/p&gt;
&lt;h2&gt;2. Linked Lists&lt;/h2&gt;
&lt;p&gt;Unlike arrays, linked lists use a series of nodes that are not stored contiguously. Each node contains a value and a pointer to the next node, creating a chain of nodes. There are several types of linked lists:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Singly Linked List&lt;/strong&gt;: Each node points to the next.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Doubly Linked List&lt;/strong&gt;: Nodes have pointers to both the next and previous nodes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Circular Linked List&lt;/strong&gt;: The last node points back to the first node.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Advantages&lt;/h3&gt;
&lt;p&gt;Linked lists are excellent for scenarios where frequent insertions and deletions occur, especially at the beginning of the list (O(1) time complexity). However, accessing an element by an index requires traversing the list sequentially (O(n)).&lt;/p&gt;
&lt;h2&gt;3. Stacks&lt;/h2&gt;
&lt;p&gt;Stacks follow a last-in, first-out (LIFO) principle, similar to a stack of plates. Key operations include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Push&lt;/strong&gt;: Adding an element to the top of the stack.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pop&lt;/strong&gt;: Removing the element from the top and returning its value.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Peek&lt;/strong&gt;: Viewing the top element without removing it.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Typical Uses&lt;/h3&gt;
&lt;p&gt;Stacks are often used in scenarios like function call tracking in recursion and implementing undo functionality in applications. All stack operations have a time complexity of O(1).&lt;/p&gt;
&lt;h2&gt;4. Queues&lt;/h2&gt;
&lt;p&gt;Queues use a first-in, first-out (FIFO) principle. Elements are added at the rear and removed from the front. The two main operations are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Enqueue&lt;/strong&gt;: Adding an element at the rear.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dequeue&lt;/strong&gt;: Removing an element from the front.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Applications&lt;/h3&gt;
&lt;p&gt;Queues are commonly used in scheduling tasks, such as print jobs in a printer or managing requests in web services. Maximum efficiency is achieved when using circular buffers.&lt;/p&gt;
&lt;h2&gt;5. Hash Tables&lt;/h2&gt;
&lt;p&gt;Hash tables store key-value pairs and use a hash function to compute the index in the underlying array, aiding in efficient lookups and storage. Key advantages include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Efficient Access&lt;/strong&gt;: Average case time complexities for inserting, deleting, and searching are usually O(1).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Collision Handling&lt;/strong&gt;: Strategies like chaining (using linked lists) or open addressing help manage collisions when two keys hash to the same index.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Example Usage&lt;/h3&gt;
&lt;p&gt;Hash tables are frequently used in database indexing and caching mechanisms, where quick access to data is critical.&lt;/p&gt;
&lt;h2&gt;6. Trees&lt;/h2&gt;
&lt;p&gt;Trees are hierarchical structures consisting of nodes connected by edges, resembling a family tree with parent-child relationships. One of the most prevalent types is the &lt;strong&gt;Binary Tree&lt;/strong&gt;, where each node has at most two children.&lt;/p&gt;
&lt;h3&gt;Special Case: Binary Search Trees&lt;/h3&gt;
&lt;p&gt;In binary search trees (BST), each node&#39;s left subtree includes values less than the node, and the right subtree contains larger values. This organization makes them exceptionally efficient for searching, inserting, and deleting nodes with a time complexity of O(log n).&lt;/p&gt;
&lt;h3&gt;Use Cases&lt;/h3&gt;
&lt;p&gt;Trees are used in efficient data management, including expression parsing in compilers and managing hierarchical database systems.&lt;/p&gt;
&lt;h2&gt;7. Heaps&lt;/h2&gt;
&lt;p&gt;Heaps are a specialized type of binary tree used primarily for implementing priority queues. They either follow the max heap property (where parent nodes are greater than their children) or the min heap property (where parent nodes are less than their children).&lt;/p&gt;
&lt;h3&gt;Performance&lt;/h3&gt;
&lt;p&gt;Operations such as insertions, deletions, and heapify (rearranging nodes to maintain heap quality) typically have a time complexity of O(log n).&lt;/p&gt;
&lt;h2&gt;8. Graphs&lt;/h2&gt;
&lt;p&gt;Graphs represent a set of nodes (or vertices) connected by edges and can be directed or undirected. They are critical in many real-world applications, such as network routing and social media connections.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Directed Graph&lt;/strong&gt;: Edges have directions, indicating a one-way relationship.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Undirected Graph&lt;/strong&gt;: Edges show a mutual connection between nodes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Use in Industry&lt;/h3&gt;
&lt;p&gt;Graphs are widely used in mapping locations with GPS, analyzing social network relationships, and managing web page links.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Mastering these eight data structures is vital for any programmer looking to improve their coding efficiency and capability. From arrays to graphs, understanding the properties and trade-offs of each structure will empower you to make informed decisions in your programming projects.&lt;/p&gt;
&lt;p&gt;For programmers serious about honing their skills, consider enrolling in structured courses that emphasize interactive learning, like those offered by Brilliant. They provide a comprehensive pathway for understanding algorithms and data structures.&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/the-essential-8-data-structures-every.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJV17Spbr230Yw6IqBNBMFrVnh12nd9SmCVRvW99srg_afI_nz_pRKuhoFJBPW16YjRob3wnaT6ZMJi4dk_DcaxdBPOqjAYFO2DNtPoH6X0GlEWZh8kdviEVuNXic-Y1BiioaX2I8utM1Zmr79igqoUrjJo51EV0BhiQNR9rvS_odg26bhsqC05frT/s72-c/maxresdefault.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-4042461439436301629</guid><pubDate>Sun, 09 Mar 2025 15:56:00 +0000</pubDate><atom:updated>2025-03-13T22:01:38.433+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">asp.net</category><category domain="http://www.blogger.com/atom/ns#">mvc design pattern</category><title>Software Design Patterns</title><description>&lt;h1&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Software Design Patterns: 7 Key Patterns Every Developer Should Know&lt;/span&gt;&lt;/h1&gt;
&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjOpc2ExKE2UxHQ_DlPD4tKD4Xhkvq1To7AyGgZw0st8qCGsojGfzA_JgVnkkeeaTIZCcJZTfI4CXTlsY34UblySTSTDHQiuT57arLesfg7UxBS6ccLiZEsKE5k8JQ8mPuA-YO-eG7aqy8mjhz6qxU1jRllNSD2D7BK9OjA0bqaXYS1pgH_XcGwcPc/s801/Creational-Design-Patterns.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;401&quot; data-original-width=&quot;801&quot; height=&quot;160&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjOpc2ExKE2UxHQ_DlPD4tKD4Xhkvq1To7AyGgZw0st8qCGsojGfzA_JgVnkkeeaTIZCcJZTfI4CXTlsY34UblySTSTDHQiuT57arLesfg7UxBS6ccLiZEsKE5k8JQ8mPuA-YO-eG7aqy8mjhz6qxU1jRllNSD2D7BK9OjA0bqaXYS1pgH_XcGwcPc/s320/Creational-Design-Patterns.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Understanding software design patterns is crucial for every developer, whether you&#39;re a beginner or an expert. Design patterns are standardized solutions to common problems encountered in software development. They help create efficient and maintainable code by providing tested, proven development paradigms. In this article, we&#39;ll explore seven essential software design patterns that can dramatically improve your coding practices.&lt;/span&gt;&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style=&quot;font-family: times;&quot;&gt;The Importance of Design Patterns&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Design patterns streamline the software development process by promoting code reusability, reducing complexity, and facilitating communication among developers. They serve as a toolkit that programmers can rely on to tackle recurrent scenarios across different programming languages and environments. The reference work for design patterns is the book written by the Gang of Four (GoF) in 1994, which formalized &lt;strong&gt;23 design patterns&lt;/strong&gt; into three main categories: &lt;strong&gt;Creational&lt;/strong&gt;, &lt;strong&gt;Structural&lt;/strong&gt;, and &lt;strong&gt;Behavioral&lt;/strong&gt;. Let&#39;s delve into each of these categories and highlight seven key patterns.&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Overview of Design Pattern Categories&lt;/span&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Creational Patterns&lt;/strong&gt;: These patterns focus on object creation mechanisms that create objects in a manner suitable for the situation. Examples include the Singleton, Factory, and Builder patterns.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Structural Patterns&lt;/strong&gt;: These patterns deal with object composition, helping to form larger structures from individual pieces. Remember the analogy of building with LEGO blocks.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Behavioral Patterns&lt;/strong&gt;: These patterns are centered around communication between objects, defining how they interact and distribute responsibilities. Notable examples include the Strategy and Observer patterns.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;&lt;span style=&quot;font-family: times;&quot;&gt;7 Essential Design Patterns&lt;/span&gt;&lt;/h2&gt;
&lt;h3&gt;&lt;span style=&quot;font-family: times;&quot;&gt;1. Singleton Pattern&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;The &lt;strong&gt;Singleton Pattern&lt;/strong&gt; is a creational pattern that restricts a class from instantiating multiple instances. It&#39;s useful when exactly one instance of a class is required to control actions. A common use case is a logging system where multiple loggers could lead to conflicts. The Singleton ensures a single logger instance is used across the application, providing a consistent logging format.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Usage&lt;/strong&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Ensures global access to the instance.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Common in logging, database connection pools, and configurations.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Drawback&lt;/strong&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Difficult to test due to its global state, and care must be taken in multithreaded environments.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;span style=&quot;font-family: times;&quot;&gt;2. Builder Pattern&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;The &lt;strong&gt;Builder Pattern&lt;/strong&gt; helps in constructing complex objects step by step. Instead of using a constructor with numerous parameters, which can be unwieldy, the Builder pattern allows for constructing objects through a more readable API.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Example&lt;/strong&gt;: When constructing an HTTP request with various optional parameters, the Builder lets you chain methods to configure your request elegantly.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Benefit&lt;/strong&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Simplifies object creation by allowing incremental construction.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;span style=&quot;font-family: times;&quot;&gt;3. Factory Pattern&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;The &lt;strong&gt;Factory Pattern&lt;/strong&gt; encapsulates the object creation process, allowing for the dynamic creation of objects without exposing the instantiation logic to the client. When you need to create various types of users (admin, moderator, user), the Factory Pattern hides the creation logic behind a factory method.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Advantages&lt;/strong&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Reduces dependency on specific classes and enhances code maintainability.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Simplifies updates in the instantiation process with changes required only in the factory class.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;span style=&quot;font-family: times;&quot;&gt;4. Facade Pattern&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;The &lt;strong&gt;Facade Pattern&lt;/strong&gt; provides a simpler interface to a complex subsystem. It acts as a front that conceals the complexity of how subsystems interact. For instance, when making an online purchase, the complexities of payment processing, inventory checks, and shipping calculations are hidden behind a simple &quot;buy now&quot; button, simplifying user interactions.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Why Use It?&lt;/strong&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;It simplifies interactions with complex systems, reducing the learning curve for new developers.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;span style=&quot;font-family: times;&quot;&gt;5. Adapter Pattern&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;The &lt;strong&gt;Adapter Pattern&lt;/strong&gt; enables incompatible interfaces to work together. This is particularly useful when using third-party libraries or APIs that don&#39;t match your application&#39;s expected interface formats.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Example&lt;/strong&gt;: If a weather service returns temperature in Celsius but your application requires Fahrenheit, the Adapter can convert the values on the fly, ensuring compatibility without altering your core application logic.&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;&lt;span style=&quot;font-family: times;&quot;&gt;6. Strategy Pattern&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;The &lt;strong&gt;Strategy Pattern&lt;/strong&gt; defines a family of algorithms encapsulated within classes, allowing for interchangeable behavior. It is particularly useful when you need to choose between different methods of performing a task. For example, different transport strategies (like driving or biking) can be represented using separate classes that follow a common interface.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Highlights&lt;/strong&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Promotes flexibility and reusability in code.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;New strategies can be added easily without affecting existing logic.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;span style=&quot;font-family: times;&quot;&gt;7. Observer Pattern&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;The &lt;strong&gt;Observer Pattern&lt;/strong&gt; allows one-to-many dependencies between objects. When the state of one object changes, all dependent observers are notified automatically. A real-world example is YouTube notifications; when a creator uploads a video, subscribers receive a notification based on their subscription.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Benefits&lt;/strong&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Minimizes the need for constant polling of state changes and improves efficiency.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Best Practices: When to Use Each Pattern&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Understanding when to deploy each design pattern is as crucial as knowing their implementations. Here are some tips:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Use Singleton&lt;/strong&gt; when you need a single point of control.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Use Builder&lt;/strong&gt; for complex object construction, especially when parameters are numerous.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Use Factory&lt;/strong&gt; when creating many similar objects to keep your code manageable.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Use Facade&lt;/strong&gt; to provide a clean interface to complex subsystems.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Use Adapter&lt;/strong&gt; when dealing with third-party services that don&#39;t match your application&#39;s architecture.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Use Strategy&lt;/strong&gt; when you have multiple ways to achieve the same result and want to change them dynamically.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: times;&quot;&gt;&lt;strong&gt;Use Observer&lt;/strong&gt; for event-driven systems requiring responsiveness and loose coupling.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Conclusion&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Design patterns are an essential part of a developer&#39;s toolkit, providing time-tested solutions to common programming challenges. By mastering the &lt;strong&gt;Singleton&lt;/strong&gt;, &lt;strong&gt;Builder&lt;/strong&gt;, &lt;strong&gt;Factory&lt;/strong&gt;, &lt;strong&gt;Facade&lt;/strong&gt;, &lt;strong&gt;Adapter&lt;/strong&gt;, &lt;strong&gt;Strategy&lt;/strong&gt;, and &lt;strong&gt;Observer&lt;/strong&gt; patterns, you equip yourself with the skills necessary for efficient problem-solving in your coding journey. Remember, using these patterns is not about following trends, but enhancing the clarity and maintainability of your code.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style=&quot;font-family: times;&quot;&gt;Ready to take your development skills to the next level? Start incorporating these design patterns in your projects today and see the difference it makes!&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/software-design-patterns.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjOpc2ExKE2UxHQ_DlPD4tKD4Xhkvq1To7AyGgZw0st8qCGsojGfzA_JgVnkkeeaTIZCcJZTfI4CXTlsY34UblySTSTDHQiuT57arLesfg7UxBS6ccLiZEsKE5k8JQ8mPuA-YO-eG7aqy8mjhz6qxU1jRllNSD2D7BK9OjA0bqaXYS1pgH_XcGwcPc/s72-c/Creational-Design-Patterns.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-4972767865864469179</guid><pubDate>Thu, 06 Mar 2025 16:39:00 +0000</pubDate><atom:updated>2025-03-06T22:24:53.871+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">blogging</category><category domain="http://www.blogger.com/atom/ns#">tech</category><title>Programming Languages</title><description>&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCO9nMWNziKqIZ8c3HFf3_vFT7dbwlFfV05Tt5nAfI3kC4FUKBTr7ODBfYtyAmGhwj8Nj01JTpsrK8DEXSdBvVpIglOOF6SAH9xZohbfnYRe8xa7Qi7AHqUeIuGHS5oWn_-MEp0aa4eK-x4Vjo-_VHPdhaZvsOhsBq6zYaRCD3hBOMOcAqlsTG_T3u/s612/digital-eye-data-network-cyber-s.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;410&quot; data-original-width=&quot;612&quot; height=&quot;214&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCO9nMWNziKqIZ8c3HFf3_vFT7dbwlFfV05Tt5nAfI3kC4FUKBTr7ODBfYtyAmGhwj8Nj01JTpsrK8DEXSdBvVpIglOOF6SAH9xZohbfnYRe8xa7Qi7AHqUeIuGHS5oWn_-MEp0aa4eK-x4Vjo-_VHPdhaZvsOhsBq6zYaRCD3hBOMOcAqlsTG_T3u/s320/digital-eye-data-network-cyber-s.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&amp;nbsp;&lt;p&gt;&lt;/p&gt;&lt;h1&gt;The Evolution of Code: A Detailed History of Programming&lt;br /&gt; Languages&lt;/h1&gt;
&lt;p&gt;Imagine a world without apps, websites, or even ATMs. That was reality before programming languages took over. From Ada Lovelace&#39;s first algorithm to today&#39;s AI-driven code, it has been quite a ride! Let&#39;s jump into the amazing story of how programming languages have changed over time.&lt;/p&gt;
&lt;p&gt;Programming languages have really taken off, growing from simple tools to powerful systems we use every day. This growth was powered by a real need for tools that were simple to use, efficient and specialized for our growing digital world.&lt;/p&gt;
&lt;h2&gt;The Pioneers: Early Programming Languages (1883-1959)&lt;/h2&gt;
&lt;p&gt;Get ready to meet the groundbreaking languages that made modern programming possible. They built the first steps!&lt;/p&gt;
&lt;h3&gt;Ada Lovelace and the Analytical Engine (1883)&lt;/h3&gt;
&lt;p&gt;Ada Lovelace was a math whiz back in the 1800s. It wasn&#39;t common for women to study math, but she was amazing. She created an algorithm for Charles Babbage&#39;s Analytical Engine. This machine was one of the first computers. Her algorithm helped it compute tricky math problems called Bernoulli numbers. People call Lovelace the &quot;princess of parallelograms&quot; due to her math skills. Her work is known as the first computer programming language.&lt;/p&gt;
&lt;h3&gt;Assembly Language (1949) and Autocode (1952)&lt;/h3&gt;
&lt;p&gt;Fast forward a bit, and we see Assembly Language. This language is closely tied to machine code. It tells the computer exactly what to do. Then, Autocode appeared. It wasn&#39;t just one language, but a group of early programming languages. Alec Glenny created the first one for the Manchester Mark 1 computer. The Autocode used a compiler, which translates human-readable code into machine code.&lt;/p&gt;
&lt;p&gt;EDSAC, a calculator made by Maurice Wilkes, used Assembly Language. It helped solve equations and discovered a prime number with 79 digits. That was huge back then!&lt;/p&gt;
&lt;h3&gt;FORTRAN, ALGOL, and COBOL (1957-1959)&lt;/h3&gt;
&lt;p&gt;Now we&#39;re cooking! In 1957, FORTRAN came on the scene. IBM made it for complicated math stuff. The name FORTRAN stands for formula translation. It helped benchmark the fastest supercomputers. Today, they still use FORTRAN.&lt;/p&gt;
&lt;p&gt;One year later, ALGOL was created. ALGOL, short for algorithmic language, inspired other languages. Think Pascal, C, C++, and Java. Most modern languages got their syntax from ALGOL. Plus, ALGOL was the first language to use code blocks with &quot;begin&quot; and &quot;end&quot; markers. It also used nested function definitions.&lt;/p&gt;
&lt;p&gt;Then, in 1959, COBOL arrived. Grace Murray Hopper developed COBOL. It was made to run on different computers. COBOL stands for common business-oriented language. You&#39;ll find it in ATMs, phone systems, and business systems. COBOL even showed up in the Terminator movie&#39;s vision display. Many mainframe computers use COBOL for older apps even today.&lt;/p&gt;
&lt;h2&gt;The Rise of High-Level Languages (1959-1978)&lt;/h2&gt;
&lt;p&gt;It was time for programming to become more user-friendly and simple. The following are the languages that played the biggest roles.&lt;/p&gt;
&lt;h3&gt;Lisp (1959) and BASIC (1964)&lt;/h3&gt;
&lt;p&gt;In 1959, Lisp was developed at MIT. Lisp means list processing language. It&#39;s the second oldest high-level language after FORTRAN. Lists are a key part of Lisp, and Lisp code is made up of lists. During that period, it was popular for artificial intelligence.&lt;/p&gt;
&lt;p&gt;In 1964, BASIC was created by John Kemeny and Thomas Kurtz at Dartmouth College. BASIC stands for beginner&#39;s all-purpose symbolic instruction code. As the name suggests, it was for students without a math background, allowing them to use computers. Later, in 1975, Bill Gates and Paul Allen changed BASIC, and it became one of Microsoft&#39;s first products.&lt;/p&gt;
&lt;h3&gt;Pascal (1970) and Smalltalk (1972)&lt;/h3&gt;
&lt;p&gt;Pascal, named after Blaise Pascal, came to be in 1970. Nicolaus Wirth developed it. Compared to earlier languages, Pascal was easier to pick up. It was popular for teaching computer programming. The language encouraged good programming habits. Apple even used Pascal in their early software.&lt;/p&gt;
&lt;p&gt;In 1972, Smalltalk came about. Alan Kay, Adele Goldberg, and Dan Ingalls at Xerox created it. What made it special? It let programmers change code while the program was running. Its primary purpose was teaching. It inspired features in newer languages like Python. In 2017, Smalltalk was the second most loved language on a Stack Overflow survey.&lt;/p&gt;
&lt;h3&gt;C and SQL (1972)&lt;/h3&gt;
&lt;p&gt;Also in 1972, C was created at Bell Labs by Dennis Ritchie. Even though prior languages were considered high-level, C was the first truly high-level one. &quot;High-level&quot; means it&#39;s closer to human language than machine code. UNIX could run on different computers due to the development of C. Many future languages were inspired by C. In the 1980s, it gained popularity. C is still one of the most popular languages and is often taught in computer science courses.&lt;/p&gt;
&lt;p&gt;In addition to C, SQL was also created in 1972. Donald Chamberlin and Raymond Boyce at IBM developed SQL. SQL stands for structured query language. It is a language for working with databases and is still used today.&lt;/p&gt;
&lt;h3&gt;MATLAB (1978)&lt;/h3&gt;
&lt;p&gt;Created in 1978 by Cleve Moler, MATLAB is short for matrix laboratory. It&#39;s considered one of the best languages for advanced math. Students and researchers use MATLAB for matrix calculations.&lt;/p&gt;
&lt;h2&gt;Object-Oriented Programming and Beyond (1983-1995)&lt;/h2&gt;
&lt;p&gt;Now we explore object-oriented programming. This changed how programs were put together.&lt;/p&gt;
&lt;h3&gt;Objective-C and C++ (1983)&lt;/h3&gt;
&lt;p&gt;In 1983, Objective-C showed up, created by Brad Cox and Tom Love. Objective-C mixes Smalltalk&#39;s messaging style with the C language. Until Swift came along in 2014, Objective-C was the main language for Apple&#39;s Mac OS and iOS.&lt;/p&gt;
&lt;p&gt;C++ also came about in 1983. Bjarne Stroustrup at Bell Labs developed it. They also know it as &quot;C with classes&quot;. It was made for system programming, adding more high-level features. Today, C++ is popular and used in software like Adobe Photoshop and Minecraft. New C++ versions still come out every three years.&lt;/p&gt;
&lt;h3&gt;Perl, Haskell, Python, Visual Basic (1987-1991)&lt;/h3&gt;
&lt;p&gt;Perl showed up in 1987. Larry Wall created it as a scripting language for text editing. Perl borrows features from C, Basic, and Lisp. Linux system administration, web development, and network programming commonly use Perl.&lt;/p&gt;
&lt;p&gt;Then, in 1990, Haskell was created, named after Haskell Brooks Curry. It&#39;s mainly a math-focused programming language. Haskell innovated with type classes, type inference, and lazy evaluation.&lt;/p&gt;
&lt;p&gt;In 1991, Python came to be. Guido van Rossum designed it. Python is easy to read and requires less code than other languages. Python is named after Monty Python. It&#39;s known for making programmers feel lazy because it is useful and easy to read. Data science often uses Python.&lt;/p&gt;
&lt;p&gt;Also in 1991, Visual Basic was created by Microsoft. Visual Basic lets users drag and drop code in a graphical interface. It was made to be simple. In 2016, Visual Basic 6.0 won a technical impact award at the DICE Awards.&lt;/p&gt;
&lt;h3&gt;R, Java, PHP, Ruby, and JavaScript (1993-1995)&lt;/h3&gt;
&lt;p&gt;In 1993, the programming language R was created at the University of Auckland in New Zealand. Ross Ihaka and Robert Gentleman developed R. Statisticians and data scientists use R for data analysis and making stats software. In January 2020, R was the 18th most popular language.&lt;/p&gt;
&lt;p&gt;1995 was a busy year for programming languages. Java was created by Sun Microsystems. They often teach it for object-oriented design. Java was meant for cable boxes but was improved for the web. Java was designed around &quot;write once, run anywhere,&quot; meaning Java apps can run on any Java Virtual Machine. More than three billion devices run Java.&lt;/p&gt;
&lt;p&gt;PHP, originally short for personal homepage, was created by Rasmus Lerdorf. People used it for web development. WordPress is written in PHP.&lt;/p&gt;
&lt;p&gt;Also, Ruby came about, created by Yukihiro Matsumoto. Ruby mixes aspects from other languages like Perl, Smalltalk, Basic, and Lisp. Like PHP, they commonly use Ruby in web development. However, Ruby runs slower than other web languages. Ruby has dynamic typing and garbage collection.&lt;/p&gt;
&lt;p&gt;Lastly, in 1995, JavaScript was created. Brendan Eich made it in 10 days. JavaScript improves web browser interactions. It&#39;s often compiled just-in-time and is core tech along with HTML and CSS for the web.&lt;/p&gt;
&lt;h2&gt;Modern Languages and Future Trends (2000-Present)&lt;/h2&gt;
&lt;p&gt;Let&#39;s look at some more modern languages and how programming continues to change.&lt;/p&gt;
&lt;p&gt;In 2000, C# was developed by Microsoft as part of the .NET initiative. C# mixes the power of C++ with the simplicity of Visual Basic. C# is like Java and is used in Microsoft products. It is object-oriented, consists of strong typing, and uses lexical scope.&lt;/p&gt;
&lt;p&gt;Scala came about in 2003, created by Martin Odersky. It mixes math-style programming with object-oriented programming. Scala aimed to improve upon some criticisms of Java. In the same year, Groovy came about. James Strachan created it as an improved version of Java. Developers like Groovy because it&#39;s simple to learn and succinct.&lt;/p&gt;
&lt;p&gt;In 2009, Google developed Go, also known as Golang. Go was made to fix problems common in Google&#39;s software systems where languages like Java and C++ fell short. In terms of syntax, Go is similar to C but with memory safety and garbage collection.&lt;/p&gt;
&lt;p&gt;In 2014, Apple developed Swift. It replaced most other languages used at Apple, such as C, C++, and Objective-C. They know Swift for being simple to use and having less room for error. Desktop and mobile apps often use Swift.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;From Lovelace&#39;s algorithm to today&#39;s AI-driven coding, the world of programming languages has changed big time. What was a niche field has turned into a crucial part of our lives. As technology keeps changing, expect even more amazing changes in programming languages. Who knows what cool languages will pop up next?&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/programming-languages.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCO9nMWNziKqIZ8c3HFf3_vFT7dbwlFfV05Tt5nAfI3kC4FUKBTr7ODBfYtyAmGhwj8Nj01JTpsrK8DEXSdBvVpIglOOF6SAH9xZohbfnYRe8xa7Qi7AHqUeIuGHS5oWn_-MEp0aa4eK-x4Vjo-_VHPdhaZvsOhsBq6zYaRCD3hBOMOcAqlsTG_T3u/s72-c/digital-eye-data-network-cyber-s.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-6480899366249004907</guid><pubDate>Wed, 05 Mar 2025 17:28:00 +0000</pubDate><atom:updated>2025-03-05T23:13:16.212+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">asp dot net</category><category domain="http://www.blogger.com/atom/ns#">c#</category><title>Getting Started With C#</title><description>&lt;div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwsnUoJzJnn5G8wu0EK2B1QWv7q4p83Jftkrs4npArixyig3_dS7HghVPFZ30VuIyp7dmYD6G4tnpuV2uALHEl5VCNqsuI1nWDju13QCaLEKbzcdpnZZMkSAfjn6ROmFNEaH8NBrOFSo9Ko9k33ac9A34q1l8GvfTDUdwfK1PpkByKu6Jy4doaT2V1HKM/s612/c-sharp-programing-language.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;426&quot; data-original-width=&quot;612&quot; height=&quot;223&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwsnUoJzJnn5G8wu0EK2B1QWv7q4p83Jftkrs4npArixyig3_dS7HghVPFZ30VuIyp7dmYD6G4tnpuV2uALHEl5VCNqsuI1nWDju13QCaLEKbzcdpnZZMkSAfjn6ROmFNEaH8NBrOFSo9Ko9k33ac9A34q1l8GvfTDUdwfK1PpkByKu6Jy4doaT2V1HKM/s320/c-sharp-programing-language.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;h1&gt;&lt;/h1&gt;&lt;h1&gt;Mastering C#: A Comprehensive Guide to Every Feature&lt;/h1&gt;

&lt;p&gt;C# is a versatile programming language that is part of the C family, known for its curly brackets and semicolons. Developed by Microsoft, C# has evolved into a fully open-source and cross-platform language. This means you can create applications that run on Windows, Linux, Mac, mobile devices (thanks to Xamarin), and even WebAssembly through Blazor. In this guide, we’ll explore all the essential features of C# that you need to know to get started.&lt;/p&gt;

&lt;h2&gt;Understanding Functions in C#&lt;/h2&gt;
&lt;p&gt;Functions are the building blocks of any program, and in C#, they are defined with a return type, a name, and parameters. The body of the function is enclosed in curly brackets, and each line of code ends with a semicolon. Here’s a simple function example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void MyFunction(int parameter) {
    // function body
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Functions can also be declared as static, using the static keyword. This means that the function belongs to the class itself rather than an instance of the class.&lt;/p&gt;

&lt;h3&gt;Comments in C#&lt;/h3&gt;
&lt;p&gt;Adding comments to your code is crucial for readability. In C#, comments begin with two forward slashes (//). This helps other developers (or your future self) understand the purpose of the code.&lt;/p&gt;

&lt;h2&gt;Declaring Variables&lt;/h2&gt;
&lt;p&gt;In C#, variables are declared by specifying a type followed by the variable name. However, modern C# developers often prefer using the &lt;strong&gt;var&lt;/strong&gt; keyword for variable declarations. Using &lt;strong&gt;var&lt;/strong&gt; allows the compiler to infer the variable type, making your code cleaner and more concise.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var myString = &quot;Hello, World!&quot;; // Compiler infers this is a string
var myNumber = 42; // Compiler infers this is an int&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Built-in Data Types&lt;/h3&gt;
&lt;p&gt;C# features several built-in data types that are similar to those in other programming languages:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;Integers:&lt;/strong&gt; Whole numbers&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Floating Point Numbers:&lt;/strong&gt; Decimal numbers&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Strings:&lt;/strong&gt; Textual data&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Creating Your Own Data Types&lt;/h3&gt;
&lt;p&gt;You can also create custom data types in C# using classes, structs, and records. Each has its own use case:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;Classes:&lt;/strong&gt; The most common type for defining complex data types.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Structs:&lt;/strong&gt; Useful for storing small amounts of data efficiently, as they are stored on the stack instead of the heap.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Records:&lt;/strong&gt; A quick and easy way to represent immutable data structures.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Classes and Access Modifiers&lt;/h2&gt;
&lt;p&gt;Classes in C# can have one of five access modifiers that define their visibility:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;Public:&lt;/strong&gt; Accessible from anywhere.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Private:&lt;/strong&gt; Accessible only within the class itself.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Protected:&lt;/strong&gt; Accessible within the class and by derived classes.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Internal:&lt;/strong&gt; Accessible only within the same assembly.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;File:&lt;/strong&gt; Accessible only within the same file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Classes can contain methods, and these methods can also have access modifiers. Importantly, you cannot have a public method in a private class. Inheritance is another critical feature, allowing classes to derive from other classes and reuse code. The &lt;strong&gt;abstract&lt;/strong&gt; keyword denotes classes or methods meant solely for inheritance.&lt;/p&gt;

&lt;h2&gt;Working with Interfaces&lt;/h2&gt;
&lt;p&gt;In C#, interfaces are defined with the &lt;strong&gt;interface&lt;/strong&gt; keyword and specify methods and properties that implementing classes must contain. This allows for a flexible design where different classes can implement the same interface in their own ways.&lt;/p&gt;

&lt;h3&gt;Properties in C#&lt;/h3&gt;
&lt;p&gt;C# offers a shorthand syntax for properties using the &lt;strong&gt;get&lt;/strong&gt; and &lt;strong&gt;set&lt;/strong&gt; keywords. You can also create read-only properties using the shorthand arrow syntax:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public int MyProperty { get; set; } // Read/write property
public int MyReadOnlyProperty =&amp;gt; myValue; // Read-only property&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Constructors and Destructors&lt;/h2&gt;
&lt;p&gt;Constructors are special methods that initialize an instance of a class. You use the &lt;strong&gt;new&lt;/strong&gt; keyword to create a new instance and pass any necessary parameters to the constructor:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public MyClass(int value) {
    // Initialization code
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Destructors, on the other hand, are used to clean up resources when an instance is no longer needed. They are defined similarly to constructors but with a tilde (~) before the class name.&lt;/p&gt;

&lt;h2&gt;Control Flow Statements&lt;/h2&gt;
&lt;p&gt;Control flow statements determine the execution path of your program. C# includes several types of control flow statements:&lt;/p&gt;

&lt;h3&gt;If Statements&lt;/h3&gt;
&lt;p&gt;Conditional execution can be achieved using &lt;strong&gt;if&lt;/strong&gt; statements:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can chain multiple conditions using &lt;strong&gt;else if&lt;/strong&gt; statements. The use of curly brackets is mandatory if there are multiple lines of code within the block.&lt;/p&gt;

&lt;h3&gt;Switch Statements&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;switch&lt;/strong&gt; statement provides a way to execute code based on the value of a variable. It’s particularly useful for pattern matching:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;switch (variable) {
    case 1:
        // Code for case 1
        break;
    case 2:
        // Code for case 2
        break;
    default:
        // Code if no cases match
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With modern C#, you can also include conditions within your cases, enhancing the switch statement&#39;s functionality.&lt;/p&gt;

&lt;h3&gt;Loops in C#&lt;/h3&gt;
&lt;p&gt;Loops allow you to execute a block of code multiple times. C# supports several loop types, including:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;for loops:&lt;/strong&gt; Useful for iterating a specific number of times.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;do-while loops:&lt;/strong&gt; Ensures at least one execution of the loop.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;foreach loops:&lt;/strong&gt; Simplifies iteration over collections.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Collections in C#&lt;/h2&gt;
&lt;p&gt;C# provides various collection types to store multiple items. The most common are:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;Arrays:&lt;/strong&gt; Fixed-size collections of elements of the same type.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Lists:&lt;/strong&gt; Dynamic arrays that can grow as needed.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Dictionaries:&lt;/strong&gt; Key-value pairs for fast lookups.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These collections implement the &lt;strong&gt;IEnumerable&lt;/strong&gt; interface, which allows for iteration using the &lt;strong&gt;foreach&lt;/strong&gt; statement. You can create custom classes that implement &lt;strong&gt;IEnumerable&lt;/strong&gt; to define how they should be iterated over.&lt;/p&gt;

&lt;h2&gt;Using Yield for Deferred Execution&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;yield&lt;/strong&gt; statement enables deferred execution, allowing you to generate results on-the-fly as needed. This is particularly useful for large datasets, as it can improve performance by only calculating values when they are accessed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public IEnumerable&lt;int&gt; GenerateNumbers() {
    for (int i = 0; i &amp;lt; 10; i++) {
        yield return i; // Returns each number one at a time
    }
}&lt;/int&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;LINQ and Functional Programming&lt;/h3&gt;
&lt;p&gt;C# supports Language Integrated Query (LINQ), which allows for querying collections in a declarative manner. With LINQ, you can use methods like &lt;strong&gt;Select&lt;/strong&gt; and &lt;strong&gt;Aggregate&lt;/strong&gt; to manipulate data easily, similar to functional programming concepts.&lt;/p&gt;

&lt;h2&gt;Asynchronous Programming in C#&lt;/h2&gt;
&lt;p&gt;Asynchronous programming is a crucial feature in C# that allows you to write non-blocking code. You can use the &lt;strong&gt;async&lt;/strong&gt; and &lt;strong&gt;await&lt;/strong&gt; keywords to control the flow of asynchronous operations:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public async Task MyAsyncMethod() {
    await SomeAsyncOperation();
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This ensures that your application remains responsive while performing long-running tasks.&lt;/p&gt;

&lt;h2&gt;Generics in C#&lt;/h2&gt;
&lt;p&gt;Generics provide a way to create classes and methods with a placeholder for the type. This allows for type safety without sacrificing performance. You can specify constraints using the &lt;strong&gt;where&lt;/strong&gt; keyword:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class MyGenericClass&lt;t&gt; where T : IComparable {
    // Class implementation
}&lt;/t&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Getting Started with C#&lt;/h2&gt;
&lt;p&gt;If you&#39;re ready to dive into C#, start by choosing an Integrated Development Environment (IDE). Visual Studio is the traditional choice, but many developers are now opting for Visual Studio Code or JetBrains Rider for a more lightweight experience. To create a new C# console application, simply use the following command in your terminal:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.NET new console&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;From there, you can start experimenting with the features we&#39;ve discussed and begin your journey in C# programming.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;C# is a powerful language with a rich set of features that can help you build applications across various platforms. Whether you&#39;re a seasoned developer or just starting, understanding these core concepts will set you up for success. Don&#39;t hesitate to explore further, practice coding, and engage with the community. Happy coding!&lt;/p&gt;

&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/getting-started-with-c.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwsnUoJzJnn5G8wu0EK2B1QWv7q4p83Jftkrs4npArixyig3_dS7HghVPFZ30VuIyp7dmYD6G4tnpuV2uALHEl5VCNqsuI1nWDju13QCaLEKbzcdpnZZMkSAfjn6ROmFNEaH8NBrOFSo9Ko9k33ac9A34q1l8GvfTDUdwfK1PpkByKu6Jy4doaT2V1HKM/s72-c/c-sharp-programing-language.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-1455190148264673232</guid><pubDate>Tue, 04 Mar 2025 15:58:00 +0000</pubDate><atom:updated>2025-03-04T21:43:21.476+05:45</atom:updated><title>Getting Started With LeetCode</title><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;h1&gt;Mastering Array Problems on LeetCode: Common Patterns and Solutions&lt;/h1&gt;
&lt;p&gt;Solving array problems efficiently is crucial for coding interviews, especially on platforms like LeetCode. This guide explores key problem-solving patterns, effective strategies, and must-know algorithms, with C# examples to help beginners and intermediate developers build confidence.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;1. Common Array Problem Patterns&lt;/h2&gt;
&lt;p&gt;Recognizing patterns in array problems helps reduce brute-force solutions and improves efficiency. Below are some of the most common approaches:&lt;/p&gt;
&lt;h3&gt;1.1 Two-Pointer Technique&lt;/h3&gt;
&lt;p&gt;The two-pointer technique is used to reduce the time complexity of problems that involve searching or modifying an array in place.&lt;/p&gt;
&lt;h4&gt;Example: Two Sum (Sorted Array)&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Find two numbers in a sorted array that add up to a given target.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use two pointers, one at the beginning and one at the end.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;public int[] TwoSumSorted(int[] numbers, int target) {
    int left = 0, right = numbers.Length - 1;
    while (left &amp;lt; right) {
        int sum = numbers[left] + numbers[right];
        if (sum == target) return new int[] { left + 1, right + 1 };
        else if (sum &amp;lt; target) left++;
        else right--;
    }
    return new int[] { -1, -1 }; // No solution
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(n)&lt;/p&gt;
&lt;hr /&gt;
&lt;h3&gt;1.2 Sliding Window&lt;/h3&gt;
&lt;p&gt;Sliding window is useful for problems involving subarrays or sequences.&lt;/p&gt;
&lt;h4&gt;Example: Longest Subarray with Sum K&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Find the longest contiguous subarray that sums to &lt;code inline=&quot;&quot;&gt;k&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;public int LongestSubarraySumK(int[] nums, int k) {
    int left = 0, sum = 0, maxLength = 0;
    for (int right = 0; right &amp;lt; nums.Length; right++) {
        sum += nums[right];
        while (sum &amp;gt; k &amp;amp;&amp;amp; left &amp;lt;= right) {
            sum -= nums[left];
            left++;
        }
        if (sum == k) maxLength = Math.Max(maxLength, right - left + 1);
    }
    return maxLength;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(n)&lt;/p&gt;
&lt;hr /&gt;
&lt;h3&gt;1.3 Prefix Sum&lt;/h3&gt;
&lt;p&gt;Prefix sum optimizes problems where multiple range queries are performed.&lt;/p&gt;
&lt;h4&gt;Example: Subarray Sum Equals K&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Find the number of subarrays that sum to &lt;code inline=&quot;&quot;&gt;k&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;public int SubarraySum(int[] nums, int k) {
    Dictionary&amp;lt;int, int&amp;gt; prefixSumMap = new Dictionary&amp;lt;int, int&amp;gt;();
    prefixSumMap[0] = 1; // Base case
    int sum = 0, count = 0;
    foreach (var num in nums) {
        sum += num;
        if (prefixSumMap.ContainsKey(sum - k))
            count += prefixSumMap[sum - k];
        if (!prefixSumMap.ContainsKey(sum))
            prefixSumMap[sum] = 0;
        prefixSumMap[sum]++;
    }
    return count;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(n)&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;2. How to Think Like a Problem Solver&lt;/h2&gt;
&lt;p&gt;When tackling algorithm challenges, follow these steps:&lt;/p&gt;
&lt;h3&gt;2.1 Understand the Problem&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Read carefully and clarify edge cases.&lt;/li&gt;
&lt;li&gt;Identify constraints (e.g., array size, negative numbers, duplicates).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;2.2 Plan Your Approach&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Recognize applicable patterns.&lt;/li&gt;
&lt;li&gt;Choose an efficient data structure (hash maps, heaps, etc.).&lt;/li&gt;
&lt;li&gt;Consider brute force first, then optimize.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;2.3 Implement and Optimize&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Write clean, modular code.&lt;/li&gt;
&lt;li&gt;Test with edge cases.&lt;/li&gt;
&lt;li&gt;Analyze time and space complexity.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;3. Top 10 Must-Know Algorithms for Coding Interviews&lt;/h2&gt;
&lt;p&gt;Here are fundamental algorithms that every programmer should master:&lt;/p&gt;
&lt;h3&gt;3.1 Binary Search&lt;/h3&gt;
&lt;p&gt;Used for searching in sorted arrays.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;public int BinarySearch(int[] arr, int target) {
    int left = 0, right = arr.Length - 1;
    while (left &amp;lt;= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) return mid;
        else if (arr[mid] &amp;lt; target) left = mid + 1;
        else right = mid - 1;
    }
    return -1;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(log n)&lt;/p&gt;
&lt;hr /&gt;
&lt;h3&gt;3.2 Sorting Algorithms&lt;/h3&gt;
&lt;h4&gt;QuickSort (Divide &amp;amp; Conquer Approach)&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;public void QuickSort(int[] arr, int left, int right) {
    if (left &amp;gt;= right) return;
    int pivot = Partition(arr, left, right);
    QuickSort(arr, left, pivot - 1);
    QuickSort(arr, pivot + 1, right);
}

private int Partition(int[] arr, int left, int right) {
    int pivot = arr[right], i = left;
    for (int j = left; j &amp;lt; right; j++) {
        if (arr[j] &amp;lt; pivot) {
            (arr[i], arr[j]) = (arr[j], arr[i]);
            i++;
        }
    }
    (arr[i], arr[right]) = (arr[right], arr[i]);
    return i;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(n log n)&lt;/p&gt;
&lt;hr /&gt;
&lt;h3&gt;3.3 Depth-First Search (DFS) &amp;amp; Breadth-First Search (BFS)&lt;/h3&gt;
&lt;p&gt;Used for tree and graph traversal.&lt;/p&gt;
&lt;h4&gt;DFS Example:&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;public void DFS(TreeNode root) {
    if (root == null) return;
    Console.WriteLine(root.val);
    DFS(root.left);
    DFS(root.right);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;BFS Example:&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;public void BFS(TreeNode root) {
    Queue&amp;lt;TreeNode&amp;gt; queue = new Queue&amp;lt;TreeNode&amp;gt;();
    queue.Enqueue(root);
    while (queue.Count &amp;gt; 0) {
        TreeNode node = queue.Dequeue();
        Console.WriteLine(node.val);
        if (node.left != null) queue.Enqueue(node.left);
        if (node.right != null) queue.Enqueue(node.right);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(n)&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Mastering array problems requires recognizing patterns, optimizing solutions, and understanding fundamental algorithms. With structured problem-solving techniques and efficient implementations, you can confidently tackle coding interviews and algorithm challenges on platforms like LeetCode. Keep practicing, analyzing different solutions, and refining your approach to become an expert problem solver! 🚀&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/getting-started-with-leetcode.html</link><author>noreply@blogger.com (Pratik)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-9182972103544646965</guid><pubDate>Sun, 02 Mar 2025 15:28:00 +0000</pubDate><atom:updated>2025-03-02T21:13:04.324+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">asp dot net</category><category domain="http://www.blogger.com/atom/ns#">asp.net</category><title>ASP.NET Core MVC</title><description>&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsHa9pE-mydaMlpUYGPzNC7YYt897FxezrgEfYBA79c85xwNiCZIldVZbQApbSofOJzPLboMcoLidS49KIdU_Zc0nyAY19m05c1AcOrtvGriilvvlPLUQr5Ui6rVYlHIMLrk3ASts6bOrL6NpOJ_1ODoytD-8lImJb9xwkcZUZii6JaIi25xwzecTAnqg/s361/240_F_397264965_O9QL56ghGrAERQMK.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;240&quot; data-original-width=&quot;361&quot; height=&quot;213&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsHa9pE-mydaMlpUYGPzNC7YYt897FxezrgEfYBA79c85xwNiCZIldVZbQApbSofOJzPLboMcoLidS49KIdU_Zc0nyAY19m05c1AcOrtvGriilvvlPLUQr5Ui6rVYlHIMLrk3ASts6bOrL6NpOJ_1ODoytD-8lImJb9xwkcZUZii6JaIi25xwzecTAnqg/s320/240_F_397264965_O9QL56ghGrAERQMK.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&lt;p&gt;&lt;/p&gt;&lt;div&gt;&lt;h1&gt;Getting Started with C#: A Beginner’s Guide&lt;/h1&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;So, you’ve decided to dive into C#! That’s an awesome choice. C# (pronounced &quot;C-sharp&quot;) is a versatile, modern programming language created by Microsoft. Whether you’re dreaming of building Windows apps, designing games with Unity, or crafting web services, C# has you covered. It’s powerful yet beginner-friendly, making it a fantastic starting point for new programmers. In this post, I’ll walk you through how to get started, what you should learn right away, and the core language fundamentals to master as you begin your C# journey.&lt;/p&gt;
&lt;h2&gt;Step 1: Setting Up Your Environment&lt;/h2&gt;&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Before you write your first line of code, you need the right tools. Here’s what you’ll need:&lt;/p&gt;
&lt;ul class=&quot;marker:text-secondary&quot;&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Visual Studio Community&lt;/strong&gt;: This free, all-in-one IDE (Integrated Development Environment) from Microsoft is perfect for beginners. It includes a code editor, debugger, and everything else you need to start coding in C#. Download it from the &lt;a href=&quot;https://visualstudio.microsoft.com/vs/community/&quot; rel=&quot;noopener noreferrer&quot; target=&quot;_blank&quot;&gt;official Visual Studio website&lt;/a&gt;.&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;.NET SDK&lt;/strong&gt;: This is the framework C# runs on. It’s bundled with Visual Studio, but if you’re using a lighter editor like Visual Studio Code, grab it separately from the &lt;a href=&quot;https://dotnet.microsoft.com/download&quot; rel=&quot;noopener noreferrer&quot; target=&quot;_blank&quot;&gt;.NET website&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Once installed, open Visual Studio, create a new &lt;strong&gt;Console App&lt;/strong&gt; project, and you’re ready to roll!&lt;/p&gt;
&lt;h2&gt;Step 2: Your First C# Program&lt;/h2&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Let’s kick things off with the classic &quot;Hello, World!&quot; program. Open your new project and paste this code:&lt;/p&gt;&lt;div class=&quot;not-prose&quot;&gt;&lt;div class=&quot;relative [&amp;amp;_div+div]:!mt-0 mt-6 mb-10 -mx-4 -mr-2 @md:-mr-4&quot;&gt;&lt;div style=&quot;background: rgb(18, 19, 20); border-radius: 0px 0px 12px 12px; color: white; font-size: 0.9em; line-height: 1.5em; margin-top: 0px; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code style=&quot;font-size: inherit; line-height: inherit; white-space: pre;&quot;&gt;&lt;span style=&quot;color: #c678dd;&quot;&gt;using&lt;/span&gt; System;

&lt;span style=&quot;color: #c678dd;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #61aeee;&quot;&gt;HelloWorld&lt;/span&gt;
{
    &lt;span style=&quot;color: #c678dd;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #61aeee;&quot;&gt;Program&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-function&quot; style=&quot;color: #c678dd;&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt; &lt;/span&gt;&lt;span class=&quot;hljs-function&quot; style=&quot;color: #c678dd;&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt; &lt;/span&gt;&lt;span class=&quot;hljs-function&quot; style=&quot;color: #61aeee;&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hljs-function hljs-params&quot; style=&quot;color: #e6c07b;&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hljs-function hljs-params&quot;&gt;[] args&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt;)&lt;/span&gt;
        {
            Console.WriteLine(&lt;span style=&quot;color: #98c379;&quot;&gt;&quot;Hello, World!&quot;&lt;/span&gt;);
        }
&lt;br /&gt;    }
&lt;br /&gt;}&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Hit the &quot;Run&quot; button (or press F5), and voilà—you’ll see &quot;Hello, World!&quot; printed in a console window. Congrats, you’re officially a C# coder! This simple program introduces you to namespaces, classes, and methods—key building blocks we’ll explore soon.&lt;/p&gt;
&lt;h2&gt;What Should You Learn When Getting Started?&lt;/h2&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;As a beginner, your goal is to build a strong foundation without feeling overwhelmed. Focus on these essential topics early on:&lt;/p&gt;
&lt;h3&gt;1. Writing and Running Code&lt;/h3&gt;
&lt;ul class=&quot;marker:text-secondary&quot;&gt;
&lt;li class=&quot;break-words&quot;&gt;Learn how to create a project in Visual Studio.&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;Understand how to compile and run your code.&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;Get comfortable with the &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;Console&lt;/span&gt; class for input (&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;Console.ReadLine()&lt;/span&gt;) and output (&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;Console.WriteLine()&lt;/span&gt;).&lt;/li&gt;&lt;br /&gt;
&lt;/ul&gt;
&lt;h3&gt;2. Variables and Data Types&lt;/h3&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Variables are like labeled boxes that store data. Start with these common types:&lt;/p&gt;
&lt;ul class=&quot;marker:text-secondary&quot;&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;int&lt;/span&gt; for whole numbers (e.g., &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;int age = 25;&lt;/span&gt;)&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;double&lt;/span&gt; for decimals (e.g., &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;double price = 19.99;&lt;/span&gt;)&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;string&lt;/span&gt; for text (e.g., &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;string name = &quot;Luna&quot;;&lt;/span&gt;)&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;bool&lt;/span&gt; for true/false values (e.g., &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;bool isCoding = true;&lt;/span&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Try playing with them in a program—ask the user for their name and age, then print it back!&lt;/p&gt;
&lt;h3&gt;3. Basic Operations&lt;/h3&gt;
&lt;ul class=&quot;marker:text-secondary&quot;&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Math&lt;/strong&gt;: Add (&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;+&lt;/span&gt;), subtract (&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;-&lt;/span&gt;), multiply (&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;*&lt;/span&gt;), divide (&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;/&lt;/span&gt;), and modulo (&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;%&lt;/span&gt; for remainders).&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;String Concatenation&lt;/strong&gt;: Combine text with &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;+&lt;/span&gt; (e.g., &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;&quot;Hello, &quot; + name&lt;/span&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;4. Control Flow&lt;/h3&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Control flow lets your program make decisions and repeat actions:&lt;/p&gt;
&lt;ul class=&quot;marker:text-secondary&quot;&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;If Statements&lt;/strong&gt;: Run code based on conditions (e.g., &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;if (age &amp;gt;= 18) { Console.WriteLine(&quot;Adult!&quot;); }&lt;/span&gt;).&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Loops&lt;/strong&gt;: Repeat code with &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;for&lt;/span&gt;, &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;while&lt;/span&gt;, or &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;foreach&lt;/span&gt; (e.g., &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;for (int i = 0; i &amp;lt; 5; i++) { Console.WriteLine(i); }&lt;/span&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;5. Methods&lt;/h3&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Methods are reusable blocks of code. You’ve already used &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;Main&lt;/span&gt;, but try creating your own:&lt;/p&gt;&lt;div class=&quot;not-prose&quot;&gt;&lt;div class=&quot;relative [&amp;amp;_div+div]:!mt-0 mt-6 mb-10 -mx-4 -mr-2 @md:-mr-4&quot;&gt;&lt;div style=&quot;background: rgb(18, 19, 20); border-radius: 0px 0px 12px 12px; color: white; font-size: 0.9em; line-height: 1.5em; margin-top: 0px; overflow-x: auto; padding: 16px;&quot;&gt;&lt;code style=&quot;font-size: inherit; line-height: inherit; white-space: pre;&quot;&gt;&lt;span class=&quot;hljs-function&quot; style=&quot;color: #c678dd;&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt; &lt;/span&gt;&lt;span class=&quot;hljs-function&quot; style=&quot;color: #c678dd;&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt; &lt;/span&gt;&lt;span class=&quot;hljs-function&quot; style=&quot;color: #61aeee;&quot;&gt;SayHello&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hljs-function hljs-params&quot; style=&quot;color: #e6c07b;&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hljs-function hljs-params&quot;&gt; name&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt;)&lt;/span&gt;
{
    Console.WriteLine(&lt;span style=&quot;color: #98c379;&quot;&gt;&quot;Hello, &quot;&lt;/span&gt; + name + &lt;span style=&quot;color: #98c379;&quot;&gt;&quot;!&quot;&lt;/span&gt;);
}&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Call it with &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;SayHello(&quot;Alex&quot;);&lt;/span&gt;.&lt;/p&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;These topics will give you a solid launchpad. As you grow, you can explore arrays, object-oriented programming (OOP), and more advanced .NET features.&lt;/p&gt;
&lt;h2&gt;C# Language Fundamentals to Master&lt;/h2&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;C# is built on a few core concepts that shape how it works. Here’s what to focus on as a beginner:&lt;/p&gt;
&lt;h3&gt;1. Syntax and Structure&lt;/h3&gt;
&lt;ul class=&quot;marker:text-secondary&quot;&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Semicolons (&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;;&lt;/span&gt;)&lt;/strong&gt;: End most statements with a semicolon.&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Curly Braces (&lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;{}&lt;/span&gt;)&lt;/strong&gt;: Define code blocks for classes, methods, and control flow.&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Case Sensitivity&lt;/strong&gt;: &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;int&lt;/span&gt; is not the same as &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;Int&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;2. Object-Oriented Programming (OOP) Basics&lt;/h3&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;C# is an OOP language, meaning it’s all about objects and classes:&lt;/p&gt;
&lt;ul class=&quot;marker:text-secondary&quot;&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Classes&lt;/strong&gt;: Blueprints for objects (e.g., &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;class Program&lt;/span&gt;).&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Methods&lt;/strong&gt;: Actions a class can perform (e.g., &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;Main&lt;/span&gt;).&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;Later, you’ll dive into properties, constructors, and inheritance—but for now, just get comfy with classes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;3. Namespaces&lt;/h3&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Namespaces organize code and prevent name clashes. &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;using System;&lt;/span&gt; lets you use tools like &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;Console&lt;/span&gt; without typing &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;System.Console&lt;/span&gt; every time.&lt;/p&gt;
&lt;h3&gt;4. Static vs. Instance&lt;/h3&gt;
&lt;ul class=&quot;marker:text-secondary&quot;&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Static&lt;/strong&gt;: Belongs to the class itself (e.g., &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;static void Main&lt;/span&gt;). You don’t need an object to use it.&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Instance&lt;/strong&gt;: Requires an object (you’ll see this when you create your own classes).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;5. Type Safety&lt;/h3&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;C# is strongly typed—variables must have a specific type, and you can’t mix them willy-nilly. This catches errors early (e.g., you can’t assign &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;&quot;hello&quot;&lt;/span&gt; to an &lt;span class=&quot;text-sm px-1 rounded-sm !font-mono bg-sunset/10 text-rust dark:bg-dawn/10 dark:text-dawn&quot;&gt;int&lt;/span&gt;).&lt;/p&gt;
&lt;h2&gt;Tips for Success&lt;/h2&gt;
&lt;ul class=&quot;marker:text-secondary&quot;&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Practice&lt;/strong&gt;: Code every day, even if it’s just a small program.&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Debug&lt;/strong&gt;: Use Visual Studio’s debugger to step through code and fix mistakes.&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Explore&lt;/strong&gt;: Try modifying examples—like adding user input or loops to &quot;Hello, World!&quot;.&lt;/li&gt;&lt;br /&gt;
&lt;li class=&quot;break-words&quot;&gt;&lt;strong&gt;Resources&lt;/strong&gt;: Check out &lt;a href=&quot;https://learn.microsoft.com/en-us/dotnet/csharp/&quot; rel=&quot;noopener noreferrer&quot; target=&quot;_blank&quot;&gt;Microsoft’s C# documentation&lt;/a&gt;, free tutorials on YouTube, or books like &lt;em&gt;C# in a Nutshell&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Wrapping Up&lt;/h2&gt;
&lt;p class=&quot;break-words&quot; style=&quot;white-space-collapse: preserve;&quot;&gt;Getting started with C# is all about taking small, steady steps. Set up your environment, write your first program, and focus on variables, control flow, and methods. As you dig into the fundamentals—syntax, OOP, and namespaces—you’ll unlock the language’s real power. Before you know it, you’ll be building your own projects and wondering why you didn’t start sooner!&lt;/p&gt;&lt;/div&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/aspnet-core-mvc.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsHa9pE-mydaMlpUYGPzNC7YYt897FxezrgEfYBA79c85xwNiCZIldVZbQApbSofOJzPLboMcoLidS49KIdU_Zc0nyAY19m05c1AcOrtvGriilvvlPLUQr5Ui6rVYlHIMLrk3ASts6bOrL6NpOJ_1ODoytD-8lImJb9xwkcZUZii6JaIi25xwzecTAnqg/s72-c/240_F_397264965_O9QL56ghGrAERQMK.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-1377536984983692544</guid><pubDate>Sat, 01 Mar 2025 13:43:00 +0000</pubDate><atom:updated>2025-03-01T20:36:05.188+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">asp dot net</category><category domain="http://www.blogger.com/atom/ns#">asp.net</category><title>ASP.NET Core MVC</title><description>&lt;h1 data-end=&quot;99&quot; data-start=&quot;0&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbj8WHciQWd_bVmNwydGaNUerZ-n0_n63KRr3I2RmkK4g-iIUsrpliiuatHforLAgEN1G01ZxhRApZ7zYHDRLhPRCER5U6Rqr0gzR10vyXkHWghaaBGLH8J_wQw3AF7k1-V0ZWyJtTn-VcmFQ-es1Rsxl1J8J-pBVOiDX5ZRvni4opU_gfdY1r5Fjc/s361/240_F_397264965_O9QL56ghGrAERQMK.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;240&quot; data-original-width=&quot;361&quot; height=&quot;213&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbj8WHciQWd_bVmNwydGaNUerZ-n0_n63KRr3I2RmkK4g-iIUsrpliiuatHforLAgEN1G01ZxhRApZ7zYHDRLhPRCER5U6Rqr0gzR10vyXkHWghaaBGLH8J_wQw3AF7k1-V0ZWyJtTn-VcmFQ-es1Rsxl1J8J-pBVOiDX5ZRvni4opU_gfdY1r5Fjc/s320/240_F_397264965_O9QL56ghGrAERQMK.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Mastering ASP.NET Core MVC: Best Practices for Model Binding, Migration, and Dependency Injection&lt;/h1&gt;&lt;p data-end=&quot;611&quot; data-start=&quot;101&quot;&gt;ASP.NET Core MVC is an essential framework for modern web development. Whether you&#39;re a seasoned developer looking to optimize your application or someone transitioning from older frameworks, mastering three critical areas—Model Binding, Migration from ASP.NET MVC to ASP.NET Core, and Dependency Injection—will equip you with the knowledge to build powerful, maintainable web applications. Let’s dive deep into these concepts and explore the best practices that will help you level up your development skills.&lt;/p&gt;&lt;h2 data-end=&quot;662&quot; data-start=&quot;613&quot;&gt;1. Mastering Model Binding in ASP.NET Core MVC&lt;/h2&gt;&lt;h3 data-end=&quot;690&quot; data-start=&quot;664&quot;&gt;What is Model Binding?&lt;/h3&gt;&lt;p data-end=&quot;1016&quot; data-start=&quot;692&quot;&gt;Model binding is the process by which ASP.NET Core MVC automatically maps HTTP request data (e.g., query strings, form values, route data) to action method parameters or model properties. This eliminates the need to manually extract data from HTTP requests, making development more streamlined and reducing boilerplate code.&lt;/p&gt;&lt;h3 data-end=&quot;1051&quot; data-start=&quot;1018&quot;&gt;Key Concepts in Model Binding&lt;/h3&gt;&lt;ul data-end=&quot;1579&quot; data-start=&quot;1052&quot;&gt;&lt;li data-end=&quot;1168&quot; data-start=&quot;1052&quot;&gt;&lt;strong data-end=&quot;1076&quot; data-start=&quot;1054&quot;&gt;Primitive Binding:&lt;/strong&gt; Model binding can automatically map simple data types like strings, integers, and decimals.&lt;/li&gt;&lt;li data-end=&quot;1340&quot; data-start=&quot;1169&quot;&gt;&lt;strong data-end=&quot;1191&quot; data-start=&quot;1171&quot;&gt;Complex Binding:&lt;/strong&gt; It can map more complex objects, such as custom classes. For instance, a &lt;code data-end=&quot;1274&quot; data-start=&quot;1265&quot;&gt;Product&lt;/code&gt; class with multiple properties can be populated from form values.&lt;/li&gt;&lt;li data-end=&quot;1449&quot; data-start=&quot;1341&quot;&gt;&lt;strong data-end=&quot;1366&quot; data-start=&quot;1343&quot;&gt;Collection Binding:&lt;/strong&gt; ASP.NET Core MVC also supports binding to collections like lists and dictionaries.&lt;/li&gt;&lt;li data-end=&quot;1579&quot; data-start=&quot;1450&quot;&gt;&lt;strong data-end=&quot;1477&quot; data-start=&quot;1452&quot;&gt;Custom Model Binders:&lt;/strong&gt; For specialized needs, like handling encrypted query parameters, you can define custom model binders.&lt;/li&gt;&lt;/ul&gt;&lt;h3 data-end=&quot;1615&quot; data-start=&quot;1581&quot;&gt;Example: Complex Model Binding&lt;/h3&gt;&lt;p data-end=&quot;1700&quot; data-start=&quot;1617&quot;&gt;Let’s consider an example where we use complex model binding to create a &lt;code data-end=&quot;1699&quot; data-start=&quot;1690&quot;&gt;Product&lt;/code&gt;:&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot; data-end=&quot;2083&quot; data-start=&quot;1702&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-[5px] h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;csharp&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;ProductController&lt;/span&gt; : &lt;span class=&quot;hljs-title&quot;&gt;Controller&lt;/span&gt;  
{  
    [&lt;span class=&quot;hljs-meta&quot;&gt;HttpPost&lt;/span&gt;]  
    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; IActionResult &lt;span class=&quot;hljs-title&quot;&gt;Create&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;Product product&lt;/span&gt;)&lt;/span&gt;  
    {  
        &lt;span class=&quot;hljs-comment&quot;&gt;// &#39;product&#39; is auto-populated from form data  &lt;/span&gt;
        &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; View(product);  
    }  
}

&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;Product&lt;/span&gt;  
{  
    &lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; Id { &lt;span class=&quot;hljs-keyword&quot;&gt;get&lt;/span&gt;; &lt;span class=&quot;hljs-keyword&quot;&gt;set&lt;/span&gt;; }  
    &lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; Name { &lt;span class=&quot;hljs-keyword&quot;&gt;get&lt;/span&gt;; &lt;span class=&quot;hljs-keyword&quot;&gt;set&lt;/span&gt;; }  
    &lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;decimal&lt;/span&gt; Price { &lt;span class=&quot;hljs-keyword&quot;&gt;get&lt;/span&gt;; &lt;span class=&quot;hljs-keyword&quot;&gt;set&lt;/span&gt;; }  
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3 data-end=&quot;2104&quot; data-start=&quot;2085&quot;&gt;Common Pitfalls&lt;/h3&gt;&lt;p data-end=&quot;2192&quot; data-start=&quot;2106&quot;&gt;While model binding is powerful, there are a few common pitfalls developers encounter:&lt;/p&gt;&lt;ul data-end=&quot;2573&quot; data-start=&quot;2194&quot;&gt;&lt;li data-end=&quot;2422&quot; data-start=&quot;2194&quot;&gt;&lt;p data-end=&quot;2335&quot; data-start=&quot;2196&quot;&gt;&lt;strong data-end=&quot;2213&quot; data-start=&quot;2196&quot;&gt;Over-Posting:&lt;/strong&gt; Malicious users might submit unexpected or additional fields.&lt;br data-end=&quot;2278&quot; data-start=&quot;2275&quot; /&gt;&lt;strong data-end=&quot;2288&quot; data-start=&quot;2280&quot;&gt;Fix:&lt;/strong&gt; Use &lt;code data-end=&quot;2301&quot; data-start=&quot;2293&quot;&gt;[Bind]&lt;/code&gt; to whitelist specific properties:&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot; data-end=&quot;2422&quot; data-start=&quot;2339&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-[5px] h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;csharp&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; IActionResult &lt;span class=&quot;hljs-title&quot;&gt;Create&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;[Bind(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Name,Price&quot;&lt;/span&gt;&lt;/span&gt;)] Product product)
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li data-end=&quot;2573&quot; data-start=&quot;2424&quot;&gt;&lt;p data-end=&quot;2573&quot; data-start=&quot;2426&quot;&gt;&lt;strong data-end=&quot;2446&quot; data-start=&quot;2426&quot;&gt;Type Mismatches:&lt;/strong&gt; Non-nullable properties might cause validation errors when no value is provided. Always ensure that data types are consistent.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h3 data-end=&quot;2611&quot; data-start=&quot;2575&quot;&gt;Best Practices for Model Binding&lt;/h3&gt;&lt;ul data-end=&quot;3028&quot; data-start=&quot;2613&quot;&gt;&lt;li data-end=&quot;2760&quot; data-start=&quot;2613&quot;&gt;&lt;strong data-end=&quot;2634&quot; data-start=&quot;2615&quot;&gt;Use ViewModels:&lt;/strong&gt; Prefer binding to ViewModels rather than directly binding to entity classes to separate concerns and improve maintainability.&lt;/li&gt;&lt;li data-end=&quot;2841&quot; data-start=&quot;2761&quot;&gt;&lt;strong data-end=&quot;2783&quot; data-start=&quot;2763&quot;&gt;Validate Models:&lt;/strong&gt; Always check &lt;code data-end=&quot;2817&quot; data-start=&quot;2797&quot;&gt;ModelState.IsValid&lt;/code&gt; before processing data.&lt;/li&gt;&lt;li data-end=&quot;3028&quot; data-start=&quot;2842&quot;&gt;&lt;strong data-end=&quot;2863&quot; data-start=&quot;2844&quot;&gt;Use Attributes:&lt;/strong&gt; Use &lt;code data-end=&quot;2880&quot; data-start=&quot;2868&quot;&gt;[FromBody]&lt;/code&gt; for API payloads, &lt;code data-end=&quot;2911&quot; data-start=&quot;2899&quot;&gt;[FromForm]&lt;/code&gt; for form submissions, and &lt;code data-end=&quot;2951&quot; data-start=&quot;2938&quot;&gt;[FromQuery]&lt;/code&gt; for query string parameters to explicitly control where the data comes from.&lt;/li&gt;&lt;/ul&gt;&lt;h2 data-end=&quot;3078&quot; data-start=&quot;3030&quot;&gt;2. Migrating from ASP.NET MVC to ASP.NET Core&lt;/h2&gt;&lt;h3 data-end=&quot;3096&quot; data-start=&quot;3080&quot;&gt;Why Migrate?&lt;/h3&gt;&lt;p data-end=&quot;3171&quot; data-start=&quot;3098&quot;&gt;Migrating from ASP.NET MVC to ASP.NET Core comes with several advantages:&lt;/p&gt;&lt;ul data-end=&quot;3471&quot; data-start=&quot;3173&quot;&gt;&lt;li data-end=&quot;3250&quot; data-start=&quot;3173&quot;&gt;&lt;strong data-end=&quot;3202&quot; data-start=&quot;3175&quot;&gt;Cross-Platform Support:&lt;/strong&gt; ASP.NET Core runs on Windows, macOS, and Linux.&lt;/li&gt;&lt;li data-end=&quot;3346&quot; data-start=&quot;3251&quot;&gt;&lt;strong data-end=&quot;3275&quot; data-start=&quot;3253&quot;&gt;Performance Gains:&lt;/strong&gt; ASP.NET Core is faster and more optimized for modern web applications.&lt;/li&gt;&lt;li data-end=&quot;3471&quot; data-start=&quot;3347&quot;&gt;&lt;strong data-end=&quot;3383&quot; data-start=&quot;3349&quot;&gt;Unified MVC/Web API Framework:&lt;/strong&gt; In ASP.NET Core, MVC and Web API have been unified, making development more consistent.&lt;/li&gt;&lt;/ul&gt;&lt;h3 data-end=&quot;3529&quot; data-start=&quot;3473&quot;&gt;Key Differences Between ASP.NET MVC and ASP.NET Core&lt;/h3&gt;&lt;ul data-end=&quot;3917&quot; data-start=&quot;3531&quot;&gt;&lt;li data-end=&quot;3658&quot; data-start=&quot;3531&quot;&gt;&lt;strong data-end=&quot;3555&quot; data-start=&quot;3533&quot;&gt;Project Structure:&lt;/strong&gt; The project file structure changes from &lt;code data-end=&quot;3604&quot; data-start=&quot;3596&quot;&gt;.xproj&lt;/code&gt; to &lt;code data-end=&quot;3617&quot; data-start=&quot;3608&quot;&gt;.csproj&lt;/code&gt; in ASP.NET Core, simplifying management.&lt;/li&gt;&lt;li data-end=&quot;3780&quot; data-start=&quot;3659&quot;&gt;&lt;strong data-end=&quot;3685&quot; data-start=&quot;3661&quot;&gt;Middleware Pipeline:&lt;/strong&gt; ASP.NET Core replaces HTTP modules and handlers with middleware, providing better flexibility.&lt;/li&gt;&lt;li data-end=&quot;3917&quot; data-start=&quot;3781&quot;&gt;&lt;strong data-end=&quot;3808&quot; data-start=&quot;3783&quot;&gt;Dependency Injection:&lt;/strong&gt; ASP.NET Core has built-in support for Dependency Injection, eliminating the need for third-party containers.&lt;/li&gt;&lt;/ul&gt;&lt;h3 data-end=&quot;3951&quot; data-start=&quot;3919&quot;&gt;Step-by-Step Migration Guide&lt;/h3&gt;&lt;ol data-end=&quot;4656&quot; data-start=&quot;3953&quot;&gt;&lt;li data-end=&quot;4106&quot; data-start=&quot;3953&quot;&gt;&lt;p data-end=&quot;3993&quot; data-start=&quot;3956&quot;&gt;&lt;strong data-end=&quot;3993&quot; data-start=&quot;3956&quot;&gt;Create a New ASP.NET Core Project&lt;/strong&gt;&lt;/p&gt;&lt;p data-end=&quot;4052&quot; data-start=&quot;3998&quot;&gt;To get started, create a new ASP.NET Core MVC project:&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot; data-end=&quot;4106&quot; data-start=&quot;4057&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-[5px] h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;bash&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre language-bash&quot;&gt;dotnet new mvc -n MyMigratedApp
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li data-end=&quot;4298&quot; data-start=&quot;4108&quot;&gt;&lt;p data-end=&quot;4142&quot; data-start=&quot;4111&quot;&gt;&lt;strong data-end=&quot;4142&quot; data-start=&quot;4111&quot;&gt;Port Controllers and Models&lt;/strong&gt;&lt;/p&gt;&lt;ul data-end=&quot;4298&quot; data-start=&quot;4147&quot;&gt;&lt;li data-end=&quot;4212&quot; data-start=&quot;4147&quot;&gt;Copy your existing controllers to the new &lt;code data-end=&quot;4204&quot; data-start=&quot;4191&quot;&gt;Controllers&lt;/code&gt; folder.&lt;/li&gt;&lt;li data-end=&quot;4298&quot; data-start=&quot;4216&quot;&gt;Update namespaces (e.g., change &lt;code data-end=&quot;4266&quot; data-start=&quot;4250&quot;&gt;System.Web.Mvc&lt;/code&gt; to &lt;code data-end=&quot;4296&quot; data-start=&quot;4270&quot;&gt;Microsoft.AspNetCore.Mvc&lt;/code&gt;).&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li data-end=&quot;4510&quot; data-start=&quot;4300&quot;&gt;&lt;p data-end=&quot;4326&quot; data-start=&quot;4303&quot;&gt;&lt;strong data-end=&quot;4326&quot; data-start=&quot;4303&quot;&gt;Update Dependencies&lt;/strong&gt;&lt;/p&gt;&lt;ul data-end=&quot;4510&quot; data-start=&quot;4331&quot;&gt;&lt;li data-end=&quot;4407&quot; data-start=&quot;4331&quot;&gt;Replace &lt;code data-end=&quot;4358&quot; data-start=&quot;4341&quot;&gt;packages.config&lt;/code&gt; with &lt;code data-end=&quot;4384&quot; data-start=&quot;4364&quot;&gt;&amp;lt;PackageReference&amp;gt;&lt;/code&gt; in the &lt;code data-end=&quot;4401&quot; data-start=&quot;4392&quot;&gt;.csproj&lt;/code&gt; file.&lt;/li&gt;&lt;li data-end=&quot;4510&quot; data-start=&quot;4411&quot;&gt;Convert configuration settings from &lt;code data-end=&quot;4461&quot; data-start=&quot;4449&quot;&gt;Web.config&lt;/code&gt; to &lt;code data-end=&quot;4483&quot; data-start=&quot;4465&quot;&gt;appsettings.json&lt;/code&gt; and environment variables.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li data-end=&quot;4656&quot; data-start=&quot;4512&quot;&gt;&lt;p data-end=&quot;4537&quot; data-start=&quot;4515&quot;&gt;&lt;strong data-end=&quot;4537&quot; data-start=&quot;4515&quot;&gt;Test Incrementally&lt;/strong&gt;&lt;/p&gt;&lt;p data-end=&quot;4656&quot; data-start=&quot;4542&quot;&gt;Test the migrated code using tools like Swagger to validate API endpoints and ensure everything works as expected.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;h3 data-end=&quot;4675&quot; data-start=&quot;4658&quot;&gt;Common Issues&lt;/h3&gt;&lt;ul data-end=&quot;4915&quot; data-start=&quot;4677&quot;&gt;&lt;li data-end=&quot;4810&quot; data-start=&quot;4677&quot;&gt;&lt;strong data-end=&quot;4697&quot; data-start=&quot;4679&quot;&gt;Session State:&lt;/strong&gt; If your application relies on session state, you must configure it with &lt;code data-end=&quot;4793&quot; data-start=&quot;4770&quot;&gt;services.AddSession()&lt;/code&gt; in &lt;code data-end=&quot;4809&quot; data-start=&quot;4797&quot;&gt;Startup.cs&lt;/code&gt;.&lt;/li&gt;&lt;li data-end=&quot;4915&quot; data-start=&quot;4811&quot;&gt;&lt;strong data-end=&quot;4830&quot; data-start=&quot;4813&quot;&gt;Static Files:&lt;/strong&gt; Use &lt;code data-end=&quot;4857&quot; data-start=&quot;4835&quot;&gt;app.UseStaticFiles()&lt;/code&gt; to serve static content like images, CSS, and JavaScript.&lt;/li&gt;&lt;/ul&gt;&lt;h3 data-end=&quot;4935&quot; data-start=&quot;4917&quot;&gt;Best Practices&lt;/h3&gt;&lt;ul data-end=&quot;5146&quot; data-start=&quot;4937&quot;&gt;&lt;li data-end=&quot;5052&quot; data-start=&quot;4937&quot;&gt;&lt;strong data-end=&quot;4974&quot; data-start=&quot;4939&quot;&gt;Use the .NET Upgrade Assistant:&lt;/strong&gt; This tool automates code analysis and helps streamline the migration process.&lt;/li&gt;&lt;li data-end=&quot;5146&quot; data-start=&quot;5053&quot;&gt;&lt;strong data-end=&quot;5077&quot; data-start=&quot;5055&quot;&gt;Migrate in Phases:&lt;/strong&gt; Adopt the &quot;Side-by-Side&quot; approach to migrate features incrementally.&lt;/li&gt;&lt;/ul&gt;&lt;h2 data-end=&quot;5209&quot; data-start=&quot;5148&quot;&gt;3. Best Practices for Dependency Injection in ASP.NET Core&lt;/h2&gt;&lt;h3 data-end=&quot;5229&quot; data-start=&quot;5211&quot;&gt;Why DI Matters&lt;/h3&gt;&lt;p data-end=&quot;5390&quot; data-start=&quot;5231&quot;&gt;Dependency Injection (DI) is a core feature of ASP.NET Core, promoting loose coupling between components, making testing easier, and improving maintainability.&lt;/p&gt;&lt;h3 data-end=&quot;5413&quot; data-start=&quot;5392&quot;&gt;Service Lifetimes&lt;/h3&gt;&lt;p data-end=&quot;5482&quot; data-start=&quot;5415&quot;&gt;In ASP.NET Core, services are registered with three main lifetimes:&lt;/p&gt;&lt;ul data-end=&quot;5717&quot; data-start=&quot;5484&quot;&gt;&lt;li data-end=&quot;5563&quot; data-start=&quot;5484&quot;&gt;&lt;strong data-end=&quot;5500&quot; data-start=&quot;5486&quot;&gt;Transient:&lt;/strong&gt; A new instance is created every time the service is requested.&lt;/li&gt;&lt;li data-end=&quot;5646&quot; data-start=&quot;5564&quot;&gt;&lt;strong data-end=&quot;5577&quot; data-start=&quot;5566&quot;&gt;Scoped:&lt;/strong&gt; A single instance is used for the duration of a single HTTP request.&lt;/li&gt;&lt;li data-end=&quot;5717&quot; data-start=&quot;5647&quot;&gt;&lt;strong data-end=&quot;5663&quot; data-start=&quot;5649&quot;&gt;Singleton:&lt;/strong&gt; A single instance is shared across all HTTP requests.&lt;/li&gt;&lt;/ul&gt;&lt;h3 data-end=&quot;5752&quot; data-start=&quot;5719&quot;&gt;Example: Service Registration&lt;/h3&gt;&lt;p data-end=&quot;5809&quot; data-start=&quot;5754&quot;&gt;Here’s how to register services in &lt;code data-end=&quot;5808&quot; data-start=&quot;5789&quot;&gt;ConfigureServices&lt;/code&gt;:&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot; data-end=&quot;6062&quot; data-start=&quot;5811&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-[5px] h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;csharp&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre language-csharp&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;ConfigureServices&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;IServiceCollection services&lt;/span&gt;)&lt;/span&gt;  
{  
    services.AddTransient&amp;lt;IEmailService, EmailService&amp;gt;();  
    services.AddScoped&amp;lt;DbContext, AppDbContext&amp;gt;();  
    services.AddSingleton&amp;lt;ICacheService, RedisCache&amp;gt;();  
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3 data-end=&quot;6107&quot; data-start=&quot;6064&quot;&gt;Common Pitfalls in Dependency Injection&lt;/h3&gt;&lt;ul data-end=&quot;6354&quot; data-start=&quot;6109&quot;&gt;&lt;li data-end=&quot;6236&quot; data-start=&quot;6109&quot;&gt;&lt;strong data-end=&quot;6136&quot; data-start=&quot;6111&quot;&gt;Captive Dependencies:&lt;/strong&gt; Accidentally making a transient service captive in a singleton, which can cause performance issues.&lt;/li&gt;&lt;li data-end=&quot;6354&quot; data-start=&quot;6237&quot;&gt;&lt;strong data-end=&quot;6258&quot; data-start=&quot;6239&quot;&gt;Over-Injection:&lt;/strong&gt; Avoid bloated constructors with too many parameters. Refactor large services into smaller ones.&lt;/li&gt;&lt;/ul&gt;&lt;h3 data-end=&quot;6399&quot; data-start=&quot;6356&quot;&gt;Best Practices for Dependency Injection&lt;/h3&gt;&lt;ul data-end=&quot;6783&quot; data-start=&quot;6401&quot;&gt;&lt;li data-end=&quot;6543&quot; data-start=&quot;6401&quot;&gt;&lt;strong data-end=&quot;6429&quot; data-start=&quot;6403&quot;&gt;Constructor Injection:&lt;/strong&gt; Prefer constructor injection over property injection to ensure the dependencies are required for object creation.&lt;/li&gt;&lt;li data-end=&quot;6671&quot; data-start=&quot;6544&quot;&gt;&lt;strong data-end=&quot;6580&quot; data-start=&quot;6546&quot;&gt;Avoid Service Locator Pattern:&lt;/strong&gt; Avoid using patterns like &lt;code data-end=&quot;6636&quot; data-start=&quot;6607&quot;&gt;HttpContext.RequestServices&lt;/code&gt;, as it goes against DI principles.&lt;/li&gt;&lt;li data-end=&quot;6783&quot; data-start=&quot;6672&quot;&gt;&lt;strong data-end=&quot;6693&quot; data-start=&quot;6674&quot;&gt;Use Interfaces:&lt;/strong&gt; Always use interfaces for service contracts to simplify testing and maintain flexibility.&lt;/li&gt;&lt;/ul&gt;&lt;h2 data-end=&quot;6798&quot; data-start=&quot;6785&quot;&gt;Conclusion&lt;/h2&gt;&lt;p data-end=&quot;7228&quot; data-start=&quot;6800&quot;&gt;Mastering ASP.NET Core MVC is essential for modern web development. By understanding the nuances of model binding, migrating from legacy frameworks, and leveraging Dependency Injection effectively, you can build scalable and maintainable applications. Follow the best practices outlined in this guide to ensure your ASP.NET Core applications are optimized, secure, and ready for the demands of today’s web development landscape.&lt;/p&gt;&lt;p data-end=&quot;7393&quot; data-is-last-node=&quot;&quot; data-is-only-node=&quot;&quot; data-start=&quot;7230&quot;&gt;By adhering to these principles, you&#39;ll be on your way to becoming a more proficient ASP.NET Core developer, capable of tackling any challenge that comes your way.&lt;/p&gt;&lt;code&gt;
  &lt;p&gt;
  &lt;/p&gt;
  &lt;/code&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/aspnet-core-mvc-mastery-guide.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbj8WHciQWd_bVmNwydGaNUerZ-n0_n63KRr3I2RmkK4g-iIUsrpliiuatHforLAgEN1G01ZxhRApZ7zYHDRLhPRCER5U6Rqr0gzR10vyXkHWghaaBGLH8J_wQw3AF7k1-V0ZWyJtTn-VcmFQ-es1Rsxl1J8J-pBVOiDX5ZRvni4opU_gfdY1r5Fjc/s72-c/240_F_397264965_O9QL56ghGrAERQMK.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-489124117675611075</guid><pubDate>Sat, 01 Mar 2025 08:32:00 +0000</pubDate><atom:updated>2025-03-01T20:58:18.488+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">asp dot net</category><category domain="http://www.blogger.com/atom/ns#">asp.net</category><category domain="http://www.blogger.com/atom/ns#">sql</category><title> Optimizing SQL Queries and C#</title><description>&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKiGFGOP_tfCG6wd5cvvGPfPWBkfEyXiEKPBL1CJHCgESCeS-JFWv_IssxamicVwzT_YTmYZAIqJy118_blRpseWce_n88R6uj-TdYEE9JqUz7ChOowSr_1lYN49Nwt3KmweG3yemNGBn9Aggd5Y01hzHFnIveRD8UwX9ZjhBrietSuphWt8BKAlty/s1800/gradient-sql-illustration_23-214.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1000&quot; data-original-width=&quot;1800&quot; height=&quot;178&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKiGFGOP_tfCG6wd5cvvGPfPWBkfEyXiEKPBL1CJHCgESCeS-JFWv_IssxamicVwzT_YTmYZAIqJy118_blRpseWce_n88R6uj-TdYEE9JqUz7ChOowSr_1lYN49Nwt3KmweG3yemNGBn9Aggd5Y01hzHFnIveRD8UwX9ZjhBrietSuphWt8BKAlty/w354-h178/gradient-sql-illustration_23-214.png&quot; width=&quot;354&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&lt;p&gt;&lt;/p&gt;&lt;h1&gt;Optimizing SQL Queries, Handling JSON Data, and Using Stored Procedures Effectively in SQL Server&lt;/h1&gt;
&lt;p&gt;SQL Server is a powerful relational database management system that provides extensive capabilities for query optimization, JSON data handling, and stored procedure execution. This guide focuses on best practices for improving SQL performance, efficiently managing JSON data, and leveraging stored procedures in C# applications.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;1. Optimizing SQL Queries for Performance: Indexing, Joins, and Best Practices&lt;/h2&gt;
&lt;p&gt;Optimizing SQL queries is essential for maintaining high-performance database operations. Below are key strategies for improving query performance:&lt;/p&gt;
&lt;h3&gt;1.1 Indexing Strategies&lt;/h3&gt;
&lt;p&gt;Indexes speed up queries by reducing the number of rows SQL Server needs to scan. Consider the following best practices:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use Clustered Indexes:&lt;/strong&gt; Each table should have a clustered index, usually on the primary key, to improve retrieval speed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Leverage Non-Clustered Indexes:&lt;/strong&gt; Create non-clustered indexes for frequently queried columns to enhance performance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Covering Indexes:&lt;/strong&gt; Include all required columns in an index to avoid lookup operations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Avoid Over-Indexing:&lt;/strong&gt; Too many indexes can degrade write performance due to index maintenance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Indexed Views:&lt;/strong&gt; Materialized views can precompute and store results to optimize query performance.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;CREATE NONCLUSTERED INDEX IX_Orders_CustomerID ON Orders(CustomerID);
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;1.2 Optimizing Joins&lt;/h3&gt;
&lt;p&gt;Understanding join types and their performance implications is crucial:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;INNER JOIN:&lt;/strong&gt; Retrieves matching records from both tables. Fastest join if indexes are used properly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LEFT JOIN:&lt;/strong&gt; Returns all rows from the left table and matching rows from the right. Optimize with indexes on the join column.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RIGHT JOIN:&lt;/strong&gt; Similar to LEFT JOIN but from the right table.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;FULL OUTER JOIN:&lt;/strong&gt; Returns all rows when there is a match in either table; can be slow on large datasets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CROSS JOIN:&lt;/strong&gt; Cartesian product of two tables; avoid unless necessary.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;SELECT o.OrderID, c.CustomerName
FROM Orders o
INNER JOIN Customers c ON o.CustomerID = c.CustomerID;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;1.3 General Best Practices&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code inline=&quot;&quot;&gt;EXPLAIN&lt;/code&gt; or &lt;code inline=&quot;&quot;&gt;SHOW PLAN&lt;/code&gt; to analyze query execution plans.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Avoid &lt;code inline=&quot;&quot;&gt;SELECT *&lt;/code&gt; and retrieve only necessary columns.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code inline=&quot;&quot;&gt;TOP&lt;/code&gt; or &lt;code inline=&quot;&quot;&gt;LIMIT&lt;/code&gt; to reduce the dataset size when possible.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Normalize database schema to reduce redundancy and optimize storage.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Batch updates instead of performing large transactions to minimize locking.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;2. Handling JSON Data in SQL Server: Storage, Queries, and Performance Tips&lt;/h2&gt;
&lt;p&gt;SQL Server supports JSON functions, making it easier to work with semi-structured data within relational databases.&lt;/p&gt;
&lt;h3&gt;2.1 Storing JSON Data&lt;/h3&gt;
&lt;p&gt;JSON can be stored in &lt;code inline=&quot;&quot;&gt;NVARCHAR(MAX)&lt;/code&gt; columns. This approach is useful when dealing with dynamic or semi-structured data.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    CustomerData NVARCHAR(MAX) -- Stores JSON data
);

INSERT INTO Customers VALUES (1, &#39;{&quot;Name&quot;: &quot;John Doe&quot;, &quot;Email&quot;: &quot;john@example.com&quot;}&#39;);
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2.2 Querying JSON Data&lt;/h3&gt;
&lt;p&gt;Use SQL Server’s built-in JSON functions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code inline=&quot;&quot;&gt;JSON_VALUE&lt;/code&gt;: Extracts a single value.&lt;/li&gt;
&lt;li&gt;&lt;code inline=&quot;&quot;&gt;JSON_QUERY&lt;/code&gt;: Extracts an array or object.&lt;/li&gt;
&lt;li&gt;&lt;code inline=&quot;&quot;&gt;OPENJSON&lt;/code&gt;: Parses JSON into rows and columns.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;SELECT JSON_VALUE(CustomerData, &#39;$.Name&#39;) AS CustomerName
FROM Customers;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2.3 Performance Considerations&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use Computed Columns with Indexing:&lt;/strong&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;ALTER TABLE Customers ADD CustomerName AS JSON_VALUE(CustomerData, &#39;$.Name&#39;) PERSISTED;
CREATE INDEX IX_Customers_CustomerName ON Customers(CustomerName);
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Avoid Storing Large JSON Blobs&lt;/strong&gt; in frequently queried tables; use separate tables for structured data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prefer JSON_TABLE in SQL Server 2022+&lt;/strong&gt; for better JSON query performance.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;3. Using Stored Procedures Effectively in C# and SQL Server&lt;/h2&gt;
&lt;p&gt;Stored procedures improve performance, enhance security, and simplify database management. Here’s how to use them effectively.&lt;/p&gt;
&lt;h3&gt;3.1 Creating and Executing Stored Procedures&lt;/h3&gt;
&lt;p&gt;Example Stored Procedure:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;CREATE PROCEDURE GetCustomerOrders
    @CustomerID INT
AS
BEGIN
    SELECT OrderID, OrderDate, TotalAmount
    FROM Orders
    WHERE CustomerID = @CustomerID;
END;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Executing from SQL:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;EXEC GetCustomerOrders @CustomerID = 1;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;3.2 Using Stored Procedures in C#&lt;/h3&gt;
&lt;p&gt;Using ADO.NET:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;using (SqlConnection conn = new SqlConnection(&quot;your_connection_string&quot;))
{
    SqlCommand cmd = new SqlCommand(&quot;GetCustomerOrders&quot;, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue(&quot;@CustomerID&quot;, 1);
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine($&quot;OrderID: {reader[&quot;OrderID&quot;]}, Total: {reader[&quot;TotalAmount&quot;]}&quot;);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;3.3 Best Practices for Stored Procedures&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use Parameters Instead of Dynamic SQL&lt;/strong&gt; to prevent SQL injection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Return Result Sets Instead of Multiple Output Parameters&lt;/strong&gt; for better readability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Minimize Business Logic in Stored Procedures&lt;/strong&gt; to maintain application-layer flexibility.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use TRY...CATCH for Error Handling&lt;/strong&gt; in stored procedures.
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;BEGIN TRY
    -- SQL logic
END TRY
BEGIN CATCH
    -- Handle error
END CATCH
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;Avoid SELECT &lt;em&gt;; Specify Required Columns&lt;/em&gt;&lt;/em&gt; for performance optimization.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;By implementing these best practices, you can significantly enhance the performance and efficiency of your SQL Server queries, JSON data handling, and stored procedure execution in C# applications. Whether you’re optimizing indexing strategies, querying JSON efficiently, or leveraging stored procedures for data access, following these principles will help you build scalable and high-performance applications. 🚀&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/03/optimizing-sql-queries-and-c.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKiGFGOP_tfCG6wd5cvvGPfPWBkfEyXiEKPBL1CJHCgESCeS-JFWv_IssxamicVwzT_YTmYZAIqJy118_blRpseWce_n88R6uj-TdYEE9JqUz7ChOowSr_1lYN49Nwt3KmweG3yemNGBn9Aggd5Y01hzHFnIveRD8UwX9ZjhBrietSuphWt8BKAlty/s72-w354-h178-c/gradient-sql-illustration_23-214.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-3035983594680516422</guid><pubDate>Fri, 28 Feb 2025 15:14:00 +0000</pubDate><atom:updated>2025-03-01T00:20:08.315+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">asp</category><category domain="http://www.blogger.com/atom/ns#">asp dot net</category><title>JSON Serialization in C# </title><description>&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_1vzmcJW8DYuegQhVd7-hTsy1_6cFoN9UKNzIJ2ywp6tq1G6E6aI7r3BXpxlN2pSJkhdaGmsnSDwc8QXeJVbEpdYW9eFfXY91iABGhnxK8tbwEcDY6cUn75I4g9VnfA4SgaxmH-1n2OCLFyEMzgXC1MrYF45EGfabToK3p829AeIM1b-jCUGvfr4W/s900/desktop-smartphone-app-developme.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;900&quot; data-original-width=&quot;900&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_1vzmcJW8DYuegQhVd7-hTsy1_6cFoN9UKNzIJ2ywp6tq1G6E6aI7r3BXpxlN2pSJkhdaGmsnSDwc8QXeJVbEpdYW9eFfXY91iABGhnxK8tbwEcDY6cUn75I4g9VnfA4SgaxmH-1n2OCLFyEMzgXC1MrYF45EGfabToK3p829AeIM1b-jCUGvfr4W/w348-h320/desktop-smartphone-app-developme.png&quot; width=&quot;348&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&lt;p&gt;&lt;/p&gt;&lt;h1&gt;Optimizing JSON Serialization in C# with System.Text.Json vs. Newtonsoft.Json&lt;/h1&gt;
&lt;p&gt;JSON serialization is a critical part of modern .NET applications, especially when dealing with APIs, data storage, and inter-service communication. In this guide, we will explore how to optimize JSON serialization in C# using &lt;code inline=&quot;&quot;&gt;System.Text.Json&lt;/code&gt; and &lt;code inline=&quot;&quot;&gt;Newtonsoft.Json&lt;/code&gt;. We will also dive into handling complex JSON structures efficiently and discuss best practices for JSON manipulation.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;1. Choosing Between System.Text.Json and Newtonsoft.Json&lt;/h2&gt;
&lt;p&gt;C# developers have two primary JSON serialization libraries: &lt;code inline=&quot;&quot;&gt;System.Text.Json&lt;/code&gt; (built-in since .NET Core 3.0) and &lt;code inline=&quot;&quot;&gt;Newtonsoft.Json&lt;/code&gt; (a widely-used third-party library). Let’s compare them:&lt;/p&gt;
&lt;h3&gt;Performance Comparison&lt;/h3&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;System.Text.Json&lt;/th&gt;
&lt;th&gt;Newtonsoft.Json&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Performance&lt;/td&gt;
&lt;td&gt;Faster due to lower memory allocation&lt;/td&gt;
&lt;td&gt;Slower due to extensive features&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serialization Speed&lt;/td&gt;
&lt;td&gt;Faster&lt;/td&gt;
&lt;td&gt;Slower&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Customization&lt;/td&gt;
&lt;td&gt;Limited options&lt;/td&gt;
&lt;td&gt;Highly customizable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CamelCase Naming&lt;/td&gt;
&lt;td&gt;Default&lt;/td&gt;
&lt;td&gt;Requires setting&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Null Handling&lt;/td&gt;
&lt;td&gt;Default ignores nulls&lt;/td&gt;
&lt;td&gt;Includes nulls by default&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3&gt;When to Use Which?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use System.Text.Json&lt;/strong&gt; if performance and memory efficiency are critical.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Newtonsoft.Json&lt;/strong&gt; if you need advanced features like custom converters, polymorphic serialization, or deep control over serialization settings.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;2. Optimizing JSON Serialization&lt;/h2&gt;
&lt;h3&gt;System.Text.Json Optimization Techniques&lt;/h3&gt;
&lt;h4&gt;2.1. Use &lt;code inline=&quot;&quot;&gt;JsonSerializerOptions&lt;/code&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;var options = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    WriteIndented = false,
    DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};

string jsonString = JsonSerializer.Serialize(myObject, options);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Reduces output size by ignoring null values.&lt;/li&gt;
&lt;li&gt;Ensures consistent casing in property names.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;2.2. Use &lt;code inline=&quot;&quot;&gt;Utf8JsonWriter&lt;/code&gt; for High-Performance Writing&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream);

writer.WriteStartObject();
writer.WriteString(&quot;name&quot;, &quot;John Doe&quot;);
writer.WriteNumber(&quot;age&quot;, 30);
writer.WriteEndObject();
writer.Flush();

string json = Encoding.UTF8.GetString(stream.ToArray());
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Avoids unnecessary object allocations.&lt;/li&gt;
&lt;li&gt;Improves serialization speed for large JSON structures.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Newtonsoft.Json Optimization Techniques&lt;/h3&gt;
&lt;h4&gt;2.3. Use &lt;code inline=&quot;&quot;&gt;JsonSerializerSettings&lt;/code&gt;&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;var settings = new JsonSerializerSettings
{
    Formatting = Formatting.None,
    NullValueHandling = NullValueHandling.Ignore,
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

string jsonString = JsonConvert.SerializeObject(myObject, settings);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Reduces JSON size by ignoring null properties.&lt;/li&gt;
&lt;li&gt;Uses camelCase naming convention.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;2.4. Use &lt;code inline=&quot;&quot;&gt;JsonTextWriter&lt;/code&gt; for Efficiency&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;using var stringWriter = new StringWriter();
using var jsonWriter = new JsonTextWriter(stringWriter);

jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName(&quot;name&quot;);
jsonWriter.WriteValue(&quot;John Doe&quot;);
jsonWriter.WritePropertyName(&quot;age&quot;);
jsonWriter.WriteValue(30);
jsonWriter.WriteEndObject();

string json = stringWriter.ToString();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Provides fine-grained control over JSON writing.&lt;/li&gt;
&lt;li&gt;Improves performance in high-throughput scenarios.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;3. Handling Complex JSON Objects in C#&lt;/h2&gt;
&lt;h3&gt;3.1. Using &lt;code inline=&quot;&quot;&gt;JsonDocument&lt;/code&gt; and &lt;code inline=&quot;&quot;&gt;JsonElement&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;For scenarios where you need to work with JSON dynamically without deserialization:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;string json = &quot;{&quot;name&quot;:&quot;John Doe&quot;,&quot;age&quot;:30,&quot;contacts&quot;:{&quot;email&quot;:&quot;john@example.com&quot;}}&quot;;

using JsonDocument doc = JsonDocument.Parse(json);
JsonElement root = doc.RootElement;

string name = root.GetProperty(&quot;name&quot;).GetString();
int age = root.GetProperty(&quot;age&quot;).GetInt32();
string email = root.GetProperty(&quot;contacts&quot;).GetProperty(&quot;email&quot;).GetString();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Avoids unnecessary deserialization overhead.&lt;/li&gt;
&lt;li&gt;Efficient for read-heavy JSON processing.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;3.2. Using &lt;code inline=&quot;&quot;&gt;JObject&lt;/code&gt; from Newtonsoft.Json&lt;/h3&gt;
&lt;p&gt;For scenarios where you need flexibility in JSON parsing:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;JObject jsonObj = JObject.Parse(json);
string name = jsonObj[&quot;name&quot;].ToString();
int age = jsonObj[&quot;age&quot;].ToObject&amp;lt;int&amp;gt;();
string email = jsonObj[&quot;contacts&quot;][&quot;email&quot;].ToString();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Provides an intuitive way to navigate JSON.&lt;/li&gt;
&lt;li&gt;More flexible but slightly slower than &lt;code inline=&quot;&quot;&gt;JsonDocument&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;4. Best Practices for JSON Parsing and Manipulation&lt;/h2&gt;
&lt;h3&gt;4.1. Avoid Large Object Allocations&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;code inline=&quot;&quot;&gt;JsonDocument&lt;/code&gt; instead of deserializing into large objects when only reading specific properties.&lt;/li&gt;
&lt;li&gt;Example: Extracting values without deserializing entire JSON.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;4.2. Use Streams for Large JSON Files&lt;/h3&gt;
&lt;p&gt;For handling large JSON files efficiently:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;using var stream = File.OpenRead(&quot;large.json&quot;);
using var json = JsonDocument.Parse(stream);
JsonElement root = json.RootElement;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Reduces memory usage by processing data as a stream.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;4.3. Use &lt;code inline=&quot;&quot;&gt;Span&amp;lt;T&amp;gt;&lt;/code&gt; and &lt;code inline=&quot;&quot;&gt;Memory&amp;lt;T&amp;gt;&lt;/code&gt; for Performance&lt;/h3&gt;
&lt;p&gt;For scenarios requiring efficient string manipulation within JSON:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;ReadOnlySpan&amp;lt;byte&amp;gt; jsonSpan = Encoding.UTF8.GetBytes(jsonString);
JsonDocument doc = JsonDocument.Parse(jsonSpan);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Avoids unnecessary memory allocations.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;5. Common Pitfalls to Avoid&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Pitfall&lt;/th&gt;
&lt;th&gt;Solution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Large object allocations&lt;/td&gt;
&lt;td&gt;Use &lt;code inline=&quot;&quot;&gt;JsonDocument&lt;/code&gt; for read-only operations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Excessive serialization/deserialization&lt;/td&gt;
&lt;td&gt;Cache serialized results where possible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Using Newtonsoft.Json unnecessarily&lt;/td&gt;
&lt;td&gt;Prefer &lt;code inline=&quot;&quot;&gt;System.Text.Json&lt;/code&gt; for better performance&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Not handling missing properties&lt;/td&gt;
&lt;td&gt;Use &lt;code inline=&quot;&quot;&gt;TryGetProperty()&lt;/code&gt; to avoid exceptions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;hr /&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Optimizing JSON serialization in C# is all about choosing the right tool for the job.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code inline=&quot;&quot;&gt;System.Text.Json&lt;/code&gt;&lt;/strong&gt; for better performance, lower memory footprint, and built-in .NET support.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code inline=&quot;&quot;&gt;Newtonsoft.Json&lt;/code&gt;&lt;/strong&gt; when you need advanced customization, LINQ-to-JSON, or legacy compatibility.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code inline=&quot;&quot;&gt;JsonDocument&lt;/code&gt; and &lt;code inline=&quot;&quot;&gt;JsonElement&lt;/code&gt;&lt;/strong&gt; for efficient JSON parsing without unnecessary allocations.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By following these best practices, you can significantly improve the performance and reliability of your JSON handling in C#. 🚀&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/02/optimizing-json-serialization-in-c-with.html</link><author>noreply@blogger.com (Pratik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_1vzmcJW8DYuegQhVd7-hTsy1_6cFoN9UKNzIJ2ywp6tq1G6E6aI7r3BXpxlN2pSJkhdaGmsnSDwc8QXeJVbEpdYW9eFfXY91iABGhnxK8tbwEcDY6cUn75I4g9VnfA4SgaxmH-1n2OCLFyEMzgXC1MrYF45EGfabToK3p829AeIM1b-jCUGvfr4W/s72-w348-h320-c/desktop-smartphone-app-developme.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-128384872821327223</guid><pubDate>Sat, 18 Jan 2025 14:36:00 +0000</pubDate><atom:updated>2025-03-01T19:16:24.953+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><title>What do you need to study while studying Javascript?</title><description>&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwjFjIXlkB6ugjGTB58VZlaVqyARrvTpGa0739-HGN0e4H1E2n0VLlANSp3MRJXzYFeos-JSGtejuiva28fKKGQnUNwerZiCa3aWSVfVssHcR7wUJD9DkgeqI3MofZy5GjzXNuEO5S-ZjqDKWd7uwg31xPgDurULzp0ixyPe28iI0udA4FklMYs_C4o1Q/s626/programmers-using-javascript-pro.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;417&quot; data-original-width=&quot;626&quot; height=&quot;213&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwjFjIXlkB6ugjGTB58VZlaVqyARrvTpGa0739-HGN0e4H1E2n0VLlANSp3MRJXzYFeos-JSGtejuiva28fKKGQnUNwerZiCa3aWSVfVssHcR7wUJD9DkgeqI3MofZy5GjzXNuEO5S-ZjqDKWd7uwg31xPgDurULzp0ixyPe28iI0udA4FklMYs_C4o1Q/s320/programmers-using-javascript-pro.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;20 Key JavaScript Concepts to Master&lt;/span&gt;&lt;/strong&gt;&lt;/h2&gt;&lt;p data-sourcepos=&quot;3:1-3:93&quot;&gt;Here are 20 essential concepts to delve deeper into as you embark on your JavaScript journey:&lt;/p&gt;&lt;ol data-sourcepos=&quot;5:1-84:0&quot; style=&quot;text-align: left;&quot;&gt;
&lt;li data-sourcepos=&quot;5:1-8:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Variables &amp;amp; Data Types:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;6:5-8:0&quot;&gt;
&lt;li data-sourcepos=&quot;6:5-6:126&quot;&gt;&lt;strong&gt;Beyond &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;:&lt;/strong&gt; Understand the nuances of each keyword, including scoping, hoisting, and immutability.&lt;/li&gt;
&lt;li data-sourcepos=&quot;7:5-8:0&quot;&gt;&lt;strong&gt;Data Type Coercion:&lt;/strong&gt; Be mindful of how JavaScript implicitly converts data types, which can lead to unexpected results.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;9:1-12:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Conditionals:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;10:5-12:0&quot;&gt;
&lt;li data-sourcepos=&quot;10:5-10:122&quot;&gt;&lt;strong&gt;Truthy &amp;amp; Falsy Values:&lt;/strong&gt; Master the concept of truthy and falsy values and how they impact conditional checks.&lt;/li&gt;
&lt;li data-sourcepos=&quot;11:5-12:0&quot;&gt;&lt;strong&gt;Nested Conditionals:&lt;/strong&gt; Learn to handle complex scenarios using nested &lt;code&gt;if...else&lt;/code&gt; statements effectively.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;13:1-16:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Loops:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;14:5-16:0&quot;&gt;
&lt;li data-sourcepos=&quot;14:5-14:133&quot;&gt;&lt;strong&gt;Iterating over Arrays:&lt;/strong&gt; Explore different looping methods (e.g., &lt;code&gt;for...of&lt;/code&gt;, &lt;code&gt;forEach&lt;/code&gt;) for efficiently working with arrays.&lt;/li&gt;
&lt;li data-sourcepos=&quot;15:5-16:0&quot;&gt;&lt;strong&gt;Breaking &amp;amp; Continuing Loops:&lt;/strong&gt; Understand how to control the flow of loops using &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; statements.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;17:1-20:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Functions:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;18:5-20:0&quot;&gt;
&lt;li data-sourcepos=&quot;18:5-18:153&quot;&gt;&lt;strong&gt;Higher-Order Functions:&lt;/strong&gt; Learn about functions that can take other functions as arguments or return functions (e.g., &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;).&lt;/li&gt;
&lt;li data-sourcepos=&quot;19:5-20:0&quot;&gt;&lt;strong&gt;Closures:&lt;/strong&gt; Grasp the concept of closures, where a function &quot;remembers&quot; the environment in which it was created.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;21:1-24:0&quot;&gt;
&lt;p data-sourcepos=&quot;21:4-21:14&quot;&gt;&lt;strong&gt;Arrays:&lt;/strong&gt;&lt;/p&gt;
&lt;ul data-sourcepos=&quot;22:5-24:0&quot;&gt;
&lt;li data-sourcepos=&quot;22:5-22:137&quot;&gt;&lt;strong&gt;Array Methods in Depth:&lt;/strong&gt; Explore advanced array methods like &lt;code&gt;findIndex()&lt;/code&gt;, &lt;code&gt;some()&lt;/code&gt;, &lt;code&gt;every()&lt;/code&gt;, and understand their use cases.&lt;/li&gt;
&lt;li data-sourcepos=&quot;23:5-24:0&quot;&gt;&lt;strong&gt;Multidimensional Arrays:&lt;/strong&gt; Learn how to work with arrays of arrays and access elements within them.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;25:1-28:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Objects:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;26:5-28:0&quot;&gt;
&lt;li data-sourcepos=&quot;26:5-26:165&quot;&gt;&lt;strong&gt;Object-Oriented Programming:&lt;/strong&gt; Understand how objects can be used to model real-world entities and how they relate to object-oriented programming principles.&lt;/li&gt;
&lt;li data-sourcepos=&quot;27:5-28:0&quot;&gt;&lt;strong&gt;Prototypal Inheritance:&lt;/strong&gt; Explore JavaScript&#39;s prototypal inheritance model and how it affects object relationships.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;29:1-32:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;code&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;this&lt;/span&gt;&lt;/code&gt;&lt;span style=&quot;font-size: small;&quot;&gt; Keyword:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;30:5-32:0&quot;&gt;
&lt;li data-sourcepos=&quot;30:5-30:147&quot;&gt;&lt;strong&gt;Contextual Binding:&lt;/strong&gt; Deeply understand how &lt;code&gt;this&lt;/code&gt; is determined in different contexts (e.g., regular functions, arrow functions, methods).&lt;/li&gt;
&lt;li data-sourcepos=&quot;31:5-32:0&quot;&gt;&lt;strong&gt;Binding &lt;code&gt;this&lt;/code&gt;:&lt;/strong&gt; Learn techniques for binding &lt;code&gt;this&lt;/code&gt; to a specific object (e.g., &lt;code&gt;bind()&lt;/code&gt;, &lt;code&gt;call()&lt;/code&gt;, &lt;code&gt;apply()&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;33:1-36:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Template Literals:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;34:5-36:0&quot;&gt;
&lt;li data-sourcepos=&quot;34:5-34:127&quot;&gt;&lt;strong&gt;Multi-line Strings:&lt;/strong&gt; Leverage template literals to easily create multi-line strings and embed expressions within them.&lt;/li&gt;
&lt;li data-sourcepos=&quot;35:5-36:0&quot;&gt;&lt;strong&gt;Tagged Templates:&lt;/strong&gt; Explore the advanced use of tagged templates to create custom string formatting functions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;37:1-40:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Destructuring:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;38:5-40:0&quot;&gt;
&lt;li data-sourcepos=&quot;38:5-38:114&quot;&gt;&lt;strong&gt;Nested Destructuring:&lt;/strong&gt; Master nested destructuring for extracting values from complex objects and arrays.&lt;/li&gt;
&lt;li data-sourcepos=&quot;39:5-40:0&quot;&gt;&lt;strong&gt;Default Values:&lt;/strong&gt; Utilize default values in destructuring to provide fallback values when certain properties are missing.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;41:1-44:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, &lt;code&gt;var&lt;/code&gt;:&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;42:5-44:0&quot;&gt;
&lt;li data-sourcepos=&quot;42:5-42:132&quot;&gt;&lt;strong&gt;Block Scoping:&lt;/strong&gt; Understand how &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; introduce block-level scoping, which can help prevent unexpected behavior.&lt;/li&gt;
&lt;li data-sourcepos=&quot;43:5-44:0&quot;&gt;&lt;strong&gt;Hoisting Implications:&lt;/strong&gt; Be aware of how hoisting affects variable accessibility and potential pitfalls with &lt;code&gt;var&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;45:1-48:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Error Handling:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;46:5-48:0&quot;&gt;
&lt;li data-sourcepos=&quot;46:5-46:117&quot;&gt;&lt;strong&gt;Custom Error Types:&lt;/strong&gt; Learn to create your own custom error types to provide more specific error information.&lt;/li&gt;
&lt;li data-sourcepos=&quot;47:5-48:0&quot;&gt;&lt;strong&gt;&lt;code&gt;finally&lt;/code&gt; Block:&lt;/strong&gt; Understand the purpose of the &lt;code&gt;finally&lt;/code&gt; block and how it can be used for cleanup operations.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;49:1-52:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Event Handling:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;50:5-52:0&quot;&gt;
&lt;li data-sourcepos=&quot;50:5-50:124&quot;&gt;&lt;strong&gt;Event Propagation:&lt;/strong&gt; Deeply understand the event bubbling and capturing phases and how to control event propagation.&lt;/li&gt;
&lt;li data-sourcepos=&quot;51:5-52:0&quot;&gt;&lt;strong&gt;Event Delegation:&lt;/strong&gt; Learn how to efficiently handle events on multiple elements using event delegation.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;53:1-56:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;JavaScript Timers:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;54:5-56:0&quot;&gt;
&lt;li data-sourcepos=&quot;54:5-54:141&quot;&gt;&lt;strong&gt;Cancelling Timers:&lt;/strong&gt; Understand how to cancel &lt;code&gt;setTimeout()&lt;/code&gt; and &lt;code&gt;setInterval()&lt;/code&gt; timers using &lt;code&gt;clearTimeout()&lt;/code&gt; and &lt;code&gt;clearInterval()&lt;/code&gt;.&lt;/li&gt;
&lt;li data-sourcepos=&quot;55:5-56:0&quot;&gt;&lt;strong&gt;RequestAnimationFrame:&lt;/strong&gt; Explore &lt;code&gt;requestAnimationFrame()&lt;/code&gt; for smoother animations and better performance.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;57:1-60:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;DOM Manipulation:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;58:5-60:0&quot;&gt;
&lt;li data-sourcepos=&quot;58:5-58:146&quot;&gt;&lt;strong&gt;Traversing the DOM:&lt;/strong&gt; Learn to navigate the DOM tree efficiently using methods like &lt;code&gt;parentNode&lt;/code&gt;, &lt;code&gt;childNodes&lt;/code&gt;, &lt;code&gt;nextElementSibling&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li data-sourcepos=&quot;59:5-60:0&quot;&gt;&lt;strong&gt;Dynamically Creating Elements:&lt;/strong&gt; Understand how to create and insert new elements into the DOM using JavaScript.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;61:1-64:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Scope &amp;amp; Closures:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;62:5-64:0&quot;&gt;
&lt;li data-sourcepos=&quot;62:5-62:103&quot;&gt;&lt;strong&gt;Private Variables:&lt;/strong&gt; Utilize closures to create private variables and methods within functions.&lt;/li&gt;
&lt;li data-sourcepos=&quot;63:5-64:0&quot;&gt;&lt;strong&gt;Module Patterns:&lt;/strong&gt; Explore how closures can be used to implement module patterns for better code organization.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;65:1-68:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Operators:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;66:5-68:0&quot;&gt;
&lt;li data-sourcepos=&quot;66:5-66:113&quot;&gt;&lt;strong&gt;Operator Precedence:&lt;/strong&gt; Understand how operator precedence affects the order of evaluation in expressions.&lt;/li&gt;
&lt;li data-sourcepos=&quot;67:5-68:0&quot;&gt;&lt;strong&gt;Bitwise Operators:&lt;/strong&gt; Learn about bitwise operators (e.g., &lt;code&gt;&amp;amp;&lt;/code&gt;, &lt;code&gt;|&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt;) and their applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;69:1-72:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Classes:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;70:5-72:0&quot;&gt;
&lt;li data-sourcepos=&quot;70:5-70:121&quot;&gt;&lt;strong&gt;Inheritance &amp;amp; Polymorphism:&lt;/strong&gt; Explore how to implement inheritance and polymorphism using JavaScript classes.&lt;/li&gt;
&lt;li data-sourcepos=&quot;71:5-72:0&quot;&gt;&lt;strong&gt;Static Methods &amp;amp; Properties:&lt;/strong&gt; Understand the purpose and usage of static members in classes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;73:1-76:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Arrays vs. Objects:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;74:5-76:0&quot;&gt;
&lt;li data-sourcepos=&quot;74:5-74:141&quot;&gt;&lt;strong&gt;Choosing the Right Data Structure:&lt;/strong&gt; Learn to choose the appropriate data structure (array or object) based on the specific use case.&lt;/li&gt;
&lt;li data-sourcepos=&quot;75:5-76:0&quot;&gt;&lt;strong&gt;Hybrid Approaches:&lt;/strong&gt; Explore how to combine arrays and objects to create more complex data structures.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;77:1-80:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;code&gt;for...in&lt;/code&gt; vs. &lt;code&gt;for...of&lt;/code&gt;:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;78:5-80:0&quot;&gt;
&lt;li data-sourcepos=&quot;78:5-78:102&quot;&gt;&lt;strong&gt;Iterating over Objects:&lt;/strong&gt; Understand when to use &lt;code&gt;for...in&lt;/code&gt; to iterate over object properties.&lt;/li&gt;
&lt;li data-sourcepos=&quot;79:5-80:0&quot;&gt;&lt;strong&gt;Iterating over Iterables:&lt;/strong&gt; Understand when to use &lt;code&gt;for...of&lt;/code&gt; to iterate over arrays, strings, and other iterable objects.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-sourcepos=&quot;81:1-84:0&quot;&gt;
&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Type Coercion:&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-sourcepos=&quot;82:5-84:0&quot;&gt;
&lt;li data-sourcepos=&quot;82:5-82:132&quot;&gt;&lt;strong&gt;Explicit Type Conversion:&lt;/strong&gt; Learn to use methods like &lt;code&gt;Number()&lt;/code&gt;, &lt;code&gt;String()&lt;/code&gt;, and &lt;code&gt;Boolean()&lt;/code&gt; for explicit type conversions.&lt;/li&gt;
&lt;li data-sourcepos=&quot;83:5-84:0&quot;&gt;&lt;strong&gt;Avoiding Unintended Coercion:&lt;/strong&gt; Understand how to write code that avoids unintended type coercion and maintains expected behavior.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;&lt;p data-sourcepos=&quot;85:1-85:180&quot;&gt;By delving deeper into these concepts and actively applying them in your projects, you&#39;ll gain a strong foundation in JavaScript and be well-prepared to tackle complex challenges.&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/01/what-do-you-need-to-study-while.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwjFjIXlkB6ugjGTB58VZlaVqyARrvTpGa0739-HGN0e4H1E2n0VLlANSp3MRJXzYFeos-JSGtejuiva28fKKGQnUNwerZiCa3aWSVfVssHcR7wUJD9DkgeqI3MofZy5GjzXNuEO5S-ZjqDKWd7uwg31xPgDurULzp0ixyPe28iI0udA4FklMYs_C4o1Q/s72-c/programmers-using-javascript-pro.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-4467262210051522665</guid><pubDate>Sun, 12 Jan 2025 13:23:00 +0000</pubDate><atom:updated>2025-03-01T19:16:47.853+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><title>JavaScript Essentials</title><description>
&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFRmF_9q-EQDpwdxxqC_8RzBZSZgF8feG10KL1no4JtVhnNQsWiq8610m2v0qYvNpN7oZtiOkkJry6sbpMrL_bYRvLGPGuzVOSLeK7sEP8ApS5qoFYMsWoclMdvuQ9kq9pFuGLBWH2c-5WXKRNfd7wgeRGMmCaoVogWw9MG18wgj_dggxrYxAa7mFpuJI/s626/programmers-using-javascript-pro.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;417&quot; data-original-width=&quot;626&quot; height=&quot;213&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFRmF_9q-EQDpwdxxqC_8RzBZSZgF8feG10KL1no4JtVhnNQsWiq8610m2v0qYvNpN7oZtiOkkJry6sbpMrL_bYRvLGPGuzVOSLeK7sEP8ApS5qoFYMsWoclMdvuQ9kq9pFuGLBWH2c-5WXKRNfd7wgeRGMmCaoVogWw9MG18wgj_dggxrYxAa7mFpuJI/s320/programmers-using-javascript-pro.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&lt;p&gt;&lt;/p&gt;&lt;h1&gt;A Quick Guide to JavaScript Essentials:&lt;/h1&gt;&lt;p&gt;JavaScript is packed with useful features that help you interact with the browser and create dynamic web pages. In this post, we’ll cover some &lt;strong&gt;basic but important&lt;/strong&gt; concepts that you might not always encounter in tutorials but are essential for everyday web development. These include manipulating the &lt;strong&gt;window object&lt;/strong&gt;, using &lt;strong&gt;&lt;code&gt;location.href&lt;/code&gt;&lt;/strong&gt; for navigation, handling &lt;strong&gt;browser events&lt;/strong&gt;, and more.&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;1. &lt;strong&gt;Manipulating the Window Object&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;The &lt;strong&gt;window object&lt;/strong&gt; lets you control the browser window or tab. You can open new windows, close them, and manage their size.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Opening a new window:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-variable language_&quot;&gt;window&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;open&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://example.com&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;_blank&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;width=600,height=400&#39;&lt;/span&gt;);
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Closing the current window:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-variable language_&quot;&gt;window&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;close&lt;/span&gt;();
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;Note: &lt;code&gt;window.close()&lt;/code&gt; only works on windows opened by &lt;code&gt;window.open()&lt;/code&gt;.&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;2. &lt;strong&gt;Using &lt;code&gt;location.href&lt;/code&gt;&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;The &lt;strong&gt;&lt;code&gt;location&lt;/code&gt; object&lt;/strong&gt; represents the current page’s URL. You can use &lt;strong&gt;&lt;code&gt;location.href&lt;/code&gt;&lt;/strong&gt; to navigate to other pages, reload the page, or manipulate the URL.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Redirecting to a new URL:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;location.&lt;span class=&quot;hljs-property&quot;&gt;href&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;https://example.com&#39;&lt;/span&gt;;
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Reloading the page:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;location.&lt;span class=&quot;hljs-title function_&quot;&gt;reload&lt;/span&gt;();
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Getting the current URL:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(location.&lt;span class=&quot;hljs-property&quot;&gt;href&lt;/span&gt;);
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;hr /&gt;&lt;h3&gt;3. &lt;strong&gt;Manipulating Browser History&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;JavaScript lets you control the browser’s history without reloading the page.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Adding to history:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;history.&lt;span class=&quot;hljs-title function_&quot;&gt;pushState&lt;/span&gt;({ &lt;span class=&quot;hljs-attr&quot;&gt;page&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt; }, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Page 1&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;?page=1&#39;&lt;/span&gt;);
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Going back and forward in history:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;history.&lt;span class=&quot;hljs-title function_&quot;&gt;back&lt;/span&gt;();  &lt;span class=&quot;hljs-comment&quot;&gt;// Go back&lt;/span&gt;
history.&lt;span class=&quot;hljs-title function_&quot;&gt;forward&lt;/span&gt;();  &lt;span class=&quot;hljs-comment&quot;&gt;// Go forward&lt;/span&gt;
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;hr /&gt;&lt;h3&gt;4. &lt;strong&gt;Handling Browser Events&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;JavaScript lets you react to various browser events like resizing the window, loading the page, or focusing on the window.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Detecting window resize:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-variable language_&quot;&gt;window&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;addEventListener&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;resize&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Window resized&#39;&lt;/span&gt;);
});
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Detecting page load:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-variable language_&quot;&gt;document&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;addEventListener&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;DOMContentLoaded&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Page loaded&#39;&lt;/span&gt;);
});
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Focus and blur events:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-variable language_&quot;&gt;window&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;addEventListener&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;focus&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-function&quot;&gt;() =&amp;gt;&lt;/span&gt; &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Window focused&#39;&lt;/span&gt;));
&lt;span class=&quot;hljs-variable language_&quot;&gt;window&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;addEventListener&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;blur&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-function&quot;&gt;() =&amp;gt;&lt;/span&gt; &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Window lost focus&#39;&lt;/span&gt;));
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;hr /&gt;&lt;h3&gt;5. &lt;strong&gt;Storing Data with LocalStorage&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;JavaScript provides &lt;strong&gt;localStorage&lt;/strong&gt; for storing data that persists even after the browser is closed.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Store data:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-variable language_&quot;&gt;localStorage&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;setItem&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;username&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;John&#39;&lt;/span&gt;);
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Retrieve data:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; username = &lt;span class=&quot;hljs-variable language_&quot;&gt;localStorage&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;getItem&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;username&#39;&lt;/span&gt;);
&lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(username);  &lt;span class=&quot;hljs-comment&quot;&gt;// John&lt;/span&gt;
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;hr /&gt;&lt;h3&gt;6. &lt;strong&gt;Popups: Alerts, Confirms, and Prompts&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;JavaScript provides simple popups for interaction with users.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Alert:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;alert&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;This is an alert!&#39;&lt;/span&gt;);
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Confirm:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;hljs-title function_&quot;&gt;confirm&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Do you want to continue?&#39;&lt;/span&gt;)) {
  &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;User confirmed&#39;&lt;/span&gt;);
} &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; {
  &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;User canceled&#39;&lt;/span&gt;);
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Prompt:&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; name = &lt;span class=&quot;hljs-title function_&quot;&gt;prompt&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;What is your name?&#39;&lt;/span&gt;);
&lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Hello, &#39;&lt;/span&gt; + name);
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;hr /&gt;&lt;h3&gt;7. &lt;strong&gt;Scrolling to the Top After Navigation&lt;/strong&gt;&lt;/h3&gt;&lt;h3&gt;&lt;p&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;Sometimes when you open a new window or navigate from one page to another, the new page might not automatically scroll to the top. To ensure that the new page starts at the top, you can use the &lt;code&gt;scrollTo()&lt;/code&gt; method.&lt;/span&gt;&lt;/p&gt;&lt;/h3&gt;&lt;h4&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;Example: Scrolling to the Top of a New Window&lt;/span&gt;&lt;/h4&gt;&lt;h3&gt;&lt;p&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;If you&#39;re opening a new window and want to make sure it starts from the top:&lt;/span&gt;&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;javascript&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; newWindow = &lt;span class=&quot;hljs-variable language_&quot;&gt;window&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;open&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://example.com&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;_blank&#39;&lt;/span&gt;);
newWindow.&lt;span class=&quot;hljs-property&quot;&gt;onload&lt;/span&gt; = &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  newWindow.&lt;span class=&quot;hljs-title function_&quot;&gt;scrollTo&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;);  &lt;span class=&quot;hljs-comment&quot;&gt;// Scrolls to the top of the new window&lt;/span&gt;
};
&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;Explanation: &lt;code&gt;newWindow.scrollTo(0, 0)&lt;/code&gt; ensures that the new window’s scroll position is set to the top-left corner (0, 0) after the page has finished loading.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;If you&#39;re navigating from one page to another in the same window, you can do this:&lt;/span&gt;&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;javascript&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;location.&lt;span class=&quot;hljs-property&quot;&gt;href&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;https://example.com&#39;&lt;/span&gt;;
&lt;span class=&quot;hljs-variable language_&quot;&gt;window&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;scrollTo&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;);  &lt;span class=&quot;hljs-comment&quot;&gt;// Scrolls to the top of the current window&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;span style=&quot;font-size: medium; font-weight: normal;&quot;&gt;Explanation: After the navigation, &lt;/span&gt;&lt;code style=&quot;font-weight: normal;&quot;&gt;window.scrollTo(0, 0)&lt;/code&gt;&lt;span style=&quot;font-size: medium; font-weight: normal;&quot;&gt; forces the current window to scroll to the top of the page.&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/span&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/h3&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;&lt;p&gt;In this post, we covered a few important &lt;strong&gt;JavaScript concepts&lt;/strong&gt; that can help you interact with the browser and manipulate page content:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Window object&lt;/strong&gt;: Open and close windows, control size.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;location.href&lt;/code&gt;&lt;/strong&gt;: Navigate to new URLs or reload the page.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Browser history&lt;/strong&gt;: Manipulate history with &lt;code&gt;pushState&lt;/code&gt; and navigate with &lt;code&gt;back()&lt;/code&gt; and &lt;code&gt;forward()&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Browser events&lt;/strong&gt;: Detect events like resize, load, and focus.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;LocalStorage&lt;/strong&gt;: Store and retrieve data in the browser.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Popups&lt;/strong&gt;: Use alerts, confirms, and prompts for user interaction.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Scrolling to the top&lt;/strong&gt;: Ensure the new window or page starts at the top using &lt;code&gt;scrollTo()&lt;/code&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Mastering these basics will help you build more interactive and user-friendly web applications. Happy coding!&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/01/javascript-essentials.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFRmF_9q-EQDpwdxxqC_8RzBZSZgF8feG10KL1no4JtVhnNQsWiq8610m2v0qYvNpN7oZtiOkkJry6sbpMrL_bYRvLGPGuzVOSLeK7sEP8ApS5qoFYMsWoclMdvuQ9kq9pFuGLBWH2c-5WXKRNfd7wgeRGMmCaoVogWw9MG18wgj_dggxrYxAa7mFpuJI/s72-c/programmers-using-javascript-pro.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-888410395308387361</guid><pubDate>Thu, 09 Jan 2025 16:23:00 +0000</pubDate><atom:updated>2025-03-01T19:17:12.731+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><title>Async Programming with JavaScript : Part 3</title><description>&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTGNPd-Tp2-ECiVq2dyjVkc8ZJUaTXkZ4KAqI04gXD-K0PogIkxUJFdJ0gRR9T5M04tN2s8emHnUOQ2ET_oxrnw_Fa7Kdi0yidkFh79amqmnG46qkxfKSQ1bJvzSZ_vSH6RSLW82pKx-5WU9mKISpd_dC3V1LB9OHseoNmOC60jFiyHq0Q-ufOi88BD1s/s626/programmers-using-javascript-pro.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;417&quot; data-original-width=&quot;626&quot; height=&quot;213&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTGNPd-Tp2-ECiVq2dyjVkc8ZJUaTXkZ4KAqI04gXD-K0PogIkxUJFdJ0gRR9T5M04tN2s8emHnUOQ2ET_oxrnw_Fa7Kdi0yidkFh79amqmnG46qkxfKSQ1bJvzSZ_vSH6RSLW82pKx-5WU9mKISpd_dC3V1LB9OHseoNmOC60jFiyHq0Q-ufOi88BD1s/s320/programmers-using-javascript-pro.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&lt;p&gt;&lt;/p&gt;&lt;h1&gt;Async Programming with JavaScript: Part 3&lt;/h1&gt;&lt;h2&gt;Advanced Asynchronous Techniques and Optimizations&lt;/h2&gt;&lt;p&gt;In the first two parts of this series, we covered the basics of asynchronous programming in JavaScript, starting with &lt;strong&gt;callbacks&lt;/strong&gt;, &lt;strong&gt;promises&lt;/strong&gt;, and &lt;strong&gt;async/await&lt;/strong&gt;. We also explored handling multiple asynchronous operations using &lt;strong&gt;&lt;code&gt;Promise.all()&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;Promise.race()&lt;/code&gt;&lt;/strong&gt;. Now, in Part 3, we’re going to dive deeper into more &lt;strong&gt;advanced asynchronous techniques&lt;/strong&gt;, focusing on &lt;strong&gt;custom promises&lt;/strong&gt;, &lt;strong&gt;async iterators&lt;/strong&gt;, and &lt;strong&gt;performance optimizations&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;By the end of this post, you&#39;ll have a solid understanding of advanced async concepts that will help you write more efficient and scalable JavaScript code.&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;1. &lt;strong&gt;Custom Promises&lt;/strong&gt;: Creating Your Own Promises&lt;/h3&gt;&lt;p&gt;While most of the time you’ll be working with built-in promises (like those from &lt;code&gt;fetch()&lt;/code&gt;, &lt;code&gt;setTimeout()&lt;/code&gt;, etc.), you can also create your own custom promises. This is useful when you want to wrap non-promise-based APIs or implement complex asynchronous logic.&lt;/p&gt;&lt;h4&gt;Example: Custom Promise for Simulating a Task&lt;/h4&gt;&lt;p&gt;Let’s simulate a custom promise that resolves after a delay.&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;delay&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;ms&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;resolve, reject&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (ms &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;) {
      &lt;span class=&quot;hljs-title function_&quot;&gt;reject&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Delay must be a positive number&#39;&lt;/span&gt;);
    } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; {
      &lt;span class=&quot;hljs-built_in&quot;&gt;setTimeout&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;() =&amp;gt;&lt;/span&gt; {
        &lt;span class=&quot;hljs-title function_&quot;&gt;resolve&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;`Waited for &lt;span class=&quot;hljs-subst&quot;&gt;${ms}&lt;/span&gt; milliseconds`&lt;/span&gt;);
      }, ms);
    }
  });
}

&lt;span class=&quot;hljs-title function_&quot;&gt;delay&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;2000&lt;/span&gt;)
  .&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;message&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(message); &lt;span class=&quot;hljs-comment&quot;&gt;// &quot;Waited for 2000 milliseconds&quot;&lt;/span&gt;
  })
  .&lt;span class=&quot;hljs-title function_&quot;&gt;catch&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;error&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(error); &lt;span class=&quot;hljs-comment&quot;&gt;// Handles any error (e.g., negative delay)&lt;/span&gt;
  });
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;The &lt;code&gt;delay()&lt;/code&gt; function returns a custom promise that resolves after the specified time (&lt;code&gt;ms&lt;/code&gt;).&lt;/li&gt;&lt;li&gt;If the delay is a negative number, the promise is rejected with an error message.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Key Points:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Custom promises give you the flexibility to wrap any asynchronous task.&lt;/li&gt;&lt;li&gt;Use &lt;code&gt;resolve()&lt;/code&gt; and &lt;code&gt;reject()&lt;/code&gt; to control the fulfillment or rejection of the promise.&lt;/li&gt;&lt;li&gt;This is especially helpful when dealing with APIs or operations that don’t natively support promises.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;2. &lt;strong&gt;Async Iterators and For-await-of&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;An &lt;strong&gt;async iterator&lt;/strong&gt; allows you to iterate over asynchronous data sources, such as reading from a file or consuming data from a remote server, in a memory-efficient and readable way.&lt;/p&gt;&lt;p&gt;In JavaScript, you can create your own async iterable objects that can be used with the &lt;code&gt;for-await-of&lt;/code&gt; loop.&lt;/p&gt;&lt;h4&gt;Example: Async Iterable for Data Streams&lt;/h4&gt;&lt;p&gt;Imagine you&#39;re consuming a stream of data from an API or a database, and you want to process each chunk of data asynchronously:&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;* &lt;span class=&quot;hljs-title function_&quot;&gt;fetchData&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; urls = [
    &lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/users&#39;&lt;/span&gt;,
    &lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts&#39;&lt;/span&gt;,
  ];

  &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; url &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt; urls) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; response = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(url);
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; data = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; response.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();
    &lt;span class=&quot;hljs-keyword&quot;&gt;yield&lt;/span&gt; data; &lt;span class=&quot;hljs-comment&quot;&gt;// Yield each piece of data asynchronously&lt;/span&gt;
  }
}

&lt;span class=&quot;hljs-keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;processData&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; data &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetchData&lt;/span&gt;()) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(data); &lt;span class=&quot;hljs-comment&quot;&gt;// Process each chunk of data as it arrives&lt;/span&gt;
  }
}

&lt;span class=&quot;hljs-title function_&quot;&gt;processData&lt;/span&gt;();
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;The &lt;code&gt;fetchData&lt;/code&gt; function is an &lt;strong&gt;async generator&lt;/strong&gt; that fetches data from multiple URLs.&lt;/li&gt;&lt;li&gt;It yields the data as it arrives, allowing us to process each piece as it is fetched.&lt;/li&gt;&lt;li&gt;The &lt;code&gt;for-await-of&lt;/code&gt; loop is used to asynchronously iterate over the yielded values.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Key Points:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Async iterators&lt;/strong&gt; allow you to iterate over data streams that may not be immediately available, such as data from APIs or file streams.&lt;/li&gt;&lt;li&gt;&lt;code&gt;for-await-of&lt;/code&gt; provides a clean, readable way to process async data.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;3. &lt;strong&gt;Performance Optimization: Minimizing Reflows and Repaints&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;When working with DOM manipulation in JavaScript, &lt;strong&gt;reflows&lt;/strong&gt; and &lt;strong&gt;repaints&lt;/strong&gt; can be a major performance bottleneck. These processes occur when changes to the DOM trigger the browser to re-render the page. Too many reflows or repaints can make a webpage feel slow and unresponsive.&lt;/p&gt;&lt;h4&gt;1. &lt;strong&gt;Batch DOM Updates&lt;/strong&gt;&lt;/h4&gt;&lt;p&gt;To minimize reflows and repaints, try to &lt;strong&gt;batch DOM updates&lt;/strong&gt; together. Instead of making multiple individual changes to the DOM, group them into one operation.&lt;/p&gt;&lt;h5&gt;Example: Batching DOM Changes&lt;/h5&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;updateUI&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-comment&quot;&gt;// Start by hiding the element to avoid intermediate renderings&lt;/span&gt;
  &lt;span class=&quot;hljs-variable language_&quot;&gt;document&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;getElementById&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;myElement&#39;&lt;/span&gt;).&lt;span class=&quot;hljs-property&quot;&gt;style&lt;/span&gt;.&lt;span class=&quot;hljs-property&quot;&gt;display&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;none&#39;&lt;/span&gt;;

  &lt;span class=&quot;hljs-comment&quot;&gt;// Perform multiple DOM manipulations&lt;/span&gt;
  &lt;span class=&quot;hljs-variable language_&quot;&gt;document&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;getElementById&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;myElement&#39;&lt;/span&gt;).&lt;span class=&quot;hljs-property&quot;&gt;textContent&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;New Content&#39;&lt;/span&gt;;
  &lt;span class=&quot;hljs-variable language_&quot;&gt;document&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;getElementById&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;myElement&#39;&lt;/span&gt;).&lt;span class=&quot;hljs-property&quot;&gt;style&lt;/span&gt;.&lt;span class=&quot;hljs-property&quot;&gt;color&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;red&#39;&lt;/span&gt;;

  &lt;span class=&quot;hljs-comment&quot;&gt;// Show the element after all changes are made&lt;/span&gt;
  &lt;span class=&quot;hljs-variable language_&quot;&gt;document&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;getElementById&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;myElement&#39;&lt;/span&gt;).&lt;span class=&quot;hljs-property&quot;&gt;style&lt;/span&gt;.&lt;span class=&quot;hljs-property&quot;&gt;display&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;block&#39;&lt;/span&gt;;
}

&lt;span class=&quot;hljs-title function_&quot;&gt;updateUI&lt;/span&gt;();
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;By hiding the element temporarily and making all changes before showing it again, we reduce the number of render operations.&lt;/li&gt;&lt;li&gt;The browser doesn’t have to re-render every change individually, improving performance.&lt;/li&gt;&lt;/ul&gt;&lt;h4&gt;2. &lt;strong&gt;Using &lt;code&gt;requestAnimationFrame&lt;/code&gt; for Animations&lt;/strong&gt;&lt;/h4&gt;&lt;p&gt;Instead of using &lt;code&gt;setTimeout()&lt;/code&gt; or &lt;code&gt;setInterval()&lt;/code&gt; for animations, it&#39;s more efficient to use &lt;code&gt;requestAnimationFrame()&lt;/code&gt;. This method tells the browser to perform animations just before the next repaint, which ensures smoother and more efficient updates.&lt;/p&gt;&lt;h5&gt;Example: Smooth Animation with &lt;code&gt;requestAnimationFrame&lt;/code&gt;&lt;/h5&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;animateElement&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; element = &lt;span class=&quot;hljs-variable language_&quot;&gt;document&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;getElementById&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;myElement&#39;&lt;/span&gt;);
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; position = &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;;

  &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;move&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
    position += &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;;
    element.&lt;span class=&quot;hljs-property&quot;&gt;style&lt;/span&gt;.&lt;span class=&quot;hljs-property&quot;&gt;transform&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;`translateX(&lt;span class=&quot;hljs-subst&quot;&gt;${position}&lt;/span&gt;px)`&lt;/span&gt;;

    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (position &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;500&lt;/span&gt;) {
      &lt;span class=&quot;hljs-title function_&quot;&gt;requestAnimationFrame&lt;/span&gt;(move);
    }
  }

  &lt;span class=&quot;hljs-title function_&quot;&gt;move&lt;/span&gt;();
}

&lt;span class=&quot;hljs-title function_&quot;&gt;animateElement&lt;/span&gt;();
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;requestAnimationFrame()&lt;/code&gt; ensures the animation runs in sync with the browser&#39;s refresh rate, leading to smoother animations and less CPU/GPU load.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Key Points:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Batching DOM updates&lt;/strong&gt; can significantly reduce the performance cost of reflows and repaints.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;requestAnimationFrame()&lt;/strong&gt; optimizes animations by synchronizing them with the browser’s rendering cycle.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;4. &lt;strong&gt;Parallel Execution of Independent Tasks&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;In some cases, you may want to execute multiple independent asynchronous operations concurrently (in parallel) without waiting for them to finish one by one.&lt;/p&gt;&lt;h4&gt;Example: Parallel Execution with &lt;code&gt;Promise.all()&lt;/code&gt;&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;parallelTasks&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; task1 = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/users&#39;&lt;/span&gt;);
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; task2 = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts&#39;&lt;/span&gt;);
  
  &lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt; {
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; [users, posts] = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;all&lt;/span&gt;([task1, task2]);
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; usersJson = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; users.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; postsJson = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; posts.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();

    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Users:&#39;&lt;/span&gt;, usersJson);
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Posts:&#39;&lt;/span&gt;, postsJson);
  } &lt;span class=&quot;hljs-keyword&quot;&gt;catch&lt;/span&gt; (error) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;error&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Error in parallel tasks:&#39;&lt;/span&gt;, error);
  }
}

&lt;span class=&quot;hljs-title function_&quot;&gt;parallelTasks&lt;/span&gt;();
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;The &lt;code&gt;Promise.all()&lt;/code&gt; function runs multiple promises concurrently and waits for all of them to resolve.&lt;/li&gt;&lt;li&gt;It ensures that all independent asynchronous operations are completed as soon as possible without waiting for each task to finish sequentially.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Key Points:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Parallel execution&lt;/strong&gt; reduces waiting time by running independent tasks concurrently.&lt;/li&gt;&lt;li&gt;Use &lt;code&gt;Promise.all()&lt;/code&gt; when the tasks are independent of each other and can run in parallel.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;5. &lt;strong&gt;Concurrency and Throttling&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;Sometimes you may want to limit the number of concurrent operations, especially when working with large numbers of tasks (like making HTTP requests). This helps prevent overloading the server or the client.&lt;/p&gt;&lt;h4&gt;Example: Throttling Concurrent Requests&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetchWithThrottle&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;urls, limit&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; results = [];
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; queue = [...urls];

  &lt;span class=&quot;hljs-keyword&quot;&gt;while&lt;/span&gt; (queue.&lt;span class=&quot;hljs-property&quot;&gt;length&lt;/span&gt; &amp;gt; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;) {
    &lt;span class=&quot;hljs-comment&quot;&gt;// Limit the number of concurrent requests&lt;/span&gt;
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; currentBatch = queue.&lt;span class=&quot;hljs-title function_&quot;&gt;splice&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, limit);

    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; batchResults = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;all&lt;/span&gt;(currentBatch.&lt;span class=&quot;hljs-title function_&quot;&gt;map&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-params&quot;&gt;url&lt;/span&gt; =&amp;gt;&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(url).&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-params&quot;&gt;res&lt;/span&gt; =&amp;gt;&lt;/span&gt; res.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;())));
    results.&lt;span class=&quot;hljs-title function_&quot;&gt;push&lt;/span&gt;(...batchResults);

    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Batch results:&#39;&lt;/span&gt;, batchResults);
  }

  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; results;
}

&lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; urls = [
  &lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts/1&#39;&lt;/span&gt;,
  &lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts/2&#39;&lt;/span&gt;,
  &lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts/3&#39;&lt;/span&gt;,
  &lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts/4&#39;&lt;/span&gt;,
  &lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts/5&#39;&lt;/span&gt;,
];

&lt;span class=&quot;hljs-title function_&quot;&gt;fetchWithThrottle&lt;/span&gt;(urls, &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;); &lt;span class=&quot;hljs-comment&quot;&gt;// Limit to 2 concurrent requests&lt;/span&gt;
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;The &lt;code&gt;fetchWithThrottle()&lt;/code&gt; function takes an array of URLs and a &lt;code&gt;limit&lt;/code&gt; to control how many requests can be sent concurrently at a time.&lt;/li&gt;&lt;li&gt;It batches the requests and waits for each batch to complete before moving on to the next one.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Key Points:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Throttling&lt;/strong&gt; helps you control the number of concurrent tasks to avoid overwhelming resources.&lt;/li&gt;&lt;li&gt;Use throttling for scenarios like API rate limiting or heavy data processing tasks.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;&lt;p&gt;In Part 3 of this series, we&#39;ve explored several &lt;strong&gt;advanced asynchronous techniques&lt;/strong&gt; and &lt;strong&gt;performance optimizations&lt;/strong&gt; to help you write more efficient and scalable JavaScript code:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Custom Promises&lt;/strong&gt;: Create your own promises for more flexibility in handling asynchronous tasks.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Async Iterators&lt;/strong&gt;: Use async iterators to handle asynchronous data streams efficiently.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Performance Optimization&lt;/strong&gt;: Minimize reflows and repaints in the DOM, and optimize animations with &lt;code&gt;requestAnimationFrame()&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Parallel Execution&lt;/strong&gt;: Run independent tasks concurrently with &lt;code&gt;Promise.all()&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Concurrency and Throttling&lt;/strong&gt;: Limit the number of concurrent operations to improve performance.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Mastering these techniques will allow you to build more efficient and responsive applications, particularly when dealing with large-scale or resource-intensive tasks.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;That wraps up our series on &lt;strong&gt;Async Programming with JavaScript&lt;/strong&gt;! Now that you’re equipped with advanced asynchronous concepts, you’re ready to take on more complex real-world scenarios with confidence.&lt;/p&gt;&lt;p&gt;Feel free to explore these techniques further and apply them to your projects. Happy coding! 🚀&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/01/async-programming-with-javascript-part-3.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTGNPd-Tp2-ECiVq2dyjVkc8ZJUaTXkZ4KAqI04gXD-K0PogIkxUJFdJ0gRR9T5M04tN2s8emHnUOQ2ET_oxrnw_Fa7Kdi0yidkFh79amqmnG46qkxfKSQ1bJvzSZ_vSH6RSLW82pKx-5WU9mKISpd_dC3V1LB9OHseoNmOC60jFiyHq0Q-ufOi88BD1s/s72-c/programmers-using-javascript-pro.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-1785257070330328224</guid><pubDate>Wed, 08 Jan 2025 16:08:00 +0000</pubDate><atom:updated>2025-03-01T19:18:27.048+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><title>Async Programming with JavaScript: Part 2</title><description>
&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4tq0Bb6cUO6OnvTyFLPvRyXXSCVYis0pqE10AF8rAWURTmtdKzjc22KBAYorj5vnvo8OzNWfOj6K7UMcE-rg4qVc9bL6MPlcDc3oh0xL9qkxLDWFvPp_3j9fiFVklePBUfuyY4OrwxSmMmp5LMt5MUHDYjcxzfKFvHyrirOWSRRzz4k2ErH2IQYllPp8/s626/programmers-using-javascript-pro.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;417&quot; data-original-width=&quot;626&quot; height=&quot;213&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4tq0Bb6cUO6OnvTyFLPvRyXXSCVYis0pqE10AF8rAWURTmtdKzjc22KBAYorj5vnvo8OzNWfOj6K7UMcE-rg4qVc9bL6MPlcDc3oh0xL9qkxLDWFvPp_3j9fiFVklePBUfuyY4OrwxSmMmp5LMt5MUHDYjcxzfKFvHyrirOWSRRzz4k2ErH2IQYllPp8/s320/programmers-using-javascript-pro.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&lt;p&gt;&lt;/p&gt;&lt;h1&gt;Async Programming with JavaScript: Part 2&lt;/h1&gt;&lt;h2&gt;Handling Multiple Asynchronous Operations&lt;/h2&gt;&lt;p&gt;In Part 1 of this series, we covered the basics of asynchronous programming, including &lt;strong&gt;callbacks&lt;/strong&gt;, &lt;strong&gt;promises&lt;/strong&gt;, and the modern &lt;strong&gt;async/await&lt;/strong&gt; syntax. Now, in Part 2, we’re going to take things further by exploring how to handle &lt;strong&gt;multiple asynchronous operations&lt;/strong&gt;. In real-world applications, it’s common to perform several tasks at once — for example, fetching data from multiple APIs, processing files in parallel, or running multiple background tasks concurrently. We&#39;ll cover:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Promise.all()&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Promise.race()&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Handling multiple asynchronous operations with async/await&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Let’s get started!&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;1. &lt;strong&gt;Promise.all()&lt;/strong&gt;: Running Promises in Parallel&lt;/h3&gt;&lt;p&gt;The &lt;code&gt;Promise.all()&lt;/code&gt; method is used to run multiple asynchronous operations &lt;strong&gt;in parallel&lt;/strong&gt;. It takes an &lt;strong&gt;array of promises&lt;/strong&gt; as input and returns a single promise that resolves when all of the input promises are fulfilled or rejects as soon as one of the promises rejects.&lt;/p&gt;&lt;h4&gt;Syntax&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;all&lt;/span&gt;([promise1, promise2, promise3])
  .&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;results&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-comment&quot;&gt;// Handle all results as an array&lt;/span&gt;
  })
  .&lt;span class=&quot;hljs-title function_&quot;&gt;catch&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;error&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-comment&quot;&gt;// Handle error if any promise is rejected&lt;/span&gt;
  });
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h4&gt;Example of &lt;code&gt;Promise.all()&lt;/code&gt;&lt;/h4&gt;&lt;p&gt;Imagine you’re fetching data from two APIs concurrently, and you want to wait for both of them to complete before continuing.&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetchData&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; userData = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/users&#39;&lt;/span&gt;);
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; postData = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts&#39;&lt;/span&gt;);

  &lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt; {
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; [users, posts] = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;all&lt;/span&gt;([userData, postData]);
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; usersJson = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; users.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; postsJson = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; posts.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();

    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Users:&#39;&lt;/span&gt;, usersJson);
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Posts:&#39;&lt;/span&gt;, postsJson);
  } &lt;span class=&quot;hljs-keyword&quot;&gt;catch&lt;/span&gt; (error) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;error&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Error fetching data:&#39;&lt;/span&gt;, error);
  }
}

&lt;span class=&quot;hljs-title function_&quot;&gt;fetchData&lt;/span&gt;();
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;Promise.all()&lt;/code&gt; takes an array of promises and resolves when all of them are fulfilled.&lt;/li&gt;&lt;li&gt;We use &lt;code&gt;await&lt;/code&gt; to get the results of the two API calls and extract their JSON data.&lt;/li&gt;&lt;li&gt;If any of the promises reject, the &lt;code&gt;.catch()&lt;/code&gt; block catches the error, ensuring proper error handling.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Key Points:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Parallel execution&lt;/strong&gt;: &lt;code&gt;Promise.all()&lt;/code&gt; allows promises to run concurrently, reducing wait time.&lt;/li&gt;&lt;li&gt;If &lt;strong&gt;any one promise fails&lt;/strong&gt;, the entire &lt;code&gt;Promise.all()&lt;/code&gt; call is rejected.&lt;/li&gt;&lt;li&gt;The result of &lt;code&gt;Promise.all()&lt;/code&gt; is an array containing the results of all the resolved promises in the same order.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;2. &lt;strong&gt;Promise.race()&lt;/strong&gt;: First Promise to Settle Wins&lt;/h3&gt;&lt;p&gt;While &lt;code&gt;Promise.all()&lt;/code&gt; waits for all promises to be fulfilled, &lt;strong&gt;&lt;code&gt;Promise.race()&lt;/code&gt;&lt;/strong&gt; resolves or rejects as soon as &lt;strong&gt;one of the promises resolves or rejects&lt;/strong&gt;. This is useful when you want to know which operation finishes first.&lt;/p&gt;&lt;h4&gt;Syntax&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;race&lt;/span&gt;([promise1, promise2, promise3])
  .&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;result&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-comment&quot;&gt;// Handle the first settled promise&lt;/span&gt;
  })
  .&lt;span class=&quot;hljs-title function_&quot;&gt;catch&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;error&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-comment&quot;&gt;// Handle error if the first promise rejects&lt;/span&gt;
  });
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h4&gt;Example of &lt;code&gt;Promise.race()&lt;/code&gt;&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; timeout = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;_, reject&lt;/span&gt;) =&amp;gt;&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;setTimeout&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;() =&amp;gt;&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;reject&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Timeout!&#39;&lt;/span&gt;), &lt;span class=&quot;hljs-number&quot;&gt;3000&lt;/span&gt;));
&lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; fetchData = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts&#39;&lt;/span&gt;);

&lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;race&lt;/span&gt;([timeout, fetchData])
  .&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;response&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;First to settle:&#39;&lt;/span&gt;, response);
  })
  .&lt;span class=&quot;hljs-title function_&quot;&gt;catch&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;error&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Error:&#39;&lt;/span&gt;, error);
  });
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;Promise.race()&lt;/code&gt; will settle as soon as either the &lt;code&gt;timeout&lt;/code&gt; or &lt;code&gt;fetchData&lt;/code&gt; promise settles (whichever happens first).&lt;/li&gt;&lt;li&gt;In this case, if the fetch takes more than 3 seconds, the &lt;code&gt;timeout&lt;/code&gt; promise will reject first, and we handle that in the &lt;code&gt;.catch()&lt;/code&gt; block.&lt;/li&gt;&lt;li&gt;&lt;code&gt;Promise.race()&lt;/code&gt; is useful when you need a &lt;strong&gt;timeout&lt;/strong&gt; mechanism or the &lt;strong&gt;first completed task&lt;/strong&gt; to be processed immediately.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Key Points:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;First settled&lt;/strong&gt;: &lt;code&gt;Promise.race()&lt;/code&gt; resolves when the first promise settles, either by resolving or rejecting.&lt;/li&gt;&lt;li&gt;Great for scenarios like &lt;strong&gt;timeouts&lt;/strong&gt;, &lt;strong&gt;competitive processes&lt;/strong&gt;, or when you want to prioritize the quickest response.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;3. &lt;strong&gt;Handling Multiple Asynchronous Operations with Async/Await&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;In the previous parts of this series, we introduced &lt;code&gt;async/await&lt;/code&gt;, which greatly simplifies asynchronous code by making it look more like synchronous code. But what happens when you need to handle &lt;strong&gt;multiple asynchronous tasks&lt;/strong&gt;?&lt;/p&gt;&lt;p&gt;With &lt;strong&gt;async/await&lt;/strong&gt;, you can use &lt;strong&gt;&lt;code&gt;Promise.all()&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;Promise.race()&lt;/code&gt;&lt;/strong&gt; in combination with &lt;code&gt;await&lt;/code&gt; to handle multiple asynchronous operations.&lt;/p&gt;&lt;h4&gt;Example: Parallel Async/Await with &lt;code&gt;Promise.all()&lt;/code&gt;&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetchData&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; userResponse = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/users&#39;&lt;/span&gt;);
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; postResponse = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts&#39;&lt;/span&gt;);

  &lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt; {
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; [userData, postData] = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;all&lt;/span&gt;([userResponse, postResponse]);

    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; users = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; userData.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; posts = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; postData.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();

    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Users:&#39;&lt;/span&gt;, users);
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Posts:&#39;&lt;/span&gt;, posts);
  } &lt;span class=&quot;hljs-keyword&quot;&gt;catch&lt;/span&gt; (error) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;error&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Error fetching data:&#39;&lt;/span&gt;, error);
  }
}

&lt;span class=&quot;hljs-title function_&quot;&gt;fetchData&lt;/span&gt;();
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Just like before, we run both fetch calls in parallel.&lt;/li&gt;&lt;li&gt;&lt;code&gt;Promise.all()&lt;/code&gt; is used to wait for both promises to resolve simultaneously.&lt;/li&gt;&lt;li&gt;We then parse the JSON data from both responses after they’re both complete.&lt;/li&gt;&lt;/ul&gt;&lt;h4&gt;Example: Handling First to Settle with &lt;code&gt;Promise.race()&lt;/code&gt;&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetchWithTimeout&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; timeout = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;_, reject&lt;/span&gt;) =&amp;gt;&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;setTimeout&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;() =&amp;gt;&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;reject&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Timeout!&#39;&lt;/span&gt;), &lt;span class=&quot;hljs-number&quot;&gt;2000&lt;/span&gt;));
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; fetchRequest = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts&#39;&lt;/span&gt;);

  &lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt; {
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; response = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;race&lt;/span&gt;([timeout, fetchRequest]);
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; data = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; response.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Fetched data:&#39;&lt;/span&gt;, data);
  } &lt;span class=&quot;hljs-keyword&quot;&gt;catch&lt;/span&gt; (error) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Error:&#39;&lt;/span&gt;, error); &lt;span class=&quot;hljs-comment&quot;&gt;// This handles both timeout or fetch errors&lt;/span&gt;
  }
}

&lt;span class=&quot;hljs-title function_&quot;&gt;fetchWithTimeout&lt;/span&gt;();
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;We use &lt;code&gt;Promise.race()&lt;/code&gt; to handle a timeout. If the fetch request takes longer than 2 seconds, the &lt;code&gt;timeout&lt;/code&gt; promise will reject first, and the error will be handled by the &lt;code&gt;catch&lt;/code&gt; block.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Key Points:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;You can use &lt;code&gt;async/await&lt;/code&gt; with &lt;code&gt;Promise.all()&lt;/code&gt; and &lt;code&gt;Promise.race()&lt;/code&gt; to manage multiple asynchronous operations.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;Promise.all()&lt;/code&gt;&lt;/strong&gt; handles tasks in parallel, while &lt;strong&gt;&lt;code&gt;Promise.race()&lt;/code&gt;&lt;/strong&gt; resolves/rejects as soon as the first promise settles.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;4. &lt;strong&gt;Handling Errors with Multiple Async Operations&lt;/strong&gt;&lt;/h3&gt;&lt;p&gt;Error handling becomes even more critical when dealing with multiple asynchronous operations. Consider these scenarios:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;What happens if one operation fails while others succeed?&lt;/li&gt;&lt;li&gt;How do you handle errors in both &lt;code&gt;Promise.all()&lt;/code&gt; and &lt;code&gt;Promise.race()&lt;/code&gt;?&lt;/li&gt;&lt;/ul&gt;&lt;h4&gt;Error Handling in &lt;code&gt;Promise.all()&lt;/code&gt;&lt;/h4&gt;&lt;p&gt;In &lt;code&gt;Promise.all()&lt;/code&gt;, if any promise fails, the entire promise will be rejected, and you can catch the error using a &lt;code&gt;catch()&lt;/code&gt; block.&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetchData&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; userData = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/users&#39;&lt;/span&gt;);
  &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; postData = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts&#39;&lt;/span&gt;);
  
  &lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt; {
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; [users, posts] = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;all&lt;/span&gt;([userData, postData]);
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; usersJson = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; users.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();
    &lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; postsJson = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; posts.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(usersJson, postsJson);
  } &lt;span class=&quot;hljs-keyword&quot;&gt;catch&lt;/span&gt; (error) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;error&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Error fetching data:&#39;&lt;/span&gt;, error); &lt;span class=&quot;hljs-comment&quot;&gt;// Handles error if any promise fails&lt;/span&gt;
  }
}
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h4&gt;Error Handling in &lt;code&gt;Promise.race()&lt;/code&gt;&lt;/h4&gt;&lt;p&gt;In &lt;code&gt;Promise.race()&lt;/code&gt;, if any promise fails (i.e., rejects first), the error will be caught immediately.&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; timeout = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;_, reject&lt;/span&gt;) =&amp;gt;&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;setTimeout&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;() =&amp;gt;&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;reject&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Timeout!&#39;&lt;/span&gt;), &lt;span class=&quot;hljs-number&quot;&gt;2000&lt;/span&gt;));
&lt;span class=&quot;hljs-keyword&quot;&gt;const&lt;/span&gt; fetchData = &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts&#39;&lt;/span&gt;);

&lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;race&lt;/span&gt;([timeout, fetchData])
  .&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;result&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;First to settle:&#39;&lt;/span&gt;, result);
  })
  .&lt;span class=&quot;hljs-title function_&quot;&gt;catch&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;(&lt;span class=&quot;hljs-params&quot;&gt;error&lt;/span&gt;) =&amp;gt;&lt;/span&gt; {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;error&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Error:&#39;&lt;/span&gt;, error); &lt;span class=&quot;hljs-comment&quot;&gt;// Handles error from either promise&lt;/span&gt;
  });
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;Key Points:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Always handle errors in &lt;code&gt;Promise.all()&lt;/code&gt; and &lt;code&gt;Promise.race()&lt;/code&gt; using &lt;code&gt;.catch()&lt;/code&gt; or &lt;code&gt;try/catch&lt;/code&gt; (in async functions).&lt;/li&gt;&lt;li&gt;In &lt;code&gt;Promise.all()&lt;/code&gt;, all promises must succeed, or the whole operation fails.&lt;/li&gt;&lt;li&gt;In &lt;code&gt;Promise.race()&lt;/code&gt;, the first promise to settle determines the result.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;&lt;p&gt;In Part 2, we explored how to handle &lt;strong&gt;multiple asynchronous operations&lt;/strong&gt; in JavaScript using &lt;code&gt;Promise.all()&lt;/code&gt; and &lt;code&gt;Promise.race()&lt;/code&gt;. These tools allow you to manage multiple promises concurrently, making it&lt;/p&gt;</description><link>https://www.ankitchamlagain.com.np/2025/01/async-programming-with-javascript-part-2.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4tq0Bb6cUO6OnvTyFLPvRyXXSCVYis0pqE10AF8rAWURTmtdKzjc22KBAYorj5vnvo8OzNWfOj6K7UMcE-rg4qVc9bL6MPlcDc3oh0xL9qkxLDWFvPp_3j9fiFVklePBUfuyY4OrwxSmMmp5LMt5MUHDYjcxzfKFvHyrirOWSRRzz4k2ErH2IQYllPp8/s72-c/programmers-using-javascript-pro.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4611029784033708994.post-1976916167551598747</guid><pubDate>Tue, 07 Jan 2025 16:18:00 +0000</pubDate><atom:updated>2025-03-01T19:18:49.015+05:45</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><title>Async Programming with JavaScript: Part 1</title><description>
&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGfDPj8rIKTirzcxwEuer8xBEaRLb4tmM-9ulaULzWAl0dfOrrC_9H_-TF9yNMXUcCORJ1W3ycrOYhFwWZg34mR50CPSY8Y_a-93RsM5kPDzQiTAF4dch7Y0vcMsT5sXgbjHcrgxNpthU1Gjq24i-r2oqn-YcCO6vGzm6ESBYvK1c79ZgmivgsyX5Wf-c/s626/programmers-using-javascript-pro.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;417&quot; data-original-width=&quot;626&quot; height=&quot;213&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGfDPj8rIKTirzcxwEuer8xBEaRLb4tmM-9ulaULzWAl0dfOrrC_9H_-TF9yNMXUcCORJ1W3ycrOYhFwWZg34mR50CPSY8Y_a-93RsM5kPDzQiTAF4dch7Y0vcMsT5sXgbjHcrgxNpthU1Gjq24i-r2oqn-YcCO6vGzm6ESBYvK1c79ZgmivgsyX5Wf-c/s320/programmers-using-javascript-pro.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&lt;p&gt;&lt;/p&gt;&lt;h1&gt;Introduction to Async Programming with JavaScript: Part 1&lt;/h1&gt;&lt;h2&gt;Master Asynchronous JavaScript for Better Performance and User Experience&lt;/h2&gt;&lt;p&gt;Asynchronous programming is a fundamental concept in modern JavaScript development. In contrast to traditional synchronous programming, where tasks are executed one after another, &lt;strong&gt;asynchronous programming&lt;/strong&gt; allows multiple tasks to be executed simultaneously. This leads to better performance, especially in applications that require handling tasks like I/O operations, API calls, or UI interactions.&lt;/p&gt;&lt;p&gt;In this post, we’ll dive into &lt;strong&gt;async programming&lt;/strong&gt; in JavaScript, exploring the concepts, syntax, and basic use cases. We’ll also walk through examples and explain why asynchronous code is so important in modern web development.&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;Why Asynchronous Programming?&lt;/h3&gt;&lt;p&gt;JavaScript is single-threaded, meaning it processes one task at a time. When performing time-consuming operations such as loading data from a server, reading files, or waiting for user input, a synchronous approach would cause the program to freeze or &quot;block,&quot; waiting for the task to finish. This can lead to poor user experiences.&lt;/p&gt;&lt;p&gt;With &lt;strong&gt;asynchronous programming&lt;/strong&gt;, JavaScript can continue executing other tasks while waiting for these time-consuming operations to complete. This is especially useful for web applications that need to handle things like:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Fetching data from a server&lt;/li&gt;&lt;li&gt;Handling user input without blocking the UI&lt;/li&gt;&lt;li&gt;Performing complex calculations in the background&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;By using asynchronous techniques, JavaScript ensures that users don&#39;t experience delays or unresponsive behavior.&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;1. Understanding Callbacks&lt;/h3&gt;&lt;p&gt;The most basic form of asynchronous programming in JavaScript is using &lt;strong&gt;callbacks&lt;/strong&gt;. A callback is simply a function passed as an argument to another function, and it’s executed after the task completes.&lt;/p&gt;&lt;h4&gt;Example of Callback&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Start&#39;&lt;/span&gt;);

&lt;span class=&quot;hljs-built_in&quot;&gt;setTimeout&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;This happens after 2 seconds&#39;&lt;/span&gt;);
}, &lt;span class=&quot;hljs-number&quot;&gt;2000&lt;/span&gt;);

&lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;End&#39;&lt;/span&gt;);
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;console.log(&#39;Start&#39;)&lt;/code&gt; is executed immediately.&lt;/li&gt;&lt;li&gt;&lt;code&gt;setTimeout()&lt;/code&gt; is an asynchronous function that takes a callback and delays its execution for 2 seconds.&lt;/li&gt;&lt;li&gt;&lt;code&gt;console.log(&#39;End&#39;)&lt;/code&gt; is executed immediately after the start, before the &lt;code&gt;setTimeout&lt;/code&gt; callback runs.&lt;/li&gt;&lt;li&gt;After 2 seconds, &lt;code&gt;console.log(&#39;This happens after 2 seconds&#39;)&lt;/code&gt; is printed.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Output:&lt;/h3&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;sql&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Start&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;End&lt;/span&gt;
This happens after &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt; seconds
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;While callbacks are simple and effective, they can lead to a problem known as &lt;strong&gt;callback hell&lt;/strong&gt; when there are multiple nested callbacks, making the code difficult to read and maintain. Here’s an example of &lt;strong&gt;callback hell&lt;/strong&gt;:&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;doSomething&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;result&lt;/span&gt;) {
  &lt;span class=&quot;hljs-title function_&quot;&gt;doSomethingElse&lt;/span&gt;(result, &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;newResult&lt;/span&gt;) {
    &lt;span class=&quot;hljs-title function_&quot;&gt;doYetAnotherThing&lt;/span&gt;(newResult, &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;finalResult&lt;/span&gt;) {
      &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(finalResult);
    });
  });
});
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;This can quickly become unmanageable as the number of nested functions increases.&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;2. Promises: A Better Way to Handle Asynchronous Code&lt;/h3&gt;&lt;p&gt;To deal with callback hell and make asynchronous code more readable, JavaScript introduced &lt;strong&gt;Promises&lt;/strong&gt;. A Promise is an object representing the eventual completion (or failure) of an asynchronous operation and its resulting value.&lt;/p&gt;&lt;p&gt;A Promise can be in one of three states:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Pending&lt;/strong&gt;: The asynchronous operation is still in progress.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Fulfilled&lt;/strong&gt;: The asynchronous operation has completed successfully.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Rejected&lt;/strong&gt;: The asynchronous operation has failed.&lt;/li&gt;&lt;/ul&gt;&lt;h4&gt;Example of a Simple Promise&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; myPromise = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;hljs-title class_&quot;&gt;Promise&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;resolve, reject&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; success = &lt;span class=&quot;hljs-literal&quot;&gt;true&lt;/span&gt;;

  &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (success) {
    &lt;span class=&quot;hljs-title function_&quot;&gt;resolve&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Operation was successful!&#39;&lt;/span&gt;);
  } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; {
    &lt;span class=&quot;hljs-title function_&quot;&gt;reject&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Something went wrong.&#39;&lt;/span&gt;);
  }
});

myPromise
  .&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;result&lt;/span&gt;) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(result); &lt;span class=&quot;hljs-comment&quot;&gt;// &quot;Operation was successful!&quot;&lt;/span&gt;
  })
  .&lt;span class=&quot;hljs-title function_&quot;&gt;catch&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;error&lt;/span&gt;) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(error); &lt;span class=&quot;hljs-comment&quot;&gt;// If rejected, logs &quot;Something went wrong.&quot;&lt;/span&gt;
  });
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;A &lt;strong&gt;new Promise&lt;/strong&gt; is created with two parameters: &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt;.&lt;ul&gt;&lt;li&gt;If the asynchronous task succeeds, we call &lt;code&gt;resolve(value)&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;If it fails, we call &lt;code&gt;reject(error)&lt;/code&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;The &lt;code&gt;then()&lt;/code&gt; method is used to handle the &lt;strong&gt;fulfilled&lt;/strong&gt; state, and the &lt;code&gt;catch()&lt;/code&gt; method is used to handle the &lt;strong&gt;rejected&lt;/strong&gt; state.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;This makes code much more readable and avoids the callback nesting problem, allowing you to handle success and failure in a cleaner, linear way.&lt;/p&gt;&lt;h4&gt;Chaining Promises&lt;/h4&gt;&lt;p&gt;Promises can be &lt;strong&gt;chained&lt;/strong&gt; together, which means you can perform multiple asynchronous operations one after the other, where each step waits for the previous one to complete before executing.&lt;/p&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;fetchData&lt;/span&gt;()
  .&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;response&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;processData&lt;/span&gt;(response);
  })
  .&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;processedData&lt;/span&gt;) {
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;saveData&lt;/span&gt;(processedData);
  })
  .&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;savedData&lt;/span&gt;) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Data saved successfully:&#39;&lt;/span&gt;, savedData);
  })
  .&lt;span class=&quot;hljs-title function_&quot;&gt;catch&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;error&lt;/span&gt;) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;error&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;An error occurred:&#39;&lt;/span&gt;, error);
  });
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;Each &lt;code&gt;.then()&lt;/code&gt; receives the resolved value from the previous promise and returns a new promise, ensuring the asynchronous operations happen in order.&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;3. Async/Await: Syntactic Sugar for Promises&lt;/h3&gt;&lt;p&gt;While &lt;strong&gt;Promises&lt;/strong&gt; are a great way to handle asynchronous code, they still involve chaining &lt;code&gt;.then()&lt;/code&gt; and &lt;code&gt;.catch()&lt;/code&gt; methods, which can get a little cumbersome when you have multiple asynchronous operations.&lt;/p&gt;&lt;p&gt;To make asynchronous code even more readable, JavaScript introduced the &lt;strong&gt;async/await&lt;/strong&gt; syntax, which provides a more synchronous-like structure for dealing with asynchronous operations.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;async&lt;/code&gt;&lt;/strong&gt; is used to declare a function that will always return a promise.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;await&lt;/code&gt;&lt;/strong&gt; is used inside an &lt;code&gt;async&lt;/code&gt; function to pause execution until the promise is resolved.&lt;/li&gt;&lt;/ul&gt;&lt;h4&gt;Example of Async/Await&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetchData&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; response = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/posts&#39;&lt;/span&gt;);
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; data = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; response.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; data;
}

&lt;span class=&quot;hljs-title function_&quot;&gt;fetchData&lt;/span&gt;()
  .&lt;span class=&quot;hljs-title function_&quot;&gt;then&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;result&lt;/span&gt;) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(result); &lt;span class=&quot;hljs-comment&quot;&gt;// Logs the fetched data&lt;/span&gt;
  })
  .&lt;span class=&quot;hljs-title function_&quot;&gt;catch&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;error&lt;/span&gt;) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(error); &lt;span class=&quot;hljs-comment&quot;&gt;// Handles errors if any&lt;/span&gt;
  });
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;The &lt;code&gt;async&lt;/code&gt; function automatically returns a promise.&lt;/li&gt;&lt;li&gt;The &lt;code&gt;await&lt;/code&gt; keyword pauses the execution of the function until the promise is resolved.&lt;/li&gt;&lt;li&gt;The code inside the &lt;code&gt;async&lt;/code&gt; function looks synchronous, even though it&#39;s handling asynchronous operations.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;This approach simplifies error handling (using &lt;code&gt;try/catch&lt;/code&gt; blocks) and makes asynchronous code look almost identical to synchronous code.&lt;/p&gt;&lt;h4&gt;Example with &lt;code&gt;try/catch&lt;/code&gt; for Error Handling&lt;/h4&gt;&lt;pre class=&quot;!overflow-visible&quot;&gt;&lt;div class=&quot;contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950&quot;&gt;&lt;div class=&quot;flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none&quot;&gt;javascript&lt;/div&gt;&lt;div class=&quot;overflow-y-auto p-4&quot; dir=&quot;ltr&quot;&gt;&lt;code class=&quot;!whitespace-pre hljs language-javascript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;getUserData&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;/span&gt;) {
  &lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt; {
    &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; userResponse = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;https://jsonplaceholder.typicode.com/users&#39;&lt;/span&gt;);
    &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; userData = &lt;span class=&quot;hljs-keyword&quot;&gt;await&lt;/span&gt; userResponse.&lt;span class=&quot;hljs-title function_&quot;&gt;json&lt;/span&gt;();
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;log&lt;/span&gt;(userData);
  } &lt;span class=&quot;hljs-keyword&quot;&gt;catch&lt;/span&gt; (error) {
    &lt;span class=&quot;hljs-variable language_&quot;&gt;console&lt;/span&gt;.&lt;span class=&quot;hljs-title function_&quot;&gt;error&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Error fetching data:&#39;&lt;/span&gt;, error);
  }
}

&lt;span class=&quot;hljs-title function_&quot;&gt;getUserData&lt;/span&gt;();
&lt;/code&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;h3&gt;How it works:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;try/catch&lt;/code&gt; is used for handling errors when using &lt;code&gt;async/await&lt;/code&gt;. If any promise in the &lt;code&gt;try&lt;/code&gt; block is rejected, the &lt;code&gt;catch&lt;/code&gt; block will handle it.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;4. Why Async Programming Matters&lt;/h3&gt;&lt;p&gt;In modern web applications, especially single-page applications (SPAs), asynchronous programming is essential for:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Non-blocking UI&lt;/strong&gt;: Asynchronous operations allow you to make network requests, load data, and handle events without freezing or blocking the UI.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Improved User Experience&lt;/strong&gt;: By fetching data asynchronously (e.g., from a database or API), users can continue interacting with the page while the data loads in the background.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Better Performance&lt;/strong&gt;: JavaScript can continue executing code while waiting for time-consuming operations (like network requests or disk I/O), improving overall performance and responsiveness.&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;&lt;p&gt;In this first part of the series, we’ve covered the basics of &lt;strong&gt;asynchronous programming in JavaScript&lt;/strong&gt;, starting with callbacks and moving on to &lt;strong&gt;Promises&lt;/strong&gt; and the more modern &lt;strong&gt;async/await&lt;/strong&gt; syntax.&lt;/p&gt;&lt;p&gt;To recap:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Callbacks&lt;/strong&gt; are the simplest form of async programming but can lead to messy and hard-to-manage code (callback hell).&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Promises&lt;/strong&gt; offer a more readable and maintainable approach to asynchronous operations, especially when dealing with success and failure.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Async/Await&lt;/strong&gt; simplifies the syntax even further, making asynchronous code look and behave like synchronous code while maintaining the non-blocking benefits.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;In the next part of the series, we’ll explore more advanced topics such as &lt;strong&gt;handling multiple asynchronous operations&lt;/strong&gt; concurrently, &lt;strong&gt;parallel execution&lt;/strong&gt; of promises, and &lt;strong&gt;working with &lt;code&gt;Promise.all()&lt;/code&gt;&lt;/strong&gt; and &lt;code&gt;Promise.race()&lt;/code&gt;.&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;Key Takeaways:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Callbacks&lt;/strong&gt; are used for basic async operations but can become hard to manage.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Promises&lt;/strong&gt; provide a more structured and readable way to handle async operations.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Async/Await&lt;/strong&gt; simplifies async code and makes it look synchronous while still being asynchronous.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Stay tuned for the next part, where we&#39;ll dive deeper into handling multiple promises and concurrency in JavaScript!&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;I hope this post gives you a solid foundation for understanding async programming in JavaScript. If you have any questions or need further clarification, feel free to leave a comment below!&lt;/p&gt;

</description><link>https://www.ankitchamlagain.com.np/2025/01/async-programming-with-javascript.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGfDPj8rIKTirzcxwEuer8xBEaRLb4tmM-9ulaULzWAl0dfOrrC_9H_-TF9yNMXUcCORJ1W3ycrOYhFwWZg34mR50CPSY8Y_a-93RsM5kPDzQiTAF4dch7Y0vcMsT5sXgbjHcrgxNpthU1Gjq24i-r2oqn-YcCO6vGzm6ESBYvK1c79ZgmivgsyX5Wf-c/s72-c/programmers-using-javascript-pro.png" height="72" width="72"/><thr:total>0</thr:total></item></channel></rss>