DEV Community

Danish Saleem
Danish Saleem

Posted on • Updated on

JavaScript for Beginners - Type Conversions

In this chapter we cover the 3 main primitive type conversions: to string, to number and to Boolean.

Make sure you check back for the next chapter which will be all about basic operators!

Converting Types

Most of the time, operators and functions in JavaScript automatically values into to the right types. For example, mathematical operators will convert values to number in order to perform calculations.

However, sometimes you need to explicitly convert values to number to a specific type. In this chapter we will cover some primitive type conversions.

String Conversion

There are a few ways to convert a value to a string but the most straight forward is by using the String() function. Some build in JavaScript functions such as alert() will always convert the value to a string in order to display it.

Examples:

let numToString = String(1); // Output: "1"

let trueToString = String(true); // Output: "true"

let falseToString = String(false); // Output: "fale"

let nullToString = String(null); // Output: "null"

let undefinedToString = String(undefined); // Output: "undefined"

let arrayToString = String([1, 2, 3]); // Output: "1, 2, 3"
Enter fullscreen mode Exit fullscreen mode

Numeric Conversion

Numeric conversions are the useful when the value is read from a string-based source where a number is expected. Mathematical functions and expressions will automatically convert string values into numbers, but it can also be achieved using the Number() function.

Examples:

let stringToNumber = Number("123"); // Output: 123

let wordToNumber = Number("Hello"); // Output: NaN

let calcToNumber = "10" / "2"; // Output: 5

let trueToNumber = Number(true); // Output: 1

let falseToNumber = Number(false); // Output: 0

let nullToNumber = Number(null); // Output: 0

let undefinedToNumber = Number(undefined); // Output: NaN

let emptyStringToNumber = Number(""); // Output: 0
Enter fullscreen mode Exit fullscreen mode

Boolean Conversion

Boolean conversions are quite simple and happen mostly in logical operations but can also be done by using the Boolean() function. Values that are considered empty (such as 0, null, NaN, undefined and empty strings) become false and other values become true.

Double exclamation marks can also be used as a shorthand Boolean conversion.

Examples:

let stringToBoolean = Boolean("Hello"); // Output: true

let stringFalseToBoolean = Boolean("false"); // Output: false

let emptyStringToBoolean = Boolean(""); // Output: false

let numberToBoolean = Boolean(123); // Output: true

let nullToBoolean = Boolean(null); // Output: false

let zeroToBoolean = Boolean(0); //output: false

let oneToBoolean = Boolean(1); // Output: true

// Shorthand Conversion

let value = "Hello";
console.log(!!value); // Output: true
Enter fullscreen mode Exit fullscreen mode

Summary

  • The most widely used conversions are to string, to number and to Boolean.
  • String conversion occurs when we output something and is usually obvious.
  • Numeric conversion occurs in mathematical operations.
  • Boolean conversion occurs in logical operations.

Let's connect đź’ś

You can follow me on Twitter, Instagram & GitHub

If you like this post. Kindly support me by Buying Me a Coffee

Buy Me a Coffee

Top comments (1)

Collapse
 
mrdanishsaleem profile image
Danish Saleem

Thanks