Spaces in variable names in ZX Spectrum BASIC

#zx-spectrum #basic #retrocomputing

Written by Anders Marzi Tornblad

When developing a tool for the ZX Spectrum, I noticed that the BASIC interpreter allows spaces in variable names. This is not possible in most other BASICs, and it is not possible in the ZX Spectrum BASIC interpreter on the ZX81. I was curious to find out why this is possible, and how it works.

When the BASIC interpreter encounters a variable name, it looks up the name in a table of variable names, called VARS. Any spaces in the variable name is removed, and the remaining characters are converted to lowercase. Because of this, all lines in the program below use the same variable:

10 LET hello world=42
20 INPUT HELL o WorLd
30 PRINT HelloWorld
Spaces in variable names in ZX Spectrum BASIC

Using spaces in variable names might seem strange at first, but it can be useful for readability. In more modern languages, it is common to use snake_case or camelCase for variable names, and in JavaScript, it's actually possible to use spaces, but only in names of object properties.

This is one of several quirks in the ZX Spectrum BASIC interpreter, that can make it a bit tricky to write programs that parse BASIC programs.