Basics

Nim Variables

Declaring Nim Variables

Nim variables use var let or const for mutability control.

Introduction to Nim Variables

In Nim, variables are central to storing and manipulating data. They can be declared using var, let, or const, each serving a different purpose in terms of mutability and scope.

Mutable Variables with var

The var keyword in Nim allows you to declare variables that can be modified or reassigned throughout their scope. This is useful when you need to keep track of changing values during the execution of your program.

Immutable Variables with let

When you declare a variable with the let keyword, it becomes immutable. This means that once the variable is assigned a value, it cannot be changed. This is particularly useful for constants or when you want to ensure that a value remains unchanged.

Constant Values with const

The const keyword is used to define constant values, which are known at compile time. Constants are useful for defining values that should remain unchanged throughout the program and can be used in compile-time expressions.

Choosing Between var, let, and const

When deciding whether to use var, let, or const, consider the following:

  • Use var when the variable's value needs to change.
  • Use let for values that should remain constant after initialization but are not known at compile time.
  • Use const for values that are known at compile time and should never change.
Previous
Syntax