Julia Variables
A variable is a name that holds a value. You can use the name later instead of writing the value again.
A variable holds a value
The first line stores 10 under the name score. The second line prints the value by using the name.
score = 10
println(score)Reassignment replaces the old value
The variable starts with the value 10. The second assignment replaces it with 25.
score = 10
score = 25
println(score)Names are case-sensitive
The names age and Age differ only in one capital letter. Julia treats them as two separate variables.
age = 20
Age = 99
println(age)
println(Age)Example
name = "Ana"
visits = 2
println(name)
println(visits)
visits = 3
println(visits)Key points
- Variable: a name that holds a value.
- Assignment: writing name = value stores a value under a name.
- Reassignment: giving a variable a new value replaces the old value.
- Naming start: a name must start with a letter or an underscore.
- Naming characters: after the first character, a name can also contain digits.
- Case: names are case-sensitive, so age and Age are different names.
In this example, name holds the text Ana and visits holds the number 2. The next two lines print both values. The last assignment replaces 2 with 3, so the final line prints 3.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
What does this code print? count = 5 count = 8 println(count)