Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# JavaScript Type Conversion Rules

JavaScript is a dynamically typed language, which means variable types can change at runtime. Type conversion can be **explicit** (type casting) or **implicit** (type coercion).

---

## Implicit Type Conversion (Type Coercion)

JavaScript automatically converts types when operating with different types.

### Addition (+) with Strings

'5' + 2 // "52" - number is coerced to string
'5' + true // "5true"
'5' + null // "5null"

### Subtraction, Multiplication, Division with Strings

'5' - 2 // 3 - string is coerced to number
'5' * 2 // 10
'10' / '2' // 5

### Boolean Conversion

true + 1 // 2
false + 1 // 1
true == 1 // true
false == 0 // true

### Loose Equality (==) vs Strict Equality (===)

'5' == 5 // true (type coercion)
'5' === 5 // false (strict type check)
null == undefined // true
null === undefined // false

---

## Explicit Type Conversion (Type Casting)

You can manually convert types using built-in functions or constructors.

### To String

String(123) // "123"
String(true) // "true"
(123).toString() // "123"

### To Number

Number('123') // 123
Number('abc') // NaN
Number(true) // 1
Number(false) // 0
Number(null) // 0
Number(undefined) // NaN

### To Boolean

Boolean(0) // false
Boolean('') // false
Boolean(null) // false
Boolean(undefined) // false
Boolean(NaN) // false
Boolean('hello') // true
Boolean(123) // true

---

## Falsy vs Truthy Values

Falsy Values:
- false
- 0, -0
- '' (empty string)
- null
- undefined
- NaN

Truthy Values:
- true
- non-zero numbers
- non-empty strings
- objects
- arrays

Examples:
if (0) console.log('Falsy'); // won't run
if ('hello') console.log('Truthy'); // runs

---

## Conversion Summary Table

Expression | Result | Type
--------------------- | --------- | --------
String(42) | "42" | string
Number("42") | 42 | number
Boolean("0") | true | boolean
Boolean("") | false | boolean
+'42' | 42 | number
!!'hello' | true | boolean
null == undefined | true | boolean
null === undefined | false | boolean

---

## Special Cases

### NaN behavior

NaN === NaN // false
isNaN(NaN) // true
Number('abc') // NaN

### parseInt vs Number

parseInt('123abc') // 123
Number('123abc') // NaN

---

## Best Practices

- Use === and !== instead of == and != to avoid type coercion surprises.
- Be explicit with conversions to make your code easier to read and debug.
- Use Number(), String(), and Boolean() for safe type conversion.
53 changes: 53 additions & 0 deletions data-conversion/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

console.log(String(123)); // Explicit conversion from number to string

let num = 123;
let str = String(num); // Explicit conversion from number to string
console.log(typeof str); // "string"

console.log(str); // "123"

// using ToString() method

let num1 = 420;

let str2 = num1.toString()

console.log(typeof str2); // "string"
console.log(str2); // "123"

let bool = true;
console.log(String(bool)); // Explicit conversion from boolean to string

console.log(typeof String(true)); // "string"

// converting to Number

let actualString = "456";
let actualNumber = Number(actualString); // Explicit conversion from string to number
console.log(typeof actualNumber); // "number"
console.log(actualNumber); // 456

// using parseInt() method
let newSteing = "899"
let newNumber = parseInt(newSteing)

console.log(typeof newNumber); // "number"
console.log(newNumber); // 899


console.log("Converting float into other use cases.")

let floatNumber = "3.14";

let floatStringCase = "3.14 meters"

console.log(parseFloat(floatNumber)); // 3.14
console.log(typeof parseFloat(floatNumber)); // "number"
console.log(floatNumber.Number); // NaN (Not a Number)
console.log(parseFloat(floatStringCase)); // 3.14

console.log("Converting to Boolean")

let boolString = "true";
console.log(Number(boolString)); // NaN (Not a Number)
27 changes: 18 additions & 9 deletions explicit-and-implicit-conversion-in-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,24 @@ Use console.log() to clearly show the before-and-after type conversions.

*/


let result = "5" - 2;
console.log("The result is: " + result);

let isValid = Boolean("false");
let ageStr = "25";
let totalAgeNum = Number(ageStr) + 5; // Number("25") => 25; 25 + 5 = 30
console.log("Total Age (explicit Number):", totalAgeNum); // Outputs: 30

// Explicit conversion of "5" to number before subtraction
let result = Number("5") - 2; // Number("5") => 5; 5 - 2 = 3
console.log("The result is:", result); // Outputs: 3

// The string "false" is truthy when used directly in boolean contexts.
// To interpret the string value as a boolean, compare explicitly to "true".
let strBool = "false";
let isValid = (strBool.toLowerCase() === "true"); // only true if the string exactly equals "true"
if (isValid) {
console.log("This is valid!");
console.log("This is valid!");
} else {
console.log("This is NOT valid!");
}

let age = "25";
let totalAge = age + 5;
console.log("Total Age: " + totalAge);
// Demonstrate an edge case: converting a non-numeric string yields NaN
let badNumber = Number("abc");
console.log('Number("abc") =>', badNumber, 'typeof =>', typeof badNumber); // NaN