If you want an external file, you still need to use a script tag, but that is to reference the external file:
Learning the Language
Each command should be on a separate line. It is not required, but is considered a best practice. Likewise, it is best practice to put a semi-colon (;) at the end of each command. This reduces the chance of errors in some cases.
- Because you want to exclude some old code that you no longer need, but may want later
- Because you want to exclude code that you think may be breaking your program
- Because you want to leave a note about what you are doing.
There are two types of comments, single line, and multi-line.
With Single Line comments, you use two forward slashes, and the comment ends at the end of that line.
With Multi-Line Comments, you use a /* to start it, and a */ to end it. It can be over as many lines as you want. It can also be used to just comment out part of a line of code. See the examples below:
// Example of a Single Line Comment /* This is a multi line comment */ /* So is this */
You can also define a variable as a run time storage device. This will store information over the long-term so it can be accessed again later.
var userName = "Tom"; // this is an example of a string // stings are enclosed with either a single quote or a double quote var age = 25; // This is an integer number var height = 6.167; // This is a floating point number var today = new Date(); // Date is a built-in object for working with dates.
When you do math, you can use variables to hold values (like in algebra), and also store, or display the results.
alert( 5 + 7 ); var x = 5; var y = 7; var z = 0; alert( x * y ); z = x - y; alert(z);