From the course: JavaScript: Best Practices for Functions and Classes

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Avoid with

Avoid with - JavaScript Tutorial

From the course: JavaScript: Best Practices for Functions and Classes

Start my 1-month free trial

Avoid with

- [Instructor] The with operator is one of a number of JavaScript tools that solves a real problem, but comes with important drawbacks. With is so widely avoided that it's automatically disallowed in strict mode, but why? The main reason to avoid with is that it can make it hard to understand just how your code arrived at a specific result. With executes a block of code in the context of a given object, which lets you more easily write object references. However, within the with block, it can be unclear whether a given reference will be found in the object or if it will instead reference or create a new separate variable. The start file for this video includes a with statement, and I can see right away that ESLint has flagged it. When I hover there, the message says that with is disallowed in strict mode. And if I try to execute my code, let me relaunch. Go live. And I open my console, and I get the same error in the…

Contents