website is under construction
Language

Values

In Ghost's little universe, the atoms that make up all matter are the built-in object types. All values are objects, and may contain any number of methods to further work with their data. Below we'll briefly introduce each.

You can ask any value what it is with the built-in type() function.

console.log(type("Ghost")) // >> string
console.log(type(3.14))    // >> number
console.log(type([1, 2]))  // >> list

Booleans

A boolean value represents truthy or falsy states. There are two boolean values, true and false.

Truthiness

Any value can be used where a condition is expected. null, false, and the empty string are falsy. Everything else is truthy — including 0, an empty list, and an empty map.

if (0) { console.log("runs") }     // >> runs
if ([]) { console.log("runs") }    // >> runs
if ("") { console.log("skipped") } // (nothing)
Only the three values above are falsy. A zero, an empty list, and an empty map are all truthy, and the empty string is the one type-specific rule worth remembering.

Lists

Lists are an ordered list of elements of possibly different types identified by a number index. Each element in a list can be accessed individually by their index. Lists are constructed as a comma separated list of elements, can contain any type of value, and are enclosed by square brackets:

[
  "Ghost",
  57.3,
  function (x) {
    return x * x
  }
]
A list literal does not allow a trailing comma after its last element. A map literal does.

Maps

Maps — sometimes called associative arrays, hashes, or dictionaries in other programming languages, store a collection of key-value pairings. Maps are constructed as a comma-separated list of key-value pairs enclosed by curly braces. Each key-value pair uses a colon to differentiate between the key and the value.

{
    "name": "Ghost",
    "value": 57.3,
    "handler": function(x) { return x * x }
}

Null

Ghost, like many languages, has a special value null. It functions a bit like void in some languages: it indicates the absence of a value. If you call a method that doesn't return anything and get its return value, you get null back.

Numbers

Ghost has a single numeric type, number, which covers whole numbers and decimals alike. There is no separate integer type to declare or convert to.

Underneath, a number holds either a 64-bit integer or a 64-bit float, and Ghost picks between them as it goes: whole-number arithmetic stays exact, and anything that produces a fraction — division included — becomes a float. Both are the same number type as far as your code is concerned.

0
1234
-5678
3.14159
12.34

See Numbers for what that means for precision.

Strings

A string value is a (possibly empty) sequence of bytes. String values can be surrounded in either double or single quotes, and can store unicode text.

string1 = "Hello, world!"
string2 = "こんにちは、世界"

A third form, the backtick-delimited template literal, interpolates ${expression}:

name = "Ghost"

console.log(`Hello, ${name}!`) // >> Hello, Ghost!

See Strings.

Dates

A date is a single instant in time, together with the time zone it should be read in — UTC unless you say otherwise. Dates are never written as a literal — they come out of the date module, and every operation on one is a function in that module rather than a method on the value, which keeps a date immutable.

import "ghost:date"

console.log(type(date.now())) // >> date

Comparisons look at the instant, so they do not depend on which zone either date carries. The zone decides what reading a calendar position back out — the year, the hour, the weekday — answers. See Date.

Durations

A duration is a calendar-and-clock span: years, months, days, hours, minutes, and seconds kept as separate components rather than collapsed into one number, because "one month" has no fixed length in days. Like a date, it comes from the date module.

import "ghost:date"

span = date.duration(1, 2, 3)

console.log(type(span)) // >> duration
console.log(span)       // >> P1Y2M3D

Unlike a date, a duration reads its own fields with methods — span.years(), span.months(), and so on.

Functions

Functions are values. They can be stored in variables, passed to other functions, and returned from them. See Functions.

Classes, Traits, and Instances

A class declaration creates a class value, new creates an instance of one, and a trait is a bundle of methods a class can adopt. See Classes.