Up to date
Published
3 min read

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

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.


Found this article helpful? You might enjoy my free newsletter. I share dev tips and insights to help you grow your coding skills and advance your tech career.

Interested in supporting this blog in exchange for a shoutout? Get in touch.


Liked this post?

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

Javascript
6 min read

AggregateError in JavaScript

Handle multiple errors at once

Sep 2, 2024
Read article
Javascript
4 min read

Intl.DurationFormat: Format Time Durations with Locale Support

Stop writing manual duration formatting code. Instead, leverage the new powerful Intl.DateTimeFormat API for internationalized time displays

Mar 13, 2025
Read article
Javascript
4 min read

Error.isError(): A Better Way to Check Error Types in JavaScript

Why the new Error.isError() method solves important cross-realm issues and provides more reliable error identification than instanceof

May 9, 2025
Read article
Javascript
5 min read

Working with JavaScript's Scheduler API

Learn how to prioritize and control task execution in JavaScript using the new Scheduler API for better performance and user experience

Nov 26, 2024
Read article
Javascript
4 min read

JavaScript compile hints: what they are and when to use them

V8's compile hints let you control which JavaScript gets compiled immediately during page load

May 12, 2025
Read article
Javascript
3 min read

navigator.clipboard - The New Asynchronous Clipboard API in JavaScript

Copy and paste text, images, and files using the new navigator.clipboard API

Dec 7, 2024
Read article
Javascript
4 min read

What is the JavaScript Pipeline Operator |>

A deep dive into how pipeline operators can make your code more readable and maintainable

Oct 29, 2024
Read article
Javascript
3 min read

JavaScript's ??= Operator: Default Values Made Simple

A guide to using ??= in JavaScript to handle null and undefined values elegantly

Nov 5, 2024
Read article
Javascript
7 min read

JavaScript Sets and Maps: Beyond Arrays and Objects

How to handle unique values and key-value pairs properly without type coercion and performance issues

Nov 17, 2024
Read article

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.