How to create a variable using a user-defined name in JavaScript ? Last Updated : 17 Feb, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will learn to create a variable using the user-defined name in JavaScript. Variables in JavaScript: Basically, it is a container that holds reusable data in JavaScript. The variable's value can be changed during program execution and should be declared before using it. Before ES6 declares a variable, we just use the var keyword to create a variable, but ES6 and later, two more keywords were added to create a variable namely let and const. Approach: In JavaScript, it is possible to create a variable using a user-defined name. First, we will create a variable using an object then, we will create a variable using this keyword. Example 1: Create a variable using a user-defined name in JavaScript with the help of an object. JavaScript // A user-defined variable created let my_var = "userVariable"; let my_value = "userValue"; let x = { // Assign value of a user-defined variable [my_var]: my_value, }; console.log(x); console.log(x[my_var]); Output: Example 2: Create a variable using a user-defined name in JavaScript with the help of this keyword. JavaScript let var_name = "newVariable"; let var_value = "newValue"; this[var_name] = var_value; console.log(var_name); console.log(this[var_name]); Output: newVariable newValue Conclusion: In this article, we learned to create a variable using the user-defined name in JavaScript with the help of two suitable approaches. Comment More infoAdvertise with us Next Article How to create a variable using a user-defined name in JavaScript ? vikashgautam11 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to create a private variable in JavaScript ? In this article, we will try to understand how we could create private variables in JavaScript. Let us first understand what are the ways through which we may declare the variables generally in JavaScript. Syntax: By using the following syntaxes we could declare our variables in JavaScript. var vari 3 min read How to Use Dynamic Variable Names in JavaScript? Dynamic variable names are variable names that are not predefined but are generated dynamically during the execution of a program. This means the name of a variable can be determined at runtime, rather than being explicitly written in the code.Here are different ways to use dynamic variables in Java 2 min read How to pass Variable to inline JavaScript using EJS Template Engine? In the EJS templating engine, we can pass variables to inline JS within our HTML templates. We can use the '<%= variable %>' syntax, where we can embed server-side variables directly into the client-side scripts. In this article, we will explore the detailed process to pass a variable to inlin 2 min read How to Convert String to Variable Name in JavaScript? In JavaScript, if we consider the variable names as string values, it is difficult to transform a variableâs name into a string. However, this problem has been handled in many ways for example, static properties of objects, eval(), and modern ways such as Map can be used to achieve the essential eff 3 min read How to Create a String with Variables in JavaScript? To create a string with variables in JavaScript, you can use several methods, including string concatenation, template literals, and the String methods. Here are the common approaches:1. Template Literals (Recommended)Template literals are the modern and most preferred way to create strings with var 2 min read JavaScript - How to Use a Variable in Regular Expression? To dynamically create a regular expression (RegExp) in JavaScript using variables, you can use the RegExp constructor. Here are the various ways to use a variable in Regular Expression.1. Using the RegExp Constructor with a VariableIn JavaScript, regular expressions can be created dynamically using 3 min read How to add a property to a JavaScript object using a variable as the name? In JavaScript, you can dynamically add a property to an object using a variable as the name. This allows you to create flexible and dynamic objects based on runtime values, enhancing code reusability and adaptability. There are several methods that can be used to add a property to a JavaScript objec 2 min read How to create Static Variables in JavaScript ? To create a static variable in JavaScript, you can use a closure or a function scope to encapsulate the variable within a function. This way, the variable maintains its state across multiple invocations of the function. Static keyword in JavaScript: The static keyword is used to define a static meth 2 min read How to use a Variable for a Key in a JavaScript Object Literal? Using a variable as a key in a JavaScript object literal enables dynamic property creation, allowing us to define object properties based on variable values.Syntax: let key="your_choice";let object = {};object[key] = "your_choice";console.log(object);Example: This shows how to use a variable for a k 1 min read How to create a GUID / UUID in JavaScript ? GUIDs (Globally Unique Identifiers) and UUIDs (Universally Unique Identifiers) are 128-bit unique identifiers used across systems to uniquely identify resources like files or objects. They are typically represented as 32-character hexadecimal strings, making them crucial for ensuring uniqueness in d 2 min read Like