What is the correct way to check if a global variable exists?


Ebrahim Byagowi:

JSLint is not passing this as valid code:

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

What is the correct way?

bigoldbrute:
/*global window */

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

if (!window.hasOwnProperty('someVar')) {
    window.someVar = 123456;
}

Related


What is the correct way to check if a global variable exists?

Ebrahim Byagowi: JSLint is not passing this as valid code: /* global someVar: false */ if (typeof someVar === "undefined") { var someVar = "hi!"; } What is the correct way? bigoldbrute: /*global window */ if (window.someVar === undefined) { window.so

What is the correct way to check if a global variable exists?

Ebrahim Byagowi: JSLint is not passing this as valid code: /* global someVar: false */ if (typeof someVar === "undefined") { var someVar = "hi!"; } What is the correct way? bigoldbrute: /*global window */ if (window.someVar === undefined) { window.so

What is the correct way to check if an array exists in JS?

Hussein When I check the length of the array, I get the following error. What is the correct way? main file if (drugPrice.mailPrice.rejectMessage.length !== 0 && Array.isArray(drugPrice.mailPrice.rejectMessage)) { //code goes here } mistake TypeError: Can

What is the correct way to check if a collection exists?

Tim Previously, I checked if the collection existed by querying the namespace. Roughly like this, check if "foo.bar" exists: return 1 === $client->selectCollection('foo','system.namespaces'); ->count(['name'=>'bar']); Since this only works

What is the correct way to check if an array exists in JS?

Hussein When I check the length of the array, I get the following error. What is the correct way? main file if (drugPrice.mailPrice.rejectMessage.length !== 0 && Array.isArray(drugPrice.mailPrice.rejectMessage)) { //code goes here } mistake TypeError: Can

What is the correct way to check if a collection exists?

Tim Previously, I checked if the collection existed by querying the namespace. Roughly like this, check if "foo.bar" exists: return 1 === $client->selectCollection('foo','system.namespaces'); ->count(['name'=>'bar']); Since this only works

Is this the correct way to check if a file exists?

beginner I'm trying to check if a file (image) exists so I can delete it before uploading a new one. Here's what I've tried so far: foreach (glob("../imgs/" . $name . ".*") as $file) { if (file_exists($file)) { unlink($file); } } Is this the c

Is this the correct way to check if a file exists?

beginner I'm trying to check if a file (image) exists so I can delete it before uploading a new one. Here's what I've tried so far: foreach (glob("../imgs/" . $name . ".*") as $file) { if (file_exists($file)) { unlink($file); } } Is this the c

Is this the correct way to check if a file exists?

beginner I'm trying to check if a file (image) exists so I can delete it before uploading a new one. Here's what I've tried so far: foreach (glob("../imgs/" . $name . ".*") as $file) { if (file_exists($file)) { unlink($file); } } Is this the c

Correct way to check if DocumentDB object exists

Strenikovlev In Microsoft's example, I see two methods to check if a DocumentDb object exists, such as Database, DocumentCollection, Document, etc: The first is by creating a query: Database db = client.CreateDatabaseQuery().Where(x => x.Id == DatabaseId).AsEn

Correct way to check if file exists in Linux

Brian I've been using it in my scripts but my colleagues disagree with me. My script accepts a file as a parameter and creates that file. Then I use the following to see if it actually gets created. ls -p | grep [filename] Then I check to see if the file I'm t

Correct way to check if DocumentDB object exists

Strenikovlev In Microsoft's example, I see two methods to check if a DocumentDb object exists, such as Database, DocumentCollection, Document, etc: The first is by creating a query: Database db = client.CreateDatabaseQuery().Where(x => x.Id == DatabaseId).AsEn

Correct way to check if DocumentDB object exists

Strenikovlev In Microsoft's example, I see two methods to check if a DocumentDb object exists, such as Database, DocumentCollection, Document, etc: The first is by creating a query: Database db = client.CreateDatabaseQuery().Where(x => x.Id == DatabaseId).AsEn

Correct way to check if DocumentDB object exists

Strenikovlev In Microsoft's example, I see two methods to check if a DocumentDb object exists, such as Database, DocumentCollection, Document, etc: The first is by creating a query: Database db = client.CreateDatabaseQuery().Where(x => x.Id == DatabaseId).AsEn

Correct way to check if DocumentDB object exists

Strenikovlev In Microsoft's example, I see two methods to check if a DocumentDb object exists, such as Database, DocumentCollection, Document, etc: The first is by creating a query: Database db = client.CreateDatabaseQuery().Where(x => x.Id == DatabaseId).AsEn