Variable type is necessary to understand to provide a valid value to that variable. In this answer, we'll learn how to get the type of a variable in Julia.
Julia provides a function typeof()
to get the type of the variable.
typeof(provide your variable name here)
This function takes a variable name as a parameter and returns its type.
Let's take a look at an example of this.
var1 = 10#display type of var1println("Type of variable var1 is ", typeof(var1))var2 = 10.5#display type of var2println("Type of variable var2 is ", typeof(var2))var3 = "Hello from Educative !!!"#display type of var3println("Type of variable var3 is ", typeof(var3))var4 = [1, 3, 5, 7, 9]#display type of var4println("Type of variable var4 is ", typeof(var4))
Line 1: We declare and initialize a variable var1
with an integer value.
Line 4: We get the type of a variable var1
using the typeof()
function.
Line 6: We declare and initialize a variable var2
with a float value.
Line 9: We get the type of a variable var2
using the typeof()
function and display it.
Line 11: We declare and initialize a variable var3
with a string value.
Line 14: We get the type of a variable var3
using the typeof()
function.
Line 16: We declare and initialize a variable var4
with a vector of integer value.
Line 19: We get the type of a variable var4
using the typeof()
function and display it.
Free Resources