Profile image of board-jumper-JG8a3
Submitted by board-jumper-JG8a3
about 13 years

parseInt Explanation?

Can someone explain the function of parseInt in the script? From what I understand:

parseInt(key.which,10)

means that key.which is the string to be parsed, and 10 means we’re using a decimal system, but other than that I can’t really understand how everything functions/fits together. Also, is there a benefit to explicitly putting 10 as the radix parameter, as I thought that a string with any non-0/0x value defaults the radix to 10?

Answer 5168fe095c4be0457d002095

16 votes

Permalink

This might be an easier answer. parseInt() will take the key.which, which happens to be the key the user pressed, and turns it into a number. Originally, when the key is pressed, it was recognized as a string but not after parseint is done with it! The 10 is simply a way to tell the computer you want this number in decimal form, the form us humans are most comfortable with, hence 10 fingers and 10 toes.

If you’re familiar with ASCII, its converting ‘a’ to 65, the ASCII code for ‘a’! Hope this helps :)

Profile image of tywadd
Submitted by tywadd
over 12 years

3 comments

Profile image of davl3232
Submitted by davl3232
over 12 years
  • Are the parseInt() and key.which included within Javascript? or are they a part of jQuery API?

  • key.which returns the string “a” or the string “65”?

  • Is parseInt() converting the key.which from “65” to 65? or just giving away ASCII values in numbers?

Profile image of objectPro67628
Submitted by objectPro67628
almost 12 years

Good question!

Profile image of AM2W
Submitted by AM2W
over 10 years

for the second part just add alert(key.which); before the function you’ll see it returns a digit even with alert(typeof(key.which) it returns a number usin the code without parseint goes fine !

Answer 50c321222544a34030004823

3 votes

Permalink

It is required to convert the key.which to integer. This is because switch statement is sensitive to data type. E.g. if (key.which == 65) would work as expected, but if (key.which === 65) would not and so would not work:

switch (key.which) {
     case 65:

There comes the need for using parseInt, Using the radix = 10 is considered to be a good practice - we are explicitly stating which conversion we want. Leaving this to autodetect may produce unpredictable results (in the unlikely event of the key.which starting with ‘0’).

Profile image of ondrejpopelka
Submitted by ondrejpopelka
about 13 years

3 comments

Profile image of anonymous
Submitted by anonymous
almost 13 years

Could you explain it to a five year old? Is it possible to understand this without knowing JavaScript?

Profile image of MAC04498
Submitted by MAC04498
over 12 years

Haha yes please. I have finished the Javascript course but I don’t remember when it said to use specific data types or using parseInt.

Profile image of davl3232
Submitted by davl3232
over 12 years

When using three =’s (equal signs), compares the data strictly, returning false if the data type is different. When using two =’s (equal signs), javascript will automatically convert things to the same data type, and then it will compare, in this case the string “65” will be the same as the number 65, no matter the fact that one is a number, and the other is a string. parseInt() converts a given string or number, into an integer number with the base number you specify through the radix.

Answer 5342af7852f8635317002237

1 vote

Permalink

whats the purpose of .which

Profile image of mosesmwai
Submitted by mosesmwai
over 11 years

Answer 519578e4935386ce2e0001b6

0 votes

Permalink

Can someone else answer this question syntax by syntax? How were supposed to know key.which represents keyboard keys?

This link doesn’t mention anything about key.which representing keystroke keys. Is this something we just have to memorize?

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/parseInt

Profile image of scriptPlayer64686
Submitted by scriptPlayer64686
over 12 years

1 comments

Profile image of objectPro67628
Submitted by objectPro67628
almost 12 years

OMG, what means this?

string The value to parse. If string is not a string, then it is converted to one. Leading whitespace in the string is ignored.

It will convert something to an integer or to string???

Popular free courses

  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      4 Lessons
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      11 Lessons
  • Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
    • Beginner Friendly.
      6 Lessons
Explore full catalog