JavaScript Syntax

A node can have three different types of attributes. An attribute containing another node, an attribute containing an array of other nodes, or an attribute containing a primitive value such as a boolean, string, or number.

Use grasp --help syntax for an overview of the syntax, and grasp --help node-type (eg. grasp --help if) for more about information about a node type.

Skip to statements, expressions, declarations, clauses, or categories of node types.

Based on the Mozilla SpiderMonkey AST format.

Misc

program (Program)

node array attributes:
body

note:

The root node of a JavaScript program's AST.

ident (Identifier)

primitive attributes:
name

example:

x

literal (Literal)

primitive attributes:
value (val)

examples:

true
1
"string"

prop (Property)

node attributes:
key,  value (val)
primitive attributes:
kind

syntax:

key: value

example:

a: 1

note:

An object expression (obj) has a list of properties, each being a property.

Statements

empty (EmptyStatement)

example:

;

block (BlockStatement)

node array attributes:
body

syntax:

{
  statement_1
  statement_2
  ...
  statement_n
}

example:

{
  x = 1;
  f();
  x++;
}

exp-statement (ExpressionStatement)

node attributes:
expression (exp)

syntax:

expression;

example:

2;

note:

When an expression is used where a statement should be, it is wrapped in an expression statement.

if (IfStatement)

node attributes:
test,  consequent (then),  alternate (alt, else)

syntax:

if (test)
  consequent
[else
  alternate]

examples:

if (even(x)) {
  f(x);
}
if (x === 2) {
  x++;
} else {
  f(x);
}

label (LabeledStatement)

node attributes:
label,  body

syntax:

label: body;

example:

outer:
for (i = 0; i < xs.length; i++) {
  for (j = 0; j < ys.length; j++) {
    if (xs[i] === ys[j]) {
      break outer;
    }
  }
}

break (BreakStatement)

node attributes:
label

syntax:

break [label];

examples:

break;
break outer;

continue (ContinueStatement)

node attributes:
label

syntax:

continue [label];

examples:

continue;
continue outerLoop;

with (WithStatement)

node attributes:
object (obj),  body

syntax:

with (object)
  body

example:

with ({x: 42}) {
  f(x);
}

switch (SwitchStatement)

node attributes:
discriminant
node array attributes:
cases

syntax:

switch (discriminant) {
  case_1
  case_2
  ...
  case_n
}

example:

switch (num) {
  case 1:
    f('one');
    break;
  case 2:
    f('two');
    break;
  default:
    f('too many');
}

return (ReturnStatement)

node attributes:
argument (arg)

syntax:

return argument;

example:

return f(2);

throw (ThrowStatement)

node attributes:
argument (arg)

syntax:

throw argument;

example:

throw new Error("oops");

try (TryStatement)

node attributes:
block,  handler,  finalizer

syntax:

try
  block
[handler]
[finally
   finalizer]

example:

try {
  result = parse(input);
} catch (error) {
  console.error(error.message);
  result = '';
} finally {
  g(result);
}

while (WhileStatement)

node attributes:
test,  body

syntax:

while (test)
  body

example:

while (x < 2) {
  f(x);
  x++;
}

do-while (DoWhileStatement)

node attributes:
test,  body

syntax:

do
  body
while (test);

example:

do {
  f(x);
  x++;
} while (x < 2);

for (ForStatement)

node attributes:
init,  test,  update,  body

syntax:

for ([init]; [test]; [update])
  body

example:

for (var x = 0; x < 2; x++) {
  f(x);
}

for-in (ForInStatement)

node attributes:
left (l),  right (r),  body

syntax:

for (left in right)
  body

example:

for (prop in object) {
  f(object[prop]);
}

debugger (DebuggerStatement)

syntax:

debugger;

example:

debugger;

Declarations

func-dec (FunctionDeclaration)

node attributes:
id,  body
node array attributes:
params

syntax:

function id([param_1], [param_2], [..., param_3])
  body

example:

function f(x, y) {
  return x * y;
}

note:

A function declaration contrasts with a function expression (func-exp).

var-decs (VariableDeclaration)

node array attributes:
declarations (decs)
primitive attributes:
kind

syntax:

var declaration_1[, declaration_2, ..., declaration_n]

example:

var x = 1, y = 2;

note:

Each declaration is a variable declarator (var-dec).

var-dec (VariableDeclarator)

node attributes:
id,  init

syntax:

id = init

example:

var x = 2

Expressions

this (ThisExpression)

example:

this

arr (ArrayExpression)

node array attributes:
elements (els)

syntax:

[element_0, element_1, ..., element_n]

examples:

[1, 2, 3]
[]

obj (ObjectExpression)

node array attributes:
properties (props)

syntax:

{
  property_1,
  property_2,
  ...,
  property_n
}

examples:

{a: 1, b: 2}
{}

func-exp (FunctionExpression)

node attributes:
id,  body
node array attributes:
params

syntax:

function [id]([param_1], [param_2], [..., param_3])
  body

example:

var f = function (x, y) {
  return x * y;
}

note:

A function expression contrasts with a function declaration (func-dec).

seq (SequenceExpression)

node array attributes:
expressions (exps)

syntax:

expression_1, expression_2, ..., expression_n

example:

a, b, c

unary (UnaryExpression)

node attributes:
argument (arg)

syntax:

operatorargument

examples:

+x
typeof x

bi (BinaryExpression)

node attributes:
left (l),  right (r)
primitive attributes:
operator (op)

syntax:

left operator right

example:

x === z

assign (AssignmentExpression)

node attributes:
left (l),  right (r)
primitive attributes:
operator (op)

syntax:

left operator right

example:

(y = 2)

update (UpdateExpression)

node attributes:
argument (arg)
primitive attributes:
operator (op),  prefix

syntax:

argumentoperator

or, if prefix

operatorargument

examples:

++x
x--

logic (LogicalExpression)

node attributes:
left (l),  right (r)
primitive attributes:
operator (op)

syntax:

left operator right

example:

x && y

cond (ConditionalExpression)

node attributes:
test,  consequent (then),  alternate (alt, else)

syntax:

test ? consequent : alternate

example:

x % 2 ? "odd" : "even"

new (NewExpression)

node attributes:
callee
node array attributes:
arguments (args)

syntax:

new callee(argument_1, argument_2, ..., argument_n)

example:

new Date(2011, 11, 11)

call (CallExpression)

node attributes:
callee
node array attributes:
arguments (args)

syntax:

callee(argument_1, argument_2, ..., argument_n)

example:

f(1,2,3)

member (MemberExpression)

node attributes:
object (obj),  property (prop)
primitive attributes:
computed

syntax:

object.property

example:

Math.PI

Clauses

switch-case (SwitchCase)

node attributes:
test
node array attributes:
consequent (then)

syntax:

case test | default :
  consequent

examples:

case 1:
  z = 'one';
  break;
default:
  z = 'two'

catch (CatchClause)

node attributes:
param,  body

syntax:

catch (param)
  body

example:

catch (e) {
  console.error(e.message);
}

Categories

Categories of node types - use grasp --help categories for an overview, and grasp --help category-name for further information about a specific category.

statement (Statement)

  • empty
  • block
  • exp-statement
  • if
  • label
  • break
  • continue
  • with
  • switch
  • return
  • throw
  • try
  • while
  • do-while
  • for
  • for-in
  • debugger

dec (Declaration)

  • func-dec
  • var-decs
  • var-dec

exp (Expression)

  • this
  • arr
  • obj
  • func-exp
  • seq
  • unary
  • bi
  • assign
  • update
  • logic
  • cond
  • new
  • call
  • member

clause (Clause)

  • switch-case
  • catch

biop (BiOp)

  • bi
  • logic
  • assign

func (Function)

  • func-dec
  • func-exp

for-loop (ForLoop)

  • for
  • for-in

while-loop (WhileLoop)

  • do-while
  • while

loop (Loop)

  • for
  • for-in
  • do-while
  • while