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 questions
Answer options
A: isNaN
B: parseInt
C: parseFloat Correct answer
D: toUpperCase
Correct Answer:
C. parseFloat
Explanation

The 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.

Why Incorrect

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.

References

- 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

Tests
Convert strings to numbers in JavaScript
Difficulty
Easy

Key idea: parseFloat() parses a string and returns a floating-point number, keeping the fractional part.

How to reach the answer

  1. The variable n2 holds the string "20.39".
  2. The requirement is to keep the decimal places, so an integer-only conversion is ruled out.
  3. parseInt would return 20 and drop .39.
  4. parseFloat("20.39") returns 20.39, which meets the requirement.

Each option

  • AIncorrect. isNaN only tests whether a value is Not-a-Number and returns a boolean; it converts nothing.
  • BIncorrect. parseInt returns an integer, so the decimal portion .39 is lost.
  • CCorrect. parseFloat returns a floating-point number, so "20.39" becomes 20.39.
  • DIncorrect. toUpperCase is a string method that changes letter case; it returns a string, not a number.

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.

Start the free practice test

More questions

Other WEB-DEVELOPMENT-APPLICATIONS practice questions

Scroll to Top