Search code examples
javascriptfromcharcode

Purpose of -1 in String.fromCharCode(k)


I have the following snippet of code :

return (AllowableCharacters.indexOf(String.fromCharCode(k)) != -1);

Now, I don't quite get the usage of -1 in this script.

The way the script reads is as follows:

first String.fromCharCode(k) != -1 is executed (k is a key code , I am dynamically getting from some other script).

Then I get the indexof(String.fromCharCode(k) != -1) from AllowableCharacters.

Which is a string something like this:

AllowableCharacters = "abc" ; 

I also understand that if the indexof can't find a value in the above string it return -1.

Coming back to my question, however, why the -1 in the script?

EDIT ::

To make my question simpler , how would you read the following line :

String.fromCharCode(k))!=-1

in simple plain english .

EDIT 2::

ok so i just read guffa's answer and made a random script to check , heres the script :

 var store = "abcdefgpS";

        function check_index(){
            console.log(store.indexOf(String.fromCharCode(83))); 
        };

in the above function the !-1 is excluded , so on console.log , if a match is found we get the indexOf where the character was found and well if a match is not found we get -1 .

NOW , now thats not what we want , what we want is a "tell me if the value is there"(return true) or "tell me if the value is not there"(return false).

so what we do is we change the above script to :

 var store = "abcdefgpS";

        function check_index(){
            console.log(store.indexOf(String.fromCharCode(83)) !-1); 
        };

which, gives ur true or false values .

now how does :

return (store.indexOf(String.fromCharCode(83)) !-1)

read as :

if (store.indexOf(String.fromCharCode(83)) !-1){
      return true;
}else { return false; } 

I don't see the if statement in . return (store.indexOf(String.fromCharCode(83)) !-1);

Thank you,

Alexander


Solution

  • You got the order of execution wrong.

    First this expression is evaluated: String.fromCharCode(k).

    Lets assume that the result is the string "b". That is used in the expression: AllowableCharacters.indexOf("b").

    Lets assume that the characer is found at the second character in the string, which has index 1, so the result is 1. That is used in the expression 1 != -1.

    As the 1 is not equal to -1, the result is true, which is returned.

    In short, the -1 is compared with the result from the indexOf method, as the methods returns -1 when it doesn't find anything.


    It's the comparison operator != that causes the value to be true or false. It compares the values, and the result depends on whether they are equal or not. An if statement isn't needed to turn the result into true or false, that's already the value of the comparison expression.

    Sometimes you see code like:

    if (AllowableCharacters.indexOf(String.fromCharCode(k)) != -1) {
      return true;
    } else {
      return false;
    }
    

    The if statement is superflous in cases like that. The expression in the if statement is already true or false, so it can be returned directly:

    return AllowableCharacters.indexOf(String.fromCharCode(k)) != -1;