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
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 :)
3 comments
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?
Good question!
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
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’).
3 comments
Could you explain it to a five year old? Is it possible to understand this without knowing JavaScript?
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.
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
Answer 519578e4935386ce2e0001b6
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
1 comments
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