0

I know if you want to check if a variable a is defined you can do this

if (typeof a !== 'undefined') {
 // the variable is defined
}

but what if you want to make a function out of it, like this

function checkDefined(name) {
 return typeof name !== 'undefined';
}
checkDefined("a");

this wouldn't work, but how can I get it to work if I have to pass a string version of the variable name?

Thanks

Abhi
4,2916 gold badges49 silver badges79 bronze badges
asked Sep 8, 2017 at 14:47
10
  • 2
    You cannot do that for local variables. What problem are you trying to solve? Commented Sep 8, 2017 at 14:48
  • 2
    Any time you find yourself using variable variables, you should be using an object instead. Commented Sep 8, 2017 at 14:49
  • 1
    For global variables you can use window[name] Commented Sep 8, 2017 at 14:50
  • In my case, I only have a string of the variable name I want to check if defined or not. Commented Sep 8, 2017 at 14:50
  • 1
    xyproblem.info Commented Sep 8, 2017 at 14:51

4 Answers 4

1

Checking in global scope(window):

var a = 'test';
var b = function() {};
function checkDefined(name) {
 return typeof this[name] !== 'undefined';
}
console.log(checkDefined("a"));
console.log(checkDefined("b"));
console.log(checkDefined("c"));

If you want check if variable or function is declared in class object you should pass new context to checkDefined method:

 function MyCustomClass() {
 this.c = 'test';
 }
 function checkDefined(name) {
 return typeof this[name] !== 'undefined';
 }
 // Create new object of type MyCustomClass
 var myCustomClassObject = new MyCustomClass();
 // In this way you can check if variable/function is defined in object
 console.log(checkDefined.apply(myCustomClassObject, ["a"]));
 console.log(checkDefined.apply(myCustomClassObject, ["b"]));
 console.log(checkDefined.apply(myCustomClassObject, ["c"]));

apply will call a function immediately letting you specify both the value of this and any arguments the function will receive

answered Sep 8, 2017 at 14:55
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain your answer instead of just posting code? Why use this when the function isn't an object property?
this is needed for checking definitions in objects. If you call this in global scope then it's refered to window, if you call this in class object - in this case MyCustomClass - it refers to MyCustomClass object context
1

Inspired by this answer. I think you can try to return with eval:

function checkDefined(name) {
 return eval("typeof " + name) !== 'undefined';
}

Example:

var a = 1;
checkDefined("a") // true
checkDefined(a) // true
checkDefined("b") // false
answered Sep 8, 2017 at 15:03

Comments

0

Local variables are properties of the currently scoped this object.

const log = output => document.querySelector('pre')
 .innerText += output + '\n'
 
/* In this example, running in the browser, `this` points to `window`, but 
 in other environments would still point to whatever is the global object.
 Bind `this` to the ``checkDefined` so to ensure it keeps the same value
 as where you are calling from. */
const checkDefined = function chkDef(v) {
 return typeof this[v] !== 'undefined'
}.bind(this)
a = 5
log(checkDefined('a'))
log(checkDefined('b'))
/* This is the same basic idea, but we make it a little more portable by
 not binding until right before we use it, so `this` has the correct
 scope. */
unboundCheckDefined = function chkDef(v) {
 return typeof this[v] !== 'undefined'
}
newScope()
function newScope() {
 c = 5
 const checkDefined = 
 unboundCheckDefined.bind(this)
 
 log(checkDefined('a'))
 log(checkDefined('b'))
 log(checkDefined('c'))
}
<pre></pre>

answered Sep 8, 2017 at 15:13

Comments

0

You should also pass on which object context you need to check if the variable is defined or not. If it's global pass the window object

function checkDefined(name, ref) {
 if(!ref) {
 ref = window;
 }
 return !!ref[name]
}
checkDefined("a"); //false if already not defined
var obj = { a: 1, b:2};
checkDefined("a",obj);//true
answered Sep 8, 2017 at 14:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.