WEB-DEVELOPMENT-APPLICATIONS practice question 1 of 25
Given the following JavaScript code: ```javascript var n1 = 10; var n2 = "20.39"; ``` Which function should a developer use to convert the content of…
Choose your answer, then check it against the explanation and the full breakdown below.
Question breakdown
Key idea: parseFloat() parses a string and returns a floating-point number, keeping the fractional part.
How to reach the answer
- The variable n2 holds the string "20.39".
- The requirement is to keep the decimal places, so an integer-only conversion is ruled out.
- parseInt would return 20 and drop .39.
- parseFloat("20.39") returns 20.39, which meets the requirement.
Each option
Common trap: parseInt looks like the generic way to turn a string into a number, but it discards everything after the decimal point.
In practice: Values read from form inputs or query strings arrive as strings. Prices, measurements, and similar values need parseFloat (or Number) before you do arithmetic on them.
Remember: Use parseFloat to keep decimals and parseInt when you want a whole number.
Drafted with AI assistance and checked against the answer key.
Keep practicing
Take the free WEB-DEVELOPMENT-APPLICATIONS practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other WEB-DEVELOPMENT-APPLICATIONS practice questions
- Question 2Which attribute displays help text in an input field without specifying an actual value f…
- Question 4Which element attaches an external CSS document to a web page?
- Question 5Which HTML tag should a developer use to create a drop-down list?
- Question 6Which method allows the end user to enter text as an answer to a question as the page loa…
- Question 8Which layout design is easiest to manage on a variety of devices?
- Question 9Which structure tag should a developer use to place contact information on a web page?
