HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JuliaVariablesData TypesOperatorsStringsArraysConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Array & Dictionary
🔀 Condition & Loop
🧩 Function
Julia›Variables

Variables

Variable assignment with =; types are inferred from the value, and the value can be changed.

Simple enough for a 5-year-old to understand

Variable assignment

Values are assigned with =. You don't need to specify the type; it is inferred from the value.

Example code
1
name = "Alex"
String variable.
VariableString variable.
2
age = 36
Integer variable.
VariableInteger variable.
3
println(name)
Alex
Output / InputAlex
4
println(age)
36
Output / Input36
VariableOutput / Input
💡 Hint: Julia figures out the type itself from the value.

About this topic

In Julia, you don't need to write the type when defining a variable; the assigned value determines the type. You can specify the type if you want, which helps the compiler especially in performance-critical code.

Unicode is free in variable names. Mathematical symbols can be used directly, and in scientific code, you can write variable names that look very much like the formula on paper. This is a choice reflecting Julia's target audience.

Scope rules require attention in Julia. A loop or function creates its own local scope; if you want to assign a value to an external variable from the inside, you must explicitly state this. Otherwise, a new local variable is created.

What you will learn

  • Variable definition
  • Specifying types
  • Scope rules
  • The cost of global variables
  • Naming conventions

Who is this for?

  • Beginners to Julia
  • Those trying to update variables inside a loop
  • Those experiencing performance issues

When to use?

Performance-critical calculations should be wrapped in a function; code running in the global scope is noticeably slower because the compiler cannot fixate the type.

Type annotation is used to document function interfaces and guide multiple dispatch; it is not needed for every local variable.

Common mistakes

Wrong

toplam = 0
for i in 1:5
    toplam = toplam + i
end
println(toplam)

The loop opens its own local scope; when executed within a script, assignment to an external variable cannot be made and a warning or error is thrown. You must explicitly state that you want to write to the global variable. The correct approach, however, is to put this code into a function.

Correct

function topla()
    toplam = 0
    for i in 1:5
        toplam = toplam + i
    end
    return toplam
end
println(topla())

Wrong

x = 5
x = "hello"
y = x + 1

Julia allows you to change the type of a variable, but the subsequent operation tries to add text and a number together and results in a MethodError. Type-changing variables also prevent the compiler from optimizing; using the same variable for different types should be avoided.

Correct

x = 5
mesaj = "hello"
y = x + 1

Wrong

const SABIT = 10
SABIT = 20

When a reassignment is made to a name defined with const, Julia warns and in some cases produces an error. Constants tell the compiler that the value will not change; since this information is used for optimization, violating it leads to unexpected behavior.

Correct

const SABIT = 10
yeni_deger = 20

Frequently asked questions

Why are global variables slow?

Because their values can change at any moment and the compiler cannot fixate the type; it has to check the type on every access. Putting performance-critical code inside a function completely eliminates this problem.

Is the variable inside the loop visible from the outside?

It is not visible; the loop opens a new scope and the names defined there disappear when the loop ends. To carry the result outside, you must define it before the loop and explicitly specify global access or put the code into a function.

Does specifying types make it faster?

Not directly. Julia infers types anyway; the real gain is in type stability, meaning that a variable's type does not change throughout the function. Specification is more valuable for documenting interfaces and guiding dispatch.

Summary

Type declaration is optional.

The loop opens its own scope.

Code running in the global scope is slow.

Performance code must be inside a function.

Using the same variable with different types should be avoided.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code