var

The var statement declares variables, and optionally initializes them.

var variable [ = value ] [, variable2 [ = value2], ...]
  • When a object or variable is explicitely declared with var, its value may optionally be initialized.
  • There are general two scopes in JS: function scope and application/global scope. Looping statements such as for or while look like "functions" and their counters are often declared within their parentheses, (EG: var i = 0) but those variables are not local to the loop, but rather are in the same scope as the loop statement.
  • Scope conffusion can cause several problems, including "hoisting":
    var i = 2;
    function test () {
        alert(i);
        var i = 3;
    }
    test(); //returns undefined!
    
    //Why? The above code is actually equivalent to the following:
    var i = 2;
    function test () {
        var i;
        alert(i);
        i = 3;
    }
    test(); //returns undefined!
    
  • If a variable is initialized without an explicit declaration (i.e. without var), then it is implicitly declared automatically and assumes the scope it was written in. There is no JavaScript equivalent to "Option Explicit" which forces explicit declarations.
  • One practice is always explictly declare variables at the top of the application or at the top of each function.
  • JavaScript variables are loosely typed, meaning that the data type can change under different circumstances.
  • The first character of must be a letter (a-z or A- Z), an underscore (_), or a dollar sign ($). As of JS 1.5 you can also use any Unicode letter or Unicode escape sequence (\uXXXX).
  • The other characters can also include numbers (0-9).
  • Identifiers must not be a reserved word.
  • JavaScript is case sensitive!

label: statement

Indicates that starting point of a statement block.

labelName: statements

let

The let statement declares variables, and optionally initializes them, in a scope limited to the block, statement, or expression on which it is used. Since Mozilla JavaScript 1.7 but not available in most other flavors of JavaScript.

// In an expression:
var a = 5;
let(a = 6) alert(a); // 6
alert(a); // 5

// In a block:
var a = 5;
var b = 10;
if (a === 5) {
    let a = 4; // The scope is inside the if-block
    var b = 1; // The scope is inside the function
    console.log(a);  // 4
    console.log(b);  // 1
}
console.log(a); // 5
console.log(b); // 1

// In a for loop:
for (let i = 0; i < 10; i++) {
    alert(i); // 1, 2, 3, 4 ... 9
}
alert(i); // i is not defined

this

this.property

Refers to the current object and is commonly used when setting multiple properties. EG:

function Car(color, make, model) {
    this.color = color;
    this.make = make;
    this.model = model;
}

Another common usage is to pass the current form to a function. EG:

<input type="button" value="Run" onClick="RunMe(this.form)">

typeof

typeof expression
typeof (expression)

Returns string indicating the type in JS. The 6 types in JS are as follows.

  • string. EG: typeof 'hi';
  • number. This includes the following.
    • integer.
      • decimal. EGs: -2, -1, 0, 1, 2, ... 8, 9, 10, 11.
      • octal. EGs: 00, 01, ... 07, 010, 011.
      • hexadecimals. EGs: 0x0, 0X1, ..., 0x9, 0XA, 0xb, ...0xF, 0x10, 0x11.
    • floating points. EGs: -12e3, 1.3E2, 2.7e-4.
  • boolean. true or false.
  • object. EG: typeof null;
  • function. EGs:
    • var f=new Function(); typeof f; //function
    • var g=new Function('return 7'); typeof f; //number
  • undefined. EGs:
    • var x='x'; typeof x; //string
    • y; typeof y; //undefined
    • typeof z; //undefined
    • typeof void(0); //undefined
    • var g=new Function(void(0)); typeof g(); //undefined
    • var h=new Function(); typeof h(); //undefined

There are some particular about applying typeof on different global objects or functions. EGs:

  • typeof Math //object
  • typeof Date //function
  • typeof Function //function
  • typeof Object //function
  • typeof String //function

with

with (object) {
    statement;
}

Establishes the default object for the statement. Assumes that each variable in the statement is a property/object of the default object.

Reserved Words

The following table lists reserved words that cannot be used as JavaScript variables, member names, function parameters, or statement labels. Oddly enough, only the ones in bold below are even used by JavaScript, the rest are reserved according to the ECMAScript specifications.

abstract
boolean
break
byte
case
catch
char
class
const
continue
debugger
default
delete
do
double
else
enum
export
extends
false
final
finally
float
for
function
goto
if
implements
import
in
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
super
switch
synchronized
this
throw
throws
transient
true
try
typeof
var
void
volatile
while
with

Intrinsic Properties

These are top level properties that are neither user defined variables or properties associated with objects.

  • infinity. The value of this infinity is always greater than any number including itself.
  • NaN. Not-a-Number. The value of this NaN is always not equal to any number including itself.
  • undefined. The value and type of a variable if it has not been defined yet.


GeorgeHernandez.comSome rights reserved