Nim is a statically typed programming language, meaning that the type of an assignment needs to be declared before using the value.

In Nim we also distinguish values that can change, or mutate, from those that can’t, but more on this later. We can declare a variable (a mutable assignment) using the var keyword, just by stating its name and type (the value can be added later) by using this syntax:

var < name > : < type >

If we already know its value, we can declare a variable and give it a value immediately:

var < name > : < type > = < value >

Angular brackets( <> ) are used to show something you can change.

So <name> is not literally the word name in angular brackets but rather any name.

Nim also has type inference ability: the compiler can automatically detect the type of a name assignment from its value, without explicitly stating the type. We’ll look more into the various types in the next chapter.

So we can assign a variable without an explicit type like this:

var < name > = < value >

An example of this in Nim looks like this:

var a : int (1) var b = 7 (2)

1 Variable a is of type int (integer) with no value explicitly set. 2 Variable b has a value of 7 . Its type is automatically detected as an integer.

When assigning names it is important to choose names that mean something for your program. Simply naming them a , b , c , and so forth will quickly become confusing. It is not possible to use spaces in a name, as that would split it into two. So if the name you choose consists of more than one word the usual way is to write it in camelCase style (notice that the first letter in a name should be lowercase).

Note however that Nim is both case- and underscore-insensitive meaning that helloWorld and hello_world would be the same name. The exception to this is the first character, which is case-sensitive. Names can also include both numbers and other UTF-8 characters, even emojis should you wish that, but keep in mind you and possibly others will have to type them.

Instead of typing var for each variable, multiple variables (not necessarily of the same type) can be declared in the same var block. In Nim, blocks are parts of code with the same indentation (same number of spaces before the first character), and the default indentation level is two spaces. You will see such blocks everywhere in a Nim program, not only for assigning names.

var c = - 11 d = "Hello" e = '!'

In Nim tabs are not allowed as indentation.

You can set up your code editor to convert pressing Tab to any number of spaces.

In VS Code, the default setting is to convert Tab to four spaces. This is easily overridden in settings ( Ctrl+, ) by setting "editor.tabSize": 2 .

As previously mentioned variables are mutable, i.e. their value can change (multiple times), but their type must stay the same as declared.

var f = 7 (1) f = - 3 (2) f = 19 f = "Hello" # error (3) (4)