<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Understanding Variables and Data Types in JavaScript]]></title><description><![CDATA[Understanding Variables and Data Types in JavaScript]]></description><link>https://jsvariablesanddatatypes.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 00:38:38 GMT</lastBuildDate><atom:link href="https://jsvariablesanddatatypes.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[JavaScript Operators: The Basics You Need to Know]]></title><description><![CDATA[Alright, now that you know a bit about how control flow works, let’s spice things up a bit. In this article, I’ll show you a bit about which operators we have in JavaScript which will really level up ]]></description><link>https://jsvariablesanddatatypes.hashnode.dev/javascript-operators-the-basics-you-need-to-know</link><guid isPermaLink="true">https://jsvariablesanddatatypes.hashnode.dev/javascript-operators-the-basics-you-need-to-know</guid><category><![CDATA[Operators]]></category><dc:creator><![CDATA[Ashraf Raza]]></dc:creator><pubDate>Fri, 13 Mar 2026 11:56:45 GMT</pubDate><content:encoded><![CDATA[<p>Alright, now that you know a bit about how <a href="https://www.thecoderaccoons.com/blog-posts/javascript-101-getting-started-with-control-flow">control flow</a> works, let’s spice things up a bit. In this article, I’ll show you a bit about which operators we have in JavaScript which will really level up how you control your code’s flow</p>
<p>Not a medium member? read the full article <a href="https://www.thecoderaccoons.com/blog-posts/javascript-101-a-beginners-guide-to-javascript-operators">here</a></p>
<h2><strong>What are operators used for?</strong></h2>
<p>Let’s first start by defining what operators are. In JavaScript, Operators are symbols or keywords that are used in order to perform a specific operation; these operations can be mathematical, logical, assignments, comparisons, and others. In a nutshell, they are used to manipulate the data and control the flow of a program as it runs.</p>
<h2><strong>Which operators do we have, and what are they used for?</strong></h2>
<p>According to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_operators">MDN Web Docs</a>, the latest operators we have can be divided into 4 groups: Arithmetic, Assignment, Logical, and others. I’ll be explaining each of them next.</p>
<h2><strong>Arithmetic Operators</strong></h2>
<p>As you may already guess, these operators allow us to make mathematical operations, the most common usage for these are things you all know by heart</p>
<pre><code class="language-plaintext">//Addition (+)
5 + 3 = 8
//Subtraction (-)
10 - 4 = 6
//Multiplication (*)
6 * 7 = 42
//Division (/)
10 / 2 = 5
</code></pre>
<p>But these are not the only ones; some arithmetic operators that are just a bit less common are:</p>
<pre><code class="language-plaintext">//Remainder (%)
12 % 5 = 2
//Exponentiation ( ** )
2 ** 3 = 8
</code></pre>
<blockquote>
<p><em>The remainder operator, represented by the percent sign (</em><em><strong>%</strong></em><em>), is</em> <em><strong>a mathematical operator used in programming to find the remainder of a division</strong></em><em>. For example,</em> <em><strong>5 % 2</strong></em> <em>results in</em> <em><strong>1</strong></em> <em>because 2 goes into 5 two times with a remainder of 1.</em></p>
</blockquote>
<p>Finally, we get the <strong>unary</strong> arithmetic variants which are used to either increase/decrease or to transform values to int or make them negative:</p>
<ul>
<li><p><em><strong>++</strong></em> /--(increment / decrement)</p>
</li>
<li><p>Unary <em><strong>+</strong></em> and — to coerce or negate</p>
</li>
</ul>
<pre><code class="language-plaintext">let x = 5;
x++;           // x becomes 6
let y = +"42"; // y is 42 (number)
let z = -10;   // z is -10
</code></pre>
<h2><strong>Assignment Operators</strong></h2>
<p>Now the assignment operators are quite simple to explain; they are used to either store or update a value in a variable, the most basic version of it is the <em><strong>=</strong></em> operator, which is used to assign. The rest of the operators are called “compound” as they basically add to the <em><strong>=</strong></em> operator.</p>
<p>These shortcuts are great to make code cleaner and help reduce repetition.</p>
<pre><code class="language-plaintext">// Basic assignment:
let count = 0;
count = 5;

// Compound Operators:
count += 2; // count + 3
count -= 3; // count - 3
count *= 3; // count * 3

//This are made to replace things like:
count = count * 3
</code></pre>
<p>There are more assignment operators, which can be further explained in the MDN you can take a look at the full list <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_operators#assignment_operators">here</a></p>
<h2><strong>Comparison Operators</strong></h2>
<p>A quick little trivia fact, as explained in the first article of this series, JavaScript was created way back in May 1995, BUT as quirky as this funny little programming language is nowadays, it used to be worse back then. I’ve mentioned before a hundred different quirks this language has, but one of them that is now “Solved” by simply using TypeScript was type comparisons. At the beginning, JavaScript will only work with Loose comparison operators (<em><strong>==</strong></em>) But around this date back in December 1999, as part of the <strong>ECMAScript 3 (ES3)</strong> edition, the Strict equality operator (<em><strong>===</strong></em>) was introduced. Making our lives just a little bit easier.</p>
<p>Now, let’s take a look at why this is such a good thing to have:</p>
<p>Comparisons let you compare two values or expressions. they are divided as follows:</p>
<ul>
<li>Loose equality <em><strong>==</strong></em> (equal) &amp; <em><strong>!=</strong></em> (not equal)</li>
</ul>
<pre><code class="language-plaintext">// Not type safe:
"3" == 3          // returns true
3 != '3'          // returns false
null == undefined // returns true
[] == false       // returns true
</code></pre>
<ul>
<li>Strict equality: <em><strong>===</strong></em>, <em><strong>!==</strong></em></li>
</ul>
<pre><code class="language-plaintext">0 === false         // false (different types)
"5" === 5           // false (different types)
"hello" === "hello" // true (same value, same type)
1 === 1             // true (same type, same value)
3 !== '3'           // true (different types)
</code></pre>
<blockquote>
<p><em>💡 Due to how JS works, the recommendation is to always use</em> <em><strong>===</strong></em><em>,unless you have a good reason to allow coercion.</em></p>
</blockquote>
<ul>
<li>Relational: <em><strong>&lt;</strong></em>, <em><strong>&gt;</strong></em>, <em><strong>&lt;=</strong></em>, <em><strong>&gt;=</strong></em></li>
</ul>
<pre><code class="language-plaintext">3 &lt; 1           // false (3 is not smaller than 1)
4 &gt; 4           // false (4 is not larger than 4)
4 &gt;= 4          // true (4 is larger or equal than 4)
4 &lt;= 5          // true (5 is smaller or equal than 4)
</code></pre>
<h2><strong>Logical Operators</strong></h2>
<p>Logical operators, as their name suggest, use the values sent in a logical way in order to return the right value. These are pretty flexible operators, on the regular basis, if used with <em><strong>boolean</strong></em> values it will return a <em><strong>boolean</strong></em>, BUT they can also be used to decide which, if any value, will be returned. There are 4 operators that are most commonly used <em><strong>&amp;&amp;</strong></em> (AND), <em><strong>||</strong></em> (OR), <em><strong>!</strong></em> (NOT), and <strong>??</strong> (NULLISH). along with <em><strong>?.</strong></em> (Optional Chaining) which was introduced later in the ECMAScript 2020 (ES2020) version of JavaScript (<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_operators">MDN Web Docs</a>).</p>
<p>Here’s how to use them:</p>
<pre><code class="language-plaintext">//Is an editor user IF the user is logged in AND has the role of "editor"
const isEditorUser= user.isLoggedIn &amp;&amp; user.role === "editor";

// Validates if maybeName can be turned to true, otherwise it returns "guest"
const name = maybeName || "Guest";

// Validates if maybeName is either NULL or UNDEFINED, otherwise it returns "guest"
const name = maybeName ?? "Guest";

// Validates IF the method or variable exists before it tries to read it
// eg. this is ussed instead of user &amp;&amp; user.address &amp;&amp; user.address.street 
const street = user?.address?.street;
</code></pre>
<h2><strong>Ternary Operators (? :)</strong></h2>
<p>Just as we saw during the last post, <em><strong>IF Else</strong></em> statements help us to keep the flow of the application based on different conditions. Ternary operators (<em><strong>? :</strong></em>) work in a pretty similar way; Think of them as a shorthand for an If Else statement, they are commonly written as {Condition} ? {result if true} : {result if false} here’s a quick example</p>
<pre><code class="language-plaintext">const status = age &gt;= 18 ? "adult" : "minor";
</code></pre>
<p>Let’s make a comparison between a regular if else and a ternary if else:</p>
<pre><code class="language-plaintext">// With IF Else
function getFinalPrice(price, discount) {
  let result;
  if (discount &gt; 0) {
    result = price - (price * discount);
  } else {
    result = price;
  }
  return result;
}

// With Ternary Operator
function getFinalPrice(price, discount) {
  return discount &gt; 0
    ? price - price * discount
    : price;
}
</code></pre>
<blockquote>
<p><em>💡 Keep in mind that while ternary operators are used to simplify the code and in a lot of cases make it less verbose, it can also really bloat it if overused or if we chain multiple ternary conditions in a row. This can become a problem for future development and maintainability so you should weight which option you will take when working on your code.</em></p>
</blockquote>
<p><a href="https://medium.com/tag/javascript?source=post_page-----41fa7b72953e---------------------------------------">  
</a></p>
]]></content:encoded></item><item><title><![CDATA[Understanding Variables and Data Types in JavaScript]]></title><description><![CDATA[If you don't understand variables and data types, everything else in JavaScript feels random. Scope bugs, unexpected undefined, strange equality behaviour, most beginner mistakes trace back here.
This]]></description><link>https://jsvariablesanddatatypes.hashnode.dev/understanding-variables-and-data-types-in-javascript</link><guid isPermaLink="true">https://jsvariablesanddatatypes.hashnode.dev/understanding-variables-and-data-types-in-javascript</guid><category><![CDATA[variables]]></category><category><![CDATA[data types]]></category><dc:creator><![CDATA[Ashraf Raza]]></dc:creator><pubDate>Fri, 13 Mar 2026 11:47:42 GMT</pubDate><content:encoded><![CDATA[<p>If you don't understand variables and data types, everything else in JavaScript feels random. Scope bugs, unexpected undefined, strange equality behaviour, most beginner mistakes trace back here.</p>
<p>This is the foundation. Get this right and the rest of the language starts making sense.</p>
<h3>Variables: What They Actually Are</h3>
<p>Think a variable as a labelled box in a memory.</p>
<p><code>let name = "John";</code></p>
<ul>
<li><p>name --&gt; label</p>
</li>
<li><p>"John" --&gt; value stored</p>
</li>
<li><p>let --&gt; how the box behaves</p>
</li>
</ul>
<p>JavaScript stores the value somewhere in memory and the variable points to it.</p>
<p>You need variables because somewhere in memory and the variable points to it.</p>
<p>You need variables because programs deal with changing data:</p>
<ul>
<li><p>User input</p>
</li>
<li><p>API responses</p>
</li>
<li><p>Calculations</p>
</li>
<li><p>State in UI</p>
</li>
</ul>
<p>Without variables, everything would be hardcoded and useless.</p>
<p><strong>Declaring Variables</strong>: <code>var</code><strong>,</strong> <code>let</code><strong>, and</strong> <code>const</code></p>
<p>JavaScript has three ways to declare variables.</p>
<p>1. <code>let</code> (Modern Default)</p>
<p><code>let age = 25;</code><br /><code>age = 36; // allowed</code></p>
<ul>
<li><p>can be reassigned</p>
</li>
<li><p>block scoped</p>
</li>
<li><p>use this by default when value changes</p>
</li>
</ul>
<p>2. <code>const</code> <strong>(Preferred when value should not change)</strong></p>
<p><code>const birthYear = 1996;</code><br /><code>birthYear = 1999; // Error</code></p>
<ul>
<li><p>cannot be reassigned</p>
</li>
<li><p>block scoped</p>
</li>
<li><p>safer by default</p>
</li>
</ul>
<p><strong>Important:</strong> <code>const</code> prevents reassignment, not mutation.</p>
<p><code>const user = { name: "Ali" };   user.name = "Mohammad"; // Allowed   user = {} // Error: not allowed</code></p>
<p>The variable is protected, not the object.</p>
<p>3. <code>var</code> <strong>(Legacy - Avoid in Modern Js)</strong></p>
<p><code>var city = "Delhi";</code></p>
<p><strong>Problems:</strong></p>
<ul>
<li><p>function scoped (not block scoped)</p>
</li>
<li><p>can be redeclared</p>
</li>
<li><p>leads to subtle bugs.</p>
</li>
</ul>
<p><code>if (true) {   var x = 10;   }      console.log(x); // 10 (leaks outside block)</code></p>
<p>With <code>let</code> :</p>
<p><code>if (true) {   let y = 10;   }      console.log(y); // ReferenceError</code></p>
<p>Rule of thumb :</p>
<ul>
<li><p>use <code>const</code> by default</p>
</li>
<li><p>use <code>let</code> if value must change</p>
</li>
<li><p>never use <code>var</code> in new code</p>
</li>
</ul>
<h3>What is Scope ?</h3>
<p>Scope defines where a variable is accessible.</p>
<p>Think scope like a room in a house. If you define a variable inside a block {}, it stays inside that block.</p>
<p><code>{   let secret = "supersecret1234";   }      console.log(secret); // Not accessible</code></p>
<p>Block scope prevents accidental collisions in larger programs. This becomes critical in loops and conditionals.</p>
<h3>Data Types in JavaScript</h3>
<p>Every value in JavaScript has a type.</p>
<p>JavaScript has 7 primitive type (defined in the ECMAScript spec)</p>
<pre><code class="language-javascript">const str = "hello";           // string
const num = 42;                // number
const big = 9007199254740993n; // bigint
const boolean = true;             // boolean
const undefined = undefined;       // undefined
const null = null;              // null
const symbol = Symbol("id");      // symbol
</code></pre>
<p>Primitives are:</p>
<ul>
<li><p>immutable</p>
</li>
<li><p>compared by value</p>
</li>
<li><p>not objects</p>
</li>
</ul>
<p>Everything else (arrays, objects, functions) in an object.</p>
<p><strong>1. String</strong></p>
<p>text data.</p>
<p><code>let name = "Mohammad";   let greeting = Hello, ${name};</code></p>
<p>Strings are immutable</p>
<p><code>let str = "hello";   str[0] = "H"; // does nothing   console.log(str); // "hello"</code></p>
<p>to change it, you create a new string</p>
<p><code>str = "H" + str.slice(1);</code></p>
<p><strong>2. Number</strong></p>
<p>JavaScript has one number type for integers and decimals.</p>
<p><code>let age = 25;   let price = 10.33;</code></p>
<p>Internally, numbers follow the <a href="https://standards.ieee.org/ieee/754/6210/">IEEE 754 floating-point standard.</a> That’s why this happens:</p>
<p><code>0.1 + 0.2 === 0.3; // false</code></p>
<p>It's not a bug. It's floating point precision.</p>
<p>If you’re handling money:</p>
<ul>
<li><p>Store values in cents (integers)</p>
</li>
<li><p>Format later</p>
</li>
</ul>
<p><code>let totalInCents = 10 + 20; // 30</code></p>
<p>Now format in USD or any other currency.</p>
<p><code>// Bad: floating-point errors in calculations   let price = 0.1 + 0.2; // 0.30000000000000004      // Good: calculate in cents, format for display   let priceInCents = 10 + 20; // 30 (calculation is accurate!)      // Use Intl.NumberFormat to display as currency   const formatter = new Intl.NumberFormat('en-US', {   style: 'currency',   currency: 'USD',   });      console.log(formatter.format(priceInCents / 100)); // "$0.30"      // Works for any locale and currency!   const euroFormatter = new Intl.NumberFormat('de-DE', {   style: 'currency',   currency: 'EUR',   });   console.log(euroFormatter.format(1234.56)); // "1.234,56 €"</code></p>
<p><strong>3. Bigint</strong></p>
<p>use for very large integers.</p>
<p><code>let big = 9007199254740993n;</code></p>
<p>you cannot mix number and bigInt;</p>
<p><code>// 10n + 5 TypeError</code></p>
<p>Use BigInt only when needed (large IDs, cryptography, precision work)</p>
<p><strong>4.Boolean</strong></p>
<p>only two values : true or false</p>
<pre><code class="language-javascript">let isAdult = age &gt;= 18;
</code></pre>
<p>Falsy values in JavaScript:</p>
<ul>
<li><p><code>false</code></p>
</li>
<li><p><code>0</code></p>
</li>
<li><p><code>""</code></p>
</li>
<li><p><code>null</code></p>
</li>
<li><p><code>undefined</code></p>
</li>
<li><p><code>NaN</code></p>
</li>
</ul>
<p>Everything else is truthy.</p>
<p><strong>5. undefined</strong></p>
<p>Means no value assigned.</p>
<p><code>let x;   console.log(x); // undefined</code></p>
<p>Also returned when :</p>
<ul>
<li><p>Function does not return</p>
</li>
<li><p>Property does not exist</p>
</li>
</ul>
<p>You usually don't assign <code>undefined</code> manually</p>
<p><strong>6.null</strong></p>
<p>Means intentionally empty.</p>
<p><code>let user = null</code></p>
<p>You assign <code>null</code> when you want to say:</p>
<p>"There is no value here on purpose"</p>
<h3>Weird JavaScript Fact</h3>
<p><code>typeof null; // "object"</code></p>
<p>This is a historical bug from 1995. It remains for backward compatibility. To check for null:</p>
<p><code>value === null;</code></p>
<p><strong>7. Symbol</strong></p>
<p>Creates unique identifiers.</p>
<p><code>let id1 = Symbol("id");   let id2 = Symbol("id");      id1 === id2; // false</code></p>
<p>Advanced use case:</p>
<ul>
<li><p>unique object keys</p>
</li>
<li><p>internal library mechanism</p>
</li>
</ul>
<p>Beginners don't need it immediately, but it's part of the language.</p>
<h3>typeof Operator</h3>
<p>Use <code>typeof</code> to check type:</p>
<p><code>typeof "hello"; // "string"   typeof 42; // "number"   typeof true; // "boolean"   typeof undefined; // "undefined"   typeof Symbol(); // "symbol"   typeof 42n; // "bigint"</code></p>
<p>But</p>
<p><code>typeof null; // "object" (legacy bug)   typeof []; // "object"</code></p>
<p>To check arrays:</p>
<p><code>Array.isArray([1, 2, 3]); // true</code></p>
<h3>Primitive vs Object</h3>
<p>Primitives:</p>
<ul>
<li><p>stored directly</p>
</li>
<li><p>compared by values</p>
</li>
<li><p>immutable</p>
</li>
</ul>
<p>Objects:</p>
<ul>
<li><p>stored by reference</p>
</li>
<li><p>compared by reference</p>
</li>
<li><p>mutable</p>
</li>
</ul>
<p>Example:</p>
<p><code>let a = 5;   let b = 5;      a === b; // true</code></p>
<p>But:</p>
<p><code>let obj1 = { name: "Ali" };   let obj2 = { name: "Ali" };      obj1 === obj2; // false</code></p>
<p>Same content, different references.</p>
<p>Understanding this prevents many state management bugs.</p>
<h3>Common Beginner Mistakes</h3>
<p>1.Using <code>var</code> in modern code</p>
<p>creates scope bugs.</p>
<p>2.Confusing <code>null</code> and <code>undefined</code></p>
<p>3.Thinking <code>const</code> makes values immutable</p>
<p>It doen't.</p>
<p>4.Ignoring floating-point issues</p>
<p>Never use decimals for financial math.</p>
<h3>Final Thoughts</h3>
<p>Variables are not just syntax. They define.</p>
<ul>
<li><p>how memory behaves</p>
</li>
<li><p>how scope protects you</p>
</li>
<li><p>how data moves through your program</p>
</li>
</ul>
<p>Data types are not trivia. They determine:</p>
<ul>
<li><p>how values are stored</p>
</li>
<li><p>how they are compared</p>
</li>
<li><p>how bugs appear</p>
</li>
</ul>
<p>If you deeply understand primitives, scope, and reassignment rules, you eliminate 60% of beginner JavaScript mistakes before they happen.</p>
<p>Master this layer first. Everything else builds on it.</p>
]]></content:encoded></item></channel></rss>