LearnFrenzy
Sign in Take a free test

String Methods · Find the Error · Question 9

Error: padStart on a number Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const invoiceNo = 42;
console.log("INV-" + invoiceNo.padStart(5, "0"));
  • A It prints INV-00042
  • B It prints INV-42
  • C It prints INV-42000
  • D TypeError: invoiceNo.padStart is not a function – numbers have no string methods; use String(invoiceNo).padStart(5, "0") Correct
Correct Answer: Option D

padStart belongs to String.prototype. Convert first: String(invoiceNo).padStart(5, "0") gives "00042".

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