<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged chaicode</title>
<link>https://coderlegion.com/tag/chaicode</link>
<description>Powered by Question2Answer</description>
<item>
<title>Beyond the Terminal: My Hunt Through the Linux File System</title>
<link>https://coderlegion.com/15771/beyond-the-terminal-my-hunt-through-the-linux-file-system</link>
<description>&lt;p&gt;At first, Linux felt like magic. You open the terminal, type a command… and things just work. No questions asked. But after a while, that “magic” started bothering me.&lt;/p&gt;
&lt;p&gt;Because I kept thinking:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;Where is all of this actually happening?&lt;/em&gt;&lt;br&gt;
&lt;em&gt;How does the system know what to do?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Like… when I connect to the internet, where is that configuration stored?&lt;br&gt;
When a user logs in, where is that data coming from?&lt;br&gt;
When something breaks, where does Linux keep track of it?&lt;/p&gt;
&lt;p&gt;These weren’t just random questions anymore. They were pulling me deeper.&lt;/p&gt;
&lt;p&gt;So instead of just &lt;em&gt;using&lt;/em&gt; Linux… I decided to &lt;strong&gt;explore it&lt;/strong&gt;. Not like a normal user.&lt;br&gt;
But like someone trying to understand what’s going on behind the scenes.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Digging into folders.&lt;br&gt;
Opening system files.&lt;br&gt;
Trying to connect the dots.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And what I discovered?&lt;/p&gt;
&lt;p&gt;It completely changed how I look at Linux. Because behind every command…&lt;br&gt;
there’s a system quietly doing a lot more than we realize.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-etc---the-place-where-system-behavior-lives&quot;&gt;&lt;code&gt;/etc&lt;/code&gt; - The Place Where System Behavior Lives&lt;/h3&gt;
&lt;p&gt;The first place I explored was &lt;code&gt;/etc&lt;/code&gt;, because I kept hearing that it’s important.&lt;/p&gt;
&lt;p&gt;So I went inside:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;cd /etc
ls
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At first, it just looked like a collection of random files. But the more I opened them, the more I realized something important.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;/etc&lt;/code&gt; is not just a folder.&lt;/p&gt;
&lt;p&gt;It is where Linux stores &lt;strong&gt;all configuration that controls system behavior&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/dba90699-454f-4cc7-9a3c-108dba3df80f.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;When I opened files like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;cat /etc/hosts
cat /etc/passwd
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I could literally see how my system is configured.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/8952535f-a89e-4628-8338-f62ce3adb2e7.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/dd7b2d29-2731-42ca-b656-cbf8384c9ad4.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;This made me realize &lt;em&gt;why this folder exists&lt;/em&gt;. Instead of hardcoding system behavior deep inside programs, Linux stores configurations as editable files. This means the system becomes flexible you can change behavior without touching the core system.&lt;/p&gt;
&lt;p&gt;The problem this solves is huge. Imagine if you had to recompile the OS just to change a setting. That would be impossible for daily use.&lt;/p&gt;
&lt;p&gt;But with &lt;code&gt;/etc&lt;/code&gt;, Linux separates &lt;strong&gt;configuration from execution&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And the insight that hit me was this:&lt;br&gt;
My system is not controlled by hidden magic. It’s controlled by &lt;strong&gt;plain text files that I can read and understand&lt;/strong&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-etc-resolv-conf---how-my-system-finds-websites&quot;&gt;&lt;code&gt;/etc/resolv.conf&lt;/code&gt; - How My System Finds Websites&lt;/h3&gt;
&lt;p&gt;One thing I never really questioned was typing something like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;ping google.com
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It just works. But then I stopped and thought:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;How does my system even understand what “&lt;/em&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;http://google.com&quot;&gt;&lt;em&gt;google.com&lt;/em&gt;&lt;/a&gt;&lt;em&gt;” is?&lt;/em&gt;&lt;br&gt;
Because at the end of the day, computers don’t understand names, they understand IP addresses.&lt;/p&gt;
&lt;p&gt;So I decided to dig into it.&lt;/p&gt;
&lt;p&gt;I ran:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;cat /etc/resolv.conf
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/bd69aef9-4629-4832-955e-75e99101d859.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;And I saw this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;nameserver 10.0.2.3
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At first, this confused me.&lt;/p&gt;
&lt;p&gt;I was expecting something like &lt;code&gt;8.8.8.8&lt;/code&gt; (Google DNS).&lt;br&gt;
But this… looked different.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;so-what-is-this-10-0-2-3&quot;&gt;So what is this &lt;code&gt;10.0.2.3&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;It turns out, this is not a public DNS server. It’s a &lt;strong&gt;local DNS resolver provided by my virtual machine environment&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Since I’m running Linux inside a VM, my system is not directly talking to the internet.&lt;/p&gt;
&lt;p&gt;Instead, it works like this:&lt;/p&gt;
&lt;p&gt;My Linux system → asks &lt;code&gt;10.0.2.3&lt;/code&gt; → which then forwards the request to actual DNS servers&lt;/p&gt;
&lt;p&gt;So basically, &lt;code&gt;10.0.2.3&lt;/code&gt; is acting like a &lt;strong&gt;middleman&lt;/strong&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;why-does-this-file-exist-&quot;&gt;Why does this file exist?&lt;/h4&gt;
&lt;p&gt;This file tells the system: “If you don’t understand a domain name, ask this nameserver.”&lt;/p&gt;
&lt;p&gt;Without this, the system wouldn’t know where to send DNS queries.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;what-problem-does-it-solve-&quot;&gt;What problem does it solve?&lt;/h4&gt;
&lt;p&gt;Humans use names like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;http://google.com&quot;&gt;google.com&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;http://youtube.com&quot;&gt;youtube.com&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But systems use IP addresses. So DNS acts as a &lt;strong&gt;translator&lt;/strong&gt; between human-friendly names and machine-friendly IPs.&lt;/p&gt;
&lt;p&gt;And &lt;code&gt;/etc/resolv.conf&lt;/code&gt; tells Linux &lt;em&gt;where that translator lives&lt;/em&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;what-i-found-interesting&quot;&gt;What I found interesting&lt;/h4&gt;
&lt;p&gt;The biggest realization for me was this: My system isn’t always directly using public DNS.&lt;br&gt;
In my case, it’s using a &lt;strong&gt;local resolver (10.0.2.3)&lt;/strong&gt;, which then handles everything behind the scenes.&lt;/p&gt;
&lt;p&gt;That means even something as simple as opening a website involves:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;local system configuration&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;virtual networking layers&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DNS forwarding&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h4 id=&quot;small-but-powerful-insight&quot;&gt;Small but powerful insight&lt;/h4&gt;
&lt;p&gt;&lt;em&gt;Just by changing this file, I can control how my system resolves domains.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Which means I can switch to a faster DNS, block certain domains, &amp;amp; control network behavior.&lt;/p&gt;
&lt;p&gt;And honestly… Something as simple as opening a website suddenly felt &lt;strong&gt;way deeper than I thought&lt;/strong&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;routing-table---how-linux-decides-where-data-goes&quot;&gt;Routing Table - How Linux Decides Where Data Goes&lt;/h3&gt;
&lt;p&gt;Next, I wanted to understand how my system sends data.&lt;/p&gt;
&lt;p&gt;So I ran:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;ip route
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What I saw was something like:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/1fddee76-f327-4faa-bb16-91158aff8197.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;This is the routing table.&lt;/p&gt;
&lt;p&gt;At a basic level, this tells the system: &lt;em&gt;“If you don’t know where to send data, send it to this gateway.”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In my case, that gateway is &lt;code&gt;10.0.2.2&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This exists because your system doesn’t just randomly send data. It needs a structured way to decide where packets go.&lt;/p&gt;
&lt;p&gt;The problem it solves is routing confusion. Without this, your system wouldn’t know whether to send data locally or over the internet.&lt;/p&gt;
&lt;p&gt;The second line (&lt;code&gt;10.0.2.0/24&lt;/code&gt;) tells the system that anything in this local network range can be accessed directly, without going through the gateway.&lt;/p&gt;
&lt;p&gt;The insight here was surprising: My system is not just sending data it’s making &lt;strong&gt;routing decisions like a mini network controller&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And since I’m using a virtual machine, &lt;code&gt;10.0.2.2&lt;/code&gt; is actually a &lt;strong&gt;virtual gateway&lt;/strong&gt;, not a physical router.&lt;/p&gt;
&lt;p&gt;I always thought networking happens “somewhere outside”. But Linux is actively involved in deciding paths. And in this case, it’s even routing traffic through a virtual layer before it reaches the real network.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-var-log---the-silent-observer-of-everything&quot;&gt;&lt;code&gt;/var/log&lt;/code&gt; - The Silent Observer of Everything&lt;/h3&gt;
&lt;p&gt;Then I explored logs:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;cd /var/log
ls
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/6804e197-0fe4-4b02-8de9-942abd566740.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;And opened files like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;cat messages
cat secure
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This folder is basically the &lt;strong&gt;memory of the system&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Everything that happens:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;logins&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;errors&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;services&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;system events&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;…it all gets recorded here. This exists because systems need a way to track what’s happening internally.&lt;br&gt;
The problem it solves is debugging and monitoring. Without logs, if something breaks, you’re blind.&lt;/p&gt;
&lt;p&gt;But what really stood out to me was this:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Linux doesn’t interrupt you with errors all the time.&lt;br&gt;
It quietly writes everything down.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It’s like the system is constantly saying:&lt;br&gt;
“I’ll tell you everything… but only if you come and look.”&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-etc-passwd-amp-etc-shadow---how-linux-handles-users-securely&quot;&gt;&lt;code&gt;/etc/passwd&lt;/code&gt; &amp;amp; &lt;code&gt;/etc/shadow&lt;/code&gt; - How Linux Handles Users Securely&lt;/h3&gt;
&lt;p&gt;To understand users, I ran:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;cat /etc/passwd
sudo cat /etc/shadow
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What I saw was interesting.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/c9f9d74e-3a88-4f9a-9772-4e9b876f4006.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;/etc/passwd&lt;/code&gt; contains user details.&lt;/p&gt;
&lt;p&gt;But passwords are not there.&lt;/p&gt;
&lt;p&gt;They’re stored in &lt;code&gt;/etc/shadow&lt;/code&gt;, which requires special permissions.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/f8886199-5b1b-460d-8c26-1fb50204d8de.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;This exists for security reasons. If everything was stored in one place, anyone could read sensitive data.&lt;/p&gt;
&lt;p&gt;The problem it solves is protecting user credentials.&lt;/p&gt;
&lt;p&gt;But the insight here was deeper:&lt;/p&gt;
&lt;p&gt;Linux separates identity from authentication.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/etc/passwd&lt;/code&gt; → who you are&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/etc/shadow&lt;/code&gt; → how you prove it&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That separation makes the system more secure by design.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;permissions---built-in-security-not-an-add-on&quot;&gt;Permissions - Built-In Security, Not an Add-On&lt;/h3&gt;
&lt;p&gt;When I ran:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;ls -l
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I saw things like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;-rwxr-xr--
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/fe6fc764-6e65-41a7-9cac-2ba703eaedbd.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;At first, it looked confusing. But then i understood: this is how Linux controls access to every file.&lt;/p&gt;
&lt;p&gt;Permissions exist because not every user should have full control over everything.&lt;/p&gt;
&lt;p&gt;The problem it solves is unauthorized access and system damage.&lt;/p&gt;
&lt;p&gt;But what really stood out was:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Security in Linux is not something extra.&lt;br&gt;
It is built directly into the file system itself.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Every file carries its own rules.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-proc---where-i-saw-the-system-live&quot;&gt;&lt;code&gt;/proc&lt;/code&gt; - Where I Saw the System Live&lt;/h3&gt;
&lt;p&gt;This was the most surprising part&lt;/p&gt;
&lt;p&gt;I explored:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;cd /proc
ls
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/9149fc76-6d64-4f95-a1b6-6e8adbb6bc22.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;And then:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;cat /proc/cpuinfo
cat /proc/meminfo
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/d758033f-d9de-42a3-8fc1-8148de180518.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/6f75e522-7ea9-4302-9705-27d31df5d31f.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;What I realized is: These are not real files stored on disk. They are generated in &lt;strong&gt;real time by the kernel&lt;/strong&gt;. This exists so users and programs can access live system data.&lt;br&gt;
The problem it solves is system monitoring.&lt;/p&gt;
&lt;p&gt;But the insight here blew my mind:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;When I read these files, I’m not reading stored data.&lt;br&gt;
I’m interacting with the system &lt;em&gt;live&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It felt like I was directly talking to Linux.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-dev---where-hardware-becomes-files&quot;&gt;&lt;code&gt;/dev&lt;/code&gt; - Where Hardware Becomes Files&lt;/h3&gt;
&lt;p&gt;Then I explored:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;ls /dev
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I saw entries for disks, terminals, and devices.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/d61e42c5-dae6-40ac-8bc4-c8aea3435d04.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;This exists because Linux treats hardware as files.&lt;/p&gt;
&lt;p&gt;The problem it solves is complexity. Instead of learning different ways to interact with devices, everything becomes file operations.&lt;/p&gt;
&lt;p&gt;And the insight here was simple but powerful: In Linux, everything is unified under one idea: files.&lt;/p&gt;
&lt;p&gt;That consistency makes the system elegant.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-boot---the-starting-point-of-everything&quot;&gt;&lt;code&gt;/boot&lt;/code&gt; - The Starting Point of Everything&lt;/h3&gt;
&lt;p&gt;Finally, I checked:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;ls /boot
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This folder contains the kernel and bootloader files.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/e1042324-f3ca-4bff-a7b6-a4b3df12c46d.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;This exists because the system needs instructions to start itself. The problem it solves is system initialization.&lt;br&gt;
And the insight: Without &lt;code&gt;/boot&lt;/code&gt;, Linux cannot even begin to run.&lt;/p&gt;
&lt;p&gt;It’s literally where everything starts.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-var-log-secure---who-tried-to-enter-my-system-&quot;&gt;&lt;code&gt;/var/log/secure&lt;/code&gt; - Who Tried to Enter My System?&lt;/h3&gt;
&lt;p&gt;While exploring logs, I went deeper:&lt;/p&gt;
&lt;p&gt;So I ran:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;sudo tail -f /var/log/secure
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/f74b3e91-c729-457b-8959-8937be20b451.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;And what I saw was honestly eye-opening.&lt;/p&gt;
&lt;p&gt;This file logs things like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;login attempts&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;sudo usage&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;authentication failures&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Basically… anything related to &lt;strong&gt;who is trying to access the system&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This exists because systems need to keep track of access.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Who logged in&lt;br&gt;
Who tried and failed&lt;br&gt;
Who used admin privileges&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Without something like this, there would be no way to monitor system security.&lt;/p&gt;
&lt;p&gt;The problem it solves is pretty serious. If something suspicious happens, like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;repeated failed login attempts&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;unauthorized access&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;misuse of sudo&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;you need a way to trace it. And that’s exactly what this file does. The insight here really changed how I see system logs.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Linux doesn’t constantly warn you about everything.&lt;br&gt;
Instead, it quietly records everything in the background.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And this file becomes like a &lt;strong&gt;security audit trail&lt;/strong&gt;. If someone tries to break into your system… the evidence is already there.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-dev-null---the-black-hole-of-linux&quot;&gt;&lt;code&gt;/dev/null&lt;/code&gt; - The Black Hole of Linux&lt;/h3&gt;
&lt;p&gt;This one was honestly one of the coolest discoveries.&lt;/p&gt;
&lt;p&gt;I started with something simple:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;ls /dev/null
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I tried this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;echo &quot;hello&quot; &amp;gt; /dev/null
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And… it disappeared.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/a07e4e13-1593-4e1c-a368-3ae8b5c8c2e7.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;No output. Nothing showed up anywhere. That’s when I realized what was happening.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;/dev/null&lt;/code&gt; is a special file that &lt;strong&gt;discards anything written to it&lt;/strong&gt;. This exists because sometimes, you don’t care about output. Maybe a command is producing too much noise, or you just want to run something silently.&lt;/p&gt;
&lt;p&gt;The problem it solves is simple but important. Without &lt;code&gt;/dev/null&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;your terminal would get cluttered&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;scripts would print unnecessary output&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;things would become harder to manage&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The insight here was actually pretty cool.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;/dev/null&lt;/code&gt; is like a black hole. Anything goes in… and nothing comes out. And suddenly, this made sense:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;command &amp;gt; /dev/null 2&amp;gt;&amp;amp;1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This basically tells the system:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Run the command, but don’t show me anything.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And now I finally understood why developers use it so often.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;systemd---the-hidden-manager-running-everything&quot;&gt;&lt;code&gt;systemd&lt;/code&gt; - The Hidden Manager Running Everything&lt;/h3&gt;
&lt;p&gt;Then I got curious about what’s running behind the scenes.&lt;/p&gt;
&lt;p&gt;So I explored:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;cd /etc/systemd/system
ls
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/69382f58e4ddfd795d01b951/e45e98ea-2cc7-495c-9822-8913298b7bb6.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;At first, it just looked like a bunch of configuration files.&lt;/p&gt;
&lt;p&gt;But then it clicked. This is where &lt;strong&gt;system services are defined and managed&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;systemd&lt;/code&gt; is basically the component that handles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;services&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;startup processes&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;background tasks&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It’s the reason your system feels “alive” even when you’re not doing anything.&lt;/p&gt;
&lt;p&gt;This exists because modern systems need automation. You don’t manually start things like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;networking&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;logging&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;system monitoring&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Every time you boot your system.&lt;/p&gt;
&lt;p&gt;The problem it solves is huge. Without something like &lt;code&gt;systemd&lt;/code&gt; you would have to manually start every service after boot . Which is clearly not practical. The insight here was interesting. Even when I’m doing nothing…&lt;br&gt;
Linux is still running multiple services silently in the background. Things just work because &lt;code&gt;systemd&lt;/code&gt; is managing everything for me.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-boot-grub2-grub-cfg---the-first-instructions-linux-follows&quot;&gt;&lt;code&gt;/boot/grub2/grub.cfg&lt;/code&gt; - The First Instructions Linux Follows&lt;/h3&gt;
&lt;p&gt;This one felt powerful.&lt;/p&gt;
&lt;p&gt;I ran:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-plaintext&quot;&gt;cat /boot/grub2/grub.cfg
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And I was looking at the configuration used by GRUB the bootloader.&lt;/p&gt;
&lt;p&gt;This file tells the system:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;which kernel to load&lt;br&gt;
how to start Linux&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This exists because before Linux even starts, something needs to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;initialize the system&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;load the kernel&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;hand over control to the OS&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And that’s exactly what the bootloader does.&lt;/p&gt;
&lt;p&gt;The problem it solves is fundamental. Without this configuration:&lt;br&gt;
the system wouldn’t even know how to boot. No startup. No OS. Nothing.&lt;/p&gt;
&lt;p&gt;The insight here made me realize how critical this file is. This is literally where the system’s startup behavior is defined. If this file gets corrupted Linux won’t even start&lt;br&gt;
And that’s when it hit me… This isn’t just another config file. This is the starting point of everything.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;After exploring all these parts, one thing became very clear to me:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Linux is not just an operating system&lt;br&gt;
It is a collection of &lt;strong&gt;well-structured systems connected through files&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;From booting → to users → to networking → to logs&lt;/p&gt;
&lt;p&gt;Everything is transparent, accessible, &amp;amp; understandable.&lt;br&gt;
And once you start exploring it like this… you stop guessing and start understanding.&lt;/p&gt;
&lt;p&gt;Linux is not hiding anything from you.&lt;br&gt;
Everything is there in files, waiting to be explored.&lt;/p&gt;
&lt;p&gt;The moment you stop just using Linux… and start reading it… that’s when things finally start clicking.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/15771/beyond-the-terminal-my-hunt-through-the-linux-file-system</guid>
<pubDate>Sat, 25 Apr 2026 05:07:24 +0000</pubDate>
</item>
<item>
<title>Understanding Variables and Data Types in JavaScript</title>
<link>https://coderlegion.com/15768/understanding-variables-and-data-types-in-javascript</link>
<description>&lt;p&gt;I remember when i first started learning JavaScript everything was so confusing, i felt completely overwhelmed.&lt;br&gt;
&lt;code&gt;Variables&lt;/code&gt;, &lt;code&gt;Data Types&lt;/code&gt;, &lt;code&gt;let, const&lt;/code&gt;, &lt;code&gt;Scope&lt;/code&gt;, &lt;code&gt;Hoisting&lt;/code&gt;, &lt;code&gt;Temporal Dead Zone (TDZ)&lt;/code&gt;... ahh so many things.&lt;/p&gt;
&lt;p&gt;It felt like everyone else understand it and i was just trying to memorize these things without actually understanding what was going on under the hood.&lt;/p&gt;
&lt;p&gt;So in this blog i’m going to cover and explain slowly, clearly &amp;amp; practically everything the way i wish someone had explained it to me when i started learning JavaScript.&lt;/p&gt;
&lt;p&gt;By the end of this blog you won’t just know the definitions but you will understand how JavaScript behaves actually.&lt;/p&gt;
&lt;p&gt;So let’s start from the very beginning.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;what-are-javascript-variables-&quot;&gt;&lt;strong&gt;What are JavaScript Variables?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Let’s forget the technical definition for now and think of a variable like a box&lt;br&gt;
So start with this Imagine you have a box&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You put something inside it&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write a lable on it&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open it later to see what’s inside the box&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Later replace what’s inside if needed&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That box is exactly what a variable is&lt;/p&gt;
&lt;p&gt;In JavaScript a variable is simply a named container that stores data so we can use it later.&lt;/p&gt;
&lt;p&gt;for example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let name = &quot;Nausheen&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How I see it in my head:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;name&lt;/code&gt; =&amp;gt; the label on the box&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&quot;Nausheen&quot;&lt;/code&gt; =&amp;gt; what's inside the box&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But the question is: Why do we need variables?&lt;br&gt;
The reason is simple because programs need memory&lt;/p&gt;
&lt;p&gt;When you log into a website, it needs to remember:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Your username&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your password&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whether you’re logged in or not&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What’s in your cart&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Without variables, JavaScript wouldn’t be able to remember anything not even for a second.&lt;/p&gt;
&lt;p&gt;Variables are the foundation of everthing&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;how-i-declare-variables-var-let-const-&quot;&gt;&lt;strong&gt;How I Declare Variables (var, let, const)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Here we go In JavaScript, we have three ways to create variables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;var&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;let&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;const&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But the thing is they are not the same. And this is where beginners usually get confused.&lt;br&gt;
So, let me break it down the way I personally understand it and where to use them.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;1-var-old-way-&quot;&gt;&lt;strong&gt;1. var (old way)&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;var age = 20;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;yup simple, this is how it works.&lt;/p&gt;
&lt;p&gt;And yes, you can also change it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;age = 21;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But here’s what I’ve learned over time:&lt;br&gt;
&lt;code&gt;var&lt;/code&gt; has some strange behaviors that can create confusion in larger programs.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;var city = &quot;Delhi&quot;;
var city = &quot;Mumbai&quot;; // No error
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It allows redeclaration in the same scope.&lt;/p&gt;
&lt;p&gt;That might not seem dangerous now, but in a big project, this can silently overwrite values &amp;amp; cause bugs that are hard to track.&lt;/p&gt;
&lt;p&gt;That’s why in morder JavaScript, we mostly avoid &lt;code&gt;var&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Not because it’s bad, but because there are safer alternatives.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;2-let-flexible-and-modern-&quot;&gt;&lt;strong&gt;2. let (Flexible and Modern)&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let score = 50;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I use &lt;code&gt;let&lt;/code&gt; when I know that value might change later.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;score = 60; // Perfectly fine
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But here’s something important:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let score = 70; // Error
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You cannot redeclare a &lt;code&gt;let&lt;/code&gt; variable in the same scope.&lt;/p&gt;
&lt;p&gt;And honestly, I like this.&lt;br&gt;
Because it prevents accidental mistakes.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;3-const-fixed-and-safe-&quot;&gt;&lt;strong&gt;3. const (Fixed and Safe)&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const birthYear = 2004;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok so, when I write &lt;code&gt;const&lt;/code&gt; I'm basically saying:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;I don’t want this value to change&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And JavaScript respects that.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;birthYear = 2005; // Error
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is why I personally follow this rule:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I use &lt;code&gt;const&lt;/code&gt; by default&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I switch to &lt;code&gt;let&lt;/code&gt; only when I need to change the value&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This makes code safer and more predictable.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;difference-between-var-let-and-const&quot;&gt;&lt;strong&gt;Difference Between var, let, and const&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;https://miro.medium.com/v2/resize:fit:875/0*KDwJv8_NmwbWIptj.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;If you’re just starting out, here’s what I’d suggest you:&lt;/p&gt;
&lt;p&gt;Use &lt;code&gt;const&lt;/code&gt; most of the time&lt;br&gt;
Use &lt;code&gt;let&lt;/code&gt; when necessary&lt;br&gt;
Forget &lt;code&gt;var&lt;/code&gt; for now&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;what-are-data-types-&quot;&gt;&lt;strong&gt;What Are Data Types?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;What’s Inside the Box?&lt;/p&gt;
&lt;p&gt;Now let’s talk about what we actually store inside variables.&lt;/p&gt;
&lt;p&gt;When I say &lt;code&gt;&quot;data type&quot;&lt;/code&gt; I'm simply asking:&lt;br&gt;
What kind of value is inside this variable?&lt;/p&gt;
&lt;p&gt;If variables are boxes, data type describes what kind of thing is inside the box.&lt;/p&gt;
&lt;p&gt;In JavaScript, there are many types. But as beginners, we focus on primitive data types.&lt;/p&gt;
&lt;p&gt;Basically, there are two categories of data types:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Primitive Data Types&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Non-Primitive Data Types&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;But in this blog, we will only focus on primitive data types.&lt;/p&gt;
&lt;p&gt;The main primitive types in JavaScript are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;String&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Number&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Boolean&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Null&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Undefined&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let’s understand them properly&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;1-string---text-data&quot;&gt;&lt;strong&gt;1. String - Text Data&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Whenever I want to store some text data, I use a string.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let name = &quot;Nausheen&quot;;
let message = &quot;Hello World&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;String are always written inside &lt;em&gt;quotes&lt;/em&gt;.&lt;br&gt;
Without quotes, JavaScript thinks it’s a variable name.&lt;/p&gt;
&lt;p&gt;Strings are used for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Name&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Emails&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Messages&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Addresses&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Basically, anything that’s text.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;2-number---numeric-values&quot;&gt;&lt;strong&gt;2. Number - Numeric Values&lt;/strong&gt;&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let age = 21;
let price = 99.99;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In JavaScript, there is only one number type.&lt;/p&gt;
&lt;p&gt;It doesn’t seperate integers and decimals unlike many other programming languages&lt;/p&gt;
&lt;p&gt;Whether it’s &lt;code&gt;21&lt;/code&gt; or &lt;code&gt;99.99&lt;/code&gt; , both are simply &lt;code&gt;number&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Numbers are used for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Age&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Score&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prices&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Calculations&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;3-boolean---true-or-false&quot;&gt;&lt;strong&gt;3. Boolean - True or False&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Booleans are very simple but very powerful.&lt;/p&gt;
&lt;p&gt;They only have two values:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;false&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let isLoggedIn = true;
let hasPermission = false;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Booleans are mainly used in decisions.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Is the user logged in?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Did the form submit successfully?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Is the password correct?&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Most conditions in programming rely on booleans.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;4-null---intentionally-empty&quot;&gt;&lt;strong&gt;4. null - Intentionally Empty&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;When I use &lt;code&gt;null&lt;/code&gt;, I'm basically saying:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;I’m intentionally keeping this empty for now.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let selectedUser = null;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This means:&lt;br&gt;
I will assign a real value later.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;5-undefined---not-assigned-yet&quot;&gt;&lt;strong&gt;5. undefined - Not Assigned Yet&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;If i declare a variable but don’t give it a value:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let score;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;JavaScript automatically assigns it &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;That means:&lt;br&gt;
The variable exist, but nothing is inside it yet.&lt;/p&gt;
&lt;p&gt;The difference between &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; is subtle but important:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;undefined&lt;/code&gt; =&amp;gt; JavaScript gives it automatically&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;null&lt;/code&gt; =&amp;gt; You assign it intentionally&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;what-is-scope-&quot;&gt;&lt;strong&gt;What is Scope?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;When I first heard the word &lt;em&gt;scope&lt;/em&gt;, it sounded technical and abstract. But one I simplified it in my mind everything become clearer.&lt;/p&gt;
&lt;p&gt;So, Scope simply answers one question:&lt;br&gt;
Where can I access this variable?&lt;/p&gt;
&lt;p&gt;That’s it……&lt;/p&gt;
&lt;p&gt;Scope determines the visibility &amp;amp; accessibility of variables in different parts of your prograrm.&lt;/p&gt;
&lt;p&gt;Let’s take an example of &lt;em&gt;“The Rooms in a House”&lt;/em&gt;&lt;br&gt;
Imagine scope as rooms inside a house&lt;/p&gt;
&lt;p&gt;Each &lt;code&gt;{ }&lt;/code&gt; block is like a separate room.&lt;/p&gt;
&lt;p&gt;If I place something inside one room, I can only access it inside that room. I cannot randomly walk into another room &amp;amp; expect that thing to be there.&lt;/p&gt;
&lt;p&gt;In JavaScript, a block looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;{
    let message = &quot;Hello there&quot;;
    console.log(message); // It works
}
console.log(message); // ReferenceError
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, &lt;code&gt;message&lt;/code&gt; was created inside the block (inside the &quot;room&quot;).&lt;/p&gt;
&lt;p&gt;So when we try to access it outside the block, JavaScript throws a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Not because the value is undefined&lt;br&gt;
But because it does not exist in that scope.&lt;/p&gt;
&lt;p&gt;That’s an important distinction.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;block-scope-let-amp-const-&quot;&gt;&lt;strong&gt;Block Scope (let &amp;amp; const)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Variables declared using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; follow block scope.&lt;/p&gt;
&lt;p&gt;A block is anything inside &lt;code&gt;{ }&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;if statements&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;for loop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;while loop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;plain curly braces&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;function&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;if (true) {
    let age = 22;
}
console.log(age); // ReferenceError
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, &lt;code&gt;age&lt;/code&gt; only exists inside that &lt;code&gt;if&lt;/code&gt; block.&lt;/p&gt;
&lt;p&gt;Once execution leaves that block, the variable is gone.&lt;/p&gt;
&lt;p&gt;This behavior makes your code safer because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It prevents accidental overwriting of variables&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It avoids naming conflicts&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It keeps variables limited to where they are needed&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is one of the main reason why JavaScript prefers &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;They give you controlled scope.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;function-scope&quot;&gt;&lt;strong&gt;Function Scope&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Before ES6, Javascript only had function scope.&lt;/p&gt;
&lt;p&gt;That means variables declared with &lt;code&gt;var&lt;/code&gt; are accessible anywhere inside the function they were declared in regardless of blocks.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;function test() {
    if (true) {
        var name = &quot;Nausheen&quot;;
    }
    
    console.log(name); // works
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even though &lt;code&gt;name&lt;/code&gt; was declared inside the &lt;code&gt;if&lt;/code&gt; block, it is accessible outside that block but still inside the function.&lt;/p&gt;
&lt;p&gt;Why?&lt;br&gt;
Because &lt;code&gt;var&lt;/code&gt; doesn't follow block scope.&lt;/p&gt;
&lt;p&gt;This difference is the root cause of many bugs in older JavaScript code.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;why-var-is-problematic-&quot;&gt;&lt;strong&gt;Why&lt;/strong&gt; &lt;code&gt;var&lt;/code&gt; &lt;strong&gt;Is Problematic?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Let’s look at this code example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;if (true) {
    var count = 10;
}
console.log(count); // Works (unexpected behavior for beginners)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, &lt;code&gt;count&lt;/code&gt; leaks outside the &lt;em&gt;block&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This can cause:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Variable collisions&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hard to debug errors&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accidental overwrites&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Confusing behavior in loops&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That’s why modern JavaScript discourages to use &lt;code&gt;var&lt;/code&gt; unless absolutely necessary.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;global-scope&quot;&gt;&lt;strong&gt;Global Scope&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;If we declare a variable outside of any block or function, it belongs to the global scope.&lt;/p&gt;
&lt;p&gt;Let’s see a Example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;let globalMessage = &quot;Hi&quot;; // Global variable
function greet() {
    console.log(globalMessage); // Accessible
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Global variables can be accessed everywhere in the program.&lt;/p&gt;
&lt;p&gt;But here’s the danger:&lt;/p&gt;
&lt;p&gt;Too many global variables make your code messy &amp;amp; hard to maintain.&lt;br&gt;
Think of global scope as the entire &lt;em&gt;house&lt;/em&gt;, if you throw everything in the living room, it becomes chaos.&lt;/p&gt;
&lt;p&gt;Good developers minimize global scope usage.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://miro.medium.com/v2/resize:fit:875/0*sST4BdA7bmqYW0ov.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;function-inside-function&quot;&gt;&lt;strong&gt;Function Inside Function&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Until now, we talked about scope like rooms in a house.&lt;/p&gt;
&lt;p&gt;Let’s now imagine something more interesting&lt;br&gt;
what if a room had another room inside it?&lt;/p&gt;
&lt;p&gt;Now let’s see what happens when functions are inside other functions…&lt;/p&gt;
&lt;p&gt;Yes this is completely valid look at this code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;function outer() {
    let outerMessage = &quot;Hello from outer&quot;;
    function inner() {
        console.log(&quot;Hello from inner&quot;);
    }
    
    inner();
}
outer();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, &lt;code&gt;outer&lt;/code&gt; is one room, &lt;code&gt;inner&lt;/code&gt; is smaller room inside it.&lt;/p&gt;
&lt;p&gt;Now let’s test something important.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;function outer() {
    let outerMessage = &quot;Hello from outer&quot;;
    function inner() {
        console.log(outerMessage); // works
    }
    
    inner();
}
outer();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The inner function can access the outer function’s variable.&lt;/p&gt;
&lt;p&gt;But, can outer access inner’s variable to, let test that also.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;function outer() {
     function inner() {
        let secret = &quot;Hidden&quot;
    }
    
    inner();
    console.log(secret); // Error
}
outer();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Oops outer cannot access inner’s variables.&lt;/p&gt;
&lt;p&gt;So, remember access only flow upward means:&lt;br&gt;
Child -&amp;gt; Parent&lt;br&gt;
Never Parent -&amp;gt; Child&lt;/p&gt;
&lt;p&gt;This idea is the foundation of lexical scope.&lt;/p&gt;
&lt;p&gt;But before we go there……&lt;br&gt;
First we need to understand something deeper.&lt;/p&gt;
&lt;p&gt;How JavaScript actually runs you code&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;how-javascript-actually-runs-your-code-execution-context-&quot;&gt;&lt;strong&gt;How JavaScript Actually Runs Your Code (Execution context)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Most beginners think JavaScript reads code line by line.&lt;/p&gt;
&lt;p&gt;Reads line 1&lt;br&gt;
Then line 2&lt;br&gt;
Then line 3…&lt;/p&gt;
&lt;p&gt;But that’s not exactly true.&lt;/p&gt;
&lt;p&gt;JavaScript runs your code in two phases.&lt;br&gt;
And this is where everything changes.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;phase-1-memory-creation-phase&quot;&gt;&lt;strong&gt;Phase 1: Memory Creation Phase&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Before executing anything…&lt;/p&gt;
&lt;p&gt;JavaScript first scans you entire code.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It allocates memory for variables&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stores function declarations&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sets up scope&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But keep this in mind:&lt;/p&gt;
&lt;p&gt;Variables don’t get their real values yet.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;var&lt;/code&gt; =&amp;gt; initialized with &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; =&amp;gt; created but not initialized&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Functions =&amp;gt; fully stored in memory&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log(name); // undefined
var name = &quot;Nausheen&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;undefined why?&lt;/p&gt;
&lt;p&gt;Because during memory phase:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;var name = undefined;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then during execution phase:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;name = &quot;Nausheen&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This behavior is called hoisting, but we’ll go deeper into that soon… yes we’re going to cover that too.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;phase-2-code-execution-phase&quot;&gt;&lt;strong&gt;Phase 2: Code Execution Phase&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Now JavaScript starts running the code line by line.&lt;/p&gt;
&lt;p&gt;Values are assigned&lt;br&gt;
Functions get executed&lt;br&gt;
&lt;code&gt;console.log()&lt;/code&gt; run&lt;/p&gt;
&lt;p&gt;Memory Phase always comes first&lt;br&gt;
Then comes the Execution Phase&lt;/p&gt;
&lt;p&gt;Simple as that. Yes nothing scary&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;call-stack&quot;&gt;&lt;strong&gt;Call Stack&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Every time a function runs… JavaScript creates a new execution context.&lt;/p&gt;
&lt;p&gt;These execution contexts are stacked on top of each other.&lt;br&gt;
Think of the call stack like a stack of plates&lt;/p&gt;
&lt;p&gt;When you call a function:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;Javascripit puts that function on top of the stack&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;When the function finishes:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;JavaScript removes it from the top&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That’s it&lt;/p&gt;
&lt;p&gt;It always works Last In, First Out (LIFO).&lt;br&gt;
The last function added is the first one removed&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;function one() {
    two();
}
function two() {
    console.log(&quot;Hello&quot;);
}
one();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s see what happens step by step:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Global code starts running&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;one()&lt;/code&gt; is called -&amp;gt; added to stack&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside &lt;code&gt;one(),&lt;/code&gt; &lt;code&gt;two()&lt;/code&gt; is called -&amp;gt; added on top&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;two()&lt;/code&gt; finishes -&amp;gt; removed&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;one()&lt;/code&gt; finishes -&amp;gt; removed&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Back to global&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So, flow looks llike this:&lt;/p&gt;
&lt;p&gt;Global&lt;br&gt;
one()&lt;br&gt;
two()&lt;/p&gt;
&lt;p&gt;Then it goes backward:&lt;/p&gt;
&lt;p&gt;two() removed&lt;br&gt;
one() removed&lt;br&gt;
back to Global&lt;/p&gt;
&lt;p&gt;This is how JavaScript manages execution&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;hoisting---what-it-actually-is-and-what-most-people-get-wrong-&quot;&gt;&lt;strong&gt;Hoisting - What It Actually is (And What Most People get Wrong)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Now that you understand execution context… Hoisting will make sense.&lt;/p&gt;
&lt;p&gt;When I first learned hoisting, someone told me:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;JavaScript moves variables to the top&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And honestly?..&lt;/p&gt;
&lt;p&gt;That explanation confused me more than it helped.&lt;/p&gt;
&lt;p&gt;Because JavaScript doesn’t physically move your code&lt;br&gt;
Nothing jumps to the top&lt;br&gt;
Your code stays exactly where you wrote it.&lt;/p&gt;
&lt;p&gt;So the question is what’s really happening?&lt;/p&gt;
&lt;p&gt;To understand hoisting properly, we have to go back to something we already learned:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;Execution context&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Memory Phase&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Execution Phase&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Hoisting is simply a result of how JavaScript prepares memory before running code.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;how-var-hoisting-actually-works&quot;&gt;&lt;strong&gt;How&lt;/strong&gt; &lt;code&gt;var&lt;/code&gt; &lt;strong&gt;Hoisting Actually Works&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Let’s see:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;console.log(name); // undefined
var name = &quot;Nausheen&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Output is undefined why not error?&lt;/p&gt;
&lt;p&gt;Because during memory phase&lt;br&gt;
JavaScript does this internally:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;var name = undefined;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then during execution phase:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;name = &quot;Nausheen&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So when &lt;code&gt;console.log&lt;/code&gt; runs,&lt;br&gt;
&lt;code&gt;name&lt;/code&gt; exists but its value is &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;So, remember this is important:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;code&gt;var&lt;/code&gt; &lt;em&gt;is declared during compilation&lt;br&gt;
It is also initialized to&lt;/em&gt; &lt;code&gt;undefined&lt;/code&gt; &lt;em&gt;during compilation&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That’s why it’s accessible early.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;why-var-can-be-dangerous&quot;&gt;&lt;strong&gt;Why&lt;/strong&gt; &lt;code&gt;var&lt;/code&gt; &lt;strong&gt;Can Be Dangerous&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Now look at this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;function example() {
  console.log(x); // undefined
if (false) {
    var x = 10;
  }
  console.log(x); // undefined
}
example();
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;undefined
undefined
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even though the &lt;code&gt;if&lt;/code&gt; block never ran.&lt;/p&gt;
&lt;p&gt;Why?&lt;br&gt;
Because &lt;code&gt;var x&lt;/code&gt; was hoisted to the function scope.&lt;/p&gt;
&lt;p&gt;This is the dangerous part.&lt;br&gt;
&lt;code&gt;var&lt;/code&gt; ignores block scope.&lt;/p&gt;
&lt;p&gt;It can silently create undefined variables in unexpected places.&lt;/p&gt;
&lt;p&gt;This is one of the biggest reasons modern JavaScript prefers &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;function-hoisting&quot;&gt;&lt;strong&gt;Function Hoisting&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Function declarations behave differently it’s even more powerful.&lt;/p&gt;
&lt;p&gt;They are not just declared&lt;br&gt;
They are fully initialized during compilation&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;greet();
function greet() {
  console.log(&quot;Hello!&quot;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works perfectly.&lt;/p&gt;
&lt;p&gt;Because during compilation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;greet: [complete function stored in memory]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The entire function body is available before execution begins.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;That’s why function declarations are called:&lt;br&gt;
Fully hoisted&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;function-expression-vs-function-declaration&quot;&gt;&lt;strong&gt;Function Expression vs Function Declaration&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Now this is important.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;sayHi();
var sayHi = function () {
  console.log(&quot;Hi&quot;);
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now this throws:&lt;br&gt;
TypeError: sayHi is not a function&lt;/p&gt;
&lt;p&gt;Why?&lt;br&gt;
Because only variable is hoisted&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;var sayHi = undefined;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which causes the error.&lt;/p&gt;
&lt;p&gt;The difference is&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Function Declaration =&amp;gt; Fully hoisted&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Function Expression =&amp;gt; Only variable hoisted&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;now-let-s-talk-about-let-and-const&quot;&gt;&lt;strong&gt;Now Let’s Talk About&lt;/strong&gt; &lt;code&gt;let&lt;/code&gt; &lt;strong&gt;and&lt;/strong&gt; &lt;code&gt;const&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Here’s where things get misunderstood&lt;/p&gt;
&lt;p&gt;Many people say:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;let and const are not hoisted&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That is wrong…&lt;/p&gt;
&lt;p&gt;They are &lt;code&gt;hoisted&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;But they behave differently&lt;/p&gt;
&lt;p&gt;During compilation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are registered in memory&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But they are NOT initialized&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This creates something called… TDZ&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://miro.medium.com/v2/resize:fit:875/0*zbRDLn8q0m6uXd3T.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;temporal-dead-zone-tdz-&quot;&gt;&lt;strong&gt;Temporal Dead Zone (TDZ)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Another scary term &lt;em&gt;TDZ.&lt;/em&gt; Ahh, now what’s that?&lt;br&gt;
Don’t worry. I’ll keep it simple&lt;/p&gt;
&lt;p&gt;This is one of the most misunderstood concepts.&lt;/p&gt;
&lt;p&gt;The Temporal Dead Zone is:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;The time between when a variable is hoisted&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;*And when it is initialized&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let’s see it:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;console.log(age);
let age = 25;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This throws:&lt;br&gt;
ReferenceError: Cannot access 'age' before initialization&lt;/p&gt;
&lt;p&gt;Why?&lt;br&gt;
Beause:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;age exists in memory&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But it is uninitialized&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is in the Temporal Dead Zone&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The TDZ starts:&lt;br&gt;
At the beginning of the block&lt;/p&gt;
&lt;p&gt;And ends:&lt;br&gt;
At the line where the variable is declard&lt;/p&gt;
&lt;h2 id=&quot;tdz-is-about-time-not-position&quot;&gt;TDZ Is About Time, Not Position&lt;/h2&gt;
&lt;p&gt;Very important, TDZ is not about where the variable is written.&lt;br&gt;
It’s about time.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function test() {
  console.log(score);
  let score = 100;
}
test();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From the moment the function starts running,&lt;br&gt;
score exists.&lt;/p&gt;
&lt;p&gt;But until let score = 100 executes,&lt;br&gt;
it is in the TDZ.&lt;/p&gt;
&lt;p&gt;That’s why accessing it throws an error.&lt;/p&gt;
&lt;h2 id=&quot;proof-that-let-is-actually-hoisted&quot;&gt;Proof That let Is Actually Hoisted&lt;/h2&gt;
&lt;p&gt;Look at this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;let x = &quot;outer&quot;;
function demo() {
  console.log(x);
  let x = &quot;inner&quot;;
}
demo();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This throws ReferenceError.&lt;/p&gt;
&lt;p&gt;If let x inside demo was NOT hoisted,&lt;br&gt;
JavaScript would use the outer x and print &quot;outer&quot;.&lt;/p&gt;
&lt;p&gt;But it doesn’t.&lt;/p&gt;
&lt;p&gt;Why?&lt;br&gt;
Because the inner x is hoisted to the function scope.&lt;br&gt;
It creates a TDZ.&lt;/p&gt;
&lt;p&gt;That proves:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;let is hoisted&lt;br&gt;
It is not initialized&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That difference change everything.&lt;/p&gt;
&lt;h2 id=&quot;how-const-behaves&quot;&gt;How const Behaves&lt;/h2&gt;
&lt;p&gt;const works exactly like let regarding hoisting and TDZ.&lt;/p&gt;
&lt;p&gt;But with one extra rule:&lt;br&gt;
You must initialize it immediately.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const name; // SyntaxError
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Correct:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const name = &quot;Nausheen&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is hoisted.&lt;br&gt;
It has TDZ.&lt;br&gt;
But it must be initialized.&lt;/p&gt;
&lt;h2 id=&quot;common-misconceptions-you-should-always-remember&quot;&gt;Common misconceptions you should always remember&lt;/h2&gt;
&lt;p&gt;“Hoisting moves code to the top”&lt;br&gt;
No.&lt;/p&gt;
&lt;p&gt;Code stays exactly where you wrote it&lt;br&gt;
Memory is prepared before execution&lt;/p&gt;
&lt;p&gt;“Only var is hoisted”&lt;br&gt;
Wrong.&lt;/p&gt;
&lt;p&gt;All declarations are hoisted:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;var&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;let&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;const  &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;function&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;class&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The difference is initialization timing.&lt;/p&gt;
&lt;p&gt;“TDZ means variable doesn’t exist”&lt;br&gt;
Wrong.&lt;/p&gt;
&lt;p&gt;The variable exists.&lt;br&gt;
It just cannot be accessed yet.&lt;/p&gt;
&lt;h2 id=&quot;why-tdz-is-actually-a-good-thing&quot;&gt;Why TDZ Is Actually a Good Thing&lt;/h2&gt;
&lt;p&gt;With var, this is allowed:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var total = total + 5;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This creates weird undefined behavior.&lt;/p&gt;
&lt;p&gt;With let or const, you immediately get an error.&lt;/p&gt;
&lt;p&gt;TDZ protects you from accidental bugs.&lt;br&gt;
It forces cleaner code.&lt;br&gt;
It makes JavaScript safer.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/15768/understanding-variables-and-data-types-in-javascript</guid>
<pubDate>Sat, 25 Apr 2026 04:08:11 +0000</pubDate>
</item>
</channel>
</rss>