Skip to content

Instantly share code, notes, and snippets.

@joeytwiddle
Last active June 23, 2018 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeytwiddle/d9ea101e3dc170764acada009ccdb117 to your computer and use it in GitHub Desktop.
Save joeytwiddle/d9ea101e3dc170764acada009ccdb117 to your computer and use it in GitHub Desktop.
Converting between types in Javascript

When you have a variable of one type but you want to convert it to another type, there are a few options available to you.

Here are some of the most popular methods:

+=================+===================+=========================+
|                 |                   |                         |
|    Goal         |    Short form *   |    Long form            |
|                 |                   |                         |
+=================+===================+=========================+
|                 |                   |                         |
|    string       |    '' + input     |    String(input)        |
|                 |                   |                         |
+-----------------+-------------------+-------------------------+
|                 |                   |                         |
|    number       |    +input         |    Number(input)        |
|                 |                   |                         |
+-----------------+-------------------+-------------------------+
|                 |                   |                         |
|    number       |    input | 0      |    Math.floor(input)    |
|    (integer)    |                   |                         |
|                 |    ~~input        |                         |
|                 |                   |                         |
+-----------------+-------------------+-------------------------+
|                 |                   |                         |
|    boolean      |    !!input        |    Boolean(input)       |
|                 |                   |                         |
+-----------------+-------------------+-------------------------+

* When using short form, be sure to use parentheses as necessary

In order to make code easier to understand and maintain, I recommend using the longer but clearer "Long form" in your code, except in the case of booleans. I make an exception for booleans because casting to a boolean is such a common operation.

So for example:

const age = Number(params.age);

const userWasFound = !!user;

const wasEaten = String(error).match(/has been eaten/i);

Using String(input) is generally safer than using input.toString() because it will not throw an error if input is null or undefined.

(Unless you want to throw an error, in which case go ahead. When you are given invalid inputs, throwing an error is often preferable to a silent failure, because it makes it easier to track down the cause of the problem.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment