πŸš€Debugging Microservices & Distributed Systems
3 min read

The Only Widely Recognized JavaScript Feature Ever Deprecated

The 'with' statement is the only feature ever deprecated in JavaScript

Deprecating features in JavaScript is rare. Only one feature in the language’s history has been officially deprecated: β€˜with’ statements.

The β€˜with’ statement was introduced early in JavaScript’s history. Its purpose was to simplify working with objects by providing a shortcut to access object properties.

const car = {
make: 'Toyota',
model: 'Corolla',
year: 2020
};
with (car) {
console.log(make); // Toyota
console.log(model); // Corolla
console.log(year); // 2020
}

The β€˜with’ statement allows you to access the properties of the car object directly, without needing to type car.make, car.model, and car.year.

Why Was with Deprecated?

The main issue was ambiguity. Because the with statement modifies the scope, it became difficult for both developers and JavaScript engines to know which variables or properties were being referred to.

What will this code print?
const car = {
make: 'Toyota'
};
function showMake() {
const make = 'Honda';
with (car) {
console.log(make);
}
}
showMake();

Will it print β€˜Toyota’ from the car object, or β€˜Honda’ from the showMake function’s scope? The ambiguity here is the problem. It could easily lead to bugs that are hard to track down.

The answer: β€˜Toyota’

The with statement extends the scope chain, so when with (car) is used, it effectively puts the properties of the car object into the scope.

In the showMake function, when console.log(make) is called inside the with (car) block, JavaScript first looks for make within the car object. Since car has a make property with the value β€˜Toyota’, it finds this first and prints β€˜Toyota’.

If the car object did not have a make property, JavaScript would then look for make in the surrounding scope, where it would find the β€˜Honda’ value. But in this case, because car has a make property, β€˜Toyota’ is printed.

Where We Stand Today (2024)

The with statement is still part of JavaScript today, but it’s considered bad practice. It can cause confusing and unpredictable behavior.

This happens because it changes the scope chain, making it hard to tell which variables are being used. This ambiguity can lead to bugs that are tough to find.

For these reasons, it’s best to avoid using with in your code.

How to Avoid Using with

Instead of using with, you should reference object properties directly. If you’re trying to shorten the syntax, consider using destructuring:

const car = {
make: 'Toyota',
model: 'Corolla',
year: 2020
};
// Destructuring assignment
const { make, model, year } = car;
console.log(make); // Toyota
console.log(model); // Corolla
console.log(year); // 2020

This code achieves a similar effect to the β€˜with’ statement but without the pitfalls. Destructuring is clear, unambiguous, and widely supported in JavaScript.

The β€˜with’ statement is often cited as the only feature ever deprecated in JavaScript, but that’s not entirely true.

While it’s one of the most prominent and well-known deprecated features, it’s not alone. Other features, such as arguments.callee in strict mode and certain uses of eval(), have also faced deprecation or strong discouragement.


Related Articles

If you enjoyed this article, you might find these related pieces interesting as well.

Recommended Engineering Resources

Here are engineering resources I've personally vetted and use. They focus on skills you'll actually need to build and scale real projects - the kind of experience that gets you hired or promoted.

Imagine where you would be in two years if you actually took the time to learn every day. A little effort consistently adds up, shaping your skills, opening doors, and building the career you envision. Start now, and future you will thank you.


This article was originally published on https://www.trevorlasn.com/blog/the-only-javascript-feature-that-was-deprecated. It was written by a human and polished using grammar tools for clarity.

Interested in a partnership? Shoot me an email at hi [at] trevorlasn.com with all relevant information.