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"

regex (RegExpLiteral)

primitive attributes:
regex

example:

/^sh+/gi

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.

spread (SpreadElement)

node attributes:
argument (arg)

template-element (TemplateElement)

primitive attributes:
tail,  value (val)

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 (let 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 (let prop in object) {
  f(object[prop]);
}

for-of (ForOfStatement)

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

syntax:

for (left of right)
  body

example:

for (let val of list) {
  f(val);
}

debugger (DebuggerStatement)

syntax:

debugger;

example:

debugger;

Declarations

func-dec (FunctionDeclaration)

node attributes:
id,  body
node array attributes:
params
primitive attributes:
generator

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

super (Super)

example:

super(x, y)

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
primitive attributes:
generator

syntax:

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

example:

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

note:

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

arrow (ArrowFunctionExpression)

node attributes:
id,  body
node array attributes:
params
primitive attributes:
generator,  expression (exp)

syntax:

([param_1], [param_2], [..., param_3]) => body

example:

(x, y) => x * y

seq (SequenceExpression)

node array attributes:
expressions (exps)

syntax:

expression_1, expression_2, ..., expression_n

example:

a, b, c

yield (YieldExpression)

node attributes:
argument (arg)

syntax:

yield argument

example:

yield x

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

template-literal (TemplateLiteral)

node array attributes:
quasis,  expressions (exps)

tagged-template-exp (TaggedTemplateExpression)

node attributes:
tag,  quasi

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);
}

Patterns

assign-prop (AssignmentProperty)

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

obj-pattern (ObjectPattern)

node array attributes:
properties (props)

array-pattern (ArrayPattern)

node array attributes:
elements (els)

rest-element (RestElement)

node attributes:
argument (arg)

assign-pattern (AssignmentPattern)

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

Classes

class-body (ClassBody)

node array attributes:
body

method (MethodDefinition)

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

class-dec (ClassDeclaration)

node attributes:
id,  superClass,  body

class-exp (ClassExpression)

node attributes:
id,  superClass,  body

meta-property (MetaProperty)

node attributes:
meta,  property (prop)

Modules

module-dec (ModuleDeclaration)

module-specifier (ModuleSpecifier)

node attributes:
local

Imports

import-dec (ImportDeclaration)

node attributes:
source
node array attributes:
specifiers

import-specifier (ImportSpecifier)

node attributes:
local,  imported

import-default-specifier (ImportDefaultSpecifier)

node attributes:
local

import-namespace-specifier (ImportNamespaceSpecifier)

node attributes:
local

Exports

export-named-dec (ExportNamedDeclaration)

node attributes:
declaration,  source
node array attributes:
specifiers

export-specifier (ExportSpecifier)

node attributes:
local,  exported

export-default-specifier (ExportDefaultDeclaration)

node attributes:
declaration

export-namespace-specifier (ExportAllDeclarationSpecifier)

node attributes:
source

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
  • for-of
  • debugger

dec (Declaration)

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

exp (Expression)

  • this
  • super
  • arr
  • obj
  • func-exp
  • arrow
  • seq
  • yield
  • unary
  • bi
  • assign
  • update
  • logic
  • cond
  • new
  • call
  • member
  • template-literal
  • tagged-template-exp

clause (Clause)

  • switch-case
  • catch

biop (BiOp)

  • bi
  • logic
  • assign

func (Function)

  • func-dec
  • func-exp

for-loop (ForLoop)

  • for
  • for-in
  • for-of

while-loop (WhileLoop)

  • do-while
  • while

loop (Loop)

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

class (Class)

  • class-exp
  • class-exp