Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: use of greaterthan with strings

Tags:

javascript

I am confused in understanding this "9">"099" returns true and "9">"99" returns false(9 is just an example, it is happening like "x">"xabc" returns false and "x">"abc" returns true, where a is smaller than x but abc is greater tha x and a,b,c,x are numbers). Thanks in advance.

like image 384
dush Avatar asked Feb 21 '13 12:02

dush


People also ask

How do you check if a string is greater than?

Java String compareTo() Method The method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).

How do you use greater than in JavaScript?

JavaScript Greater-than or Equal-to (<=) Comparison Operator is used to check if the first operand is greater than or equal to the second operand. Greater-than or Equal-to operator returns a boolean value.

How do you compare strings in JavaScript?

In JavaScript, strings can be compared based on their “value”, “characters case”, “length”, or “alphabetically” order: To compare strings based on their values and characters case, use the “Strict Equality Operator (===)”.


2 Answers

it is happening like "x">"xabc" returns true and "x">"abc" returns false, where a is smaller than x but abc is greater tha x and a,b,c,x are numbers)

Yes (except that "abc" is not greater than "x", and those are characters, not numbers). It's a textual comparison, the numbers in the strings are not converted to numbers before comparing them. So the comparison works character by character, stopping the first time it finds a difference. In your "99" > "099" case, since the "9" in the left-hand string is greater than the "0" in the right-hand string, the result is determined by just the first character. (The same thing happens in "x" > "abc", because the "x" is greater than the "a".)

Note that there's a very big difference between:

console.log("99" > "099"); // "true"

and

console.log(99 > "099"); // "false"

In the latter case, because one of the operands is a number, the JavaScript engine will try to convert the other operand into a number and then compare the numbers. In the former case, because both operands are strings, it won't, it'll do a textual comparison.

Side note: Be careful of numeric strings starting with 0 (like "099"). If they end up being implicitly converted to a number, they may get treated as octal (base 8) depending on the JavaScript engine being used.

like image 180
T.J. Crowder Avatar answered Nov 11 '22 18:11

T.J. Crowder


x>abc is returns true because x has ascii value more than a

but x>xbc is false though x has ascii value equal to x, but for the second character in both the string, the first string x has only one character, while the second string xbc has b as second character..

in x>abc

x is compared with a, when in first character position x is greater than a, hence it returns true

in second example x>xbc, first x is compared with x, which returns 0, since both have same ascii value..

but then b is compared with "" since "x" has only "x" while "xbc" has "b" as second charatcer.. being in existence hence xb is greater than x

so it returns false

like image 32
Saswat Avatar answered Nov 11 '22 17:11

Saswat