0

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));

2
  • 1
    keyCode is not a string, it's a number. Commented Apr 10, 2017 at 19:47
  • console.log(trkarakterler.indexOf(tusASII + '')); But is better if you user Number values instead... :) Commented Apr 10, 2017 at 20:09

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Or, convert the search term to a string.
To be more accurate, there are no integers in JavaScript. The variable type is number.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.