What is WITH for in JavaScript

February 17, 2022

What is WITH for in JavaScript

In this article we will learn in detail what the WITH in JavaScript is for and when we should or should not use this instruction in our projects.

What is WITH for in JavaScript cover

Hey programmer, how are you? Let’s learn more about JavaScript!

Using WITH is very simple: when we need to access several properties of the object, we declare it in a WITH function.

Then the accessibility of the object will be simpler

Let’s see an example

let person = {
name: 'Matheus',
age: 29,
job: 'Programmer'

with(person) {
console.log("His name is " + name + " and the is " + age + " years old.");

With this expression we have the following result:

His name is Matheus and the has 29 years old.

We then use WITH to have quick accessibility to several properties and form our string


The WITH can generate an ambiguity problem between properties of an object and variables of our software

That is, if we have a variable with the name of test and a property with the name of test

As the program progresses, the use of both starts to become ambiguous for code debugging and making maintenance difficult.

So be careful with it!


In this article we learned  what the WITH in JavaScript is for and how to use it

Briefly: it serves to access several members of an object in a faster way

But as we have also seen, it is easy to create an ambiguity between property names and software variables, we must be careful

Want to learn more about JavaScript? Click here!

Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x