ZetCode

Julia Operators Tutorial

last modified November 18, 2026

Operators in Julia are symbols or keywords used to perform operations on variables and values. They are essential for arithmetic, logical, and comparison tasks. This tutorial covers basic and advanced operators with practical examples.

Julia supports arithmetic, comparison, logical, bitwise, and assignment operators. These operators are used to manipulate data and control program flow. All examples below were verified with Julia 1.13.4; each listing is a standalone main.jl file.

Arithmetic Operators

Arithmetic operators perform mathematical operations like addition, subtraction, multiplication, and division.

main.jl
x = 10
y = 5

println(x + y)
println(x - y)
println(x * y)
println(x / y)
println(x % y)

The code performs the five basic arithmetic operations. Note that / always returns a floating-point value (2.0), while % gives the remainder of an integer division.

$ julia main.jl
15
5
50
2.0
0

Comparison Operators

Comparison operators compare two values and return a boolean result.

main.jl
x = 10
y = 5

println(x == y)
println(x != y)
println(x > y)
println(x < y)
println(x >= y)
println(x <= y)

The code compares two values and prints the result of each comparison. The operators test equality, inequality, and the usual orderings; every result is either true or false.

$ julia main.jl
false
true
true
false
true
false

Logical Operators

Logical operators are used to combine conditional statements.

main.jl
a = true
b = false

println(a && b)
println(a || b)
println(!a)

The code demonstrates the logical AND, OR, and NOT operators. They work on Bool values and are commonly used in conditions.

$ julia main.jl
false
true
false

Bitwise Operators

Bitwise operators perform operations on binary representations of integers.

main.jl
x = 10
y = 5

println(x & y)
println(x | y)
println(x ⊻ y)
println(~x)
println(x << 1)
println(x >> 1)

The code demonstrates bitwise AND, OR, XOR (⊻), NOT, and the left and right shift operators. For example, 10 & 5 is 0 because the two values share no set bits.

$ julia main.jl
0
15
15
-11
20
5

Assignment Operators

Assignment operators assign values to variables, often combining an operation with the assignment.

main.jl
x = 10
x += 5
x -= 3
x *= 2
x /= 4
println(x)

The code updates x step by step. As with /, the division assignment /= produces a floating-point result, so the final value is 6.0.

$ julia main.jl
6.0

Ternary Operator

The ternary operator is a shorthand for an if-else statement.

main.jl
x = 10
y = 5

result = x > y ? "x is greater" : "y is greater"
println(result)

The condition before the ? is evaluated; if it is true the value before the : is chosen, otherwise the value after it is chosen. Here x > y is true, so the first string is printed.

$ julia main.jl
x is greater

Range Operator

The range operator creates a sequence of numbers.

main.jl
r = 1:10
println(r)

The : operator builds a UnitRange from 1 to 10. Ranges are used for iteration and indexing; they do not store every value explicitly.

$ julia main.jl
1:10

Pipe Operator

The pipe operator passes the result of one expression as an argument to another.

main.jl
result = 16 |> sqrt |> x -> x^2
println(result)

The value 16 is passed to sqrt, and that result is passed to the anonymous function that squares it. The code therefore calculates the square of the square root of 16.

$ julia main.jl
16.0

Broadcasting Operator

The broadcasting operator applies a function to each element of an array.

main.jl
arr = [1, 2, 3]
result = sqrt.(arr)
println(result)

The dot before the parentheses broadcasts sqrt over the array, so each element is converted to its square root individually.

$ julia main.jl
[1.0, 1.4142135623730951, 1.7320508075688772]

Best Practices for Using Operators

A few guidelines help keep expressions correct and readable.

Source

Julia Mathematical Operations documentation

Julia Base documentation

In this article we have worked with operators in Julia: arithmetic, comparison, logical, bitwise, assignment, ternary, range, pipe, and broadcasting. Variables are introduced in Julia variables, and control flow is covered in Julia control flow.

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.