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.

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?
Answer options
Question 1 of 25

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