The general rules for constructing names for variables (unique
identifiers) are:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter
- Names can also begin with $ and _ (but we will not use it in this tutorial)
- Names are case sensitive (y and Y are different variables)
- Reserved words (like JavaScript keywords) cannot be used as names
- JavaScript identifiers are case-sensitive.
Types of Data
- Numbersà 1, 2, 3
var length = 16; - Stringsà ‘Zombies freak me out!’
Must always be surrounded by quote marks
var lastName = "Johnson";
- Booleanà true, false
var x = true; var y = false; - Objects
var x = { firstName:"John", lastName:"Doe" }; - Arrays
var cars = ["Saab", "Volvo", "BMW"];
Comments
Add comments to your script to explain what it does. It will also make
your code easier for others to read and understand.
Add a single-line comment by placing two forward slash characters //
in front of your comment. Anything after the slashes won’t be interpreted by
the browser.
Add a multi-line comment by starting with the /* characters and
ending with the */ characters. Anything between these characters won’t be
interpreted by the browser.
/These comments are typically reserved for describing how an entire script file works or to comment
out an entire block of script. /
//this function does something awesome!
function doSomethingAwesome() {