How to convert between types in JS?

JavaScript: How to convert between types in JS?

If you have some variable in JS, you might end up needing to convert it to another data type. For example, to change from a string to an integer value. Here is how to do it.

Converting to a string

Convert From number to string in JS

If you wrap a number in String( some_int_or_float ), it will return a string object.

  1. let numeric_val = 1234;
  2. console.log(numeric_val);
  3. console.log(typeof numeric_val);
  4.  
  5. let string_from_numeric = String( numeric_val) ;
  6. console.log(string_from_numeric);
  7. console.log(typeof string_from_numeric);
1234
number
1234
string

Alternatively you can just do .toString() on a number variable and it will return a string.

  1. let num = 123;
  2. let num_as_string = num.toString();

Convert from a boolean to a string in JS

To convert from a boolean to a string, it follows a similar pattern as from a number to a string. Just wrap it in String ( true ) and it will return a 4 char string 'true' (or 'false' ).

  1. let your_bool = false;
  2. console.log(false);
  3. console.log(typeof false);
  4.  
  5. let string_from_your_bool = String( your_bool) ;
  6. console.log(string_from_your_bool);
  7. console.log(typeof string_from_your_bool);
false
boolean
false
string

You can also use the .toString() method:

  1. let your_bool = false;
  2. your_bool_as_string_2 = your_bool.toString();
  3. console.log("Type: " + (typeof your_bool_as_string_2) + " and its value: " + your_bool_as_string_2);
Type: string and its value: false

How to convert from an array to a string in JS

To convert from an array to a string in Javascript, you just wrap an array in String( ['a','b','c'] ) and it will return a comma separated list of the items.

  1. let array = [100,200,333,444];
  2. let array_as_string = String(array);
  3. console.log(array_as_string);
  4. // logs the string "100,200,333,444"

Convert from a date to a string in JS

You can wrap a Data object in String ( new Date ) to get it as a string.

  1. let some_date = new Date;
  2. console.log(some_date);
  3. console.log(typeof some_date);
  4.  
  5. let string_from_some_date = String( some_date ) ;
  6. console.log(string_from_some_date);
  7. console.log(typeof string_from_some_date);
Wed Jan 12 2016 01:50:10 GMT+0200 (Central European Summer Time)
object
Wed Jan 12 2016 01:50:10 GMT+0200 (Central European Summer Time)
string

How to convert to numbers

How to convert from a string to a number:

To convert from a string (which contains a number) you can do it like this:

  1. let a_number_as_a_string = '1234'; // this is a number
  2.  
  3. let converted_to_a_number = Number( a_number_as_a_string ); // now it is a number!
  4. // alternative ways to do this:
  5.  
  6. // as an integer
  7. let int_value = parseInt(a_number_as_a_string);
  8. // as a float:
  9. let float_value = parseFloat(a_number_as_a_string);
  10. let float_value_2 = parseFloat("3.14");

However if you tried to convert a string such as 'asdfg' it would return NaN (not a number) as obviously there is no way to convert 'asdfg' to a number.

Convert a boolean to a number in JS

This will convert bools to numbers. A true value gets converted to 1, and false to 0. Null (which isn't a boolean) also would convert to 0.

  1. let a_boolean = true;
  2.  
  3. let converted_to_a_number = Number( a_boolean ); // now it is a number! it's value is 1

Automatic type coercion

JS can do some conversion without you having to explicitly tell it to.

For example, in the following code because it starts with a string (a_string), when it gets to the + a_number it will convert a_number to its string value.

  1. let a_string = "Hello, world ";
  2. let a_number = 1234;
  3.  
  4. console.log(a_string + a_number);
Hello, world 1234
webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to convert between types in JS?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the JavaScript language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to see all of the options for an artisan command?

What is the difference between ( for ... in ) and ( for ... of ) in javascript?

How to find an item by Primary Key in Eloquent, or throw a ModelNotFoundException exception if it isn't found?

How to set the Expires header in .htaccess

How to sort an array in javascript?

How to remove .php from the end of URLs with .htaccess

How to add the default charset to UTF-8 in .htaccess

When is __destruct called in PHP?

How to replace whitespace with a single space

How to make multiple routes share the same URL structure or URL parameters