Code structure
Much like algebra, in JavaScript, we have a set way of structuring code.
in algebra, variables are used to represent a value that is initially unknown.
(Example)
x = 1 + 2
In JavaScript, variables act as containers for storing values. We can declare the name of a variable and also assign it a value by using “var.”
(Example)
var x = 1 + 2;
In this example, the variable “x” has been assigned the value of 3.
Variables can also hold text values by placing the text in “quotation marks.”
(Example)
var x = "One";
In JavaScript, we refer to the text as a “string.” We will go into more detail with strings and their different uses as they pertain to FastField in a later article.
Each field in your form can hold a value, and as such, that value can be assigned to a variable.
(Example)
var x = $fieldkey_1$;
As usual, we place ‘$’ signs around our field key. Now that you have assigned “x” to the value of “fieldkey_1,” let's take it a bit further.
(Example)
var x = $fieldkey_1$;
var y = $fieldkey_2$;
return y+b;
Let’s say in this example that “x” (fieldkey_1) has an entered value of “3,” and that y (fieldkey_2) has an entered value of “5.” This would mean that we would “return” the value of “8.”
We can also add strings together; however, there are a few things you should be aware of.
If you are adding things together if there is at least one string, it will concatenate (combine side by side) as opposed to mathematically adding them.
Strings can contain numbers too, and they are not just limited to text.
(Example)
var x = "123";
var y = 123;
In this example, var “x” is a string, whilst var "y" is an integer (number).
If you were to add these two together, you would not get “246,” you would instead get “123123.”
(Example)
var x = "123";
var y = 123;
return x+y;
123123
Again, this is because at least one of the items is a string. For more information on basic arithmetic in JavaScript, you can reference W3Schools' guide to JavaScript arithmetic.