JavaScript’s dynamic typing allows variables to hold any value type, but this flexibility can cause unexpected behaviour , especially with type coercion and comparisons. This article highlights key examples to illustrate how JavaScript handles these cases. Declaring and Checking Variable Types

We start with a simple variable declaration:

let a = "2";

Here, the variable a is assigned a string value "2". To verify the type of the variable, we can use the typeof operator:

console.log(typeof a); // "string"

This confirms that a is indeed a string. Even though it contains a numeric character, its type is still "string".

Implicit Type Coercion in Comparisons
Now, let’s compare this string with a number:

console.log(a > 5); // false

At first glance, this might seem confusing. Why would "2" be considered greater than 5? The answer lies in JavaScript's implicit type coercion. When comparing a string with a number using relational operators (>, <, etc.), JavaScript automatically attempts to convert the string into a number. So, "2" becomes 2, and the expression 2 > 5 evaluates to false.

We then check the type again:

console.log(typeof a); // "string"

This proves that although the value was coerced to a number during the comparison, the original variable a remains a string.

Comparing null and undefined
Understanding how null and undefined behave in comparisons is crucial, as they often lead to bugs if not handled carefully.

console.log(null > 0); // false
console.log(null == 0); // false
console.log(null == null); // true

null > 0: JavaScript treats null as 0 in numeric comparisons, but this still results in false because the rules are subtle.
null == 0: This is false because null only loosely equals undefined, not any numeric value.
null == null: This returns true because any value is always loosely equal to itself.
Now, comparing null with undefined:

console.log(null > undefined); // false
console.log(null < undefined); // true
console.log(null <= undefined); // true
console.log(null >= undefined); // false

These comparisons show the unpredictable behavior that arises from trying to order null and undefined. Though they seem similar, JavaScript treats them differently based on the context and operator.

Comparisons with >= and Type Coercion

Let's look at more comparisons:

console.log(null >= 0); // true
console.log(undefined >= 0); // false

null >= 0 evaluates to true because JavaScript treats null as 0.
undefined >= 0 is false because undefined cannot be converted to a meaningful number, resulting in NaN, which is never greater than or equal to any number.

*Strict Equality (===) vs Loose Equality (==)
*

console.log("5" === 5); // false

Strict equality (===) checks both the value and the type. Although "5" and 5 might look the same, one is a string and the other is a number—so the result is false.
Logging Simple Strings

console.log("6"); // "6"

This final line simply logs the string "6" to the console, which behaves as expected without any coercion or comparison.
Final Thoughts:
These examples demonstrate the importance of understanding how JavaScript handles type coercion, equality, and comparison. Subtle rules govern how values like null, undefined, strings, and numbers interact, and relying on implicit behaviour, can lead to bugs. When in doubt, prefer strict equality (===) and always be aware of the data types you're working with.

*For more detailed explanation *: https://github.com/shifa-23/JS-vault/blob/main/basics1/comparison.js