Core Syntax at a Glance
This quick reference covers the Julia syntax you'll reach for constantly: variable assignment, control flow, function definitions, and collections. Julia uses 1-based indexing (the first element of an array is x[1], not x[0]), semicolons are optional at the end of a line, and # starts a comment, with #= ... =# for a multi-line block comment.
Cricket analogy: A quick reference sheet is like a laminated fielding-position card a captain keeps in their pocket during a match, a fast lookup for where each player stands rather than a full coaching manual to page through mid-over.
Control Flow and Functions
Function definitions use the function ... end block form or the compact one-line form f(x) = x^2 + 1; both create identical methods. Conditionals use if / elseif / else / end, and the ternary operator cond ? a : b is idiomatic for short expressions. Anonymous functions use arrow syntax, x -> x^2, and are commonly passed directly to higher-order functions like map(x -> x^2, [1, 2, 3]) or filter(x -> x % 2 == 0, 1:10).
Cricket analogy: The one-line function form is like a captain giving a one-word field instruction, 'slip', that both the fielder and the whole team instantly understand, versus a full timeout explaining the exact positioning; both achieve the same result with different verbosity.
Collections and Broadcasting
Arrays ([1, 2, 3]), tuples ((1, "a", 3.0)), and dictionaries (Dict("a" => 1, "b" => 2)) are the core collection types. The dot syntax for broadcasting, f.(collection), applies any function elementwise without writing an explicit loop, and it fuses multiple dotted operations into a single pass: y = sin.(x) .+ cos.(x) .* 2 allocates only one output array even though it looks like three separate operations.
Cricket analogy: Broadcasting a function with dot syntax is like a coach applying the same fielding drill to every player in the squad in one announced session, rather than pulling each player aside individually to repeat the same instruction one at a time.
# Variables and 1-based indexing
x = [10, 20, 30]
first_elem = x[1] # 10, not x[0]
# Control flow
function classify(n)
if n < 0
return "negative"
elseif n == 0
return "zero"
else
return "positive"
end
end
# Compact function + ternary
square(x) = x^2
sign_word(n) = n >= 0 ? "non-negative" : "negative"
# Collections
nums = [1, 2, 3, 4, 5]
squares = map(x -> x^2, nums)
evens = filter(x -> x % 2 == 0, nums)
d = Dict("a" => 1, "b" => 2)
# Broadcasting (fused, single allocation)
y = sin.(nums) .+ cos.(nums) .* 2Broadcasting with the dot syntax isn't just shorthand for a loop, it's a compiler-level fusion optimization. Chained dotted operations like f.(g.(x)) .+ h.(x) compile into a single pass over the array with one allocation, instead of allocating an intermediate array for each dotted call.
Remember Julia is 1-indexed: x[1] is the first element and x[end] is the last. Porting code from Python or C, where x[0] is the first element, is one of the most common sources of off-by-one bugs for newcomers.
- Julia arrays and most collections are 1-indexed; use x[1] for the first element and x[end] for the last.
- Functions can be defined with the full function...end block or the compact one-line f(x) = ... form; both are equivalent.
- The ternary operator cond ? a : b and anonymous functions x -> expr are idiomatic for short, inline logic.
- Dict("key" => value) constructs a dictionary; the => operator forms key-value pairs.
- Dot broadcasting, f.(collection), applies a function elementwise and fuses chained dotted operations into a single allocation.
- Comments use # for single-line and #= ... =# for multi-line blocks.
- map and filter with anonymous functions are the idiomatic way to transform and select from collections without an explicit loop.
Practice what you learned
1. What is the index of the first element of a Julia array x?
2. Which syntax constructs a dictionary in Julia?
3. What does the dot syntax in y = sin.(x) .+ cos.(x) accomplish beyond applying functions elementwise?
4. Which of these refers to the last element of a Julia array x?
5. What is the equivalent of f(x) = x^2 + 1 written with the block form?
Was this page helpful?
You May Also Like
Julia vs Python
A practical comparison of Julia and Python for numerical computing, covering performance, syntax, and ecosystem tradeoffs.
Julia Best Practices
Guidelines for writing fast, idiomatic, and maintainable Julia code, from type stability to package structure.
Julia Interview Questions
Common interview questions and model answers covering Julia fundamentals, performance, and multiple dispatch.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics