1.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 or "output screen" provides visual output to the developer (that's you!).
Important - when using JavaScript in a website, the user does not see the console.
We can print "to the console" with console.log();
For example: console.log("This is a string of text");
Will output This is a string of text
There are special "escape characters" for new lines (\n), tabs (\t), slash (\\), and more.
1.2 - Variables
Table of Contents:
- Full written lesson with examples
- Video lesson from CodeHS
- Practice task instructions
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.
Variables can be different types of data:
let myNumber = -45;
let myString = "Some text, in quotes";
let myBoolean = true; // or false
Strings
Can be combined (concatenated) using the '+' operator
myString = "This " + "that"; // the value is now 'This that'Strings cannot be subtracted
myString = "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