Skip to main content

Command Palette

Search for a command to run...

Set Variable As A Key Name In JavaScript Object

Published
โ€ขView as Markdown
Set Variable As A Key Name In JavaScript Object
C

CapsCode: Empowering Innovation Through Education and Technology

CapsCode is a pioneering edtech and web development company committed to transforming the lives of students and businesses alike. Founded with the vision of creating a brighter, tech-driven future, CapsCode empowers students by providing world-class coding education while enabling businesses to achieve their goals with cutting-edge IT solutions.

For Students At CapsCode, we believe that coding is the language of tomorrow. Through engaging and industry-relevant courses, personalized mentoring, and hands-on workshops, we help students develop: Problem-solving skills: Preparing them for real-world challenges. Technical expertise: Equipping them with practical knowledge of coding and web development. Confidence in technology: Making them future-ready for careers in IT and beyond.

We foster an environment where students not only learn to code but also learn to innovate, collaborate, and think critically.

For Businesses CapsCode offers bespoke IT solutions tailored to the unique needs of businesses. From responsive web development to advanced digital marketing strategies, our services are designed to help companies grow their online presence and streamline their operations. Our key offerings include: Custom website and software development E-commerce platform creation Lead generation and digital marketing Professional email and Google Business setup

We act as partners in innovation, helping businesses leverage technology to reach new heights.

Our Mission Our mission is to empower individuals and organizations by bridging the gap between technology and accessibility. We aim to inspire a new generation of coders while helping businesses thrive in a competitive digital landscape.

Why Choose CapsCode? Over 9 years of IT industry expertise. A blend of education and professional IT services under one roof. A commitment to fostering growth, innovation, and success.

Join us in shaping the futureโ€”whether you're a student eager to unlock your potential or a business ready to embrace the possibilities of technology.

Learn. Build. Grow. With CapsCode.

Hello Devs,

In this blog you are going to learn a very important and useful concept of JavaScript which you will definitely going to use or might have used in Production application, i.e.: How to make any variable name as key of an object in JS.

I know you might be thinking when and in which scenario you will have to use the variable name as a key name in JS. Let me take your next 2 mins to explain the scenario. Let say you have a function, and that function is used to add some keys to an object. The key name which you are going to add in that object is dynamic depending on the button click or something like this, and in the Handler function you are passing the argument which will be used as the new key name. So how you will make that argument name as a key of an object?


Solution:

STEP 1:

Make an object

let obj = {};

STEP 2:

let key = "someKey";

STEP 3:

then use [] to set it.

obj[key] = someValue; // this is same as obj.someKey=someValue

but the best way and the recommended way is using spread operator (...)

obj = { ...obj, [key]: somaValue };

NOTE: if you are having any doubt regarding why we are using [] for setting the keys, please have a look at DOT & BRACKET Notation in Javascript

Examples:

let's assume that we have 3 buttons, and there is a OnClickHandler function. On click of buttons we are calling this handler function and passing some key and value as a argument to this handler function and then we have to set this key and value in an object.

<button onclick=OnClickHandler("button1", 10)>Click 1</button>
<button onclick=OnClickHandler("button2", 20)>Click 2</button>
<button onclick=OnClickHandler("button3", 30)>Click 3</button>
let obj = {};
function OnClickHandler(key, value) {
  obj = { ...obj, [key]: value };
}

console.log(obj); // {button1:10, button2:20, button3:30}

Other scenarios where this concept is required:

  1. while updating session storage value.
  2. while updating state value in reactjs
  3. while switching between light and dark mode in any website

Thank you for reading this far. This is a brief introduction on How to use variable as a key name in JavaScript.

Hope it's a nice and informative read for you. If you find this article useful, like and share this article. Someone could find it useful too.

If you find anything technically inaccurate, please feel free to reach out to us.

VISIT https://www.capscode.in/blog TO LEARN MORE.

See you in my next Blog article, Take care!!

IF MY ARTICLE HELPED YOU

Buy Me A Coffee

Thanks,

CapsCode