# Check whether a variable is defined

**URL:** https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018
**Category:** New to Julia
**Created:** [December 18, 2016, 4:59am UTC](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018 "2016-12-18T04:59:49Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Diger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diger/32/36786_2.png) [@Diger](https://discourse.julialang.org/u/Diger)
#### Post date: [December 18, 2016, 4:59am UTC](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018/1 "2016-12-18T04:59:49Z")

</div>

is there a simple check to see whether a variable is defined?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 18, 2016, 5:11am UTC](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018/2 "2016-12-18T05:11:44Z")

</div>

Just do `isdefined(:var)`

---

<div class="post-metadata">

### Author: ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)
#### Post date: [December 18, 2016, 8:45am UTC](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018/3 "2016-12-18T08:45:38Z")

</div>

`isdefined` will only work within the global scope of a module, i.e. the result of

```julia
julia> function test(x)
           isdefined(:x)
       end
test (generic function with 1 method)

julia> test(1)
false

```

might not be what you expect it to be.

If you need something for local scopes as well then try the following macro:

```julia
julia> macro isdefined(var)
           quote
               try
                   local _ = $(esc(var))
                   true
               catch err
                   isa(err, UndefVarError) ? false : rethrow(err)
               end
           end
       end

```

And then use it like so

```julia
julia> function test(x)
           @isdefined x
       end
test (generic function with 1 method)

julia> test(1)
true

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 18, 2016, 6:01pm UTC](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018/4 "2016-12-18T18:01:18Z")

</div>

Interesting. I never knew that it had this issue. I would like to see this in Base.

---

<div class="post-metadata">

### Author: ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)
#### Post date: [December 19, 2016, 7:48am UTC](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018/5 "2016-12-19T07:48:49Z")

</div>

> [@ChrisRackauckas](#):
>
> I would like to see this in Base.

I’ve not really found it that useful for anything aside from writing tests that check whether a macro expansion works correctly, i.e.

```julia
function my_test()
    @my_macro arg1 arg2
    @test @isdefined generated_var
    @test @isdefined other_generated_var
end

```

so probably not worth adding to `Base` just for that.

I guess it really depends what @Diger is needing to do with `isdefined`, but in “normal” code I’d try to avoid needing to doing any kind of control flow based on whether a variable is defined or not.

---

<div class="post-metadata">

### Author: ![Diger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diger/32/36786_2.png) [@Diger](https://discourse.julialang.org/u/Diger)
#### Post date: [January 5, 2017, 5:45pm UTC](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018/6 "2017-01-05T17:45:52Z")

</div>

Sorry for my late answer. What does  
quote  
…  
end  
mean ?  
Also why do u wrap a function around “everything” ??  
isdefined(:x) does work for my purposes.  
In which sense however do u mean doesn’t it work?  
I see that in your first example where u wrap isdefined in a function it gives false when u use it with integers instead of symbols, while isdefined(1) or isdefined(:1) would give you an error…

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [April 20, 2019, 12:16pm UTC](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018/7 "2019-04-20T12:16:57Z")

</div>

For anyone coming back to this today,  
in the post julia 1.0 world.

There now exists a macro that checks if a variable is defined in the current scope.  
`@isdefined`, and it does not do so via the throwing and catching of errors, so it not hideously slow.

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [September 14, 2023, 12:57pm UTC](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018/8 "2023-09-14T12:57:45Z")

</div>

I read all the stuff, but it was still not clear to me how to test if the variable `Conda.ROOTENV` exists.  
The documentation was not sufficient for me, I tried a lot of variants also with the macro `@isdefined`  
and in the end, I think I succeeded:

```julia
using Conda
isdefined(Conda, :ROOTENV)

```
