2.2 - Variables
Review
Web browsers can run a scripting language called JavaScript. The syntax is very similar to C, C++, C#, Java, etc...
Last class we learned about something called the console.
The JavaScript console provides printed output to the developer (that's you!).
on a website, the user normally does not see the console but they can - so be careful what you put there.
We can print to the console with console.log();
For example: console.log("This is a string of text")
There are special "escape characters" for new lines (\n), tabs (\t), slash (\\), and more.
2.2 - Variables
A shortened version of the lesson is below.
A more detailed lesson is in the repository: 2.2 - Variables
Synopsis:
Variables hold data in the memory of the computer.
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. I will essentially assume you cheated if I see var in your code.
Variables can be modified and reused:
When modifying the value of a variable you do not need to use the let keyword again - the variable already exists!
let age = 7;
age = 25;
age = age + 1;
Variables can be different types of data:
let my_number = -45;
let my_string = "Some text, in quotes";
let my_boolean = true; // or false
Strings
Can be combined (concatenated) using the '+' operator
my_string = "This " + "that"; // the value is now 'This that'Strings cannot be subtracted
my_string = "This that" - "that"; // not possible
Booleans
Anything with value is considered 'true'
let sample1 = 5; // considered true
let sample2 = 0; // considered false
let sample3; // considered false