# 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:** 1
**Showing post:** 3

<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

```

---

_[View the full topic](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018)._
