-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add examples to help understand the topic * fix formatting * merge last 2 paragrapghs (as they were very similar)
- Loading branch information
Showing
1 changed file
with
22 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,39 @@ | ||
# Constants | ||
|
||
In the world of Coda, we understand the importance of stability and unchanging elements. Constants stand as steadfast pillars, offering values that remain constant throughout your code's journey. Allow us to illuminate the path of constants within Coda. | ||
In simple terms, a constant is something that doesn't change. It is like a fixed value that stays the same no matter what. Think of a number of days in a week - it's always 7! (And weekend has always only 2 days, unfortunately) | ||
|
||
One example of a constant in nature is the speed of light. The speed of light in a vacuum is about 299,792 kilometers per second (or about 186,282 miles per second). This means that light always travels at this speed, and it never changes. | ||
|
||
An analogy that could help you understand constants is a faucet. When you turn on a faucet, the water flow can be adjusted from a tiny drip to a steady stream. In this case, the water pressure can be considered a variable because it can change. However, the temperature of the water coming out of the faucet can be considered a constant because it remains the same (for example, warm or cold) regardless of the water pressure. | ||
|
||
So, a constant is something that doesn't change, like the speed of light or the temperature of water from a faucet. | ||
|
||
## Declaration of Constants | ||
|
||
Much like variables, constants hold a prominent place in Coda's syntax. To declare a constant, you can rely on the `const` keyword. This serves as a clear indicator that the value you assign is intended to remain unchanged. | ||
You can assign any type of value to `const` variable - numeric, text or boolean. | ||
|
||
```js | ||
const x = 5 | ||
const y = "Hello, World!" | ||
const z = true | ||
const pi = 3.14159265359; | ||
const firstMonth = "JANUARY"; | ||
const isTrue = true; | ||
``` | ||
|
||
## Consistency with Flexibility | ||
While constants share common attributes with variables, such as types and other properties, they bear a distinctive trait: once defined, a constant cannot be reassigned. This rigidity ensures that the value you set remains a rock-solid foundation for your code's execution. | ||
|
||
The most important thing to remember about contants is that **they cannot be changed**. Once you assign a value to your constant, you will never be able to reassign. | ||
|
||
In programming, such property is called `immutability`. For example, the genetic code of an individual is immutable. This means that the DNA sequence that makes up your genes cannot be changed. It is the blueprint that determines your physical characteristics, and it cannot be altered. | ||
|
||
```js | ||
const x = 5 | ||
x = 10 // This will result in an error | ||
const pi = 3.14159265359; | ||
pi = 10; // This will result in an error, rightly! | ||
``` | ||
|
||
## The Symphony of Properties | ||
|
||
In a harmonious convergence, constants align with variables in their essence. Types, expressions, and all the facets you appreciate about variables are seamlessly woven into the fabric of constants. This consistency simplifies your coding experience and streamlines your understanding of these foundational elements. | ||
|
||
## A Gentle Reminder | ||
As a friendly reminder, remember that constants are not immune to the allure of types and the captivating dance of expressions. While they remain immutable, their capacity to embody various types ensures your code remains versatile and expressive. | ||
The constants are immutable, but not immune to the allure of types and the captivating dance of expressions. | ||
|
||
In other words, you can treat `const` as other Coda variables - just the special ones. You will soon notice how great they are! |