- Part 1: Creating and Using Variables
- Part 2: Data Types and Arrays
- Part 3: Functions
- Part 4: Objects
- Part 5: Document Object Model
- Part 6: Conditional Statements
- Part 7: Loops
Creating and Using Variables
The metaphor of a bucket is often useful in thinking about a variable. When using a variable, the two common tasks are either creating (“putting something in the bucket”) or using (“seeing what is already in the bucket”).
The value of the variable bucket, then, is 1.
Variables can also be created using many different possible names. The only requirement is that the name of the variable not start with a number or contain spaces. They can contain the underscore.
Using Math on Variables
The assignment of a variable need to be just a numerical value. It can also be some type of mathematical operation between other variables. Frequently, it is useful to create variables to store values as part of a much larger project to allow the programmer to more easily understand some process.
Any mathematical operation like addition, subtraction, multiplication, and division can be carried out on variables and used as part of its assignment.
Because it would be inefficient to keep creating new variables with each operation, the assignment process can happen without the keyword “var” in the line. In these cases, the value of the variable is changed instead of creating a new variable.
Like with creating variables as explained earlier, the lines are read from its right-hand side to its left-hand. Some type of calculation is done on the right-hand side and its final value is assigned as the value of the variable on the left.
For testing purposes, web browsers provide functionality called a console. The term is borrowed from a past usage of where code would be input in one place, a console, and then run somewhere else. In order to test the code, any error could be shown where the programmer was: the console.
In webpage terminology, the console is a place where errors or warnings show up. Often, when the web browser encounters a problem or something it does not understand, it will put information in the console.
Finding the console is often different in every web browser.
In Google Chrome, the console is part of the Developer Tools. Can be be accessed through the extended menu of the page and then More Tools –> Developer Tools. It is the second tab.
In Mozilla Firefox, the console can be found through Web Developer –> Web Console.