ZetCode

Julia Variables Tutorial

last modified November 18, 2026

Variables in Julia are used to store data values. They are dynamically typed, meaning their type is determined from the value assigned to them; an explicit type annotation is optional. This tutorial covers basic definitions and practical examples of using variables in Julia.

Variables can store various data types, including numbers, strings, and arrays. They are case-sensitive and can contain Unicode characters. The examples were verified with Julia 1.13.4.

Basic Variable Assignment

This example demonstrates how to assign a value to a variable and print it.

main.jl
x = 10
println(x)

The variable x is assigned the value 10. The = character is the assignment operator; println writes the value followed by a newline to the terminal.

$ julia main.jl
10

String Variable

This example shows how to assign a string to a variable.

main.jl
name = "Julia"
println(name)

The variable name is assigned the string "Julia". String literals are enclosed in double quotes.

$ julia main.jl
Julia

Multiple Assignments

This example demonstrates how to assign multiple values to multiple variables.

main.jl
a, b, c = 1, 2, 3
println("$a $b $c")

The variables a, b, and c are assigned the values 1, 2, and 3 respectively. This is called destructuring: the values on the right are unpacked into the variables on the left. The interpolated string prints them separated by spaces.

$ julia main.jl
1 2 3

Variable Reassignment

This example shows how to reassign a value to a variable.

main.jl
x = 5
println(x)

x = 10
println(x)

The variable x is first assigned 5, then reassigned to 10. A variable can hold a value of any type; reassigning it to a value of a different type is allowed as well.

$ julia main.jl
5
10

Type Inference

This example demonstrates Julia's type inference.

main.jl
y = 3.14
println(typeof(y))

Julia infers the type from the assigned value, so y is a Float64. The typeof function reports the concrete type of a value. A specific type can also be requested with the :: operator, for example y::Float64 = 3.14.

$ julia main.jl
Float64

Unicode Variable Names

This example shows how to use Unicode characters in variable names.

main.jl
δ = 0.0001
println(δ)

The variable δ is assigned the value 0.0001. Julia allows Unicode letters in identifiers, which is convenient for mathematical code.

$ julia main.jl
0.0001

Constants

This example demonstrates how to declare a constant.

main.jl
const PI = 3.14159
println(PI)

The const keyword marks PI as a constant. It signals that the binding will not be reassigned, which helps the compiler generate more efficient code. Reassigning a constant produces a warning.

$ julia main.jl
3.14159

Global and Local Variables

This example shows the difference between global and local variables.

main.jl
x = 10
function foo()
    y = 5
    println(x + y)
end
foo()

The global variable x is defined outside any function and is accessible inside foo, while y is local to foo and exists only while the function runs. Calling foo() prints their sum.

$ julia main.jl
15

Best Practices for Variables

A few simple habits keep variable usage clear and predictable.

Source

Julia Variables documentation

Julia Types documentation

In this article we have worked with variables in Julia: assignment, multiple assignments, type inference, Unicode names, constants, and variable scope. Operators are covered in Julia operators and strings in Julia strings.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.