How does the Javascript '>' operator compare characters with a space?

I am trying to understand this expression:

((ch = stream.getChar()) > ' ') 

Here getChar() gets the character. How is this higher order operator if any char is greater than empty space?

Is it possible?

+7
source share
7 answers

White space has a character code. Although this doesn't seem like much, it still has value. In the same way, a character is taken from a stream. A comparison of the character codes of these values ​​is the result of the output.

+11
source

Let's take the ganger in the language specification (the algorithm itself is described here ) (note that it defines < , but the operator > just flips the resulting value).

What the operator does is try to convert both operands to primitive types with preference for numbers:

 2. a. Let py be the result of calling ToPrimitive(y, hint Number). 2. b. Let px be the result of calling ToPrimitive(x, hint Number). 

In our case, x === stream.getChar() and y === ' ' . Since both operands are already primitive strings, this leads to the original values ​​( px = x, py = y ), and we go to:

 4. Else, both px and py are Strings 

Now it checks if one of the operands is prefixes of the other, for example:

 'abc' > 'abcd' // false 'foo' > 'foobar' // false 

What matters if getChar() results in a space, since space is a prefix of itself:

 ' ' > ' ' // false 

We proceed to search for the first character in x and y , which are in the same position in the lines, but have different characters:

Let k be the smallest non-negative integer such that the character at position k inside px differs from the character at position k inside py. (There must be a k, since neither String is a prefix of another.)

(e.g. 'efg' and 'efh' , we want g and h )

The selected characters are then converted to their integer values:

 Let m be the integer that is the code unit value for the character at position k within px. Let n be the integer that is the code unit value for the character at position k within py. 

And finally, a comparison is made:

 If m < n, return true. Otherwise, return false. 

And what is it compared to space.


tl; dr It converts both arguments to their integers, and compares them.

+9
source

In Javascript, strings are compared alphabetically. These expressions are true:

  'abacus' <= 'calculator' 'abacus' < 'abate' 
+3
source

In most programming languages ​​(if not all), characters are represented by an internal number. When you do equality / more or less than it checks that you are actually checking, is the base number.

hence in JS:

 alert('c' > 'b'); // alerts true alert('a' > 'b'); // alerts false 

The space character also has a numerical representation, so the check is valid.

+2
source

[string] > [string] will compare the character according to their representative values ​​(see ASCII table )

+1
source

Characters are stored in computer memory as a number (usually bytes or two).

Each character has a unique identification number.

By checking that the character is larger than the space, you are actually taking your place in the table.

See http://en.wikipedia.org/wiki/ASCII for more details.

0
source

Look at this link, it will explain how the comparison works on JS: http://javascript.about.com/od/decisionmaking/a/des02.htm Basically, you compare the ASCII value of each character with the ASCII value of the space, which is also a character and therefore has the corresponding ASCII value.

0
source

All Articles