Flanagan's "JavaScript - The Definitive Guide" gives the following on page 653:

var variables = ""
for (var name in this)
    variables += name + "\n";
Answer from user181548 on Stack Overflow
🌐
BrainStation®
brainstation.io › learn › javascript › variables
JavaScript Variables (2026 Tutorial & Examples) | BrainStation®
February 4, 2025 - JavaScript variables are a way of storing information or data in memory and giving it a name to easily remember where the information or data is stored.
🌐
W3Schools
w3schools.com › js › js_variables.asp
JavaScript Variables
Variables are identified with unique names called identifiers. Names can be short like x, y, z. Names can be descriptive age, sum, carName. The rules for constructing names (identifiers) are: Names can contain letters, digits, underscores, and dollar signs. Names must begin with a letter, a $ sign or an underscore (_). Names are case sensitive (X is different from x). Reserved words (JavaScript keywords) cannot be used as names.
Discussions

Get all Javascript Variables? - Stack Overflow
Is there a way for javascript to detect all assigned variables? For example, if one js file creates a bunch of vars (globally scoped), can a subsequent file get all the vars without knowing what th... More on stackoverflow.com
🌐 stackoverflow.com
Is there a JavaScript equivalent of PHP’s “variable variables”? - Stack Overflow
I know it's possible in PHP to have variable variables, where one can access a variable by a name stored in another variable. For example, $x = "variable"; $$x = "Hello, World!"; More on stackoverflow.com
🌐 stackoverflow.com
How do JavaScript’s global variables really work?
TLDR; Everyone lied to you when they said let and const don't create global variables. They absolutely do .. it's just that they don't create window. global variables. More on reddit.com
🌐 r/javascript
47
137
July 7, 2019
Question: Most efficient way to use variables to avoid GC?
The number one thing I've seen writing performant JavaScript code has been making sure that things stay as unboxed primitives (ints, double, etc). Preferably in large typed arrays. Chances are your function itself isn't that hot, I bet the loop is though. Declare primitives once at the top of the function an they'll get allocated on the stack and incur zero GC. (Unless you do something like myInt["someProp"] = "something", in which case it's boxed and slow) More on reddit.com
🌐 r/javascript
20
28
October 7, 2015
🌐
LinkedIn
linkedin.com › pulse › what-variables-do-we-use-them-javascript-daniele-manca
What are variables and what do we use them for in Javascript?
October 9, 2023 - In summary, variables in JavaScript are essential for storing and managing data. They allow you to work with values, perform calculations, and make your code dynamic by referencing and updating data as needed throughout your program.
🌐
Tutorial Republic
tutorialrepublic.com › javascript-tutorial › javascript-variables.php
Defining Variables in JavaScript (let Vs var) - Tutorial Republic
In the above example we have created three variables, first one has assigned with a string value, the second one has assigned with a number, whereas the last one assigned with a boolean value. Variables can hold different types of data, we'll learn about them in later chapter. In JavaScript, variables can also be declared without having any initial values assigned to them.
🌐
Laravel
laravel.com › docs › 12.x › blade
Blade Templates | Laravel 12.x - The clean stack for Artisans and agents
The from method will return a string JSON.parse JavaScript statement that will convert the given object or array into a valid JavaScript object: ... The latest versions of the Laravel application skeleton include a Js facade, which provides convenient access to this functionality within your Blade templates: ... You should only use the Js::from method to render existing variables as JSON.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › javascript › javascript variables
JavaScript Variables - Scaler Topics
April 9, 2024 - JavaScript variables, often referred to as 'what is variable in javascript', acts as containers that store values that your code can use later. They can hold many types of data, such as numbers, texts, or more complex objects.
🌐
DataFlair
data-flair.training › blogs › javascript-variable-tutorial
JavaScript Variables - A to Z Guide for a Newbie in JavaScript! - DataFlair
August 7, 2019 - Variable means anything that can vary and holds the data value and we can change it anytime. A variable must have a unique name. Variables in JavaScript are tanks which keeps a hold on metamorphic data. Today in this tutorial, we will learn about variables in JavaScript.
🌐
AlmaBetter
almabetter.com › bytes › tutorials › javascript › variables-in-javascript
JavaScript Variables
June 22, 2023 - Variables are used to store the data and to modify the visual representation based on user input. Web Animations: JavaScript is commonly used to create animations on web pages.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-variables
JavaScript Variables - GeeksforGeeks
Variables can be declared using var, let, or const. JavaScript is dynamically typed, so types are decided at runtime.
Published   January 19, 2026
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › Variables
Storing the information you need — Variables - Learn web development | MDN
If you are using a desktop browser, ... more information on how to access this tool). A variable is a container for a value, like a number we might use in a sum, or a string that we might use as part of a sentence....
🌐
Boston University
bu.edu › housing › wp-content › themes › r-housing › js › vendor › pannellum › pannellum.htm
Pannellum
Javascript is required to view this panorama. (It could be worse; you could need a plugin.)
🌐
Reddit
reddit.com › r › learnprogramming › comments › tshpoc › variables_in_javascript
Variables in JavaScript : r/learnprogramming
A subreddit for all questions related to programming in any programming language (Contributions are only allowed in English!).
🌐
Programiz
programiz.com › javascript › variables-constants
JavaScript Variables and Constants
A JavaScript variable is a container for storing data, while a constant is a variable whose value cannot be changed. In this tutorial, you will learn about JavaScript variables and constants with the help of examples.
🌐
Medium
medium.com › @techwithtwin › understanding-javascript-variables-techwithtwin-05b3977a1124
Understanding JavaScript Variables | TechWithTwin | by TechWithTwin | Medium
June 5, 2025 - Understanding JavaScript Variables | TechWithTwin A variable is a container used to store data or information in a program. In JavaScript, we use special keywords to declare variables: let — used …
🌐
Robin Wieruch
robinwieruch.de › javascript-variable
JavaScript Variable Tutorial for Beginners
Actually variables can be changed if you assign a new value to them. You don't need the var statement though, because the variable has been declared before. ... In this section, you have used a string primitive from a set of available JavaScript data types.
🌐
W3Schools
w3schools.com › js › js_scope.asp
JavaScript Scope
All scripts and functions in the same web page can access a variable with global scope. Each JavaScript function have their own scope.
Top answer
1 of 9
181

tl;dr: Don't use eval!

There is no single solution for this. It is possible to access some global variables dynamically via window, but that doesn't work for variables local to a function. Global variables that do not become a property of window are variables defined with let and const, and classes.

There is almost always a better solution than using variable variables! Instead you should be looking at data structures and choose the right one for your problem.

If you have a fixed set of names, such as

// BAD - DON'T DO THIS!!!
var foo = 42;
var bar = 21;

var key = 'foo';
console.log(eval(key));

store those names/values as properties of an object and use bracket notation to look them up dynamically:

// GOOD
var obj = {
  foo: 42,
  bar: 21,
};

var key = 'foo';
console.log(obj[key]);

In ES2015+ it's even easier to do this for existing variables using concise property notation:

// GOOD
var foo = 42;
var bar = 21;
var obj = {foo, bar};

var key = 'foo';
console.log(obj[key]);


If you have "consecutively" numbered variables, such as

// BAD - DON'T DO THIS!!!
var foo1 = 'foo';
var foo2 = 'bar';
var foo3 = 'baz';

var index = 1;
console.log(eval('foo' + index));

then you should be using an array instead and simply use the index to access the corresponding value:

// GOOD
var foos = ['foo', 'bar', 'baz'];
var index = 1;
console.log(foos[index - 1]);

2 of 9
43

If you are desperate to do this you can either try using eval():

var data = "testVariable";
eval("var temp_" + data + "=123;");
alert(temp_testVariable);

Or using the window object:

var data = "testVariable";
window["temp_" + data] = 123;
alert(window["temp_" + data]);
🌐
Linguine Code
linguinecode.com › guides › javascript › beginners › variables
JavaScript Variables
In this guide, I go over what is a JavaScript variable, how to declare a variable, naming convetions and patterns.