JavaScript Type Conversion

JavaScript Type Conversion refers to the process of converting one data type to another.

For example, converting a string to a number, or a number to a boolean.

This can be done using built-in JavaScript functions such as parseInt() and parseFloat() for converting strings to numbers, String() for converting a value to a string, and Number() for converting a value to a number.

Additionally, the unary plus + and the double negation !! operators can be used for type conversion.

How to convert Strings to Numbers

In JavaScript, there are several ways to convert strings to numbers:

  • The parseInt() function: This function takes a string as an argument and returns an integer. For example, parseInt("10") returns the number 10.
  • The parseFloat() function: This function works similar to parseInt(), but returns a floating-point number. For example, parseFloat("3.14") returns 3.14.
  • The unary plus + operator: This operator can also be used to convert a string to a number. For example, +"10" returns the number 10.
  • The Number() function: This function takes any value as an argument and returns a number. For example, Number("10") returns the number 10.
  • The ~~ (double tilde) operator: This operator can also be used to convert string to numbers. It first converts the given string to a number and then applies the bitwise NOT operator twice, which is used to truncate the decimal value and return an integer.

It's important to note that if the string cannot be converted to a number, these methods will return NaN (Not a Number).

Here are some examples of converting strings to numbers using different methods:

Using the parseInt() function:

let num1 = parseInt("10"); // returns 10
let num2 = parseInt("10.5"); // returns 10
let num3 = parseInt("10 20 30"); // returns 10

Using the parseFloat() function:

let num1 = parseFloat("3.14"); // returns 3.14
let num2 = parseFloat("-2.71828"); // returns -2.71828
let num3 = parseFloat("10.5"); // returns 10.5

Using the unary plus + operator:

let num1 = +"10"; // returns 10
let num2 = +"3.14"; // returns 3.14
let num3 = +""; // returns 0

Using the Number() function:

let num1 = Number("10"); // returns 10
let num2 = Number("3.14"); // returns 3.14
let num3 = Number(""); // returns 0

Using the ~~ (double tilde) operator:

let num1 = ~~"10"; // returns 10
let num2 = ~~"3.14"; // returns 3
let num3 = ~~""; // returns 0

Please note that these examples are for demonstration purposes only, in a real-world scenario you should validate the input before converting it to make sure that it's a valid string representation of a number.

How to convert Numbers to Strings

In JavaScript, there are several ways to convert numbers to strings:

  • The String() function: This function takes any value as an argument and returns a string. For example, String(10) returns the string "10".
  • The toString() method: This method can be called on a number and returns a string representation of that number. For example, (10).toString() returns the string "10".
  • The concatenation operator +: This operator can be used to concatenate a number with an empty string, effectively converting it to a string. For example, "" + 10 returns the string "10".
  • The template literals: This operator allows you to include expressions inside strings. For example,The number is ${10}` returns the string "The number is 10"

It's important to note that these methods will convert a number to a string representation of that number, but keep in mind that it's not the same as formatting a number, If you want to format a number you can use .toFixed() or .toPrecision()

Here's an example of each one:

Using the String() function:

let num = 10;
let str = String(num); // returns "10"

Using the toString() method:

let num = 10;
let str = num.toString(); // returns "10"

Using the concatenation operator +:

let num = 10;
let str = "" + num; // returns "10"

Using template literals:

let num = 10;
let str = `The number is ${num}`; // returns "The number is 10"

In addition, you can use libraries like numeral.js, moment.js to format numbers and dates.

How to convert Dates to Numbers

In JavaScript, there are several ways to convert a Date object to a number:

  • The getTime() method: This method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. For example, new Date().getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • The valueOf() method: This method returns the primitive value of a Date object, which is the number of milliseconds since January 1, 1970, 00:00:00 UTC. This method is equivalent to the getTime() method.
  • The unary plus + operator: You can use the unary plus operator to convert a Date object to a number. The operator calls the valueOf() method of the object, which returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Here's an example of each one:

Using the getTime() method:

let date = new Date();
let num = date.getTime(); // returns the number of milliseconds since January 1, 1970, 00:00:00 UTC

Using the valueOf() method:

let date = new Date();
let num = date.valueOf(); // returns the number of milliseconds since January 1, 1970, 00:00:00 UTC

Using the unary plus + operator:

let date = new Date();
let num = +date; // returns the number of milliseconds since January 1, 1970, 00:00:00 UTC

Please note that the timestamp returned by these methods is the number of milliseconds since January 1, 1970, 00:00:00 UTC, if you want to represent this timestamp in another format, like seconds or minutes, you will need to divide or multiply the value accordingly.

How to convert Dates to Strings

In JavaScript, there are several ways to convert a Date object to a string:

  • The toString() method: This method returns a human-readable string representation of a Date object. For example, new Date().toString() returns a string in the format "Wed Jan 20 2021 00:00:00 GMT-0500 (Eastern Standard Time)".
  • The toUTCString() method: This method returns a string representation of a Date object in UTC format. For example, new Date().toUTCString() returns a string in the format "Wed, 20 Jan 2021 05:00:00 GMT"
  • The toDateString() method: This method returns the date portion of a Date object as a human-readable string. For example, new Date().toDateString() returns a string in the format "Wed Jan 20 2021"
  • The toLocaleString() method: This method returns a string representation of the date and time in a format that is appropriate to the host environment's current locale. For example, new Date().toLocaleString() returns a string in the format "01/20/2021, 12:00:00 AM"
  • The toISOString() method: This method returns a string representation of a Date object in ISO format. For example, new Date().toISOString() returns a string in the format "2021-01-20T05:00:00.000Z"

Here's an example of each one:

Using the toString() method:

let date = new Date();
let str = date.toString(); // returns a human-readable string representation of the date

Using the toUTCString() method:

let date = new Date();
let str = date.toUTCString(); // returns a string representation of the date in UTC format

Using the toDateString() method:

let date = new Date();
let str = date.toDateString(); // returns the date portion of the date as a human-readable string

Using the toLocaleString() method:

let date = new Date();
let str = date.toLocaleString(); // returns a string representation of the date and time in the current locale

Using the toISOString() method:

let date = new Date();
let str = date.toISOString(); // returns a string representation of the date in ISO format

In addition, you can use libraries like moment.js to format dates in a more flexible way.

How to convert Booleans to Numbers

In JavaScript, there are several ways to convert a boolean value to a number:

  • The Number() function: This function takes any value as an argument and returns a number. For example, Number(true) returns the number 1, Number(false) returns the number 0.
  • The + operator: You can use the unary plus operator to convert a boolean value to a number. For example, +true returns the number 1, +false returns the number 0.
  • The bitwise NOT operator ~: You can use the bitwise NOT operator ~ to convert a boolean value to a number. For example, ~true returns the number -2, ~false returns the number -1.
  • The ternary operator ?: You can use the ternary operator ? to convert a boolean value to a number. For example, true ? 1 : 0 returns the number 1, false ? 1 : 0 returns the number 0.

Here's an example of each one:

Using the Number() function:

let bool = true;
let num = Number(bool); // returns 1

Using the + operator:

let bool = true;
let num = +bool; // returns 1

Using the bitwise NOT operator ~:

let bool = true;
let num = ~bool; // returns -2

Using the ternary operator ?:

let bool = true;
let num = bool ? 1 : 0; // returns 1

It's important to note that these methods will convert a boolean value to a number representation of that boolean, true to 1 and false to 0.

How to convert Booleans to Strings

In JavaScript, there are several ways to convert a boolean value to a string:

  • The String() function: This function takes any value as an argument and returns a string. For example, String(true) returns the string "true", String(false) returns the string "false".
  • The toString() method: This method can be called on a boolean value and returns a string representation of that value. For example, true.toString() returns the string "true", false.toString() returns the string "false".
  • The concatenation operator +: This operator can be used to concatenate a boolean value with an empty string, effectively converting it to a string. For example, "" + true returns the string "true", "" + false returns the string "false".
  • The ternary operator ?: You can use the ternary operator ? to convert a boolean value to a string. For example, true ? "true" : "false" returns the string "true", false ? "true" : "false" returns the string "false".

Here's an example of each one:

Using the String() function:

let bool = true;
let str = String(bool); // returns "true"

Using the toString() method:

let bool = true;
let str = bool.toString(); // returns "true"

Using the concatenation operator +:

let bool = true;
let str = "" + bool; // returns "true"

Using ternary operator ?:

let bool = true;
let str = bool ? "true" : "false"; // returns "true"

It's important to note that these methods will convert a boolean value to a string representation of that boolean, true to "true" and false to "false".

JS Basics