JSFiddle - React, Tailwind, and code Playground

by Pankaj Kargirwar

JavaScript

const myObject = {};
const propertyName = 'dynamicProperty';

// If the property exists, increment by 1; if not, create and assign 0
myObject[propertyName] = (myObject.hasOwnProperty(propertyName) ? myObject[propertyName] : 0) + 1;

console.log(myObject[propertyName]); // Output: 0 (if the property didn't exist before)
                                      //         or 1 (if the property existed and was initially 0)
                                      //         or the incremented value otherwise