# How to check whether a identifier is defined within the current scope?

**URL:** <https://discourse.julialang.org/t/how-to-check-whether-a-identifier-is-defined-within-the-current-scope/7470>\
**Category:** General Usage\
**Created:** [December 3, 2017, 6:56am UTC](https://discourse.julialang.org/t/how-to-check-whether-a-identifier-is-defined-within-the-current-scope/7470 "2017-12-03T06:56:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [December 3, 2017, 6:56am UTC](https://discourse.julialang.org/t/how-to-check-whether-a-identifier-is-defined-within-the-current-scope/7470/1 "2017-12-03T06:56:38Z")

</div>

Hi everyone,

Is there a way to check whether an identifier is defined in the current scope?

```nohighlight
function f(x)
    z =1
    already_defined = #check whether z is defined here
    if already_defined
        z = do_stuff_on(z)
        z+=1
    else
       z =init_z(1)  
    end
end

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 3, 2017, 7:14am UTC](https://discourse.julialang.org/t/how-to-check-whether-a-identifier-is-defined-within-the-current-scope/7470/2 "2017-12-03T07:14:28Z")

</div>

Only on 0.7 this is possible:

```julia
help?> @isdefined                                                                                                                                                        
  @isdefined s -> Bool                                                                                                                                                   
                                                                                                                                                                         
  Tests whether variable s is defined in the current scope.                                                                                                              
                                                                                                                                                                         
  Examples                                                                                                                                                               
  ≡≡≡≡≡≡≡≡≡≡                                                                                                                                                             

  julia> function f()                                                                                                                                                    
             println(@isdefined x)                                                                                                                                       
             x = 3                                                                                                                                                       
             println(@isdefined x)                                                                                                                                       
         end                                                                                                                                                             
  f (generic function with 1 method)                                                                                                                                     
  
  julia> f()                                                                                                                                                             
  false                                                                                                                                                                  
  true                                                                                                                                                                   

```

---

<div class="post-metadata">

**Author:** ![denius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denius/32/3607_2.png) [@denius](https://discourse.julialang.org/u/denius)\
**Post date:** [December 4, 2017, 4:28pm UTC](https://discourse.julialang.org/t/how-to-check-whether-a-identifier-is-defined-within-the-current-scope/7470/4 "2017-12-04T16:28:04Z")

</div>

Along `@isdefined` on 0.7 there is exist `isdefined()` on 0.6, thus you can use

```julia
if VERSION < v"0.7.0-DEV"
    macro isdefined(x)
        esc(isdefined(x))
    end
    export @isdefined
end

@isdefined(somevar)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 4, 2017, 5:22pm UTC](https://discourse.julialang.org/t/how-to-check-whether-a-identifier-is-defined-within-the-current-scope/7470/5 "2017-12-04T17:22:42Z")

</div>

> [@denius](#):
>
> there is exist isdefined() on 0.6

I thought that only worked on the global scope.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 4, 2017, 5:54pm UTC](https://discourse.julialang.org/t/how-to-check-whether-a-identifier-is-defined-within-the-current-scope/7470/6 "2017-12-04T17:54:51Z")

</div>

Yes, that only works for global variables. The `@isdefined` macro that works for locals requires language support and is not possible to backport to 0.6.x.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [July 6, 2018, 10:26pm UTC](https://discourse.julialang.org/t/how-to-check-whether-a-identifier-is-defined-within-the-current-scope/7470/7 "2018-07-06T22:26:33Z")

</div>

In case you still need to do this in v0.6. Here is a macro that checks if a variable is defined in a local scope: [Check whether a variable is defined - #3 by mike](https://discourse.julialang.org/t/check-whether-a-variable-is-defined/1018/3)
