(or jump ahead to your task)
Giving output is fairly easy with console.log( )
. But what if we wanted to hold on to a value to be used later or for calculations?
Perhaps we want to hold a lot of values like a quadratic equation, the locations of items in a 3D space, a list of colours, phone numbers, or financials.
Don't feel like reading? Watch this video.
Variables hold numbers or text directly in the memory in the computer. We reserve a block of space by asking for one, just like in math class:
Let w represent the number of watermelons
Let apple represent the number of apples
w = 2
apples = 7
Let shopping_list = w + apples
Variables in JavaScript are very similar:
let w = 2;
let apples = 7;
let myList = w + apples; // sets the variable 'myList' to 9
console.log(myList); // output the value of 'myList'
Note - you might see the keyword var
being used online instead of let
. You should always use let
when creating a variable.
After being declared, the variable can be used as much as you want. Think of it like a box, a bucket, or envelope.
- To declare variables, we will use the
let
statement (similar to math class). - We use the 'assignment' operator (a single =) to assign or change the value.
- It is not like algebra where left = right
- Variables can be reused, manipulated, and destroyed.
For example, if we want to change the value of w
to 4:
let w = 2;
w = 4;
โ๏ธ We did not use the keyword let
both times, only when declaring the variable.
๐ Note: You can declare a variable without giving it a value.
- Create a new code file (call it
1.2 - Variables.js
or something similar) - Create a variable called
age
and make it equal to your age - Output your age to the console
- Run your code to see what happens
let myNumber = -45;
let myString = "Some text, in quotes";
let myBoolean = true; // or false
Strings can be combined (concatenated) using the +
operator
let myString = "This is text";
myString = "This " + "and " + "that"; // the value is now 'This and that'
Strings cannot be subtracted!
myString = "This and that" - "that"; // not possible
Anything with value is considered 'true'
let sample = true; // obviously true
let sample1 = 5; // considered true
let sample2 = 0; // considered false
let sample3; // considered false
Need more help on variables? (Tutorial on variables) (w3schools reference)
Code IDEs have numbers on each line to easily talk-about and find specific areas. If there is an error, the interpreter will give you the line number it thinks is the problem. For example, this error says the problem is on line 12:
/home/example/index.js:12
SyntaxError: Unexpected token ')'
- Create a variable
length
and set it equal to 10 - Create a variable
width
and set it equal to 5 - Calculate the area of a rectangle using
length
andwidth
- store the answer in a new variablearea
(Note: to multiply, use the 'asterisk'*
) Example:
let x = 3;
let multiplied_value = x * 4; // This would be 12
- Output the value of the
area
variable to the console. - Output the area again, but this time like this:
Area: 50
{use thearea
variable for the number}
- Create a variable called
first_name
and set the value to "Santa". - Create a variable called
last_name
and set the value to "Claws". - On the next code line, set the value of
last_name
to "Claus". - Using the variables you made in 1 and 2, print "I still believe in Santa Claus." to the console.
No need to humble brag. Find something to learn.
For example: learn how to start a C++ program and recreate the above task in C++.