# Variables & Logic
Variables can be created and assigned in BBScript, with their scope being limited to the post the script is defined in. Variables are expected to only contain alphabets (`a-z` or `A-Z`) or underscore (`_`) in their name, and are case-sensitive. BBScript has no dynamic variable naming, and expect all variables to be a literal without any starting/ending quotes. This means `foo` is the variable `foo`.
BBScript 2 has a unique resolution for undeclared variables, where if a literal being referenced is not an existing variable, it will be treated as string. For example, if `foo` is not yet defined, it will be treated as the string `"foo"`.
To create a variable, `=` operator is used. In the following snippet, the variable `foo` is assigned the value `"bar"`.
```bbscript
(= foo "bar")
(print foo)
```
At `print`, it will log the string `"bar"` into the console.
---
## `=` Set
Sets a value to the variable name.
```bbscript
(= varName value)
```
varName
: The name of the variable to set the value to or create. This should be single string or literal conforming to the restrictions of only alphabets or underscores (`a-zA-Z_`).
: This _can_ be a string or resolve to a string, but it is better for clarity to make this a literal.
value
: The value to be assigned to the variable. This can be anything.
---
## `print`
Prints the value of the arguments in the console. The console can be accessed by right clicking on the page, and clicking on the `console` tab.
```bbscript
(print arg ...)
```
arguments unlimited
: The list of values to print to the console.
---
## `++` Increment
Increment the value of a variable by 1. Operation is done inplace.
```bbscript
(++ varName)
```
varName
: The variable to increment the value of. The variable must refer to a number value.
---
## `--` Decrement
Decrement the value of a variable by 1. Operation is done inplace.
```bbscript
(-- varName)
```
varName
: The variable to decrement the value of. The variable must refer to a number value.
---
## `if`
Performs a conditional check and executes the then or else block.
```bbscript
(if (conditional) (then) (else))
```
conditional
: The conditional block to be tested. _Should_ resolve to a boolean, but will use Javascript truthy-falsy values.
(then)
: Sub-block to execute if conditional is true.
(else)
: Sub-block to execute if conditional is false.
Return
: This function can return the value of the then/else block depending on the conditional and will pass the value to the parent. By this way, if statements can be chained inside other functions.
---
## `==` Equal
Returns true or false if the first argument is loosely equal to the second argument.
```bbscript
(== val1 val2)
```
arguments
: The two values to compare against each other. Performs loose evaluation, i.e. number `1` is equal to string `"1"`.
Return
: Returns true or false based on conditional.
---
## `!=` Not Equal
Returns true or false if the first argument is not loosely equal to the second argument.
```bbscript
(!= val1 val2)
```
arguments
: The two values to compare against each other.
Return
: Returns true or false based on conditional.
---
## `and`
Returns true if all arguments evaluate to true, false otherwise. Performs quick fallout if any argument resolves to false and prevents following arguments from being evaluated.
```bbscript
(and arg1 arg2 ...)
```
arguments unlimited
: Arguments that should be true or false. _Should_ resolve to a boolean, but Javascript truthy-falsy is also valid.
Return
: Returns true if all arguments resolve to true. False otherwise.
---
## `or`
Returns true if any argument evaluates to true, false otherwise. Performs quick fallout if any argument resolves to true, and prevents following arguments from being evaluated.
```bbscript
(or arg1 arg2 ...)
```
arguments unlimited
: Arguments that should be true or false. _Should_ resolve to a boolean, but Javascript truthy-falsy is also valid.
Return
: Returns true if any argument resolves to true. False otherwise.
---
## `>` Greater Than
Returns true or false if the first argument is loosely greater than to the second argument.
```bbscript
(> val1 val2)
```
arguments
: The two values to compare against each other. Performs loose evaluation, i.e. number `2` is greater than string `"1"`.
Return
: Returns true or false based on conditional.
---
## `>=` Greater Than or Equal to
Returns true or false if the first argument is loosely greater than or equal to the second argument.
```bbscript
(>= val1 val2)
```
arguments
: The two values to compare against each other. Performs loose evaluation, i.e. number `1` is greater than or equal to string `"1"`.
Return
: Returns true or false based on conditional.
---
## `<` Less Than
Returns true or false if the first argument is loosely less than to the second argument.
```bbscript
(< val1 val2)
```
arguments
: The two values to compare against each other. Performs loose evaluation, i.e. number `2` is less than string `"1"`.
Return
: Returns true or false based on conditional.
---
## `<=` Less Than or Equal to
Returns true or false if the first argument is loosely less than or equal to the second argument.
```bbscript
(<= val1 val2)
```
arguments
: The two values to compare against each other. Performs loose evaluation, i.e. number `1` is less than or equal to string `"1"`.
Return
: Returns true or false based on conditional.
---
## `stop`
Ends the execution of the script.
```bbscript
(stop)
```
---
## `setTimeout`
Delays the execution of a provided script by a given amount of seconds. Does not block the rest of the script from executing.
```bbscript
(setTimeout duration (function))
```
duration
: The amount of time to wait before executing the given function in seconds. Must resolve to a number.
(function)
: A script to execute at the end of the timeout.
Returns
: Returns a handle id that can be used to pre-maturely stop the timer. See [`clearTimeout`](#cleartimeout).
---
## `clearTimeout`
Prematurely end the timer of a `setTimeout` function and prevent execution of associated script.
```bbscript
(clearTimeout id)
```
id
: The handle id value that the `setTimeout` returned.
**Example**: The following script sets a timeout of 1 second. At the end of 1 second, it will print `"hello world"`. However, there is a `clearTimeout` of the handle that immediately stops the timer and prevents the print.
```bbscript
(= handle (setTimeout 1 (print "hello world")))
(clearTimeout handle)
```
---
## `setInterval`
Continuously the execution of a provided script every given amount of seconds. Does not block the rest of the script from executing.
```bbscript
(setInterval duration (function))
```
duration
: The amount of time to wait between each execution of the given function in seconds. Must resolve to a number.
(function)
: A script to execute every `duration` seconds.
Returns
: Returns a handle id that can be used to stop the cycle. See [`clearInterval`](#clearInterval).
---
## `clearInterval`
Stops the cycle of the `setInterval` function and prevent any further execution of associated script.
```bbscript
(clearInterval id)
```
id
: The handle id value that the `setInterval` returned.
**Example**: The following script sets a interval of 1 second. Every 1 second, it will print `"hello world"`. However, there is a `clearInterval` of the handle that immediately stops the cycle and prevents the print.
```bbscript
(= handle (setInterval 1 (print "hello world")))
(clearInterval handle)
```
---
## `time`
Returns the current time in milliseconds.
```bbscript
(time)
```
Return
: Returns the number of milliseconds since the epoch. Resolve to a number
---
## `group`
Combines multiple script functions as a single function block. This is often used in [`if`](#if), [`setTimeout`](#setTimeout), [`setInterval`](#setInterval), and [each](#each).
```bbscript
(group (script1) (script2))
```
scripts unlimited
: The scripts to be bundled together as one single function
Returns
: Returns the value of the last script if applicable
**Example**: Is often used to chain commands together in if statements. In the following example, if `1` equals `1`, it will print `"hello"` and `"test"`
```bbscript
(if (== 1 1) (group
(print "hello")
(print "test")
)
)
```
As BBScript 2 ignores whitespace, it is better for clarity to separate group scripts into individual lines.
---
## `each`
Performs a given function on each element of an array. This acts like a `for-of` loop in Javascript.
```bbscript
(each array (function) token)
```
array
: The array of elements to perform the function on.
(function)
: The function to apply the logic on a given element. The element can be referenced using the token provided.
token optional
: The token to use to reference the element. Must resolve to a string.
: _Default_ - by default, uses `_`
**Example**:
```bbscript
(= sum 0)
(each [1 2 3] (group
(print _)
(= sum (+ sum _))
))
(print sum)
```
Will print:
```
1
2
3
6
```
See also [BBScript 2 Arrays](./BBScript:-BBScript-2:-Arrays.md)