$$Include{docs/siteMacros.textCarver}

# Variables
Variables are the heart of the TextCarver language.

$$example{}
\$\$name = {Alex}
Hello \$\$name{}

$$exampleRight{}
<br>

Hello Alex

$$endExample{}

Variables always begin with a lowercase letter and built in commands always begin with an uppercase letter.



### Early versus late evaluation
Code inside single braces is evaluated immediately:

$$example{}
\$\$name = {Alex}
\$\$greeting = {Hello \$\$name{}}
\$\$greeting{}
\$\$name = {Chris}
\$\$greeting{}

$$exampleRight{}
<br><br>

Hello Alex
<br>

Hello Alex

$$endExample{}

Code inside double braces is evaluated later, when it’s used:

$$example{}
\$\$name = {Alex}
\$\$greeting = {{Hello \$\$name{}}}
\$\$greeting{}
\$\$name = {Chris}
\$\$greeting{}

$$exampleRight{}
<br><br>

Hello Alex
<br>

Hello Chris

$$endExample{}

In the first example, the value of \$\$greeting is immediately set to Hello Alex. But in the second example, because it uses double braces, \$\$greeting is set to Hello \$\$name{}.

This is a somewhat subtle feature that’s easy to get wrong. As a general rule, use single braces when you’re referring to a variable in your text and double braces when you’re defining a big, complicated variable:

$$exampleSimple{}
Hello, \$\$name{}
\$\$myBigComplicatedMacro = {{…

$$endExample{} 


### Multi-line variables
Variables can contain multiple lines:

$$example{}
\$\$people = {Alex
Blair
Chris}
\$\$people{}

$$exampleRight{}
Alex
Blair
Chris

$$endExample{}

If you prefer, you can use a pipe symbol (|) to separate lines of a variable:

$$example{}
\$\$people = {Alex | Blair | Chris}
\$\$people{}

$$exampleRight{}
Alex
Blair
Chris

$$endExample{}


### Variables can have properties
You can assign properties to variables:

$$example{}
\$\$person.first = {Alex}
\$\$person.last = {Bobo}
My first name is: \$\$person.first{}
My last name is: \$\$person.last{}

$$exampleRight{}
My first name is: Alex
My last name is: Bobo

$$endExample{}

You can assign multiple properties at once:

$$example{}
\$\$person = {|.first Alex |.last Bobo}
My first name is: \$\$person.first{}
My last name is: \$\$person.last{}

$$exampleRight{}
My first name is: Alex
My last name is: Bobo

$$endExample{}


### Indexing into variables
You can access individual lines of a variable:

$$example{}
\$\$people = {Alex
Blair
Chris}
\$\$people[1]{}

$$exampleRight{}
Blair

$$endExample{}


### Appending to variables
You can append to a variable using +=:

$$example{}
\$\$people = {Alex}
\$\$people += {Blair}
\$\$people{}

$$exampleRight{}
Alex
Blair

$$endExample{}



[Next: Conditionals](2bConditionals.html)
