# Numbers (Values)

## Values

Dynamically-[typed](https://hackernoon.com/i-finally-understand-static-vs-dynamic-typing-and-you-will-too-ad0c2bd0acc7) (or loosely-typed) languages like Javascript take the guesswork out of memory management. These languages treat a number as just a value - no specific - so we don't need to worry about declaring a `double` vs. an `integer`.&#x20;

Statically-[typed](https://hackernoon.com/i-finally-understand-static-vs-dynamic-typing-and-you-will-too-ad0c2bd0acc7) (or strongly-typed) languages like C++ do not have active memory management. It is up to the programmer to know and declare the exact type of variable (memory space) required. This is because a number without decimals (`integer`) takes less memory than a number with decimals (`double` or `float`).

## Mathematics

Any sort of mathematics can be done in most programming languages. It is up to the programmer to learn the built-in mathematics as well as to create any new required computations. Here are the basic mathematical commands available. \
Assume `x` has a value of `19`.  (`let x = 19;`)

| Code    | Description                                                                                                                                                               |
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `x + 3` | Add. Returns 22                                                                                                                                                           |
| `x - 4` | Subtract. Returns 15                                                                                                                                                      |
| `x * 2` | Multiply. Returns 38                                                                                                                                                      |
| `x / 4` | Divide. Returns 4.75                                                                                                                                                      |
| `x % 4` | <p>Modulo. Returns the <em>remainder</em> after a division.<br>In this case, it returns 3 since 0.75 of 4 is 3.</p><p>If no remainder (ie. 10 % 5), modulo returns 0.</p> |

### BEDMAS (PEMDAS)

Computers and programming languages do their best to follow proper order of operations for mathematics. That being said, the interpreter or compiler can only interpret your code as best as possible. For this reason, it's important to utilize brackets (or parentheses) `( )` properly!

For example,  `1 + 8 / 2` is quite different from `(1 + 8) / 2`.

## Assignment Operators

Assigning a numeric value to a variable is straight-forward. Any math that is done must also be stored, either in a new variable or back into the current one. The list below is an incomplete list of the ways you can assign a value to memory.

| Code                                                                                                                       | Description                                                                                                                                                                                                       |
| -------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `x = 3`                                                                                                                    | [Assign](/unit-2/variables/numbers.md#left-hand-assignment) the value 3 to variable `x`.                                                                                                                          |
| `++x` or `x++`                                                                                                             | Increment the value of `x` by one *before* or *after* the current value is utilized.                                                                                                                              |
| `--x` or `x--`                                                                                                             | Decrement the value of `x` by one *before* or *after* the current value is utilized.                                                                                                                              |
| <p><code>x = x + 2</code></p><p><code>x = x - 2</code></p><p><code>x = x \* 2</code></p><p><code>x = x / 2</code></p>      | <p>Add, subtract, multiply, or divide some value, in this case 2, to <code>x</code>.<br>This <a href="/pages/-LNhwaKEdFezr0Na3tSQ#left-hand-assignment"><em>assigns</em></a> <code>x</code> to the new value.</p> |
| <p>Shortcuts:</p><p><code>x += 2</code></p><p><code>x -= 2</code></p><p><code>x \*= 2</code></p><p><code>x /= 2</code></p> | <p>These also add, etc, any value, to <code>x</code>. <br>They are a shortcut to the lines above.</p>                                                                                                             |

### Left-Hand Assignment

There is a standard in programming that the variable or item being used to contain data is on the left of an operator and the mathematics or item(s) being assigned to that variable is on the right.

![](https://docs.google.com/drawings/d/e/2PACX-1vSR-1W_ru_skQKjTkAG4Np8eTgHMcMq__fwb_xLLn2zP6QxCYYyTezV4EgJNQYHwgg5Tt1k9G-38GaI/pub?h=100)

**Important:**  `x = 5` will assign 5 to the variable x while `5 = x` will throw an exception because it is not possible to store x into the value 5. This becomes important when comparing two items (see below). **You** *can not* **use a single `=` to compare, it assigns**.

## Comparison Operators

In order to make decisions we must be able to compare values. Comparisons typically return a `true` or `false`. Below is an incomplete list of the ways you can compare two or more values.

| Code         | Description                                               | Notes                                                                                            |
| ------------ | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| `==`         | Equal in value                                            | `5 == "5"` is `true`                                                                             |
| `===`        | Equal in value *and* type                                 | `5 === "5"` is `false`                                                                           |
| `!=`         | Not equal                                                 | <p><code>5 != "5"</code> is <code>false</code> <br>Since they are equal in value</p>             |
| `!==`        | Not equal in value *or* type                              | <p><code>5 !== "5"</code> is <code>true</code> </p><p>Since they are not equal in type</p>       |
| `<` or `>`   | Less than or greater than                                 | From left-to-right                                                                               |
| `<=` or `=>` | <p>Less than or equal to <br>greater than or equal to</p> | From left-to-right                                                                               |
| `&&`         | Logical operator *and*                                    | `true && false` is `false`                                                                       |
| `\|\|`       | Logical operator *or*                                     | `true \|\| false` is `true`                                                                      |
| `!`          | Logical operator *not*                                    | <p><code>!true</code> is <code>false</code></p><p><code>!(5 < 1)</code> is <code>true</code></p> |

### Greater / Less Than

It is easy to confuse the terminology or logic with the operators `<`, `>`, `<=`, and `>=`. **You should be reading it from left-to-right**. Here are some examples:

> `4 < 10` is read "four is less than 10"\
> `100 > 6` is read "one hundred is greater than six"\
> `someVariable >= 0` is read "some variable is greater than or equal to 0\
> `x <= y` is read "x is less than or equal to y"

{% hint style="danger" %}
This will become *critical* when you begin to use [**`if`**](/unit-3/conditionals/ifelse.md) statements and [**`loops`**](/unit-3/loops.md).
{% endhint %}

## Undefined

When a variable is created in memory, it has no value. It has a name, but the variable itself has not been *defined*.

```javascript
let x = 3;  // Has the value 3
let y;      // Has no value, specifically it has no definition
console.log(x + "\n" + y)  
/* Output:
3
undefined
*/
```

## NaN

If a variable has value (is defined) but cannot be represented with a number (value) Javascript returns `NaN` which represents "Not a Number".

You can check to see if something is a number:

```javascript
let myNumber = 3;
let myString = "Hello";
isNaN(myNumber);  // false
isNaN(myString);  // true
/*  Remember, to print those results, use console.log()
    and to store them, you need a variable:
        let result = isNaN(myString);                  */
```


---

# Agent Instructions: 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://cs.brash.ca/unit-2/variables/numbers.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.
