<?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: Abhijeet231</title>
    <description>The latest articles on DEV Community by Abhijeet231 (@abhijeet231).</description>
    <link>https://dev.to/abhijeet231</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3328435%2Fe77a84b7-95c4-452e-b89b-edc9e2a9970b.jpg</url>
      <title>DEV Community: Abhijeet231</title>
      <link>https://dev.to/abhijeet231</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhijeet231"/>
    <language>en</language>
    <item>
      <title>Routing in Angular.</title>
      <dc:creator>Abhijeet231</dc:creator>
      <pubDate>Thu, 30 Jul 2026 16:57:12 +0000</pubDate>
      <link>https://dev.to/abhijeet231/routing-in-angular-19ha</link>
      <guid>https://dev.to/abhijeet231/routing-in-angular-19ha</guid>
      <description>&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjd1sgso6jeybq1bggjps.jpg" 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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjd1sgso6jeybq1bggjps.jpg" alt=" " width="735" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hi, I'm Abhi. I write articles - real ones, not AI slop. Human written, human friendly &amp;amp;  for humans. &lt;/p&gt;

&lt;p&gt;So  I'm a full-stac developer ( MERN / PERN ). React, Express, Node, that's my comfort zone. But recently I started learning Angular, and  routing in Angular is a whole different game. Some things are surprisingly nice, some things made me go "why is this so complicated?"&lt;/p&gt;

&lt;p&gt;So instead of just learning and forgetting, I thought why not write about it? If you're also a React dev exploring Angular for the first time, this one's for you.&lt;/p&gt;

&lt;p&gt;Stay tuned and have a look at the sections below.&lt;/p&gt;

&lt;h1&gt;
  
  
  What’s in this Article !
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;How Routing Works in Angular (the mental model, &lt;code&gt;RouterModule&lt;/code&gt;, &lt;code&gt;app-routing.module&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Basic Routing Example&lt;/li&gt;
&lt;li&gt;Lazy Loading (brief intro)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before starting, I would like to mention -- I am not an expert in Angular. I just started exploring it and I'm sharing my knowledge here. So you get to see a React guy's POV while learning Angular sideways.&lt;/p&gt;

&lt;h1&gt;
  
  
  How Routing Works in Angular.
&lt;/h1&gt;

&lt;p&gt;So unlike React - where we have to install npm packages and use React Router or TanStack Router for routing, because React doesn't have routing on its own - in Angular it's totally different. It provides a prebuilt routing architecture that we can use while building projects, and this is what they use in production grade products too.&lt;/p&gt;

&lt;p&gt;This is called the &lt;strong&gt;Angular Router&lt;/strong&gt; ( @angular/router ). It comes built-in when you create a project using the Angular CLI. &lt;/p&gt;

&lt;p&gt;We don’t have to do  npm install, or any  third party package, nothing. Just create a project and start routing.&lt;/p&gt;

&lt;p&gt;So how to use this? Since i’m using  Angular 22 , so we are going to stick to this, we'll be using the standalone approach. If you see older tutorials using RouterModule.forRoot() and app-routing.module.ts , that's the old way. We are not doing that here.&lt;/p&gt;

&lt;p&gt;Now before jumping into code, let me quickly explain the key pieces you need to know:&lt;/p&gt;

&lt;h2&gt;
  
  
  Routes
&lt;/h2&gt;

&lt;p&gt;This is just an array where you define which path should load which component. If you've used React Router, think of it like your route config array. Something like &lt;code&gt;{ path: 'about', component: AboutComponent }&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  ProvideRouter()
&lt;/h2&gt;

&lt;p&gt;This is how you register your routes in &lt;code&gt;app.config.ts&lt;/code&gt;. You pass your routes array inside this function and Angular takes care of the rest. &lt;/p&gt;

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

&lt;p&gt;This is the placeholder in your HTML where the matched component will render. If you know React Router, this is basically &lt;code&gt;&amp;lt;Outlet /&amp;gt;&lt;/code&gt;. You put this in your &lt;code&gt;app.component.html&lt;/code&gt; and Angular will swap components in and out of this spot based on the URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  routerLink
&lt;/h2&gt;

&lt;p&gt;This is Angular's version of &lt;code&gt;&amp;lt;Link to="/" /&amp;gt;&lt;/code&gt; from React Router. Instead of using &lt;code&gt;&amp;lt;a href=""&amp;gt;&lt;/code&gt; (which will reload the whole page), you use &lt;code&gt;routerLink&lt;/code&gt; for client-side navigation. Looks like this: &lt;code&gt;&amp;lt;a routerLink="/about"&amp;gt;About&amp;lt;/a&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's it. These four things are all you need to set up basic routing in Angular. If you already know React Router, here's a quick cheat sheet:&lt;/p&gt;




&lt;h1&gt;
  
  
  Basic Routing Example
&lt;/h1&gt;

&lt;p&gt;So , now it’s time to build something. Now we are going to build an Angular app with 4/5 pages. &lt;/p&gt;

&lt;p&gt;Follow the steps below: &lt;/p&gt;

&lt;h2&gt;
  
  
  Step - 1 :
&lt;/h2&gt;

&lt;p&gt;Create a new Angular app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;ng&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;angular&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;routing&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will ask you few questions, mae sure to click yes while setting up a new app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step - 2 :
&lt;/h2&gt;

&lt;p&gt;Generate the Components&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;ng&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="nx"&gt;about&lt;/span&gt;
&lt;span class="nx"&gt;ng&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="nx"&gt;contact&lt;/span&gt;
&lt;span class="nx"&gt;ng&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="nx"&gt;home&lt;/span&gt;
&lt;span class="nx"&gt;ng&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="nx"&gt;pricing&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this your folder structure should look somethig like this- &lt;/p&gt;

&lt;p&gt;!image.png&lt;/p&gt;

&lt;p&gt;You can add some basic text in those components tempalte files. So that you can identify the pages easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step - 3 :
&lt;/h2&gt;

&lt;p&gt;Now it’s time to define the routes. &lt;/p&gt;

&lt;p&gt;In the app folder, you can see a file named as - app.route.ts .&lt;/p&gt;

&lt;p&gt;Open this file and initially it shold look like this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Routes&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/router&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;now we are going to write the routing of our project here . &lt;/p&gt;

&lt;p&gt;For this- you need to import all components and add them inside the array. this should look like this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Routes&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/router&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;About&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./about/about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Contact&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./contact/contact&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./home/home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Pricing&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./pricing/pricing&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;About&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;contact&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Contact&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pricing&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Pricing&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
     &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;**&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NotFound&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;If you've used React Router, this is basically your route config array. &lt;/p&gt;

&lt;p&gt;That ** refers to wildcard route, you can obviously create a not-found page and assign it there .&lt;/p&gt;

&lt;h2&gt;
  
  
  Step - 4 :
&lt;/h2&gt;

&lt;p&gt;Now it’s time to register the routes.&lt;/p&gt;

&lt;p&gt;Open the app.config.ts file and here we need to register the routes in our app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ApplicationConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;provideBrowserGlobalErrorListeners&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;provideRouter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/router&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.routes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;appConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ApplicationConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;provideBrowserGlobalErrorListeners&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;provideRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;You might have to do this manually or there are high chances it is already done for you, depends on how you configured your app. &lt;/p&gt;

&lt;p&gt;Now your entire app has the context of your routing. Now it’s time to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step - 5 :
&lt;/h2&gt;

&lt;p&gt;Now it’s time to add  and navigation links. Open the app.ts  file  and write this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;RouterOutlet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;RouterLink&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/router&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="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;RouterOutlet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;RouterLink&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;protected&lt;/span&gt; &lt;span class="nx"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;angular-routing&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Important thing to notice here is - In React we don’t  need to do this, but in Angular, standalone components need to explicitly declare what they use. Took me a while to get used to this.&lt;/p&gt;

&lt;p&gt;Now open the app.html file which is the main template file and write this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="na"&gt;routerLink&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="na"&gt;routerLink&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/about"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;About&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="na"&gt;routerLink&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/contact"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;router&lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="na"&gt;outlet&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;router&lt;/span&gt;&lt;span class="err"&gt;-&lt;/span&gt;&lt;span class="na"&gt;outlet&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are basically creating a navbar here and providing the context for routing here.&lt;/p&gt;

&lt;p&gt;If you want you can create separate navbar and use that here. I”m doing this to keep things easy and minimal.&lt;/p&gt;




&lt;h1&gt;
  
  
  Lazy Loading.
&lt;/h1&gt;

&lt;p&gt;For a small app, this is no big deal. But imagine you are working on a production grade product and you have 50 pages. &lt;/p&gt;

&lt;p&gt;But your user just want to see the Home page, but their browser is downloading all 50 pages. Obviously this is not the optimised way to handle things. &lt;/p&gt;

&lt;p&gt;Now this is where Lazy Loading comes to the game. It only loads a page’s code when the user actually navigate to it. Not before it. &lt;/p&gt;

&lt;p&gt;In React this is the same concept but the difference is only how we implement this concept. &lt;/p&gt;

&lt;p&gt;As we discussed earlier how to define routes -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AboutComponent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./pages/about/about.component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AboutComponent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where we add lazy loading concept. Now if you want  this page to lazily load then you have to do this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;loadComponent&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="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./pages/about/about.component&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;AboutComponent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Important thing to remember - you don’t have to import it at the top.&lt;/p&gt;

&lt;p&gt;Now about component will only load when someone actually clicks on it or navigates to it.&lt;/p&gt;




&lt;p&gt;So, that's it for Part 1. We covered how routing works in Angular, set up a basic routing example from scratch, and looked at lazy loading. If you made it this far, you know enough to start building stuff with Angular routing.&lt;/p&gt;

&lt;p&gt;In Part 2 we’ll learn about : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Route Parameters &amp;amp; Query Params&lt;/li&gt;
&lt;li&gt;Nested / Child Routes&lt;/li&gt;
&lt;li&gt;Route Guards&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you found this helpful, share it with your dev friends who are still stuck in the React bubble. And follow me so you don’t miss part 2 .&lt;/p&gt;

&lt;p&gt;Happy Coding🌻&lt;/p&gt;

</description>
      <category>angular</category>
      <category>webdev</category>
      <category>fullstack</category>
      <category>typescript</category>
    </item>
    <item>
      <title>JavaScript OOP Explained Without Making Your Brain Hurt</title>
      <dc:creator>Abhijeet231</dc:creator>
      <pubDate>Tue, 10 Mar 2026 07:56:37 +0000</pubDate>
      <link>https://dev.to/abhijeet231/javascript-oop-explained-without-making-your-brain-hurt-31ff</link>
      <guid>https://dev.to/abhijeet231/javascript-oop-explained-without-making-your-brain-hurt-31ff</guid>
      <description>&lt;p&gt;If you are someone who has just started learning Object-Oriented Programming, or you are thinking about learning it, then this is the perfect place to get a basic understanding of OOP..&lt;/p&gt;

&lt;p&gt;So let me set the context first, I'm a JavaScript developer (MERN) and i have been using functional programming from past 5/6 months.&lt;/p&gt;

&lt;p&gt;Built some project around it. But recently I got some exposure to Object-Oriented Programming, and out of curiosity i learend about it, By the way, I'm still learning.&lt;/p&gt;

&lt;p&gt;But in todays articel&amp;nbsp;, i'm going to share all my understanding till now and will try to convey it in a beginner friendly way. So let's get started….&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhqnm86hyztji2qjhz0k.jpg" 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%2Fyhqnm86hyztji2qjhz0k.jpg" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Object Oriented Programming
&lt;/h2&gt;

&lt;p&gt;So you must be thinking what does Object Oriented Programming even mean&amp;nbsp;!!&lt;/p&gt;

&lt;p&gt;Let me tell you if you are someone like me&amp;nbsp;, who has some prior experience with JavaScript and been buildign things around it and still don't know about Object Oriented Programming.&lt;/p&gt;

&lt;p&gt;Then it's 101% sure what you are using till now is known as functional Programming.&lt;/p&gt;

&lt;p&gt;So in functional Programming you basically use functions to write code, While writing controller logic, handling database connections, or building API routes, you are mostly using functions everywhere.&lt;/p&gt;

&lt;p&gt;And i'm preety sure, you mus have thought about it&amp;nbsp;, if not then now you should connect the dots&amp;nbsp;, you will notice that you have been using functions almost everywhere.. So that's what Functional Programming mean.&lt;/p&gt;

&lt;p&gt;But Object Oriented Programming is a way of writing code where we organise things as Objects. These objects contains&amp;nbsp;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              1.Properties
              2.Methods
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;barnd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;honda&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;white&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;start&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Car started&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="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Car Started&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, brand and color are properties, and start is a method.&lt;/p&gt;

&lt;p&gt;This syntax is called as - Object Literal.&lt;/p&gt;

&lt;p&gt;Using this syntax, you can access the object's properties and methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Class in JavaScript
&lt;/h2&gt;

&lt;p&gt;Class in JavaScript is a blueprint for creating objects.&lt;/p&gt;

&lt;p&gt;When we need many Objects with similar dynamic properites (same key but different value for each object) we use class syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;color&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;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;brand&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;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;start&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="nx"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; started&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;car1&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;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Honda&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;White&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;car2&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;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;BMW&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Black&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now you can create as many objects you need&amp;nbsp;, and they all have the same properties but differnet value, which you pass when creating the object.&lt;/p&gt;

&lt;p&gt;Now, if you're not just vibe coding, after seeing the above code you might be thinking:&lt;/p&gt;

&lt;p&gt;"Dude, what do constructor and the new keyword even mean?" why are we using these&amp;nbsp;?&lt;/p&gt;

&lt;p&gt;Don't worry you don't have to go to chatGpt and ask all this. Because i already did that for you. 😌&lt;/p&gt;

&lt;p&gt;But you can obviously go and argue with GPT&amp;nbsp;, ask him questions to increase your understanding.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgbmxd6gosnvooomvoh4q.jpg" 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%2Fgbmxd6gosnvooomvoh4q.jpg" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Constructor &amp;amp; new keyword&amp;nbsp;??
&lt;/h2&gt;

&lt;p&gt;A constructor is a special method that runs automatically when an object is created.&lt;/p&gt;

&lt;p&gt;It is usually used to initialize properties for the object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;color&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;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;brand&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;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;start&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="nx"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; started&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;car1&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;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Honda&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;White&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;car2&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;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;BMW&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Black&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code the constructor is takign 2 values - brand and color &amp;amp; assigning them to the object using this keyword. &lt;/p&gt;

&lt;p&gt;Now as you can see - while creating eveyr object - each time we are using new keyword, but why? &lt;br&gt;
So when we do -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const car1 = new Car("Honda", "White");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now JavaScript is doing several things behind the scenes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It creates a new empty object in memory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It links that object to the Car class prototype.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It runs the constructor and passes the arguments ("Honda", "White").&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It returns the newly created object.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So after running this code, now the object looks something like this -&lt;br&gt;
&lt;code&gt;{&lt;br&gt;
  brand: "Honda",&lt;br&gt;
  color: "White"&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And it also has the access to all methods declared inside the class.&lt;/p&gt;

&lt;p&gt;Every time we are doing - new Car()&amp;nbsp;, we are creating new objects.&lt;/p&gt;

&lt;p&gt;This whole "class" thing in javascript allows us to create as many objects we want by using a single blueprint.&lt;/p&gt;

&lt;p&gt;But you must have been thinking, if we create many objects, will the methods be copied many times?&lt;/p&gt;

&lt;p&gt;The answer is no..&lt;br&gt;
So let's talk about memory management a little bit&amp;nbsp;, so t hat you can have a better clarity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fudydpxsl5984zjhi4qjm.jpg" 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%2Fudydpxsl5984zjhi4qjm.jpg" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How memory optimization works
&lt;/h2&gt;

&lt;p&gt;All objects created from a class share the same methods through the prototype. This helps JavaScript avoid duplicating methods in memory.&lt;/p&gt;

&lt;p&gt;So when we write this code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&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;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;start&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Car started&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The start() method is not duplicated for every object. Instead, JavaScript stores it once in something called the prototype, and every object simply references it.&lt;/p&gt;

&lt;p&gt;So this means -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each object stores its own data.&lt;/li&gt;
&lt;li&gt;But all objects share the same method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we have one more important thing to cover, which is known as Encapsulation. So let's try to understand what does it even mean&amp;nbsp;? And how it works&amp;nbsp;?&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Idea of Encapsulation
&lt;/h2&gt;

&lt;p&gt;Encapsulation is one of the core ideas in Object-Oriented Programming.&lt;/p&gt;

&lt;p&gt;In simple terms, encapsulation means hiding the internal details of an object and allowing access only through controlled methods.&lt;br&gt;
Think abut it like using a keyboard.&lt;/p&gt;

&lt;p&gt;You press a specific key and the key types in you computer. But don't directly manipulate the keyboards internal circuits.&lt;/p&gt;

&lt;p&gt;The complex implemention is hidden from you.&lt;br&gt;
In Programming, encapsulation works the same way. We protect important data inside a class and allow interaction through specific methods.&lt;/p&gt;

&lt;p&gt;In JavaScript, encapsulation is mainly done using&amp;nbsp;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;private fields (#)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Getters&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Setters&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Closures&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And If you are using Typescript then you also have public &amp;amp; private keywords like Java or C++.&lt;/p&gt;

&lt;p&gt;For now, I'm just sharing the basic idea of encapsulation here, because I'm still learning it myself and don't fully understand it yet.&lt;/p&gt;

&lt;p&gt;Once I learn it properly and get a deeper understanding, I'll definitely write another article explaining it in detail.&lt;/p&gt;

&lt;p&gt;So stay tuned! And if this blog helped you in any way, feel free to follow me for more learning notes and articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;OOP organises code using objects&lt;br&gt;
Classes act as blueprint for objects&lt;br&gt;
The constructor initializes object data&lt;br&gt;
The new keyword creates a new object&lt;br&gt;
Methods are shared through prototypes for memory efficiency&lt;br&gt;
Encapsulation hides internal detials &amp;amp; protects data&lt;/p&gt;

&lt;p&gt;If you found this helpful, feel free to connect with me on my social handles. I regularly share about what I'm learning on my social handles.&lt;br&gt;
🔹&lt;a href="https://www.linkedin.com/in/abhijit-ghosh-63b624235/" rel="noopener noreferrer"&gt;LinkedIn 🌻&lt;/a&gt;&lt;br&gt;
🔹&lt;a href="https://x.com/Abhijit_091" rel="noopener noreferrer"&gt;X 🌻&lt;/a&gt;&lt;/p&gt;

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