A variable
declaration is used to assign a value to an identifier in the current scope. They can depend on resources, other variables or inputs. They can be assigned any valid value.
Unlike many imperative languages, variables cannot be reassigned, and must be assigned when declared. A variable cannot have the same name as a parameter, resource, output or another variable in the same scope.
Unlike parameters or outputs, variables do not require declaring the type. The type is inferred from the value of the variable.
There are no constraints on placement of variable declarations. They can be mixed with any other valid declarations in any order.
The examples below cover variable declaration using hard-coded and calculated values.
var myString = 'my string value'
var location = resourceGroup().location
var iAmTrue = true
var iAmFalse = false
var hasItems = length(myArray) >= 0
var meaningOfLifeTheUniVerseAndEverything = 42
var lengthOfMyArray = length(myArray)
var myObject = {
first: 1
second: 2
}
var keys = listKeys(myResource.id, myResource.apiVersion)
var myArray = [
'item 1'
'item 2'
]
var myOtherArray = createArray('first', 'second', 'third')