JavaScript With Statement Explained – A Deep Dive
JavaScript’s built-in with statement specifies the default object for the given property and gives us a shorthand for writing long object references. More precisely, it adds the given object to the head of the scope chain.
Note: Use of the with statement is discouraged. It can lead to strange bugs. That said, it’s important to understand how it works because it may exist in legacy code.
With Function Syntax
Usage
with (expression) {
statement;
}
expression: An expression that evaluates to an object that will become the default object inside its scope.
statement: Code that will run with the evaluated expression as the default object
Example
let car = { color: "red" };
with (car) {
console.log(color);
}
// prints 'red'
As you can see, the car object becomes the default object in the scope. The object’s properties become available without using the dot (.) operator.
If the variable already exists in the parent scope, it will be overwritten:
let color = "blue";
let car = { color: "red" };
with (car) {
console.log(color);
}
// prints 'red'
Why Shouldn’t I Use ‘With’?

Using
withis not recommended, and is forbidden in ECMAScript 5 strict mode. The recommended alternative is to assign the object whose properties you want to access to a temporary variable.
While using with can make long pieces of code easier to read due to the removal of long accessor paths, the danger of potential bugs due to ambiguity isn’t worth the convenience.
with (car.make.model) {
let size = width * height * length;
}
// vs
let size = car.make.model.width * car.make.model.height * car.make.model.length;
There’s an argument to be made that code will be smaller and easier to send to the browser by using ‘with’ statements. While true, the gains are negligible, especially when compared to what code minifiers do.
Related Articles
JavaScript Map Function Explained - A Deep Dive
Jan 12, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
The built-in JavaScript map function returns a new array, where each element in the new array is the result of the corresponding element in the old array after being passed through a callback function. Later in the article, we’ll do a deep dive into some more advanced concepts regarding the map function and its uses.
Which Method of Iteration in JavaScript is Fastest?
Nov 08, 2019 by Lane Wagner - Boot.dev co-founder and backend engineer
There are many ways to traverse an array in Javascript. In this benchmark, we will look at five different ways and the pros and cons of each. Keep in mind that these benchmarks were run in a Chrome browser on Codepen.io. Results will vary by browser/interpreter.
Singletons in ES6 - The Good, The Bad, The Ugly
Nov 04, 2019 by Lane Wagner - Boot.dev co-founder and backend engineer
Singletons are fairly controversial as far as I can tell, especially in JavaScript programming. Let’s take a look at what they are, when to (maybe) use them, and when not to.
How to Recursively Traverse JSON Objects
Sep 22, 2019 by Lane Wagner - Boot.dev co-founder and backend engineer
I’ve found that it’s pretty rare that I need recursion in application code, but every once in a while I need to write a function that operates on a tree of unknown depth, such as a JSON object, and that’s often best solved recursively. Even though recursion is rare, it is important to recognize when a problem is best solved recursively so that we can implement a good solution when the time comes.