LearnFrenzy
Sign in Take a free test

Data Types · Find the Error · Question 4

Error: calling a method on a number literal Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
console.log(5.toFixed(2));
  • A SyntaxError: the dot after 5 is read as a decimal point – write (5).toFixed(2) or 5..toFixed(2) Correct
  • B It prints 5.00
  • C TypeError: 5 has no methods
  • D It prints 5
Correct Answer: Option A

"5." is the start of a decimal number, so toFixed is unexpected. Parentheses, a space before the dot (5 .toFixed(2)) or a second dot fix it; a variable (n.toFixed(2)) has no such problem.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links