On this page:
2.1 Racket  Script’s Java  Script FFI Primitive
#%js-ffi
2.2 Racket  Script’s Java  Script FFI API
$
$$
$/  new
$/  throw
$/  undefined
$/  null
$/  this
$/  arguments
$/  obj
$/  :  =
$/  array
$/  require
$/  require/  *
$>
$/  typeof
$/  instanceof
$/  binop
$/  +
js-string->string
js-string
$/  str
2.3 Reader Extensions
8.12

2 The RacketScript-JavaScript FFI🔗ℹ

 (require racketscript/interop)
  package: racketscript-compiler

RacketScript supports direct interoperability with most JavaScript features. This section explains how to invoke plain JavaScript in a RacketScript program.

2.1 RacketScript’s JavaScript FFI Primitive🔗ℹ

RacketScript’s #%js-ffi form compiles directly to various JavaScript features. The first argument is a symbol that indicates the kind of JavaScript code to be generated and the rest are the arguments for that kind of operation.

NOTE: Users most likely should not be using this form. Instead, use the API described in the RacketScript’s JavaScript FFI API section, which will expand to the appropriate call to #%js-ffi.

syntax

(#%js-ffi 'var)
(#%js-ffi 'ref obj prop-id)
(#%js-ffi 'index obj prop-expr)
(#%js-ffi 'assign x e)
(#%js-ffi 'new expr)
(#%js-ffi 'throw exn)
(#%js-ffi 'undefined)
(#%js-ffi 'null)
(#%js-ffi 'this)
(#%js-ffi 'arguments)
(#%js-ffi 'object [fld v] ...)
(#%js-ffi 'array args ...)
(#%js-ffi 'typeof obj)
(#%js-ffi 'instanceof obj type)
(#%js-ffi 'string str)
(#%js-ffi 'require mod)
(#%js-ffi 'operator 'op operand ...)

Summary of JavaScript operations supported by #%js-ffi:

2.2 RacketScript’s JavaScript FFI API🔗ℹ

syntax

($ jsid)

($ expr sym)
($ expr expr)
($ expr expr ...)
 
jsid = valid JS identifier (alphanumeric underscore and dollar chars)
 
  sym : symbol?
Syntax for accessing Javascript variables and properties.

syntax

($$ dot-chain e ...)

 
dot-chain = symbol or identifier consisting of multiple dot-separated names
Shorthand for multiple $s. Allows more direct use of dot notation in RacketScript. E.g., ($$ window.document.write) corresponds to window.document.write in JavaScript.

syntax

($/new constructor-expr)

JavaScript object construction. Equivalent to (#%js-ffi 'new constructor-expr).

syntax

($/throw exn)

Throw a JavaScript exception. Equivalent to (#%js-ffi 'throw exn).
The JavaScript undefined value. Equivalent to (#%js-ffi 'undefined)

syntax

$/null

The JavaScript null object. Equivalent to (#%js-ffi 'null).

syntax

$/this

The JavaScript this keyword. Equivalent to (#%js-ffi 'this).
The JavaScript arguments object containing the arguments passed to a function. Equivalent to (#%js-ffi 'arguments).

syntax

($/obj [fld v] ...)

 
fld = identifier
JavaScript object literal notation, i.e., brace notation, where fld are identifiers representing the object’s properties, and v ... are values assigned to those properties. Equivalent to (#%js-ffi 'object fld ... v ...)

syntax

($/:= e v)

JavaScript assignment statement. Equivalent to (#%js-ffi 'assign e v). e should be a symbol, or a #%js-ffi 'var, 'ref, or 'index call.

syntax

($/array e ...)

JavaScript array literal notation, where ($/array 1 2 3) compiles to [1,2,3]. Equivalent to (#%js-ffi 'array e ...)

syntax

($/require mod)

($/require mod *)
 
  mod : string?
JavaScript import statement.

Often used with define, e.g., (define express ($/require "express")) compiles to:

import * as express from "express";

Equivalent to (#%js-ffi 'require mod) or (#%js-ffi 'require '* mod)

syntax

($/require/* mod)

 
  mod : string?
JavaScript import all statement.

Shorthand for ($/require mod *)

syntax

($> e call ...)

 
call = id
  | (meth arg ...)
JavaScript chaincall.

For example:

($> (#js.res.status 400) (send #js"Bad Request"))

is compiles to res.status(400).send("Bad Request")

Equivalent to nested #%js-ffi calls (with 'var, 'ref, or 'index).

syntax

($/typeof e)

($/typeof e type)
 
  type : 
(and/c string?
       (or/c "undefined" "object" "boolean" "number" "string" "function"))
JavaScript typeof operator.

The first form returns a string representing the typeof the given JavaScript value. Equivalent to (#%js-ffi 'typeof e).

The second form is shorthand for checking the type of a value. For example, ($/typeof 11 "number") is compiles to

typeof 11 === "number";

Equivalent to ($/binop === (#%js-ffi 'typeof e) ($/str v))

syntax

($/instanceof e type)

 
e = JavaScript Object
Returns a boolean indicating whether JavaScript object e is an instance of type.

Equivalent to (#%js-ffi 'instanceof e type)

syntax

($/binop op operand1 operand2)

JavaScript infix binary function call.

Equivalent to (#%js-ffi 'operator 'op operand1 operand2)

syntax

($/+ operand ...)

Multi-argument infix calls to JavaScript + (can be used as either concat or addition). Equivalent to multiple nested calls to $/binop.

procedure

(js-string->string jsstr)  string?

  jsstr : JSstring
Converts a JS string to a RacketScript string.

procedure

(js-string str)  JSstring

  str : string?
Converts a RacketScript string to a JS string.

syntax

($/str s)

Converts a Racket string to a JS string, or vice versa, using js-string->string or js-string.

2.3 Reader Extensions🔗ℹ

#lang racketscript/base includes reader extensions that make it easier to interoperate with JavaScript. Specifically, RacketScript’s reader recognizes three delimiters: