Variables

Published May 29, 2024
Contribute to Docs

In Julia, variables are used to store data. They can hold various data types and are mutable by default, allowing their values to be changed after assignment.

Syntax

variable_name = value
  • variable_name: This is the chosen name for the variable, following Julia’s naming rules and conventions.
  • value: Represents the data or value assigned to the variable, which can be of various types supported by Julia.

Example

integer_variable = 10
float_variable = 3.14
string_variable = "Julia"
boolean_variable = true
println("Integer variable: ", integer_variable)
println("Float variable: ", float_variable)
println("String variable: ", string_variable)
println("Boolean variable: ", boolean_variable)

The output for the above code is as follows:

Integer variable: 10
Float variable: 3.14
String variable: Julia
Boolean variable: true

Variable Nomenclature

When declaring a variable in Julia, the variable name goes first, optionally followed by a type annotation and then the value.

If a variable does not need a value to be assigned immediately, it can be declared without a type and a value can be assigned later:

name = value # Without type annotation
name::Type = value # With type annotation

Note: Variables in Julia are dynamic and can hold values of different types, but you can optionally declare the type for performance optimization and type-checking.

All contributors

Looking to contribute?

Learn More on Codecademy