Comments are notes written inside Julia source code to explain logic, document assumptions, or temporarily prevent code from running. Julia ignores comments while evaluating the program.
Julia supports single-line comments beginning with # and multiline comments enclosed by #= and =#.
Julia single-line comments with the hash symbol
To write a single-line comment in Julia, place the # character before the comment text. Everything from # to the end of that line is ignored.
# This is a single-line comment in Julia
A single-line comment can appear on its own line above the code it describes.
# Calculate the area of a rectangle
length = 8
width = 5
area = length * width
println(area)
40
Adding an inline comment after Julia code
You can also place a comment after a Julia statement. The code before # is evaluated, while the text after it is ignored.
temperature = 28 # Temperature in degrees Celsius
println(temperature)
Inline comments are useful for short clarifications. Longer explanations are usually easier to read when placed on a separate line above the relevant code.
Julia multiline comments using #= and =#
A multiline comment begins with #= and ends with =#. Julia ignores all text and code between these delimiters, even when the comment spans several lines.
#=
This is a multiline comment.
It can continue across several lines.
=#
The following example uses a multiline comment to describe a calculation.
#=
Calculate the total price by multiplying
the unit price by the quantity.
=#
unit_price = 12.5
quantity = 4
total = unit_price * quantity
println(total)
50.0
Commenting out several Julia statements
Multiline comments can temporarily disable a group of statements while testing or debugging a program.
value = 10
#=
value = value * 2
println("Intermediate value: ", value)
=#
println("Final value: ", value)
Final value: 10
Because the two statements inside #= and =# are ignored, the value remains 10.
Nested multiline comments in Julia
Julia allows multiline comments to be nested. This means that one #= ... =# comment can appear inside another multiline comment.
#=
Outer comment starts here.
#=
This is a nested comment.
=#
Outer comment continues here.
=#
println("Program continues")
Program continues
Nested comments are particularly useful when a block of code already contains multiline comments and you want to comment out the entire block temporarily.
Julia comments and docstrings are different
A string written with triple quotation marks is not a multiline comment in Julia. When placed immediately before a function, type, or module, it can become a docstring that Julia’s documentation system can display.
"""
square(number)
Return the square of `number`.
"""
function square(number)
return number * number
end
println(square(6))
36
Use # or #= ... =# for comments that should be ignored. Use a docstring when you want documentation to be associated with a Julia definition.
Choosing between single-line and multiline Julia comments
- Use
#for a short explanation, reminder, or inline note. - Use
#= ... =#for an explanation that requires several lines. - Use multiline delimiters to disable a section of code temporarily.
- Use a docstring instead of a comment when documenting a function, type, macro, or module for users of the code.
Common mistakes with comments in Julia
- Using only one delimiter: A multiline comment must begin with
#=and end with=#. - Reversing the closing symbols: The correct closing delimiter is
=#, not#=. - Treating triple-quoted text as a comment: Triple-quoted text creates a string and may act as a docstring in the appropriate position.
- Explaining obvious statements: Comments are more useful when they explain intent, assumptions, units, or non-obvious decisions.
- Leaving outdated comments: Update comments whenever the related code changes.
Julia comments example with single-line and multiline notes
# Number of hours worked
hours = 7.5
# Hourly payment rate
rate = 20.0
#=
The gross payment is calculated before
any deductions or adjustments are applied.
=#
gross_payment = hours * rate
println("Gross payment: ", gross_payment)
Gross payment: 150.0
Frequently asked questions about Julia comments
How do I comment out multiple lines in Julia?
Place #= before the first line and =# after the final line. Julia ignores everything enclosed by these delimiters.
Can Julia multiline comments be nested?
Yes. Julia supports nested #= ... =# comments, so a multiline comment can contain another multiline comment.
Can I add a comment after a Julia statement?
Yes. Add # after the statement. Julia evaluates the code before the symbol and ignores the remaining text on that line.
Are triple quotes used for multiline comments in Julia?
No. Triple quotation marks create a multiline string. A triple-quoted string placed before a definition can serve as a docstring, but it is not Julia’s multiline-comment syntax.
Does Julia execute code written inside a comment?
No. Julia ignores text and statements inside valid single-line or multiline comments during evaluation.
Editorial QA checklist for Julia comment examples
- Confirm every single-line comment begins with
#. - Confirm every multiline comment has matching
#=and=#delimiters. - Verify nested multiline-comment delimiters are correctly balanced.
- Do not describe triple-quoted strings or docstrings as Julia comments.
- Run each executable example and verify that its displayed output matches.
TutorialKart.com