admin管理员组

文章数量:1134248

JSLint is not passing this as a valid code:

/* global someVar: false */
if (typeof someVar === "undefined") {
    var someVar = "hi!";
}

What is the correct way?

JSLint is not passing this as a valid code:

/* global someVar: false */
if (typeof someVar === "undefined") {
    var someVar = "hi!";
}

What is the correct way?

Share Improve this question edited Nov 29, 2015 at 16:58 Ebrahim Byagowi asked Jul 21, 2012 at 22:24 Ebrahim ByagowiEbrahim Byagowi 11.2k6 gold badges73 silver badges89 bronze badges 2
  • 1 Note that the var is not scoped to the if block. It's as if you had written var someVar above the if. Ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – leewz Commented Oct 11, 2016 at 5:41
  • 3 Went down a huge rabbit-hole after reading only the code in the answers here, and not the one in the question. If you want to write code that works on both Nodejs and the browser, this is the correct way to check if a variable is undefined, regardless of what JSLint may think about it. – Tobias Cohen Commented Jun 14, 2019 at 2:15
Add a comment  | 

12 Answers 12

Reset to default 104
/*global window */

if (window.someVar === undefined) {
    window.someVar = 123456;
}

if (!window.hasOwnProperty('someVar')) {
    window.someVar = 123456;
}
/**
 * @param {string} nameOfVariable
 */
function globalExists(nameOfVariable) {
    return nameOfVariable in window
}

It doesn't matter whether you created a global variable with var foo or window.foo — variables created with var in global context are written into window.

If you are wanting to assign a global variable only if it doesn't already exist, try:

window.someVar = window.someVar || 'hi';

or

window['someVar'] = window['someVar'] || 'hi';

try

variableName in window

or

typeof window[variableName] != 'undefined'

or

window[variableName] !== undefined

or

window.hasOwnProperty(variableName)

I think this is actually a problem with JSLint. It will issue the following error:

Unexpected 'typeof'. Compare directly with 'undefined'.

I believe this is bad advice. In JavaScript, undefined is a global variable that is, usually, undefined. But some browsers allow scripts to modify it, like this: window.undefined = 'defined'. If this is the case, comparing directly with undefined can lead to unexpected results. Fortunately, current ECMA 5 compliant browsers do not allow assignments to undefined (and will throw an exception in strict mode).

I prefer typeof someVar === "undefined", as you posted, or someVar in window as Susei suggested.

if (typeof someVar === "undefined") {
    var someVar = "hi!";
}

will check if someVar (local or global) is undefined.

If you want to check for a global variable you can use

if(window['someVar'] === undefined) {
    ...
}

assuming this is in a browser :)

As of ES6 most of other answers, including the accepted answer, are incorrect, because global variables defined by let or const, or resulting from a class declaration, do not have corresponding properties on the global object (window in a browser, or global in node.js). Several of them—mainly the ones which use typeof—can also be fooled by global variables which exist but which are set to undefined.

The only fully general way to test to see if a global variable exists—regardless of whether it has been declared using var, let or const, created via a function or class declaration, created by assignment (i.e., myVar = value at the top level of a program without any declaration for myVar) or by creating a property on the global object (i.e., window.myVar = value)—is to attempt to access it via a global eval and see if TypeError is thrown.

(This builds on an idea presented by Ferran Maylinch, but with a trick to ensure that it will work properly even when encapsulated in a function.)

function globalExists(varName) {
  // Calling eval by another name causes evalled code to run in a
  // subscope of the global scope, rather than the local scope.
  const globalEval = eval;
  try {
    globalEval(varName);
    return true;
  } catch (e) {
    return false;
  }
}

undeclared = undefined;
const myConst = undefined;
let myLet;
var myVar;

globalExists('undeclared')    // => true
globalExists('myConst')       // => true
globalExists('myLet')         // => true
globalExists('myVar')         // => true
globalExists('nonexistent')   // => false
globalExists('globalExists')  // => true - can see itself.
globalExists('varName')       // => false - not fooled by own parameters.
globalExists('globalEval')    // => false - not fooled by local variable.

Note that this makes use of eval, so all the usual caveats apply: you should not supply an untrusted value as the parameter, and if you must use an untrusted value you should check to make sure that varName is a valid JavaScript identifier. Doing so is out of scope for this question, but it can be done using a (rather complex) regular expression—just beware that the correct regexp depends on the version of ECMAScript you are using, whether the code is a script or (ES6) module, whether it is in an async function, etc. etc.

I see no reference to globalThis here, so I'll add my answer that uses it.

All of the below if statements will work in Node.js and any browser. They should also work perfectly in Bun and Deno, though I haven't tested those two runtimes.

The bottom two, as noted in the comment, work in TypeScript strict mode, so I'd recommend those.

if (globalThis.someVar) {
    console.log('global someVar exists 1');
}
if (typeof globalThis.someVar !== 'undefined') {
    console.log('global someVar exists 2');
}
if (typeof someVar !== 'undefined') {
    console.log('global someVar exists 3');
}
if ('someVar' in globalThis && globalThis.someVar) {
    console.log('global someVar exists 4');
}

// TypeScript ok

if (globalThis.hasOwnProperty('someVar')) {
    console.log('global someVar exists 5');
}
if ('someVar' in globalThis) {
    console.log('global someVar exists 6');
}

bfavaretto is incorrect.

Setting the global undefined to a value will not alter tests of objects against undefined. Try this in your favorite browsers JavaScript console:

var udef; var idef = 42;
alert(udef === undefined); // Alerts "true".
alert(idef === undefined); // Alerts "false".
window.undefined = 'defined';
alert(udef === undefined); // Alerts "true".
alert(idef === undefined); // Alerts "false".

This is simply due to JavaScript ignoring all and any values attempted to be set on the undefined variable.

window.undefined = 'defined';
alert(window.undefined); // Alerts "undefined".

This would be a simple way to perform the check .

But this check would fail if variableName is declared and is assigned with the boolean value: false

if(window.variableName){

}

I think the best solution is the following:

if(window.hasOwnProperty('foo')) {
    console.log('Variable is not declared');
}

The following solution will not work if the variables is declared but is not assigned (var foo;).

typeof foo === 'undefined'

If you are not sure whether a global variable is defined, you can always try accessing it and see what happens.

function node_env(name) {
    try {
        return process.env[name];
    } catch (ignore) {}
}

本文标签: javascriptWhat is the correct way to check if a global variable existsStack Overflow