A variable, in Julia, is a name associated (or bound) to a value. It's useful when you want to store a value (that you obtained after some math, for example) for later use. For example:
# Assign the value 10 to the variable xjulia> x = 1010# Doing math with x's valuejulia> x + 111# Reassign x's valuejulia> x = 1 + 12# You can assign values of other types, like strings of textjulia> x = "Hello World!""Hello World!"
Julia provides an extremely flexible system for naming variables. Variable names are case-sensitive, and have no semantic meaning (that is, the language will not treat variables differently based on their names).
julia> x = 1.01.0julia> y = -3-3julia> Z = "My string""My string"julia> customary_phrase = "Hello world!""Hello world!"julia> UniversalDeclarationOfHumanRightsStart = "人人生而自由,在尊严和权利上一律平等。""人人生而自由,在尊严和权利上一律平等。"
Unicode names (in UTF-8 encoding) are allowed:
julia> δ = 0.000011.0e-5julia> 안녕하세요 = "Hello""Hello"
In the Julia REPL and several other Julia editing environments, you can type many Unicode math symbols by typing the backslashed LaTeX symbol name followed by tab. For example, the variable nameδ
can be entered by typing\delta
-tab, or evenα̂⁽²⁾
by\alpha
-tab-\hat
-tab-\^(2)
-tab. (If you find a symbol somewhere, e.g. in someone else's code, that you don't know how to type, the REPL help will tell you: just type?
and then paste the symbol.)
Julia will even let you shadow existing exported constants and functions with local ones (although this is not recommended to avoid potential confusions):
julia> pi = 33julia> pi3julia> sqrt = 44julia> length() = 5length (generic function with 1 method)julia> Base.lengthlength (generic function with 79 methods)
However, if you try to redefine a built-in constant or function already in use, Julia will give you an error:
julia> piπ = 3.1415926535897...julia> pi = 3ERROR: cannot assign a value to imported variable Base.pi from module Mainjulia> sqrt(100)10.0julia> sqrt = 4ERROR: cannot assign a value to imported variable Base.sqrt from module Main
Variable names must begin with a letter (A-Z or a-z), underscore, or a subset of Unicode code points greater than 00A0; in particular,Unicode character categories Lu/Ll/Lt/Lm/Lo/Nl (letters), Sc/So (currency and other symbols), and a few other letter-like characters (e.g. a subset of the Sm math symbols) are allowed. Subsequent characters may also include ! and digits (0-9 and other characters in categories Nd/No), as well as other Unicode code points: diacritics and other modifying marks (categories Mn/Mc/Me/Sk), some punctuation connectors (category Pc), primes, and a few other characters.
Operators like+
are also valid identifiers, but are parsed specially. In some contexts, operators can be used just like variables; for example(+)
refers to the addition function, and(+) = f
will reassign it. Most of the Unicode infix operators (in category Sm), such as⊕
, are parsed as infix operators and are available for user-defined methods (e.g. you can useconst ⊗ = kron
to define⊗
as an infix Kronecker product). Operators can also be suffixed with modifying marks, primes, and sub/superscripts, e.g.+̂ₐ″
is parsed as an infix operator with the same precedence as+
. A space is required between an operator that ends with a subscript/superscript letter and a subsequent variable name. For example, if+ᵃ
is an operator, then+ᵃx
must be written as+ᵃ x
to distinguish it from+ ᵃx
whereᵃx
is the variable name.
A particular class of variable names is one that contains only underscores. These identifiers are write-only. I.e. they can only be assigned values, which are immediately discarded, and their values cannot be used in any way.
julia> x, ___ = size([2 2; 1 1])(2, 2)julia> y = ___ERROR: syntax: all-underscore identifiers are write-only and their values cannot be used in expressionsjulia> println(___)ERROR: syntax: all-underscore identifiers are write-only and their values cannot be used in expressions
The only explicitly disallowed names for variables are the names of the built-inKeywords:
julia> else = falseERROR: syntax: unexpected "else"julia> try = "No"ERROR: syntax: unexpected "="
Some Unicode characters are considered to be equivalent in identifiers. Different ways of entering Unicode combining characters (e.g., accents) are treated as equivalent (specifically, Julia identifiers areNFC. Julia also includes a few non-standard equivalences for characters that are visually similar and are easily entered by some input methods. The Unicode charactersɛ
(U+025B: Latin small letter open e) andµ
(U+00B5: micro sign) are treated as equivalent to the corresponding Greek letters. The middle dot·
(U+00B7) and the Greekinterpunct·
(U+0387) are both treated as the mathematical dot operator⋅
(U+22C5). The minus sign−
(U+2212) is treated as equivalent to the hyphen-minus sign-
(U+002D).
An assignmentvariable = value
"binds" the namevariable
to thevalue
computed on the right-hand side, and the whole assignment is treated by Julia as an expression equal to the right-hand-sidevalue
. This means that assignments can bechained (the samevalue
assigned to multiple variables withvariable1 = variable2 = value
) or used in other expressions, and is also why their result is shown in the REPL as the value of the right-hand side. (In general, the REPL displays the value of whatever expression you evaluate.) For example, here the value4
ofb = 2+2
is used in another arithmetic operation and assignment:
julia> a = (b = 2+2) + 37julia> a7julia> b4
A common confusion is the distinction betweenassignment (giving a new "name" to a value) andmutation (changing a value). If you runa = 2
followed bya = 3
, you have changed the "name"a
to refer to a new value3
… you haven't changed the number2
, so2+2
will still give4
and not6
! This distinction becomes more clear when dealing withmutable types likearrays, whose contentscan be changed:
julia> a = [1,2,3] # an array of 3 integers3-element Vector{Int64}: 1 2 3julia> b = a # both b and a are names for the same array!3-element Vector{Int64}: 1 2 3
Here, the lineb = a
doesnot make a copy of the arraya
, it simply binds the nameb
to thesame arraya
: bothb
anda
"point" to one array[1,2,3]
in memory. In contrast, an assignmenta[i] = value
changes thecontents of the array, and the modified array will be visible through both the namesa
andb
:
julia> a[1] = 42 # change the first element42julia> a = 3.14159 # a is now the name of a different object3.14159julia> b # b refers to the original array object, which has been mutated3-element Vector{Int64}: 42 2 3
That is,a[i] = value
(an alias forsetindex!
)mutates an existing array object in memory, accessible via eithera
orb
. Subsequently settinga = 3.14159
does not change this array, it simply bindsa
to a different object; the array is still accessible viab
. Another common syntax to mutate an existing object isa.field = value
(an alias forsetproperty!
), which can be used to change amutable struct
. There is also mutation via dot assignment, for exampleb .= 5:7
(which mutates our arrayb
in-place to contain[5,6,7]
), as part of Julia'svectorized "dot" syntax.
When you call afunction in Julia, it behaves as if youassigned the argument values to new variable names corresponding to the function arguments, as discussed inArgument-Passing Behavior. (Byconvention, functions that mutate one or more of their arguments have names ending with!
.)
While Julia imposes few restrictions on valid names, it has become useful to adopt the following conventions:
'_'
), but use of underscores is discouraged unless the name would be hard to read otherwise.Type
s andModule
s begin with a capital letter and word separation is shown with upper camel case instead of underscores.function
s andmacro
s are in lower case, without underscores.!
. These are sometimes called "mutating" or "in-place" functions because they are intended to produce changes in their arguments after the function is called, not just return a value.For more information about stylistic conventions, see theStyle Guide.
Settings
This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.