This is a field guide to JavaScript.

  1. // Single-line comment // Single-Line Comment

    This is a tag for a single-line comment. Everything from this character forward until the end of the line is disregarded by the JavaScript engine.

  2. <!-- Single-line comment // Alternate Single-Line Comment

    This is another tag for a single-line comment, derived from HTML. Everything from this character forward until the end of the line is disregarded by the JavaScript engine.

    The HTML closing comment tag, “-->”, is not required.

  3. /* Multi-line comment line 1, // Multi-Line Comment Opening Tag

    This is a tag for multi-line comments. Everything between the opening “/*” tag and the closing “*/” tag is disregarded by the JavaScript engine.

    This style of comment can also comment out a partial line.

  4.    and line 2.
  5. */ // Multi-Line Comment Closing Tag
  6. "use strict"; // Strict Mode Statement

    This statement enables strict mode for a script. It must precede any other statements.

    It can be activated inside a function, but not inside block statements using “{}”, such as if/else statements. Concatenating conflicting scripts will make the entire script use strict mode.

    Strict mode:

    • converts mistakes into errors/exceptions:
      • accidental declaration of global variables (variables first referenced without var, let, or const)
      • assignment to a non-writable object, such as the global NaN variable or an object’s non-writable property
      • deletion of undeletable properties (such as an object’s Prototype property)
      • duplicate property names
      • declaration of octal numbers (numbers preceded with a “0”)
    • simplifies variable uses:
    • makes “eval” and “arguments” simpler:
      • prohibition of binding or assignments to “eval” and “arguments” keywords
      • prohibition of aliasing properties of “arguments” objects created within code
      • removal of “arguments.callee” support
    • improves JavaScript security
    • removal of forced object boxing for “this” keyword
    • removal of access to JavaScript stack via “caller” and “arguments” keywords
    • removal of access to function call’s variables from “arguments” keyword
    • addition of more reserved keywords
    • prohibition of function statements not at the top level of a script or function

  7. true; // Reserved Word/Literal

    This word is one of JavaScript’s reserved keywords. They are reserved and not for use as identifiers.

    Current reserved keywords are

    • break
    • case
    • class
    • catch
    • const
    • continue
    • debugger
    • default
    • delete
    • do
    • else
    • export
    • extends
    • finally
    • for
    • function
    • if
    • import
    • in
    • instanceof
    • let
    • new
    • return
    • super
    • switch/li>
    • this
    • throw
    • try
    • typeof
    • var
    • void
    • while
    • with
    • yield

    Future reserved keywords are:

    • abstract
    • await
    • boolean
    • byte
    • char
    • double
    • enum
    • final
    • float
    • goto
    • implements
    • int
    • interface
    • long
    • native
    • package
    • private
    • protected
    • public
    • short
    • static
    • synchronized
    • transient
    • volatile

    Current reserved keywords for literals:

    • true
    • false
    • null

  8. var exampleVariable1; // Variable Declaration

    This keyword declares a variable.

    The default value of a declared variable without an assignment is either “undefined” or “”, depending on the browser.

    The scope of a declared variable is the execution context in which they are declared. (Undeclared variables are global.)

    Declared variables are created before any code is executed. Thus, they do not need to be declared before they are used. (This is called “hoisting”.)

    Variable names, like all JavaScript identifiers, must start with:

    • a letter
    • underscore
    • $ sign
    • Unicode letters

    Declared variables are non-configurable properties of their execution context and thus cannot be deleted.

  9. var exampleVariable2, exampleVariable3 // Multiple Variable Declaration

    Multiple variables can be declared on the same line, separated by commas.

  10. var exampleVariable = 1; // Declared Variable with Assignment

    Values can be assigned to variables when they are declared.

  11. undeclaredVariable; // Undeclared Variable

    Variables declared without the “var” keyword are undeclared variables. They become properties of the global object, and thus have global scope.

    Assigning values to an undeclared variable throws a ReferenceError in strict mode.

    Undeclared variables do not exist until the code assigning to them is executed, even though they have global scope.

    Undeclared variables can be deleted, unlike declared variables.

  12. const exampleConst = 5 // ECMAScript 6: Const Constant Declaration

    This keyword declares a constant, read-only reference to a value. The value of the referenced object may change, but the variable identifier cannot be redefined in that scope. Its value cannot change through assignment.

    Constants must be initialized with a value, unlike variables.

  13. let exampleLocal = 5; // ECMAScript 6: let Declaration

    This keyword declares a block-level local variable.

    If used, it must be in a script labeled: “<script type="application/javascript;version=1.7"> ” or greater.

  14. $exampleBoolean = true; // Boolean Primitive Type

    This is a variable assignment of the boolean primitive type.

    The possible values of a boolean type are:

    • true
    • false

    A number of values are equal (but not identical) to false: false, 0, "", null, undefined, and NaN. All other values are true.

    NOTE: Do not confuse the boolean primitive type with the built-in Boolean global object. THe latter is a wrapper for the former.

    NOTE: JavaScript is dynamically typed! You can assign this variable a string value.

  15. $exampleNull = null; // Null Primitive Type

    This is a variable assignment of the null primitive type. It represents no object value.

    Null is a literal (not a property of the global object like “undefined”). It is equal to, but not identical to, “undefined”.

    It evaluates to false in boolean contexts and to 0 in numeric contexts.

  16. $exampleUndefined = undefined; // Undefined Primitive Type

    This is a variable assignment of the undefined primitive type.

    Undefined is a property of the global object. (In other words, it is a variable of global scope.)

    It evaluates to false in a boolean context and to NaN in numeric contexts.

    A variable that has not been assigned a value is undefined. Methods and statements return undefined if one of their variables being evaluated is undefined. A function returns undefined if a value is not returned.

  17. $exampleNumber = 5; // Number Primitive Type

    This is a declaration of the Number primitive type.

    The possible values of a number type are:

    Its usual wrapper the Number object.

  18. $exampleString = "hello"; // String Primitive Object

    This is a declaration of a string primitive type.

    A string is a set of 16-bit integer values, with each element occupying a 0-indexed position in the string. It is not mutable.

    Its usual wrapper the String object.

  19. $exampleSymbol = Symbol(); // ECMAScript 6: Symbol Primitive Type

    This is a declaration of a Symbol primitive type.

    A symbol is a unique and immutable data type that may be used as an identifier for object properties.

    Even symbols of the same value and type are not considered identical to each other.

  20. $exampleObject = new Object(); // Object Declaration

    This is a declaration of an object.

    Objects, like primitive types, are basic to JavaScript.

    Objects can be seen as collections of properties. (Properties are objects scoped to an object. Functions are a type of object.)

  21. $exampleNumber = parseInt(“5.1”); // String-to-Number Conversion

    This function parses a string value and returns a whole number.

  22. $exampleNumber = parseFloat(“5.1”); // String-to-Number Conversion

    This function parses a string value and returns a floating point number.

  23. $exampleNumber = +”5.1″; // String-to-Number Conversion (Operator)

    The unary plus operator will also change string values to number values, as will any operator other than addition. (The addition operator for strings is reserved for concatenation.)

  24. //