DEV Community

Shifa
Shifa

Posted on

Understanding Type Conversion and Type Checking in JavaScript

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));
Enter fullscreen mode Exit fullscreen mode

πŸ” 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

Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

πŸ” 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
Enter fullscreen mode Exit fullscreen mode

🟠 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);
Enter fullscreen mode Exit fullscreen mode

πŸ” 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

Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

πŸ” 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
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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

Output:

undefined
undefined
number
NaN
Enter fullscreen mode Exit fullscreen mode

πŸ” Converting undefined to Boolean
Code:

let isloggedin = undefined;
let booleanIsLoggedIn = Boolean(isloggedin);
console.log(booleanIsLoggedIn);
Enter fullscreen mode Exit fullscreen mode

πŸ” 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
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ 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.

Top comments (0)