JavaScript Type Conversion
- Converting Strings to Numbers
- Converting Numbers to Strings
- Converting Dates to Numbers
- Converting Numbers to Dates
- Converting Booleans to Numbers
- Converting Numbers to Booleans
Converting Strings to Numbers
In JavaScript, you can convert strings to numbers using various methods. Here are some common ways to do it:
- Using parseInt() and parseFloat():
- Using the Unary Plus Operator (+):
- Using Number() constructor:
- Using the *1 or /1 trick:
Converting Numbers to Strings
In JavaScript, you can convert numbers to strings using various methods. Here are some common ways to do it:
- Using the String() function:
- Using the toString() method:
- Using string concatenation:
- Using template literals:
Converting Dates to Numbers
Converting dates to numbers in JavaScript typically involves obtaining the timestamp representation of the date. A timestamp represents the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC (the Unix Epoch).
Here's how you can convert dates to numbers:
- Using the getTime() Method:
Converting Dates to Strings
In JavaScript, you can convert dates to strings using various methods to represent the date in a human-readable format. Here are some common ways to do it:
Method | Description |
---|---|
getDate() | Get the day as a number (1-31) |
getDay() | Get the weekday a number (0-6) |
getFullYear() | Get the four digit year (yyyy) |
getHours() | Get the hour (0-23) |
getSeconds() | Get the seconds (0-59) |
getMinutes() | Get the minutes (0-59) |
getMilliseconds() | Get the milliseconds (0-999) |
getTime() | Get the time (milliseconds since January 1, 1970) |
getMonth() | Get the month (0-11) |
Converting Booleans to Numbers
Converting Booleans to Numbers in JavaScript is straightforward because JavaScript automatically coerces Boolean values to numbers when they are used in numeric contexts. Here's how you can convert Booleans to numbers:
Example
let boolTrue = true; let boolFalse = false; let numTrue = boolTrue * 1; // 1 let numFalse = boolFalse * 1; // 0
Converting Booleans to Strings
In JavaScript, you can convert Booleans to strings using various methods. Here are some common ways to do it:
Example
let boolTrue = true; let boolFalse = false; let strTrue = boolTrue.toString(); // "true" let strFalse = boolFalse.toString(); // "false"
Basic Structure
Automatic Type Conversion
Automatic type conversion, also known as type coercion, is a feature in JavaScript where the runtime automatically converts values from one data type to another when needed.
This process occurs implicitly, without explicit instructions from the programmer. JavaScript employs type coercion in various scenarios, such as arithmetic operations, comparisons, and concatenations.
Arithmetic Operations:
Example
let num = 5; let str = "10"; let result = num + str; // result is "510"
Comparisons:
Example
let num = 10; let str = "10"; console.log(num == str); // true
Concatenation:
Example
let num = 10; let str = "The value is: "; let result = str + num; // result is "The value is: 10"