website is under construction
Standard Library

Globals

Ghost's standard library is import-only. Two names are the exception: console and type are reachable from any script with no import at all. Everything else — math, date, random, os, file, path, json, http, ghost — has to be imported by name before a script can use it.

console.log(type(3.14)) // >> number — both available with no import

import "ghost:math"

console.log(math.sqrt(16)) // >> 4
A script says what it uses. console and type are global because they are reached for constantly and are small enough in surface to earn it — not because "frequently used" is a standing invitation to add a third.

console

The console module writes to and reads from the terminal. console.log() is how a Ghost program writes a line of output.

console.log("Hello, world!")     // >> Hello, world!
console.log("count:", 3, true)   // >> count: 3 true
console.log()                    // >> (a blank line)

Multiple arguments are joined with a single space and each is converted to its string form first, so — unlike the + operator — console.log() is happy to mix types.

Inside an http.handle() callback, console.log() writes to the response body rather than the terminal.

See Console for the rest of the module: labelled output, output without a trailing newline, and reading a line of input.

type()

Returns the type of the given value as a lowercase string. The name it answers with is the same one every error message uses.

console.log(type(1))          // >> number
console.log(type(3.14))       // >> number
console.log(type("Ghost"))    // >> string
console.log(type(true))       // >> boolean
console.log(type(null))       // >> null
console.log(type([1, 2]))     // >> list
console.log(type({ a: 1 }))   // >> map
console.log(type(console))    // >> library_module

Functions report function, classes report class, and an instance of any class reports instancetype() names the kind of value, not the class it came from.

class Dog {}

console.log(type(Dog))         // >> class
console.log(type(new Dog()))   // >> instance

type() takes exactly one argument.