Variable scope and naming
JavaScript is a relatively informal language and does not require you to define variables before assigning values to them. Neither does it enforce strict type checking, or signal when a variable is redefined. This makes JavaScript fast and easy to work with, but can quickly lead to illegible code and confounding errors, especially since we can create variables that overwrite built-in ones.
One bug that can defy debugging is when you declare a variable with the same name as a built-in one, like work, conn and current, so you will need to familiarize yourself with the reserved names used by Security Directory Integrator.
Another common problem occurs when you create new variables that redefine existing ones, perhaps used in included Configs or Script Libraries. These mistakes can be avoided if you are conscious about the naming of variables and their scope. Scope defines the sphere of influence of a variable, and in SDI we talk about global variables; those which are available in all Hooks, Script Components and attribute maps, and those that are local to a function. To get a better understanding of scope first understand that every AL has its own Script Engine, and therefore runs in its own script context. Any variable not specifically defined as local inside a function declaration is global for that Script Engine. So the following code will create a global variable:
myVar = "Know thyself";This variable will be available from this point on for the life the AL. Making this variable local requires two steps: using the var keyword when declaring the variable, and putting the declaration inside a function:function myFunc() { var myVar = "Know thyself"; }Now myVar as defined above will cease to exist after the closing curly brace. Note that placing the variable inside a function is not enough; you have to use var as well to indicate that you are declaring a new, local, variable.var glbVar = "This variable has global scope"; glbVar2 = "Another global variable"; function myFunc() { var lclVar = "Locally scoped within this block"; glbVar3 = "This is global, since "var" was not used"; };As long as you declare a local variable within a function then we can call it what you like. As soon as it goes out of scope, any previous type and value are restoredEven though the var keyword is not required for defining global variables, it is best practice to do so. And it is recommended that you define your variables at the top of your script, including enough comments to give the reader an understanding of what they will be used for. This approach not only improves the legibility of your code, it also forces you to make conscious decisions about variable naming and scope.1
Parent topic:
Java + Script ≠ JavaScript1 Function naming works a little differently. Programming languages like Java identify a function by the combination of its name plus the number and types of parameters. JavaScript just uses the name. So if you have multiple definitions of the same function, JavaScript will only "remember" the last one — regardless of whether that definition has a different number of parameters.