Type Conversion

JavaScript is a loosely-typed language, meaning you can convert between different types easily. There are two types of type conversion: implicit (automatic) and explicit (manual).

Implicit Conversion

JavaScript automatically converts types when needed.

let result = '5' + 5;
console.log(result); // '55' (string)

let subtract = '5' - 3;
console.log(subtract); // 2 (number)
  • String Conversion: When using the + operator with a string, JavaScript converts the other operand to a string.
  • Number Conversion: When using the -, *, /, or % operators with a string, JavaScript converts the string to a number.

Explicit Conversion

Explicit conversion requires using functions or methods to convert types.

// String Conversion
let num = 123;
let str = String(num);
console.log(typeof str); // 'string'

// Number Conversion
let str = '123';
let num = Number(str);
console.log(typeof num); // 'number'

// Boolean Conversion
let truthy = Boolean(1);
let falsy = Boolean(0);
console.log(truthy); // true
console.log(falsy); // false

Common Methods

  • String(value): Converts the given value to a string.
  • Number(value): Converts the given value to a number.
  • Boolean(value): Converts the given value to a boolean.

Quirks and Special Cases

JavaScript has some quirks and special cases when it comes to type conversion, especially with falsy values. Falsy values in JavaScript include false, 0, '', null, undefined, and NaN.

Falsy Values

Falsy values in JavaScript include false, 0, '', null, undefined, and NaN. These values behave as false when converted to a boolean.

if (!undefined) {
  console.log('This is falsy');
}

Converting User Input

let input = prompt('Enter a number:');
let number = Number(input);
alert(`You entered: ${number}`);

Boolean Context

Values in JavaScript can be coerced into boolean context in conditions and loops. Understanding how different values behave in boolean context is crucial.

let userLoggedIn = 'true';
if (Boolean(userLoggedIn)) {
  console.log('User is logged in');
}
Notes
  • Be mindful of implicit type conversions that may lead to unexpected results.
  • Always prefer explicit type conversion when the type needs to be certain.
  • Test and verify the type conversions in your code to avoid bugs.
Understanding type conversion helps prevent bugs and ensures your code behaves as expected. Practice converting between types and recognize when implicit conversions might occur.

FAQ

Q: What is the difference between implicit and explicit conversion?

A: Implicit conversion happens automatically by JavaScript, often during operations involving different types. Explicit conversion requires using functions or methods to manually convert types.

Q: Why does `'5' + 5` result in `'55'` instead of `10`?

A: The `+` operator, when used with a string, concatenates the operands instead of adding them. Use `Number()` to convert the string to a number before adding.

Q: What are falsy values in JavaScript?

A: Falsy values are values that evaluate to `false` in a boolean context. They include `false`, `0`, `''`, `null`, `undefined`, and `NaN`.

Q: How can I check the type of a variable?

A: Use the `typeof` operator to check the type of a variable.

Example:

console.log(typeof 'hello'); // 'string'
Q: What is the result of `null == undefined`?

A: `null` and `undefined` are considered equal with the `==` operator because they both represent the absence of value. However, they are not equal with the `===` operator.