JavaScript is a dynamically typed language, which means variables can hold values of any data type and can be changed at runtime. This dynamic nature requires careful handling of type checking and type conversion, especially when dealing with user inputs or API responses.

This post walks through how JavaScript behaves with different types of variables and what happens when you convert them using the Number() and Boolean() functions.

🟢 Declaring and Checking a String Number:

let score = "33";
console.log(typeof score);
console.log(typeof (score));

🔍 Explanation:
Here, a variable score is assigned the string value "33". Using typeof, we check the data type, and as expected, it prints:

Copy
string
string

Even though "33" looks like a number, it is stored as a string.

**
🔁 Converting String to Number**

let valueInNumber = Number(score);
console.log(typeof (valueInNumber));
console.log(valueInNumber);

🔍 Explanation:

The Number() function attempts to convert the string "33" into a number. Since "33" is a valid numeric string, conversion is successful.

Output:

Copy
number
33

🟠 Trying to Convert a Non-Pure Numeric String

Code:

let score1 = "33abc";
console.log(typeof score1);
console.log(typeof (score1));
let valueInNumber1 = Number(score1);
console.log(typeof (valueInNumber1));
console.log(valueInNumber1);

🔍 Explanation:

Here, the string "33abc" contains non-numeric characters. While typeof still returns string, the conversion fails because it's not a valid number.

Output:

Copy
string
string
number
NaN

The result is NaN (Not-a-Number), but the type is still considered a number in JavaScript.

🟡 Converting null to a Number
Code:

let score2 = null;
console.log(typeof score2);
console.log(typeof (score2));
let valueInNumber2 = Number(score2);
console.log(typeof (valueInNumber2));
console.log(valueInNumber2);

🔍 Explanation:
typeof null returns "object" (a long-standing quirk in JavaScript). When converted using Number(null), it results in 0.

Output:

Copy
object
object
number
0

So null is treated as a falsy value and converts to 0.

🔴 Converting undefined to a Number
Code:

let score3 = undefined;
console.log(typeof score3);
console.log(typeof (score3));
let valueInNumber3 = Number(score3);
console.log(typeof (valueInNumber3));
console.log(valueInNumber3);

🔍 Explanation:
typeof undefined is undefined. When passed to Number(), it becomes NaN because undefined has no numeric value.

Output:

undefined
undefined
number
NaN

🔁 Converting undefined to Boolean
Code:

let isloggedin = undefined;
let booleanIsLoggedIn = Boolean(isloggedin);
console.log(booleanIsLoggedIn);

🔍 Explanation:
The Boolean() function converts a value to true or false based on whether it’s considered truthy or falsy.

Since undefined is a falsy value, the conversion returns:

Output:

false

💡 Final Thoughts
Understanding how JavaScript handles type coercion is crucial for writing predictable and bug-free code. Use typeof, Number(), and Boolean() wisely to control how data is interpreted and processed in your applications.

For source code visit: https://github.com/shifa-23/JS-vault/blob/main/basics1/convrsion.js.