Choose where you’d like to start

Variables

Overview

A variable is the address of a temporary value in the computer memory.

Every time we declare a variable with a value, it gets stored in a certain address in RAM. And the variable name is the usual way to refer to the stored value.

How is a variable useful?

Variables don't just act as virtual addresses for information, but they also allow us to manipulate its value as the program runs.

There may be times when you need to use a particular value in multiple places in a code. In such cases you can declare a variable once and use it wherever required. If there is an update required to that value, you do not have to update it in multiple places in the code, instead you can just update the variable. 

For example, imagine you have this calculation 1+1+1 which you use in multiple places in your code:

When you have to update the calculation, for example, changing it to 1*2*3, you will need to update the calculation in all instances in the code. Instead, you can assign the original calculation to a variable, and then simply update the calculation just once in the variable:

math = 1+1+1;
math = 1*2*3;

One change to the definition of a variable is much easier than searching for ‘1+1+1’ and replacing it with ‘1*2*3'. Therefore, the load of multiple database calls to retrieve the same information is negated using a variable which is stored once and used wherever needed.

Variables also provide a way of labelling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.

Variables in Deluge

Variables in Deluge are dynamically-typed. Unlike other languages, users don't have to declare the data type of the variable before using it. 

The variables in Deluge do not have a global scope, i.e, they can be utilized only in the action script where they are declared.

Typically, assignment of values to variables happens either at the top of a script (if the variable is used widely) or as close to where it is used. This helps readability and maintenance of code. The variable is on the left, followed by an equal sign, and again followed by the item being assigned, as shown below:

tax = 0.1;

Things to keep in mind:

  • Variable names can begin with upper case letters, lower case letters, or underscore (_) and can contain upper case letters, lower case letters, numbers, and underscore(_).
  • Variable names cannot have spaces. Use an underscore(_) where you need spaces.
  • Variable names are case-sensitive. "var" and "Var" will be considered as 2 different variable names.
  • Use full words instead of cryptic abbreviations while declaring a variable. It makes it easier to understand the code.
  • Reserved keywords like true, false, null, void and return cannot be declared as variables.

Get Started Now

Execute