JavaScript Check If Variable Exists (Is Defined/Initialized)

JavaScript check if variable exists (is defined/initialized)

The typeof operator will check if the variable is really undefined.

if (typeof variable === 'undefined') {
// variable is undefined
}

The typeof operator, unlike the other operators, doesn't throw a ReferenceError exception when used with an undeclared variable.

However, do note that typeof null will return "object". We have to be careful to avoid the mistake of initializing a variable to null. To be safe, this is what we could use instead:

if (typeof variable === 'undefined' || variable === null) {
// variable is undefined or null
}

For more info on using strict comparison === instead of simple equality ==, see:
Which equals operator (== vs ===) should be used in JavaScript comparisons?

How can I check if a variable exists (at all) in JS?

You need to use the typeof operator to see if a variable exist or no
you need to test if the variable you are concerned with is undefined of null like so :

if (typeof variable === 'undefined' || variable === null) {
// variable does not exist
}

Check if variable is defined javascript

Use typeof

if(typeof variable === "undefined"){
/* It is undefined */
}

Checking if a variable exists in javascript

A variable is declared if accessing the variable name will not produce a ReferenceError. The expression typeof variableName !== 'undefined' will be false in only one of two cases:

  • the variable is not declared (i.e., there is no var variableName in scope), or
  • the variable is declared and its value is undefined (i.e., the variable's value is not defined)

Otherwise, the comparison evaluates to true.

If you really want to test if a variable is declared or not, you'll need to catch any ReferenceError produced by attempts to reference it:

var barIsDeclared = true; 
try{ bar; }
catch(e) {
if(e.name == "ReferenceError") {
barIsDeclared = false;
}
}

If you merely want to test if a declared variable's value is neither undefined nor null, you can simply test for it:

if (variableName !== undefined && variableName !== null) { ... }

Or equivalently, with a non-strict equality check against null:

if (variableName != null) { ... }

Both your second example and your right-hand expression in the && operation tests if the value is "falsey", i.e., if it coerces to false in a boolean context. Such values include null, false, 0, and the empty string, not all of which you may want to discard.

JavaScript check if variable exists (is defined/initialized)

The typeof operator will check if the variable is really undefined.

if (typeof variable === 'undefined') {
// variable is undefined
}

The typeof operator, unlike the other operators, doesn't throw a ReferenceError exception when used with an undeclared variable.

However, do note that typeof null will return "object". We have to be careful to avoid the mistake of initializing a variable to null. To be safe, this is what we could use instead:

if (typeof variable === 'undefined' || variable === null) {
// variable is undefined or null
}

For more info on using strict comparison === instead of simple equality ==, see:
Which equals operator (== vs ===) should be used in JavaScript comparisons?

Check if variable is defined in Typescript?

The TypeScript compiler won't let you access a variable it doesn't know about, since most of the time such accesses are errors.

If you want the compiler to believe that such a variable is in scope, you could declare it first:

declare var variablename: string | undefined;
if (typeof variablename !== 'undefined') { /*do something here*/ }

That doesn't change the emitted JavaScript at all. It just tells the compiler to act as if there is a var named variablename in scope whose type is string | undefined (in your use case it might be some type other than string but I needed an example). In other words, it assumes that your JavaScript will run in a context where variablename of such a type is around.

This isn't exactly what you want, since it's possible at runtime that there is no such variable. Unfortunately there's no way to tell the compiler that the variable might be in scope and that typeof can be used to check it. Variables are either in scope (and you can access them) or they're not (and you can't). There was a proposal at microsoft/TypeScript#23602 to have a way to conditionally declare variables, but nothing came of it. Declaring the variable as definitely-existing but of a type with | undefined in it is as close as you can get, I think.

Playground link to code

how to test if a variable is valid or whether it is initialized or not in C?

Assuming the above array is a local variable and not a global, the values of the array are uninitialized and in fact you can't check whether a variable is uninitialized or not, as simply attempting to read such a variable can trigger undefined behavior.

It sounds like you want all array elements to start with the value 0. The simplest way to do this is to initialize the array as such:

int stuff[9][9] = {{ 0 }};

Which explicitly sets element [0][0] to 0 and implicitly sets all other elements to 0.

Alternately, you can create a loop to set all values to 0 to start before doing your "regular" processing.



Related Topics



Leave a reply



Submit