Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 19 / 68

Constants (const)

This lesson covers declaring constants with Julia's const, with the aim of handling values that must not change safely.

The const keyword creates a constant whose type cannot change later. In Julia, marking a global variable const also improves execution speed.

The sample code defines the maximum score as const MAX_SCORE = 100 and uses it in a message. Writing a constant name in capitals with underscores, as MAX_SCORE, is a convention shared by many languages.

A common early stumble is that const does not forbid changing the value entirely. It forbids changing the type; a value of the same type can sometimes still be assigned.

In professional work, marking globals const has the practical benefit of letting Julia's compiler optimise, which speeds the program up.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)