JavaScript Introduction | Types | Type Conversion

Type Conversion

JavaScript is a language with very flexible type checking.
JavaScript variables do not have a fixed type, and you can assign a value of another type to the same variable again.

var num = 20; // Number type 20
num = "twenty"; // String type "twenty"
var num;      // You can assign to a variable several times, but redeclaration is ignored.

Implicit Type Conversion

When a value of another type appears where JavaScript expects a specific type, JavaScript automatically converts and uses that value.
In other words, even if a number appears where a string value is expected, JavaScript automatically converts the number to a string.

10 + "string"; // number 10 is converted to a string for string concatenation.
"3" * "5";     // both strings are converted to numbers for multiplication.
1 - "string";  // NaN

In the third example above, the string must be converted to a number for subtraction, but unlike the string in the second example, it cannot be converted to a number.
Therefore, because JavaScript cannot automatically convert the type in a meaningful way, it returns the value NaN.

NaN stands for Not a Number, and it means an undefined or unrepresentable value.
NaN is a read-only value of the Number type that is returned when 0 is divided by 0 or when an arithmetic operation is attempted with an operand that cannot be converted to a number.

Explicit Type Conversion

JavaScript often uses implicit type conversion, but it also provides ways to convert types explicitly.
The global functions provided by JavaScript for explicit type conversion are as follows.

  1. Number()
  2. String()
  3. Boolean()
  4. Object()
  5. parseInt()
  6. parseFloat()
Number("10"); // number 10
String(true); // string "true"
Boolean(0);   // boolean false
Object(3);    // same result as new Number(3), the number 3

Converting Numbers to Strings

The simplest way to convert a number to a string is to use the String() function. You can also use the toString() method, which is available on values of every type except null and undefined.

The Number object provides the following methods for converting numbers to strings.

  1. toExponential()
  2. toFixed()
  3. toPrecision()
Method Description
toExponential() Converts a number to a string using exponential notation, with one digit in the integer part and the specified number of digits in the decimal part.
toFixed() Converts a number to a string using the specified number of digits in the decimal part.
toPrecision() Converts a number to a string using the specified number of significant digits.

A method is a property whose value is a function.

Converting Boolean Values to Strings

You can convert a boolean value to a string by using the String() function or the toString() method.

String(true);     // string "true"
false.toString(); // string "false"

Converting Dates to Strings or Numbers

You can convert a date to a string by using the String() function or the toString() method. In JavaScript, the Date object is the only type that can be converted to both a string and a number.

The Date object provides the following methods for converting dates to numbers.

  1. getDate()
  2. getDay()
  3. getFullYear()
  4. getMonth()
  5. getTime()
  6. getHours()
  7. getMinutes()
  8. getSeconds()
  9. getMilliseconds()
Method Description
getDate() Returns the day of the month as a number. (1 to 31)
getDay() Returns the day of the week as a number. (Sunday: 0 to Saturday: 6)
getFullYear() Returns the year as a four-digit number.
getMonth() Returns the month as a number. (January: 0 to December: 11)
getTime() Returns the time from January 1, 1970 to the current time as a number of milliseconds.
getHours() Returns the hour as a number. (0 to 23)
getMinutes() Returns the minutes as a number. (0 to 59)
getSeconds() Returns the seconds as a number. (0 to 59)
getMilliseconds() Returns the milliseconds part of the seconds as a number. (0 to 999)
String(Date());        // Mon May 16 2016 19:35:25 GMT+0900
Date().toString();     // Mon May 16 2016 19:35:25 GMT+0900
var date = new Date(); // create a Date object
date.getFullYear();    // 2016
date.getTime();        // 1463394925632 -> returns the time from January 1, 1970 to now in milliseconds.

Converting Strings to Numbers

The simplest way to convert a string to a number is to use the Number() function.

JavaScript also provides two global functions for converting strings to numbers.

  1. parseInt()
  2. parseFloat()
Method Description
parseInt() Parses a string and returns an integer in a specified radix.
parseFloat() Parses a string and returns a floating-point number.

Converting Boolean Values to Numbers

You can convert a boolean value to a number by using the Number() function.

Number(true);  // number 1
Number(false); // number 0