Vanta Logo
SPONSOR
Automate SOC 2 & ISO 27001 compliance with Vanta. Get $1,000 off.
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.

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.


More Articles You Might Enjoy

If you enjoyed this article, you might find these related pieces interesting as well. If you like what I have to say, please check out the sponsors who are supporting me. Much appreciated!

Javascript
6 min read

AggregateError in JavaScript

AggregateError helps you handle multiple errors at once in JavaScript. This makes your code easier to manage and more reliable.

Sep 2, 2024
Read article
Javascript
7 min read

WeakRefs in JavaScript: Explained In Simple Terms

Understanding how WeakRef helps manage memory in JavaScript

Jan 7, 2025
Read article
Javascript
4 min read

Embrace Intermediate Variables and Early Returns in JavaScript

Early returns and intermediate variables make your code easier to reason about

Aug 30, 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
Javascript
3 min read

JavaScript's &&= Operator: Understanding Logical AND Assignment

Use the &&= operator to safely update truthy values while preserving falsy states

Nov 5, 2024
Read article
Javascript
7 min read

JavaScript Truthy and Falsy: A Deep Dive

Grasp JavaScript's type coercion with practical examples and avoid common pitfalls

Oct 27, 2024
Read article
Javascript
7 min read

JavaScript Operators: '||' vs '&&' vs '??'

Master JavaScript logical operators with practical examples and best practices

Oct 26, 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
4 min read

Understanding Bitwise Shifts in JavaScript: << and >>

A practical guide to left and right shift operators in JavaScript

Nov 12, 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.