LearnFrenzy
Sign in Take a free test

Event Loop · Find the Error · Question 5

Error: a string passed to setTimeout Answer, explanation & discussion

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

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

main.js
console.log("scheduling");
setTimeout("console.log('reminder')", 500);
  • A It prints reminder after 500 ms in Node.js
  • B SyntaxError: strings cannot contain code
  • C TypeError in Node.js: setTimeout needs a function (browsers eval the string, which is unsafe) – pass () => console.log("reminder") Correct
  • D It prints scheduling twice
Correct Answer: Option C

The browser's string form works like eval and is blocked by most Content Security Policies; Node.js does not support it at all.

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