<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hargun Singh</title>
    <description>The latest articles on DEV Community by Hargun Singh (@hargun_singh).</description>
    <link>https://dev.to/hargun_singh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1537043%2Fefddce4d-7679-4a61-9761-8c46906fab93.jpeg</url>
      <title>DEV Community: Hargun Singh</title>
      <link>https://dev.to/hargun_singh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hargun_singh"/>
    <language>en</language>
    <item>
      <title># Building an AI Agent with RAG: A Simple Guide to Vector Databases and Embeddings</title>
      <dc:creator>Hargun Singh</dc:creator>
      <pubDate>Mon, 15 Sep 2025 11:55:27 +0000</pubDate>
      <link>https://dev.to/hargun_singh/-building-an-ai-agent-with-rag-a-simple-guide-to-vector-databases-and-embeddings-2ch4</link>
      <guid>https://dev.to/hargun_singh/-building-an-ai-agent-with-rag-a-simple-guide-to-vector-databases-and-embeddings-2ch4</guid>
      <description>&lt;p&gt;Ever wondered how AI can answer questions about your documents? The secret lies in RAG (Retrieval-Augmented Generation) - a powerful technique that combines vector databases with language models to create intelligent assistants.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore how to build an AI agent that can understand and answer questions about your documents using RAG, vector databases, and embeddings.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is RAG and Why Do We Need It?
&lt;/h2&gt;

&lt;p&gt;RAG stands for &lt;strong&gt;Retrieval-Augmented Generation&lt;/strong&gt;. It's a technique that makes AI responses more accurate by grounding them in real documents.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;: Traditional AI models can "hallucinate" - they make up facts that sound convincing but aren't true.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution&lt;/strong&gt;: RAG retrieves relevant information from your documents first, then uses that context to generate accurate answers.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Three Key Components
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Embeddings: Converting Text to Numbers
&lt;/h3&gt;

&lt;p&gt;Embeddings are numerical representations of text that capture meaning. Think of them as a "fingerprint" for words and sentences.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// "machine learning" becomes:&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...]&lt;/span&gt; &lt;span class="c1"&gt;// 1536 numbers for OpenAI's model&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this matters&lt;/strong&gt;: Similar concepts get similar embeddings. "car" and "automobile" would have very similar numerical representations, even though they're different words.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Vector Database: The Smart Search Engine
&lt;/h3&gt;

&lt;p&gt;A vector database (like Pinecone) stores all your document embeddings and can instantly find the most similar pieces of text to any query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Traditional search&lt;/strong&gt;: Looks for exact keyword matches&lt;br&gt;
&lt;strong&gt;Vector search&lt;/strong&gt;: Understands meaning and context&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Language Model: The Answer Generator
&lt;/h3&gt;

&lt;p&gt;The LLM (like GPT) takes the retrieved context and generates human-like answers.&lt;/p&gt;
&lt;h2&gt;
  
  
  How RAG Works: Step by Step
&lt;/h2&gt;

&lt;p&gt;Let's trace through what happens when you ask "How do I install this library?":&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: Convert Your Question to Numbers
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queryEmbedding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;embeddingService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateEmbedding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;How do I install this library?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Result: [0.2, -0.1, 0.9, ...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Step 2: Find Similar Content
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;similarChunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;pineconeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySimilar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queryEmbedding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Result: Returns 5 most relevant document chunks&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Step 3: Generate Answer with Context
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;llmService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateAnswer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;relevantChunks&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Result: "To install this library, run 'npm install package-name' in your terminal..."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  The Complete Flow
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1720xfmguordeiv7o9h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1720xfmguordeiv7o9h.png" alt="Whole flow how documents are processed" width="800" height="250"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Why RAG is Powerful
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. &lt;strong&gt;Accurate Answers&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Only uses information from your actual documents&lt;/li&gt;
&lt;li&gt;Prevents AI from making up facts&lt;/li&gt;
&lt;li&gt;Every answer is backed by source material&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  2. &lt;strong&gt;Context-Aware&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Understands meaning, not just keywords&lt;/li&gt;
&lt;li&gt;Can find relevant information even with different wording&lt;/li&gt;
&lt;li&gt;Maintains conversation context&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  3. &lt;strong&gt;Scalable&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Works with any type of document&lt;/li&gt;
&lt;li&gt;Easy to add new data sources&lt;/li&gt;
&lt;li&gt;Handles large amounts of information efficiently&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Real-World Example: How RAG Works in Practice
&lt;/h2&gt;

&lt;p&gt;Let's walk through a complete example of how RAG processes a user query step by step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User asks&lt;/strong&gt;: "What's the refund policy for this product?"&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: Document Processing (Done Once)
&lt;/h3&gt;

&lt;p&gt;First, our system processes all documents and converts them to searchable chunks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Document: "Terms of Service.pdf"&lt;/span&gt;
&lt;span class="c1"&gt;// Chunk 1: "Our refund policy allows returns within 30 days..."&lt;/span&gt;
&lt;span class="c1"&gt;// Chunk 2: "Refunds are processed within 5-7 business days..."&lt;/span&gt;
&lt;span class="c1"&gt;// Chunk 3: "For digital products, refunds are only available if..."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each chunk gets converted to an embedding (a list of 1536 numbers) and stored in Pinecone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Query Processing (Real-time)
&lt;/h3&gt;

&lt;p&gt;When the user asks their question:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2a. Convert Question to Embedding&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queryEmbedding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;embeddingService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateEmbedding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What's the refund policy for this product?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Result: [0.2, -0.1, 0.9, 0.3, -0.4, ...] (1536 numbers)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2b. Search Vector Database&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;similarChunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;pineconeService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySimilar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queryEmbedding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Returns top 3 most similar chunks based on meaning, not keywords&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2c. Retrieve Context&lt;/strong&gt;&lt;br&gt;
The system finds these relevant chunks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chunk 1: "Our refund policy allows returns within 30 days of purchase if the product is unused and in original packaging."&lt;/li&gt;
&lt;li&gt;Chunk 2: "Refunds are processed within 5-7 business days and will be credited back to your original payment method."&lt;/li&gt;
&lt;li&gt;Chunk 3: "For digital products, refunds are only available if you haven't downloaded the content yet."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2d. Generate Answer&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;llmService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateAnswer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;relevantChunks&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The LLM combines the user's question with the retrieved context to generate a natural response.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Final Response
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;User sees&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Based on our terms of service, you can request a full refund within 30 days of purchase if the product is unused and in original packaging. Refunds are processed within 5-7 business days and will be credited back to your original payment method. For digital products, refunds are only available if you haven't downloaded the content yet."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Source&lt;/strong&gt;: [Terms of Service.pdf, Section 4.2]&lt;/p&gt;

&lt;h3&gt;
  
  
  Why This Works Better Than Traditional Search
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Traditional keyword search&lt;/strong&gt; would look for exact matches like "refund policy" and might miss relevant information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RAG with vector search&lt;/strong&gt; understands that "return policy", "money back guarantee", and "refund policy" all mean the same thing, so it finds all relevant information regardless of the exact wording used.&lt;/p&gt;

&lt;p&gt;The key insight: RAG doesn't just search for keywords - it searches for &lt;strong&gt;meaning&lt;/strong&gt;, making it much more powerful and accurate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with RAG
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Choose Your Tools
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Embeddings&lt;/strong&gt;: OpenAI's text-embedding-ada-002&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vector Database&lt;/strong&gt;: Pinecone (or alternatives like Weaviate, Qdrant)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LLM&lt;/strong&gt;: OpenAI GPT-3.5 or GPT-4&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Process Your Documents
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Convert documents to chunks&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;processDocuments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;documents&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Generate embeddings&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;embeddings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateEmbeddings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Store in vector database&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;storeInVectorDB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;embeddings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Query Your Knowledge Base
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Convert question to embedding&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queryEmbedding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateEmbedding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Find similar content&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;searchVectorDB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queryEmbedding&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Generate answer&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateAnswer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;question&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RAG&lt;/strong&gt; combines retrieval and generation for accurate AI responses&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Embeddings&lt;/strong&gt; convert text to numbers that capture meaning&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vector databases&lt;/strong&gt; enable fast, semantic search&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context&lt;/strong&gt; from your documents prevents AI hallucination&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Source citations&lt;/strong&gt; build trust and allow verification&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;RAG is just the beginning. You can extend this approach with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple document types (PDFs, CSVs, web pages)&lt;/li&gt;
&lt;li&gt;Hybrid search (combining vector and keyword search)&lt;/li&gt;
&lt;li&gt;Fine-tuned models for specific domains&lt;/li&gt;
&lt;li&gt;Real-time document updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The beauty of RAG is its simplicity - it takes your existing documents and makes them intelligently searchable through natural language.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Ready to build your own RAG-powered AI agent? Start with a simple document, get the basic flow working, then gradually add more sophisticated features. The future of knowledge management is here, and it's powered by vectors!&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Why the "this" Keyword Behaves Differently in Regular Functions and Arrow Functions</title>
      <dc:creator>Hargun Singh</dc:creator>
      <pubDate>Sat, 27 Jul 2024 18:29:47 +0000</pubDate>
      <link>https://dev.to/hargun_singh/why-the-this-keyword-behaves-differently-in-regular-functions-and-arrow-functions-52j6</link>
      <guid>https://dev.to/hargun_singh/why-the-this-keyword-behaves-differently-in-regular-functions-and-arrow-functions-52j6</guid>
      <description>&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword in JavaScript can be confusing because it behaves differently in regular functions and arrow functions. In this blog post, we will explain how &lt;code&gt;this&lt;/code&gt; works in both types of functions, explore why these differences exist, and provide the basic knowledge you need to understand &lt;code&gt;this&lt;/code&gt; in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Regular Functions
&lt;/h3&gt;

&lt;p&gt;Regular functions in JavaScript are defined using the &lt;code&gt;function&lt;/code&gt; keyword. The value of &lt;code&gt;this&lt;/code&gt; in these functions depends on how the function is called. Here are several examples:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Global Context
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Non-Strict Mode:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;regularFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;regularFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Logs the global object (window in browsers)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; In non-strict mode, when a function is called in the global context (not as a method of an object), &lt;code&gt;this&lt;/code&gt; refers to the global object (&lt;code&gt;window&lt;/code&gt; in browsers or &lt;code&gt;global&lt;/code&gt; in Node.js).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strict Mode:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;regularFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;regularFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Logs undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; In strict mode, &lt;code&gt;this&lt;/code&gt; remains &lt;code&gt;undefined&lt;/code&gt; when a function is called in the global context, providing a safer environment by preventing accidental modifications to the global object.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Method Call
&lt;/h4&gt;

&lt;p&gt;When a function is called as a method of an object, &lt;code&gt;this&lt;/code&gt; refers to that object.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Logs obj&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; In this case, &lt;code&gt;this&lt;/code&gt; points to &lt;code&gt;obj&lt;/code&gt; because the function is called as a method of &lt;code&gt;obj&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strict Mode:&lt;/strong&gt; The behavior remains the same in strict mode.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Constructor Call
&lt;/h4&gt;

&lt;p&gt;When a function is used as a constructor (called with the &lt;code&gt;new&lt;/code&gt; keyword), &lt;code&gt;this&lt;/code&gt; refers to the newly created instance.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Logs "Hello, my name is Alice"&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;person2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Logs "Hello, my name is Bob"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; When called with &lt;code&gt;new&lt;/code&gt;, &lt;code&gt;this&lt;/code&gt; inside the &lt;code&gt;Person&lt;/code&gt; constructor function refers to the new instance being created. Each new instance (&lt;code&gt;person1&lt;/code&gt; and &lt;code&gt;person2&lt;/code&gt;) gets its own &lt;code&gt;name&lt;/code&gt; property and &lt;code&gt;sayHello&lt;/code&gt; method.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strict Mode:&lt;/strong&gt; The behavior remains the same in strict mode.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. Explicit Binding
&lt;/h4&gt;

&lt;p&gt;You can explicitly bind &lt;code&gt;this&lt;/code&gt; using &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt;, or &lt;code&gt;bind&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;regularFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;regularFunction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Logs obj&lt;/span&gt;
  &lt;span class="nx"&gt;regularFunction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Logs obj&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;boundFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;regularFunction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;boundFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;            &lt;span class="c1"&gt;// Logs obj&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; immediately invoke the function with &lt;code&gt;this&lt;/code&gt; set to &lt;code&gt;obj&lt;/code&gt;, while &lt;code&gt;bind&lt;/code&gt; creates a new function with &lt;code&gt;this&lt;/code&gt; permanently bound to &lt;code&gt;obj&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strict Mode:&lt;/strong&gt; The behavior remains the same in strict mode.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Arrow Functions
&lt;/h3&gt;

&lt;p&gt;Arrow functions, introduced in ES6, do not have their own &lt;code&gt;this&lt;/code&gt; context. Instead, they inherit &lt;code&gt;this&lt;/code&gt; from the surrounding (lexical) scope. This makes arrow functions useful in certain situations.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Lexical &lt;code&gt;this&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Arrow functions inherit &lt;code&gt;this&lt;/code&gt; from the scope in which they are defined.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Non-Strict Mode:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrowFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nf"&gt;arrowFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Logs the global object (window in browsers)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; Arrow functions do not have their own &lt;code&gt;this&lt;/code&gt;; they use &lt;code&gt;this&lt;/code&gt; from the surrounding scope. Here, it refers to the global object because the function is defined in the global scope.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strict Mode:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrowFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nf"&gt;arrowFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Logs undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; In strict mode, if the surrounding scope is global, &lt;code&gt;this&lt;/code&gt; remains &lt;code&gt;undefined&lt;/code&gt; as inherited from the surrounding scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Method Call
&lt;/h4&gt;

&lt;p&gt;Unlike regular functions, arrow functions do not get their own &lt;code&gt;this&lt;/code&gt; when called as methods. They inherit &lt;code&gt;this&lt;/code&gt; from the enclosing scope.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Logs the global object (window in browsers) or undefined in strict mode&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; Arrow functions do not bind their own &lt;code&gt;this&lt;/code&gt; but inherit it from the lexical scope. In this example, &lt;code&gt;this&lt;/code&gt; refers to the global object or &lt;code&gt;undefined&lt;/code&gt; in strict mode, not &lt;code&gt;obj&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strict Mode:&lt;/strong&gt; Logs &lt;code&gt;undefined&lt;/code&gt;, not &lt;code&gt;obj&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Arrow Function Inside Another Function
&lt;/h4&gt;

&lt;p&gt;When an arrow function is defined inside another function, it inherits &lt;code&gt;this&lt;/code&gt; from the outer function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrowFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;

      &lt;span class="nf"&gt;arrowFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;outerFunction&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Logs obj, because `this` in arrowFunction is inherited from outerFunction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; In this case, &lt;code&gt;this&lt;/code&gt; inside the arrow function refers to the same &lt;code&gt;this&lt;/code&gt; as in &lt;code&gt;outerFunction&lt;/code&gt;, which is &lt;code&gt;obj&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strict Mode:&lt;/strong&gt; The behavior remains the same in strict mode.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. Arrow Function in Event Handlers
&lt;/h4&gt;

&lt;p&gt;Arrow functions in event handlers inherit &lt;code&gt;this&lt;/code&gt; from the surrounding lexical scope, not from the element that triggers the event.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// Logs the global object (window in browsers) or undefined in strict mode&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; The arrow function does not bind &lt;code&gt;this&lt;/code&gt; to the button element; it inherits it from the surrounding scope, which is the global scope or &lt;code&gt;undefined&lt;/code&gt; in strict mode.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strict Mode:&lt;/strong&gt; Logs &lt;code&gt;undefined&lt;/code&gt;, not the button element.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why These Differences?
&lt;/h3&gt;

&lt;p&gt;The difference between regular functions and arrow functions lies in how they handle &lt;code&gt;this&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Regular Functions:&lt;/strong&gt; The value of &lt;code&gt;this&lt;/code&gt; is dynamic and determined by the call-site (how the function is called).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arrow Functions:&lt;/strong&gt; The value of &lt;code&gt;this&lt;/code&gt; is lexical and set at the time the function is defined, based on the &lt;code&gt;this&lt;/code&gt; value of the enclosing execution context.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key Concepts to Understand
&lt;/h3&gt;

&lt;p&gt;To understand the behavior of &lt;code&gt;this&lt;/code&gt; in JavaScript, you need to know the following concepts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Execution Context:&lt;/strong&gt; The context in which code is executed, affecting how &lt;code&gt;this&lt;/code&gt; is determined.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Call-site:&lt;/strong&gt; The location in code where a function is called, influencing the value of &lt;code&gt;this&lt;/code&gt; in regular functions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lexical Scoping:&lt;/strong&gt; How &lt;code&gt;this&lt;/code&gt; is inherited in arrow functions from the surrounding scope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strict Mode:&lt;/strong&gt; How strict mode changes the default behavior of &lt;code&gt;this&lt;/code&gt; in certain contexts.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Regular Functions:&lt;/strong&gt; &lt;code&gt;this&lt;/code&gt; is dynamic and determined by the call-site.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arrow Functions:&lt;/strong&gt; &lt;code&gt;this&lt;/code&gt; is lexical and determined by the surrounding scope when the function is defined.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these distinctions will help you write more predictable and maintainable JavaScript code. Whether you're using regular functions or arrow functions, knowing how &lt;code&gt;this&lt;/code&gt; works is crucial for effective JavaScript development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Your Turn!
&lt;/h3&gt;

&lt;p&gt;I’d love to hear your thoughts and experiences with the this keyword. Have you encountered any tricky situations where the behavior of this caused unexpected results?&lt;/p&gt;

&lt;p&gt;Feel free to leave your questions or comments below. Engaging with the content and asking questions is a great way to deepen your understanding and foster a community discussion. Your feedback will help me create more insightful and useful content in the future!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Custom Hooks in React: Use Cases and Significance</title>
      <dc:creator>Hargun Singh</dc:creator>
      <pubDate>Sat, 20 Jul 2024 05:03:50 +0000</pubDate>
      <link>https://dev.to/hargun_singh/custom-hooks-in-react-use-cases-and-significance-4931</link>
      <guid>https://dev.to/hargun_singh/custom-hooks-in-react-use-cases-and-significance-4931</guid>
      <description>&lt;p&gt;Custom hooks in React are an excellent way to encapsulate reusable logic, manage state, and handle side effects in a way that keeps your code clean and maintainable. Here are some key use cases and the significance of creating custom hooks:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Reusing Logic Across Components&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Fetching data from an API.&lt;/strong&gt;&lt;br&gt;
You can create a custom hook like useFetch to encapsulate the logic for fetching data and handle loading, success, and error states. This logic can then be reused across multiple components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from 'react';

const useFetch = (url) =&amp;gt; {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() =&amp;gt; {
    fetch(url)
      .then((response) =&amp;gt; response.json())
      .then((data) =&amp;gt; {
        setData(data);
        setLoading(false);
      })
      .catch((error) =&amp;gt; {
        setError(error);
        setLoading(false);
      });
  }, [url]);

  return { data, loading, error };
};

export default useFetch;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Managing Complex State Logic&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Managing form state&lt;/strong&gt;&lt;br&gt;
Custom hooks can be used to manage form state and validation logic in a reusable way.&lt;br&gt;
&lt;/p&gt;

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

const useForm = (initialState) =&amp;gt; {
  const [values, setValues] = useState(initialState);

  const handleChange = (event) =&amp;gt; {
    const { name, value } = event.target;
    setValues({
      ...values,
      [name]: value,
    });
  };

  const resetForm = () =&amp;gt; {
    setValues(initialState);
  };

  return [values, handleChange, resetForm];
};

export default useForm;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Abstracting Side Effects&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Synchronizing with local storage.&lt;/strong&gt;&lt;br&gt;
You can create a custom hook to manage state that synchronizes with local storage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from 'react';

const useLocalStorage = (key, initialValue) =&amp;gt; {
  const [value, setValue] = useState(() =&amp;gt; {
    const storedValue = localStorage.getItem(key);
    return storedValue ? JSON.parse(storedValue) : initialValue;
  });

  useEffect(() =&amp;gt; {
    localStorage.setItem(key, JSON.stringify(value));
  }, [key, value]);

  return [value, setValue];
};

export default useLocalStorage;

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Significance of Custom Hooks
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Code Reusability&lt;/strong&gt;&lt;br&gt;
Custom hooks allow you to reuse logic across multiple components without duplicating code, promoting the DRY (Don't Repeat Yourself) principle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Separation of Concerns&lt;/strong&gt;&lt;br&gt;
By moving logic out of components and into hooks, you can keep your component code cleaner and more focused on rendering, while the custom hook handles the logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Testability&lt;/strong&gt;&lt;br&gt;
Custom hooks can be tested independently from the components that use them, making it easier to write unit tests for complex logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Consistency&lt;/strong&gt;&lt;br&gt;
Using custom hooks ensures consistent behavior across different parts of your application, as the same logic is used wherever the hook is invoked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Custom hooks in React are a powerful tool for creating reusable, maintainable, and testable code. They help you encapsulate complex logic, manage state and side effects efficiently, and promote best practices like separation of concerns and code reusability. By leveraging custom hooks, you can keep your components clean and focused on their primary purpose: rendering UI.&lt;/p&gt;

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