> For the complete documentation index, see [llms.txt](https://icheka-ozuru.gitbook.io/sonarlang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://icheka-ozuru.gitbook.io/sonarlang/syntax-basics/variables.md).

# Variables

If you don't know how data types work in **Sonar**, please visit the [**Data Types**](/sonarlang/syntax-basics/data-types.md) section first.

When we create programs, we will usually want to store some kind of data so that we can use it at some later point in our program. For example, if we were writing a phonebook, we would want to be able to save the names and phone numbers of our contacts.

In Sonar, we can save our data by using **variables.**

### **What is a Variable?**

A variable can be defined as a temporary location for storing data in our programs. Variables are temporary because they don't save their data forever. Instead, they keep data for as long as it takes our programs to end. This way, we can use our data while our program is doing work, but as soon as our program's work is finished, our data (which will no longer be needed now) will be automatically erased from the computer. The computer does this so that it will always have enough space in its memory to do other things (like playing music, streaming Netflix or running your own programs).

In Sonar, it is very easy to create variables. Variables are created using the **let** keyword.

> A "keyword" is a special word that has special meaning in Sonar. Keywords are often used to perform very important functions that make it possible for our programs to work.

To create a variable, you need three things:

1. The **let** keyword
2. A name for your variable
3. An **equals** symbol, followed by the data to store in your variable

For example:

```javascript
>> let name = "Cinderella"
// [1] [2]    [3]
```

{% hint style="info" %}
Sonar programs read like English.
{% endhint %}

This will create a variable in your program called "name". Your new **name** variable will contain the data "Cinderella".

We can use this variable in our program simply by writing its name:

```javascript
>> "Her name is " + name
// outputs Her name is Cinderella
```

Variables can be used to store all kinds of data:

{% code lineNumbers="true" %}

```javascript
let name = "Bob" // string
let age = 10 // number
let is_healthy = true // boolean
```

{% endcode %}

And variables that hold data can be used directly in programs:

```javascript
>> 1 + 2
// outputs 3
```

is equivalent to:

```javascript
>> let one = 1
>> let two = 2
>> one + two
// outputs 3
```

Variables and their data are always interchangeable.

> The process of creating a variable is called "variable declaration".

### Variable Re-assignment

After a variable has been created (or "declared"), it can be re-assigned. This means that its value can be changed to a different one. To re-assign a variable, simply write the name of the variable, followed by an **equals** (=) symbol, and the value you want to assign to the variable.

{% code lineNumbers="true" %}

```javascript
let month = "January"
print(month)
// outputs January

month = "February" // assign the string "February" to month
print(month)
// outputs February
```

{% endcode %}

A variable that has not been declared cannot be assigned to or used.

```javascript
favourite_food = "Pizza"
// this will cause a special kind of computer error called "syntax error" because
// "favourite_food" has not been declared using the "let" keyword
```

### Double-operator Assignment

Imagine you need have a variable that has a value of 1, and you need to add 2 to it, you might write:

{% code lineNumbers="true" %}

```javascript
let number = 1
number = number + 2
print(number)
// outputs 3
```

{% endcode %}

Unlike many programming languages, **Sonar** allows you to use a simpler, faster variable assignment syntax to obtain the same effect:

{% code lineNumbers="true" %}

```javascript
let number = 1
number += 2 // <-- double-operator assignment here
print(number)
// outputs 3
```

{% endcode %}

This will add 2 to the original value of `number` (1), and assigns the result back to `number`.

There are four double-operator assignment operators:

<table><thead><tr><th width="249">Operator</th><th width="249">Example</th><th width="250">Expanded form</th></tr></thead><tbody><tr><td>+=</td><td>variable += 1</td><td>variable = variable + 1</td></tr><tr><td>-=</td><td>variable -= 1</td><td>variable = variable - 1</td></tr><tr><td>*=</td><td>variable *= 1</td><td>variable = variable * 1</td></tr><tr><td>/=</td><td>variable /= 1</td><td>variable = variable / 1</td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://icheka-ozuru.gitbook.io/sonarlang/syntax-basics/variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
