Skip to content

Integers

Integer types

Boa supports the following integer types:

TypeSize (in bytes)Signed ?Minimum valueMaximum value
u81No0255
u162No065535
u324No04294967295
u648No018446744073709551615
i81Yes-128127
i162Yes-3276832767
i324Yes-21474836482147483647
i648Yes-92233720368547758089223372036854775807

Signed integers encoding

Boa's signed integers (i8, i16, i32, i64) are guaranteed to be encoded in 2's complement.

Integer literals

Integers in Boa can be written in the following bases:

  • Base 2, with the 0b prefix
  • Base 8, with the 0o prefix
  • Base 16, with the 0x prefix.
boa
let base_2 = 0b10000000
let base_8 = 0o200
let base_16 = 0x80

The "post" prefix" part of an integer literal can contain _ (but cannot start with one), to improve legibility:

boa
let one_billion = 1_000_000_000

Integer literals and type inference

When creating a variable storing an integer without any type annotation, Boa will, in the following order:

  • Assume it is a i32 if the value fits inside a i32
  • Assume it is a i64 if the value fits inside a i64
  • Assume it is a u64 if the value fits inside a u64
  • Raise a compilation error.

The following example illustrates this behavior:

boa
let a = 12 // 12 fits into an i32, so a is an i32
let b = 2147483648 // b is too big for an i32 but fits in an i64 so it is an i64
let c = 9223372036854775808 // c is too big for an i64 but fits in an u64 so it is an u64
let d = 18446744073709551616 // d is too big for an u64 so a compilation error is raised

If the type of the variable is specified and the value does not fit inside it, a compilation error is directly raised:

boa
let a: u8 = 256 // compilation error because 256 is too big to fit in an u8

Integer literals suffixes

It is possible to bypass the behavior defined in the previous section by adding a suffix to an integer literal to specify its type:

boa
let a = 12i8 // 'a' is an i8 but would have been a i32 without the suffix
let b = 0u64 // 'b' is an u64 but would have been a i32 without the suffix

An integer literal defined with an incompatible suffix triggers a compilation error:

boa
let a = 65535u8 // 65535 cannot fit in an u8, so a compilation error is triggered

If type type of the variable is precised and an integer suffix is present, they must correspond to the same type or a compilation error is triggered:

boa
let a: i32 = 8u64 // a's type is defined as i32 but the integer literal is defined as an u64. Boa cannot guess which one is correct, so a compilation error is triggered.