Numbers (Values)
Math is the basis of computer science. We looked at strings first but that's only because we're used to typing words and sentences. Also, we learned about output. What about numeric variables?
Values
Dynamically-typed (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
.
Statically-typed (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 |
| Add. Returns 22 |
| Subtract. Returns 15 |
| Multiply. Returns 38 |
| Divide. Returns 4.75 |
| Modulo. Returns the remainder after a division. In this case, it returns 3 since 0.75 of 4 is 3. If no remainder (ie. 10 % 5), modulo returns 0. |
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 |
| Assign the value 3 to variable |
| Increment the value of |
| Decrement the value of |
| Add, subtract, multiply, or divide some value, in this case 2, to |
Shortcuts:
| These also add, etc, any value, to |
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.
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 |
|
| Equal in value and type |
|
| Not equal |
|
| Not equal in value or type |
Since they are not equal in type |
| Less than or greater than | From left-to-right |
| Less than or equal to greater than or equal to | From left-to-right |
| Logical operator and |
|
| Logical operator or |
|
| Logical operator not |
|
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 0x <= y
is read "x is less than or equal to y"
Undefined
When a variable is created in memory, it has no value. It has a name, but the variable itself has not been defined.
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:
Last updated