// NODE-JS — BUILTIN REFERENCE

node-js v0.1.0 · JavaScript on fusevm · lex/parse → AST → bytecode → Cranelift JIT · JsHost object heap · binary node · MIT · in active development

Docs GitHub
// Color scheme

>_BUILTIN REFERENCE

Every reserved keyword, global, and core object/type method the current node-js build recognizes, grouped by keyword set then globals then namespace then type. This page is generated from the corpus (src/lsp.rs) by the gen-docs binary, so it stays in sync with what the runtime actually knows about. Keywords mirror parser.rs; each global and method mirrors a real dispatch arm in src/builtins.rs.

Keyword

# var

declare a function-scoped (hoisted) variable

var x = 1; x   // => 1

# let

declare a block-scoped variable

let x = 2; x   // => 2

# const

declare a block-scoped binding that cannot be reassigned

const x = 3; x   // => 3

# function

define a function (declaration or expression)

function f() { return 9; } f()   // => 9

# return

return a value from the current function (undefined if omitted)

(function () { return 7; })()   // => 7

# if

conditional branch on a truthy test

let x; if (true) x = 1; x   // => 1

# else

fallback branch of an if

let x; if (false) x = 1; else x = 2; x   // => 2

# while

loop while the condition is truthy

let i = 0; while (i < 3) i++; i   // => 3

# do

do/while loop: run the body once, then repeat while truthy

let i = 0; do { i++; } while (i < 3); i   // => 3

# for

C-style loop, or for/of and for/in iteration

let s = 0; for (let i = 0; i < 3; i++) s += i; s   // => 3

# of

iterate values of an iterable: `for (x of iterable)`

let s = 0; for (const n of [1, 2, 3]) s += n; s   // => 6

# in

for/in key iteration, and the property-membership operator

"a" in { a: 1 }   // => true

# switch

multi-way branch on a discriminant with case labels

let x; switch (2) { case 2: x = "b"; break; } x   // => 'b'

# case

a labeled branch inside a switch

let x; switch (1) { case 1: x = "a"; } x   // => 'a'

# default

the fallback branch of a switch

let x; switch (9) { default: x = "d"; } x   // => 'd'

# break

exit the nearest enclosing loop or switch

let x; for (const n of [1, 2, 3]) { if (n === 2) break; x = n; } x   // => 1

# continue

skip to the next iteration of the nearest loop

let s = 0; for (const n of [1, 2, 3]) { if (n === 2) continue; s += n; } s   // => 4

# new

construct an instance: `new Ctor(args)`

new Error("boom").message   // => 'boom'

# this

the receiver of the current call

({ n: 5, get() { return this.n; } }).get()   // => 5

# typeof

the type tag of a value as a string

typeof 3   // => 'number'

# void

evaluate an expression and yield undefined

void 0   // => undefined

# delete

remove a property from an object

const o = { a: 1 }; delete o.a; o.a   // => undefined

# instanceof

test whether an object was built by a constructor

new Error("e") instanceof Error   // => true

# throw

raise an exception value

try { throw "x"; } catch (e) { e }   // => 'x'

# try

run a block, routing exceptions to catch/finally

let x; try { x = 1; } finally { x = 2; } x   // => 2

# catch

handle an exception thrown in the try block

try { null.x; } catch (e) { "caught" }   // => 'caught'

# finally

block that always runs after try/catch

let x; try { x = 1; } finally { x = 3; } x   // => 3

# true

the boolean true literal

true && 1   // => 1

# false

the boolean false literal

false || 2   // => 2

# null

the intentional-absence value

null ?? 5   // => 5

Global

# undefined

the value of an unassigned binding or missing property

typeof undefined   // => 'undefined'

# NaN

the not-a-number float; unequal to itself

NaN === NaN   // => false

# Infinity

the positive-infinity float

1 / 0 === Infinity   // => true

# globalThis

the global object

typeof globalThis   // => 'object'

# parseInt

parse the leading integer of a string (optional radix)

parseInt("42px")   // => 42

# parseFloat

parse the leading floating-point number of a string

parseFloat("3.14abc")   // => 3.14

# isNaN

true if the coerced number is NaN

isNaN("x")   // => true

# isFinite

true if the coerced number is finite

isFinite(10)   // => true

# String

convert a value to its string form

String(123)   // => '123'

# Number

convert a value to a number

Number("3.5")   // => 3.5

# Boolean

convert a value to its truthiness

Boolean("")   // => false

# Array

build an array from the given elements

Array(1, 2, 3)   // => [ 1, 2, 3 ]

# Error

construct a generic error with a message

new Error("boom").message   // => 'boom'

# TypeError

construct a type error (name is 'TypeError')

new TypeError("bad").name   // => 'TypeError'

# RangeError

construct a range error (name is 'RangeError')

new RangeError("oob").name   // => 'RangeError'

console

# console.log

write args to stdout, space-separated, ending in a newline

console.log("hi", 1)   // prints: hi 1

# console.error

write args to stderr

console.error("oops")   // prints oops to stderr

# console.warn

write args to stderr (warning channel)

console.warn("careful")   // prints careful to stderr

# console.info

write args to stdout (info channel)

console.info("note")   // prints note

# console.debug

write args to stdout (debug channel)

console.debug("dbg")   // prints dbg

Math

# Math.PI

the ratio of a circle's circumference to its diameter

Math.PI   // => 3.141592653589793

# Math.abs

absolute value of a number

Math.abs(-5)   // => 5

# Math.floor

largest integer <= x

Math.floor(3.7)   // => 3

# Math.ceil

smallest integer >= x

Math.ceil(3.2)   // => 4

# Math.round

round to the nearest integer (ties toward +Infinity)

Math.round(2.5)   // => 3

# Math.trunc

the integer part of x, dropping any fraction

Math.trunc(-4.7)   // => -4

# Math.sign

the sign of x as -1, 0, or 1

Math.sign(-8)   // => -1

# Math.sqrt

square root of x

Math.sqrt(16)   // => 4

# Math.cbrt

cube root of x

Math.cbrt(27)   // => 3

# Math.pow

x raised to the power y

Math.pow(2, 10)   // => 1024

# Math.exp

e raised to the power x

Math.exp(0)   // => 1

# Math.log

natural logarithm of x

Math.log(1)   // => 0

# Math.max

largest of the arguments

Math.max(3, 1, 2)   // => 3

# Math.min

smallest of the arguments

Math.min(3, 1, 2)   // => 1

# Math.hypot

the square root of the sum of squares of the arguments

Math.hypot(3, 4)   // => 5

# Math.sin

sine of x (radians)

Math.sin(0)   // => 0

# Math.cos

cosine of x (radians)

Math.cos(0)   // => 1

# Math.tan

tangent of x (radians)

Math.tan(0)   // => 0

# Math.random

a pseudo-random float in [0, 1)

Math.random() < 1   // => true

JSON

# JSON.stringify

serialize a value to a JSON string

JSON.stringify({ a: 1 })   // => '{"a":1}'

# JSON.parse

parse a JSON string into a value

JSON.parse("[1,2]")   // => [ 1, 2 ]

Object

# Object.keys

an array of an object's own enumerable keys

Object.keys({ a: 1, b: 2 })   // => [ 'a', 'b' ]

# Object.values

an array of an object's own enumerable values

Object.values({ a: 1, b: 2 })   // => [ 1, 2 ]

# Object.entries

an array of [key, value] pairs

Object.entries({ a: 1 })   // => [ [ 'a', 1 ] ]

# Object.assign

copy source properties onto a target object (in place)

Object.assign({ a: 1 }, { b: 2 })   // => { a: 1, b: 2 }

# Object.fromEntries

build an object from [key, value] pairs

Object.fromEntries([["a", 1]])   // => { a: 1 }

# Object.freeze

make an object immutable and return it

Object.freeze({ a: 1 }).a   // => 1

Number

# Number.isInteger

true if the value is an integer number

Number.isInteger(3)   // => true

# Number.isNaN

true only if the value is exactly NaN (no coercion)

Number.isNaN(NaN)   // => true

# Number.isFinite

true if the value is a finite number (no coercion)

Number.isFinite(10)   // => true

# Number.isSafeInteger

true if the value is an integer within +/-2^53-1

Number.isSafeInteger(2 ** 53)   // => false

# Number.parseInt

same as the global parseInt

Number.parseInt("20", 10)   // => 20

# Number.parseFloat

same as the global parseFloat

Number.parseFloat("1.5")   // => 1.5

String static

# String.fromCharCode

a string built from the given UTF-16 code units

String.fromCharCode(65, 66)   // => 'AB'

Array static

# Array.isArray

true if the value is an array

Array.isArray([1, 2])   // => true

# Array.from

build an array from an iterable or array-like

Array.from("ab")   // => [ 'a', 'b' ]

# Array.of

build an array from the given arguments

Array.of(1, 2, 3)   // => [ 1, 2, 3 ]

Array method

# push

append items to the end; returns the new length

const a = [1]; a.push(2); a   // => [ 1, 2 ]

# pop

remove and return the last item

[1, 2, 3].pop()   // => 3

# shift

remove and return the first item

[1, 2, 3].shift()   // => 1

# unshift

prepend items; returns the new length

const a = [2]; a.unshift(1); a   // => [ 1, 2 ]

# map

a new array of the results of calling fn on each item

[1, 2, 3].map(x => x * 2)   // => [ 2, 4, 6 ]

# filter

a new array of the items for which fn is truthy

[1, 2, 3, 4].filter(x => x % 2 === 0)   // => [ 2, 4 ]

# forEach

call fn on each item for effect; returns undefined

let s = 0; [1, 2, 3].forEach(x => s += x); s   // => 6

# reduce

fold the array to a single value with an accumulator

[1, 2, 3].reduce((a, b) => a + b, 0)   // => 6

# join

concatenate items into a string with a separator

[1, 2, 3].join("-")   // => '1-2-3'

# slice

a shallow copy of a [start, end) sub-range

[1, 2, 3, 4].slice(1, 3)   // => [ 2, 3 ]

# splice

remove/insert items in place; returns the removed items

const a = [1, 2, 3]; a.splice(1, 1); a   // => [ 1, 3 ]

# concat

a new array joining this array with more arrays/values

[1].concat([2, 3])   // => [ 1, 2, 3 ]

# indexOf

index of the first matching item, or -1

[1, 2, 3].indexOf(2)   // => 1

# lastIndexOf

index of the last matching item, or -1

[1, 2, 1].lastIndexOf(1)   // => 2

# includes

true if the array contains the value

[1, 2, 3].includes(2)   // => true

# find

the first item for which fn is truthy, else undefined

[1, 2, 3].find(x => x > 1)   // => 2

# findIndex

the index of the first item for which fn is truthy, else -1

[1, 2, 3].findIndex(x => x > 1)   // => 1

# some

true if fn is truthy for any item

[1, 2, 3].some(x => x > 2)   // => true

# every

true if fn is truthy for every item

[1, 2, 3].every(x => x > 0)   // => true

# reverse

reverse the array in place

[1, 2, 3].reverse()   // => [ 3, 2, 1 ]

# sort

sort in place (default: by string order)

[3, 1, 2].sort()   // => [ 1, 2, 3 ]

# flat

a new array with sub-arrays flattened one level (or by depth)

[1, [2, [3]]].flat()   // => [ 1, 2, [ 3 ] ]

# flatMap

map each item then flatten the result one level

[1, 2].flatMap(x => [x, x])   // => [ 1, 1, 2, 2 ]

# fill

overwrite a range with a value in place

[1, 2, 3].fill(0)   // => [ 0, 0, 0 ]

# at

the item at an index, allowing negative indexing

[1, 2, 3].at(-1)   // => 3

String method

# toUpperCase

a copy with all cased characters uppercased

"abc".toUpperCase()   // => 'ABC'

# toLowerCase

a copy with all cased characters lowercased

"ABC".toLowerCase()   // => 'abc'

# charAt

the character at an index

"hi".charAt(1)   // => 'i'

# charCodeAt

the UTF-16 code unit at an index

"A".charCodeAt(0)   // => 65

# codePointAt

the Unicode code point at an index

"A".codePointAt(0)   // => 65

# slice

a substring over a [start, end) range (negatives allowed)

"hello".slice(1, 3)   // => 'el'

# substring

a substring over a [start, end) range (no negatives)

"hello".substring(0, 2)   // => 'he'

# split

an array of substrings split on a separator

"a,b,c".split(",")   // => [ 'a', 'b', 'c' ]

# trim

a copy with leading and trailing whitespace removed

"  hi  ".trim()   // => 'hi'

# trimStart

a copy with leading whitespace removed

"  hi".trimStart()   // => 'hi'

# trimEnd

a copy with trailing whitespace removed

"hi  ".trimEnd()   // => 'hi'

# replace

a copy with the first match of a substring replaced

"aaa".replace("a", "b")   // => 'baa'

# replaceAll

a copy with every match of a substring replaced

"aaa".replaceAll("a", "b")   // => 'bbb'

# repeat

the string repeated n times

"ab".repeat(3)   // => 'ababab'

# startsWith

true if the string starts with the prefix

"hello".startsWith("he")   // => true

# endsWith

true if the string ends with the suffix

"hello".endsWith("lo")   // => true

# padStart

pad on the left to a target length

"5".padStart(3, "0")   // => '005'

# padEnd

pad on the right to a target length

"5".padEnd(3, "0")   // => '500'

Number method

# toFixed

a fixed-point string with n digits after the decimal point

(3.14159).toFixed(2)   // => '3.14'

# toPrecision

a string with n significant digits

(123.456).toPrecision(4)   // => '123.5'

# toString

the string form of a number in an optional radix

(255).toString(16)   // => 'ff'

More