Docs Menu

Docs HomeView & Analyze DataMongoDB Shell

Code Scoping

On this page

  • Example: Restricting Scope

When JavaScript is loaded into mongosh, top-level functions and variables defined with const, var, and let are added to the global scope.

Consider the following code:

const SNIPPET_VERSION = "4.3.2";
var loadedFlag = true;
let unloaded = false;
function isSnippetLoaded(loadedFlag) {
return ( loadedFlag ? "Snippet is loaded" : "Snippet is not loaded" )
}

The variables, SNIPPET_VERSION, loadedFlag, and unloaded are added to the global scope along with the function, isSnippetLoaded().

To avoid collisions with functions and variables defined in other code, be sure to consider scope as you write scripts. As a best practice, MongoDB recommends wrapping your code to limit scope. This guards against accidental scope collisions with similarly named elements in the global scope.

One way to keep functions and variables out of global scope is to wrap your code like this:

'use strict';
(() => {
...
})()

Tip

use strict; is for use in scripts. If you enter use strict; in the mongosh console directly, mongosh will switch to a database called strict.

Compare the following code samples. They are very similar, but the second one is written in a way that restricts variable scope.

Sample 1: Unrestricted scope.

let averageGrossSales = [ 10000, 15000, 9000, 22000 ];
const Q1_DISCOUNT = .10;
const Q2_DISCOUNT = .15;
const Q3_DISCOUNT = .06;
const Q4_DISCOUNT = .23;
function quarterlySales(grossAmount, discount ) {
return grossAmount * discount ;
}
function yearlySales() {
let annualTotal = 0;
annualTotal += quarterlySales(averageGrossSales[0], Q1_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[1], Q2_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[2], Q3_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[3], Q4_DISCOUNT );
return annualTotal ;
}

Sample 2: Restricted scope.

(() => {
let averageGrossSales = [ 10000, 15000, 9000, 22000 ];
const Q1_DISCOUNT = .10;
const Q2_DISCOUNT = .15;
const Q3_DISCOUNT = .06;
const Q4_DISCOUNT = .23;
function quarterlySales(grossAmount, discount ) {
return grossAmount * discount ;
}
globalThis.exposedYearlySales = function yearlySales() {
let annualTotal = 0;
annualTotal += quarterlySales(averageGrossSales[0], Q1_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[1], Q2_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[2], Q3_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[3], Q4_DISCOUNT );
return annualTotal ;
}
} )()

In Sample 2, the following elements are all scoped within an anonymous function and they are all excluded from the global scope:

  • The main function, yearlySales()

  • The helper function, quarterlySales()

  • The variables

The globalThis.exposedYearlySales = function yearlySales() assignment statement adds exposedYearlySales to the global scope.

When you, call exposedYearlySales() it calls the yearlySales() function. The yearlySales() function is not directly accessible.

←  Differences Between require() and load()Script Considerations →