I have two strings: 50% and 60%. I'm trying to compare the two strings and figure out that 60% is bigger than 50%. What would be the easiest and most clean way to compare the two strings? I can always remove the % sign, convert the strings to ints and compare them. But is there a better way?
You can use parseInt to parse only the numeric part.
let x = "50%";
let y = "60%";
parseInt(x)>parseInt(y)?console.log(`${x} is greater than ${y}`):console.log(`${y} is greater than ${x}`);
Console: 60% is greater than 50%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With