Ümit Aparı
Ümit Aparı

Reputation: 568

javascript searching string with indexOf

I'm trying to search for a keycode in an array with array.indexOf(string); method but even though the keycode I am searching for is declared in the array it couldn't find it and returns -1 where am I mistaken?

Console Screenshot

PS: However when I try to declare the string I'm trying the search manually (like var string = "219") it does work.

PS-2: As you can see in the console I'm logging the variable tusASII to ensure variable does have the right value to search for

var tusASII = event.keyCode;
var trkarakterler = ["219","221","186","73","191","220"];
console.log(tusASII);
console.log(trkarakterler.indexOf(tusASII));

Upvotes: 0

Views: 62

Answers (1)

JCOC611
JCOC611

Reputation: 19709

There is a type mismatch. The array should contain numbers, like so:

 var trkarakterler = [219, 221, 186, 73, 191, 220];

The idea is that key codes are numbers, rather than strings.

Upvotes: 3

Related Questions