Can you use ternary operations for type conversion in JavaScript?
Here’s an example of using a ternary operation to convert a value to a different type:
let input = "42";
let number = typeof input === "string" ? parseInt(input) : input;
console.log(number); // Output: 42 (as a number)
console.log(typeof number); // Output: number
In the above example, we convert the value of input
, which is initially a string, into a number by using the parseInt()
function. The ternary operator checks if the type of input
is a string and if true, it converts it to a number using parseInt()
. If the type is not a string, it leaves the value as is.
Ternary operations can be a convenient way to handle type conversions in certain situations. However, it’s important to note that they should be used with caution as they can make the code less readable if overused or used in complex conditions.
#JavaScript #TypeConversion