0

Why "12" > "2" return false??

21st Jun 2023, 1:19 AM
Hariharan
Hariharan - avatar
11 Answers
+ 16
If you compare strings, the comparison is character by character. And in this case 2 is greater than 1
21st Jun 2023, 1:34 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 3
Codenath You (probably) got a point right there!
21st Jun 2023, 4:13 AM
Dragon RB
Dragon RB - avatar
+ 3
javascript is quirky though... it allows console.log("12">"2") console.log("12">2) console.log(12>"2") console.log(12>2) things you can't do in other languages.So you have to be extra careful.
21st Jun 2023, 6:22 AM
Bob_Li
Bob_Li - avatar
+ 3
Bob_Li ya it will will simply an error if we compare a string with a number , i don't know much about js but in c++ according to my knowledge it will give error too
22nd Jun 2023, 4:52 PM
Alhaaz
Alhaaz - avatar
+ 2
This is because you're comparing string..
21st Jun 2023, 2:37 AM
Dragon RB
Dragon RB - avatar
+ 2
Hariharan here , both 12 and 2 are string if you convert them int int the output will be true (1)
22nd Jun 2023, 2:40 PM
Alhaaz
Alhaaz - avatar
+ 2
Alhaaj or even just one of them. If not set on strict mode, javascript takes the liberty of converting the other string to number, which while convenient in some cases, is generally considered a sloppy behavior. Strict mode would throw an error if you compared numbers and strings.
22nd Jun 2023, 3:51 PM
Bob_Li
Bob_Li - avatar
+ 2
Comparison between strings always compare their first character then second then on. you put quotation marks in value hence it means it is a string, so please remove, if you want to make it a integer. If liked my answer please upvote.
23rd Jun 2023, 12:58 AM
ARYAN JAT👑💯💯
ARYAN JAT👑💯💯 - avatar
+ 1
Yo guys?
22nd Jun 2023, 5:49 PM
Sudarshan Kumar
Sudarshan Kumar - avatar
+ 1
Do you mean "string" > "string"? Of course it will be return false, Please remove " symbol from your integer
22nd Jun 2023, 11:45 PM
Rendix Alextian
Rendix Alextian - avatar
0
Thank you all â˜ș
21st Jun 2023, 6:16 AM
Hariharan
Hariharan - avatar