JavaScript Reference: Syntax and Structure


Case Sensitivity

JavaScript is a case-sensitive language, which means that the strings 'hello', 'Hello', and 'HELLO' would not be equal. JavaScript keywords, like function and for, must be written in lowercase. Uppercase letters can be used when creating variable names. See the Style section for recommended use of lower and upper case letters.

Semicolons

Statements can be separated by a semicolon (;) or by placing each statement on its own line. Statements separated by a line break can still end with a semicolon, but it is not required.
// Two statements, separated by line break and semicolon:
var x = 'hello';
var y = 20;

// The same statements, separated by line breaks only:
var x = 'hello'
var y = 20

// Two statements, separated only by a semicolon:
var x = 'hello'; var y = 20

// All three versions above are equivalent. 

Comments

Single line comments are preceded by //:
// Comments can be written by themselves...
var x = 5 		// or on the same line as code

Multiple lines can be commented, when surrounded by the characters /* and */:
/* This is also a comment
   that has multiple lines
   for lots of information */

Quotation Marks

Strings in JavaScript can be surrounded by either single or double quotation marks.
var newString = "I'm a string with double quotation marks"
var aNewString = 'This is a string with single quotation marks'
var anotherNewString = 'And I'm a string with too many quotation marks'
The reason you'd want to use a mix of both can be seen in the color highlighting of the above example; the apostrophe in the word "I'm" was interpreted as the end of the string. Instead, we'll surround the string in double quotes:
var anotherNewString = "And I'm a string with too many quotation marks"

Variables

Types of Variables

A variable in JavaScript can hold one of several data types, including numbers, strings, arrays, booleans, functions, objects, etc. JavaScript is a dynamic language, so we don't need to specify the type of our variable, and we can change it at any time. We assign a value to a variable using the '=' operator.
sampleVariable = 6							// define sampleVariable to have value 6
sampleVariable = "now I'm a string"		// define sampleVariable to be a string

You'll still need to pay attention to variable type when performing operations. For instance, we can add two number values together, and create a function that takes an array as a parameter, but we can't 'add' a string and a number or pass a string to that function.

You can retrieve the type of a variable using the typeof operator:
var sampleString = 'Hello world'
var x = false
var k = 5
var t
typeof sampleString		// returns 'string'
typeof x						// returns 'boolean'
typeof k						// returns 'number'
typeof t						// returns 'undefined'

We can also define multiple variables on one line:
var x = 5, y = 9, z = true
var a = b = 2
console.log( x,y,z,a,b )
would result in
5 9 true 2 2


Declaring and Initializing Variables

JavaScript variables are declared with var. The variable can be given (almost) any name you choose, and may optionally be given a value. Variables whose value is not provided are 'undefined' objects.
var sampleString = 'Hello world'
var x 
var k = 5
console.log( sampleString, x, k )
would result in
Hello world undefined 5


Global and Local Variables

Variables that are created outside of a function are global, meaning that any script on the page can access their value. This includes variables defined on additional .js files that are included in the page's heading. Variables declared within functions or statements are local to the function, and are not available outside that function or statement, even after the function is executed. Variables that are not declared within a function are automatically treated as global.
function test() {
    var x = 10
    y = 5
}
console.log(y)
console.log(x)
would result in
5 ReferenceError: Can't find variable: x

This includes variables that appear in statements, like for loops:
for ( i = 0; i < 10; i++ ) {
    // do something
}
console.log(i)
would result in
10 // current value of i

Naming Variables, Functions, and Objects

Variables should be given a name that adheres to the following requirements and conventions:
  • Can be formed from the 26 letters a,b,c,...,z (upper or lower case), the 10 digits 0,1,2,...,9, or an underscore _ symbol. $ signs are permitted but not recommended.
  • Local variables (most!) should start with a lowercase letter
  • Global variables (rare!) should be formed with uppercase letters
Here's a tool to help you check if a variable name is ok.