What is the difference between eval() and Function() constructor?
Short answer: Both execute dynamic code, but: eval() executes in the current scope (less safe). Function() executes in a new scope (safer). Example: eval("var a = 5"); const b = new Function("return 5;"); ⚠ Both are discouraged due to security and performance issues. Bootstrap Interview Questions
Example code
Both execute dynamic code, but: eval() executes in the current scope (less safe). Function() executes in a new scope (safer). Example: eval("var a = 5");
const b = new Function("return 5;"); ⚠ Both are discouraged due to security and performance issues. Bootstrap Interview Questions
Real-world example (ShopNest)
In ShopNest’s cart UI, a click handler closes over productId. Use let in loops so each button keeps the correct id.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png