Posted At: Jul 14, 2024 - 174 Views
![JavaScript and TypeScript Type Conversions 2024](https://plambindia.com/storage/blogs/type-conversion.png)
Type Conversions in JavaScript and TypeScript
In JavaScript and TypeScript, values can be converted from one type to another automatically or explicitly.
String Conversion
String conversion occurs when you need to represent a value as a string.
Example in JavaScript:
let num = 42;
console.log(typeof num); // number
num = String(num); // now num is a string "42"
console.log(typeof num); // string
Example in TypeScript:
let num: any = 42;
console.log(typeof num); // number
num = String(num); // now num is a string "42"
console.log(typeof num); // string
- In both examples, the number
42
is converted to the string"42"
.
Numeric Conversion
Numeric conversion happens automatically in mathematical operations, but you can also explicitly convert values to numbers.
Example in JavaScript:
let bool = false;
console.log(typeof bool); // boolean
let num = Number(bool); // becomes a number 0
console.log(typeof num); // number
Example in TypeScript:
let bool: boolean = false;
console.log(typeof bool); // boolean
let num: number = Number(bool); // becomes a number 0
console.log(typeof num); // number
- In these examples, the boolean
false
is converted to the number0
. - If the string cannot be converted to a valid number, the result is
NaN
.
Example in JavaScript:
let value = Number("not a number");
console.log(value); // NaN
Example in TypeScript:
let value: number = Number("not a number");
console.log(value); // NaN
Numeric Conversion Rules:
Value | Becomes |
undefined | NaN |
null | 0 |
true/false | 1/0 |
string | The number read from the string or NaN if conversion fails |
Examples:
console.log(Number(" 456 ")); // 456
console.log(Number("456abc")); // NaN
console.log(Number(false)); // 0
console.log(Number(true)); // 1
Boolean Conversion
- Boolean conversion is straightforward. Values that are "empty" become
false
, and all other values becometrue
.
Example in JavaScript:
console.log(Boolean(123)); // true
console.log(Boolean(0)); // false
console.log(Boolean("JavaScript")); // true
console.log(Boolean("")); // false
Example in TypeScript:
console.log(Boolean(123)); // true
console.log(Boolean(0)); // false
console.log(Boolean("JavaScript")); // true
console.log(Boolean("")); // false
- Even the string
"0"
and strings with spaces aretrue
.
Examples:
console.log(Boolean("0")); // true
console.log(Boolean(" ")); // true
Summary
The three primary type conversions in JavaScript and TypeScript are:
String Conversion: Can be done with String(value)
.
- String Conversion: Can be done with
String(value)
. - Examples:
String(42) // "42"
,String(null) // "null"
Numeric Conversion: Can be done with Number(value)
.
Rules:
undefined
becomesNaN
null
becomes0
true
becomes1
,false
becomes0
- Strings are converted to numbers if possible, otherwise
NaN
Boolean Conversion: Can be done with Boolean(value)
.
Rules:
0
,null
,undefined
,NaN
,""
becomefalse
- All other values become
true
Most of these rules are intuitive, but some can be tricky:
undefined
converts toNaN
in numeric conversion."0"
and strings with spaces convert totrue
in boolean conversion.
By understanding these conversions, you can handle data types more effectively in your JavaScript and TypeScript programs.
Call to Action
How do you think AI will shape the future of technology? Share your thoughts in the comments below. For more insights into the latest tech trends, visit our website PlambIndia and stay updated with our blog.
Follow Us
Stay updated with our latest projects and insights by following us on social media:
- LinkedIn: PlambIndia Software Solutions
- PlambIndia: Plambindia Software Solution.
- WhatsApp Number: +91 87663 78125
- Email: contact@plambIndia.com , kuldeeptrivedi456@gmail.com
Become a Client
Explore our diverse range of services and find the perfect solution tailored to your needs. Select a category below to learn more about how we can help transform your business.
Kuldeep Trivedi
plot no 1 / 2 suraj mall compound mal compound
+918766378125
contact@plambindia.com