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 the variable `n2` from a string to a number while keeping the decimal places?
Full set: 131 questionsThe parseFloat() function is the correct choice because it parses a string argument and returns a floating-point number. Unlike parseInt(), it recognizes and preserves the decimal part of a number represented in a string. In this case, parseFloat("20.39") will correctly evaluate to the number 20.39. This function stops parsing when it encounters a character that is not part of the number format, which makes it robust for this specific task.
A. isNaN is a function that checks if a value is "Not-a-Number"; it does not perform any type conversion.
B. parseInt converts a string to an integer, truncating any decimal places. It would incorrectly return 20.
D. toUpperCase is a string method that converts a string to uppercase letters; it is irrelevant for number conversion.
- Mozilla Developer Network (MDN). (2023). parseFloat(). Global Objects. Retrieved from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/GlobalObjects/parseFloat (See the main description and first example).
- Haverbeke, M. (2018). Eloquent JavaScript (3rd ed.). No Starch Press. Chapter 3: Functions, Section "Optional Arguments". (Discusses number conversion functions).
- MIT OpenCourseWare. (2016). 6.170 Software Studio, Lecture 10: JavaScript. Retrieved from https://ocw.mit.edu/courses/6-170-software-studio-spring-2016/resources/mit6170s16lec10/ (Covers JavaScript's type system and conversion functions).
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?
