Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 19 / 68

Constants (the upper-case convention)

This lesson covers R's convention for constants, with the aim of expressing clearly that a value is not meant to change.

R has no const keyword as other languages do, but there is a convention of writing a variable name in capitals to signal "this value is not meant to change".

The sample code defines the maximum score with the capitalised MAX_SCORE <- 100 and uses it in a message. It can still be changed, but the capitals tell the reader it is intended as a constant.

A common early stumble is that this capitalisation is purely a convention — the language does not forbid reassignment. The developer has to take care not to overwrite it by accident.

In professional work, this convention is a basic technique for avoiding magic numbers and conveying intent.

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

๐Ÿงช This site can't compile or run R 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)