JavaScript Greater than operation without type conversion

How would I modify this to be true without using &&(which is messy)?

7 > '3' // False

Possible there is no shortcut? No true === equivalent for greater than/less than operations?

How about:

console.log(7 > Number('3'))//true


I was looking for a way to make the statement false, currently it is true because Java automatically does type conversion. I wanted to find a way to stop this. If there isn’t an easy way than that makes sense.

I’m confused. Are you talking about Java or JavaScript?

Hi, sorry I meant javascript

So you wanna return false if two parts of the expression have different types?

Sorry let me clarify.

7 == '7' // True
7 === '7' // False


9 > '7' // True
9 >= '7' // True, But I want it to work like the line below
9 >= '7' && typeof 9 === typeof '7' // False, which is what I want to happen in the line above, but I don't know if that exists.

Why do you care about whether or not you use &&? If the code works, what is the problem?

I don’t, my question was just whether there was an easier way of writing it.

Just to be clear what do you want the above expression to be? True or False?

This is actually very interesting because there’s no such thing as >==. If you found a logic to this, I suggest going with it as I don’t think there’s a common approach to this. If there is, it would be nice to know myself.

1 Like

So you want some kind of <== and >== to exist?(i have no idea for what purpose tho)
Never saw anything like that.
You can browse through list of operators:

i am having hard time trying to answer the question ‘why would i need anything like this’.

No reason, just trying to have a complete understanding of the language as I learn it.

xynoan explained what I wanted exactly. Sorry I didnt make myself very clear.

No worries. If you will run into situations where you will need some >==, share it please.

Just a note on evaluation. It would be better to write it:

typeof 9 === typeof '7' && `9 >= '7'

The reason is if they are not the same type, there is no reason to even make the second comparison. It would produce false faster this way when it needs to.

1 Like