In this article, we will cover how to set all properties inside a JavaScript object to false. To achieve this, we need to generate an array from the object keys using the `Object.keys()` method and then iterate over the generated array and change the corresponding value for each key to `false`.
Set All Properties Inside A JavaScript Object To False
First, let’s start by creating a new object and initializing it with values –
const obj = {
key1: "value1",
key2: "value2",
key3: true
};
Then, we will create an array with the object keys using the `Object.keys()` method –
const keys = Object.keys(obj);
If we print the `keys` array on the console, we will get all the keys in the `obj` object –
console.log(keys);

Now, we will loop over the array of keys and for each key, we access its corresponding value inside the object and set it to `false`. We will use a `for..each` loop to achieve this –
keys.forEach((key) => {
obj[key] = false;
});
If we print `obj` on the console now, we will get our object printed and we will see that all the values inside of the object are set to `false`.
console.log(obj);

Finally, we can wrap our logic into a function so that we can apply it on any object we like.
const setKeysFalse = (object) => {
const keys = Object.keys(object);
keys.forEach((key) => {
object[key] = false;
});
return object;
};
If we call `setKeysFalse` function inside a `console.log()` statement, we will get back our object with all the values set to false.
console.log(setKeysFalse(obj));
