Home JavaScript Tutorial Type Conversions in JavaScript

Type Conversions in JavaScript

by Anup Maurya
36 minutes read

In JavaScript, type conversions allow you to convert a value from one data type to another. JavaScript has two types of type conversions: implicit and explicit. Implicit type conversions are done automatically by JavaScript, while explicit type conversions are done by the developer using a built-in function or operator.

Implicit Type Conversions

JavaScript automatically converts data types when an operation requires it. For example, if you add a number and a string, JavaScript will convert the number to a string and concatenate the two strings.

Let’s take a look at some examples of implicit type conversions in JavaScript:

// Adding a number and a string
let x = 5;
let y = "10";
let z = x + y; // z will be "510"

// Adding a string and a boolean
let a = "Hello";
let b = true;
let c = a + b; // c will be "Hellotrue"

// Comparing a number and a string
let num = 10;
let str = "10";
let result = num == str; // result will be true

In the examples above, JavaScript automatically converted the number to a string in the first example, and the boolean to a string in the second example. In the third example, JavaScript converted the string to a number for the comparison.

Explicit Type Conversions

Explicit type conversions allow you to convert a value from one data type to another using built-in functions or operators. Here are some of the most commonly used functions and operators for explicit type conversions in JavaScript:

  1. parseInt(): This function converts a string to a number. It takes a string as an argument and returns an integer. If the string does not start with a number, it returns NaN (Not a Number).
let str = "123";
let num = parseInt(str); // num will be 123
  1. parseFloat(): This function converts a string to a floating-point number. It takes a string as an argument and returns a floating-point number. If the string does not start with a number, it returns NaN.
let str = "3.14";
let num = parseFloat(str); // num will be 3.14
  1. toString(): This method converts a number to a string. It takes no arguments and returns a string.
let num = 123;
let str = num.toString(); // str will be "123"
  1. String(): This function converts a value to a string. It takes a value as an argument and returns a string.
let num = 123;
let str = String(num); // str will be "123"
  1. Number(): This function converts a value to a number. It takes a value as an argument and returns a number. If the value cannot be converted to a number, it returns NaN.
let str = "123";
let num = Number(str); // num will be 123
  1. Boolean(): This function converts a value to a boolean. It takes a value as an argument and returns true or false. The following values are converted to false: false, 0, “”, null, undefined, and NaN. All other values are converted to true.
let num = 0;
let bool = Boolean(num); // bool will be false

Conclusion

Type conversions are an important part of JavaScript programming. By understanding the different types of type conversions and how to use them, you can write more flexible and robust code. Remember to use implicit type conversions carefully, as they can sometimes lead to unexpected results.

related posts

Leave a Comment

Enable Notifications OK No thanks