In this lesson, we will discuss the use of variables
in programming, with a focus on the following key points:
- What are
variables
, and how are they used in a computer program? - Learn to work with math operations in JavaScript.
For the Python version of this lesson, please click here.
The act of storing information in programming languages is important. Often, during simple or complex calculations, we need to reference some saved
information at a later point. In programming languages, this storing of information is performed using variables
. In this lesson, we will discuss how variables
are used, and some basic math operations
that can be utilized.
Variables
Variables can store and manipulate data in a programming language. Data values can be assigned to a variable using the assignment operator
-- which is the equal sign
(=
). Once a value is assigned to the variable, it corresponds to that value unless it is re-assigned. Note: re-assignment touches upon mutability, which we'll cover later.
Let's see some code examples in JavaScript
below.
Variables in JavaScript are commonly declared by using a var
keyword, followed by a variable name, assignment operator (equals sign =
), and a corresponding value.
1var number = 21;
2console.log(number); // prints 21
This will display the value 21
, and this is verified by printing the variable value on the console.
In ES6, the variable declaration keywords let
and const
were introduced. We covered these keywords briefly in the last lesson. var
has some weaknesses in terms of scope, and let
and const
are now preferred. For a deep dive of the difference between the three, check out this article.
1let number = 21;
2console.log(number); // prints 21
Let's test your variable knowledge with some short questions!
Are you sure you're getting this? Is this statement true or false?
Is the following code snippet a valid way to declare a variable in JavaScript?
1var 12num = 12;
Press true if you believe the statement is correct, or false otherwise.
Try this exercise. Click the correct answer from the options.
What is the value stored in variable c
?
1var a = 10;
2var b = a;
3var c = b;
Click the option that best answers the question.
- 20
- 0
- 10
- Gives an error
Basic Math Operations
In any programming language, one of the very basic tasks is to perform various mathematical calculations. These calculations can be performed by using the arithmetic operators that we have in basic maths! Using these operators combined with some other different aspects of the programming language can produce interesting results. A list of some common mathematical operators in programming languages is given below. The examples assume that the values of a
and b
are equal to 10.
Operator | Operation | Example |
---|---|---|
+ | Addition | a + b = 20 |
- | Subtraction | a - b = 10 |
* | Multiplication | a * b = 100 |
/ | Division | a / b = 1 |
% | Modulus | a % b = 0 |
** | Exponent | a ** b = 1 x 10^10 |
Let's see these operations in action by looking at some code examples in JavaScript.
Math in JavaScript
Math operations in JavaScript can be performed by directly specifying a mathematical statement. If you're using any variables in the calculation, make sure to initialize them beforehand.
1var a = 20;
2var b = 10;
3
4console.log(a + b); // prints 30
5console.log(a - b); // prints 10
6console.log(a * b); // prints 200
You can also save these values in further variables, such as:
1var a = 20;
2var b = 10;
3var c = a + b;
4console.log(c); // prints 30
This is helpful in situations where you need to do more complex calculations, which may require multiple steps.
You must be wondering by now, are these the only mathematical operations supported by JavaScript?
The answer is no.
JavaScript supports an abundance of other operations, however, they are not provided using simple operators. These can be accessed through the JavaScript's built-in Math
object. Simply use Math.<operator-name>
and you can access more maths operators from there such as square root, floor and ceiling functions among many others. You will have to be careful with the spellings of these operators as they are built-in within the programming language.
Let's see some examples.
1var x = Math.floor(4.5);
2console.log(x); // prints 4
3
4var y = Math.sqrt(100);
5console.log(y); //prints 10
A comprehensive list of all operations provided by the JavaScript Math module is available here.
Let's test your knowledge. Click the correct answer from the options.
What will be the output of the following code block in JavaScript?
1var a = 10;
2var b = 36;
3var c = Math.sqrt(b) + a;
4console.log(c);
Click the option that best answers the question.
- 6
- 16
- 36
- 30
Other than the basic operators, JavaScript also has some language-specific operators. Two of these are increment
(denoted by ++
) and decrement
(denoted by --
) operators. The increment operator increases the value of the number by 1, and the decrement operator decreases the value of the number by 1. However, the order of this operator is important. If the increment or decrement operator is placed before the variable name, it is known as the pre-increment
or pre-decrement
operator and the increment or decrement in value occurs before returning it. If the increment or decrement operator is placed after the variable name, it is known as the post-increment
or post-decrement
operator and the increment or decrement in value occurs after returning it.
Let's see some examples.
xxxxxxxxxx
var w = 10;
var x = ++w;
console.log(x); // prints 11
var y = x--;
console.log(y); // prints 11
var z = --y;
console.log(z); //prints 10
Are you sure you're getting this? Fill in the missing part by typing it in.
What will be the output of the following code block in JavaScript?
1var a = 8;
2var b = 12;
3var c = b * a + a;
4console.log(--c);
Write the missing line below.
Summary
In this lesson, basic programming concepts about variables and mathematical operators were introduced. The most important thing about working with these basic concepts is to ensure that there are no syntax errors. Mastering these concepts is important as they will be the building blocks to further techniques in programming!
One Pager Cheat Sheet
- In this lesson, we will discuss how
variables
are used, as well as specific math operations in programming. - A variable is a
placeholder
for storing data values in a programming language which can be assigned or re-assigned using theequal sign
(=
). - Variables in JavaScript are declared by
var
,let
, orconst
, and the preferred form for a deep dive islet
andconst
, which offers improved scope overvar
. - The code snippet is invalid due to the lack of a valid
variable name
after thevar
keyword. - The value of
c
is assigned the same value asb
, which is 10. - Using aritmetic operators, one can perform basic
math operations
like addition, subtraction, multiplication, division, modulus and exponentiation in any programming language. - Math operations in JavaScript can be performed by directly specifying a mathematical statement or by assigning the result to a variable
c
, such asvar c = a + b
for more complex calculations. - JavaScript provides an abundance of mathematical operations through its
Math
object, and you can see examples of some of these with the available list at Mozilla Developer Network. - The output of the code block is
Math.sqrt(b) + a
which results in 16. - JavaScript comes with language-specific operators such as
increment
(++)
anddecrement
(--)
which work differently depending on whether they are pre- or post- placed in relation to a variable. - The value of the
variable
c
is decremented by 1 from 96 to 103 when--c
is applied before thevariable
is returned. - Basic programming concepts such as
variables
andmathematical operators
are building blocks for developing further techniques, and it is important to ensure no syntax errors occur.