Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if JS object exists

Tags:

javascript

I need to discover at runtime if an object with a given name exists, but this name is stored in a variable as a string.

For example, in my Javascript I have an object named test, then at runtime I write the word "text" in a box, and I store this string in a variable, let's name it input. How can I check if a variable named with the string stored in input variable exist?

like image 351
Naigel Avatar asked Jan 17 '23 13:01

Naigel


1 Answers

If the object is in the global scope;

var exists = (typeof window["test"] !== "undefined");

like image 164
Alex K. Avatar answered Feb 10 '23 12:02

Alex K.