# Test for existence of a variable

**URL:** <https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 10, 2020, 1:40pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845 "2020-07-10T13:40:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 10, 2020, 1:40pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/1 "2020-07-10T13:40:32Z")

</div>

Hi. I have been using Julia for more than a year, but because Julia is Julia, I still feel like a Newbie, so please be gentle.  
I have searched everywhere for a simple test of existence of a variable, like exist() in MATLAB. I saw a post for isdefined(x) but it was old and didn’t work (seems to want 2 arguments, which I didn’t understand and could find any _clear_ documentation on). At the moment I am using

function exist(x); ind=true; try; x=x; catch; ind=false; end; return ind; end

Isn’t there a simple native function?

Thanks

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [July 10, 2020, 1:44pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/2 "2020-07-10T13:44:06Z")

</div>

[https://docs.julialang.org/en/v1/base/base/#Core.isdefined](https://docs.julialang.org/en/v1/base/base/#Core.isdefined)

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 10, 2020, 1:46pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/3 "2020-07-10T13:46:28Z")

</div>

Thanks. Sorry but I saw those examples and they were of no help to me.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [July 10, 2020, 1:51pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/4 "2020-07-10T13:51:50Z")

</div>

I think `@isdefined` is closer to what the OP asked for:

[https://docs.julialang.org/en/v1/base/base/#Base.@isdefined](https://docs.julialang.org/en/v1/base/base/#Base.@isdefined)

It’s usually more common just to assign `nothing` to `x` on top of the function though, and then checking for `x === nothing` in the body. Otherwise you might run into problems if `x` is already defined in global scope.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 10, 2020, 1:57pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/5 "2020-07-10T13:57:48Z")

</div>

Hi

Thanks! Exactly what I needed. Surprised it did not come up in any  
of my searches. Is there a special place for ‘@’ functions?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [July 10, 2020, 2:03pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/6 "2020-07-10T14:03:52Z")

</div>

I don’t know what you mean by ampersand functions. You might be interested in the [Noteworthy Differences](https://docs.julialang.org/en/v1/manual/noteworthy-differences/) section of the manual, where some differences from MATLAB are highlighted.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 10, 2020, 2:05pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/7 "2020-07-10T14:05:52Z")

</div>

Sorry, I meant functions beginning with the ‘@’ (which is obviously not the ampersand!)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 10, 2020, 2:06pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/8 "2020-07-10T14:06:52Z")

</div>

Do you mean [macros](https://docs.julialang.org/en/v1/manual/metaprogramming/#man-macros-1)?

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 10, 2020, 2:14pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/9 "2020-07-10T14:14:07Z")

</div>

Sorry - yes, macros then. In this case, there is a very handy in-built macro doing (arguably) exactly what most people would be wanting who looked up this part of ‘Help’, yet the macro you mentioned doesn’t appear anywhere in the documentation that I can see. I am sure there must be lots of other in-built macros that are not clearly documented? Or perhaps there is somewhere where they all hide?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [July 10, 2020, 2:24pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/10 "2020-07-10T14:24:13Z")

</div>

If you think it should be mentioned at a specific point in the manual, a [PR](https://github.com/JuliaLang/julia/pulls) would certainly be appreciated. I don’t think it’s reasonable to expect that every macro gets mentioned in the main documentation though, since there are quite a lot of them in Base and I don’t think this would be that useful to most people.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 10, 2020, 2:31pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/11 "2020-07-10T14:31:29Z")

</div>

I think a mention of this macro in the help would have really saved me (and you!) alot of time and bother here, but I am appreciative of your taking the time.  
I also teach lots of students, and while I am sure there is beauty in the abstraction to the 2-argument version of the function, I know that every single one of my (hundreds) of students would be looking for the simple (single-argument) answer, which is the one you gave me. I’m just wondering why it has to be so hard to find? Things like this are a barrier to my using Julia in teaching which I would like to do.  
Thanks

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 10, 2020, 3:11pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/12 "2020-07-10T15:11:07Z")

</div>

> [@compleat](#):
>
> I know that every single one of my (hundreds) of students would be looking for the simple (single-argument) answer, which is the one you gave me.

Just as a data point, I have never reached for a `@isdefined` macro or felt the need to check whether or not a variable exists in scope. If a variable doesn’t exist, I get an error, and that’s fine.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 10, 2020, 4:14pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/13 "2020-07-10T16:14:13Z")

</div>

I also never had the need for such a function/ macro (neither in Julia nor in an other language).

@compleat I am curious about your use case!

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 10, 2020, 5:25pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/14 "2020-07-10T17:25:49Z")

</div>

It has to do with backwards compatibility of some things I am writing.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 10, 2020, 8:47pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/15 "2020-07-10T20:47:53Z")

</div>

> [@compleat](#):
>
> I also teach lots of students, and while I am sure there is beauty in the abstraction to the 2-argument version of the function, I know that every single one of my (hundreds) of students would be looking for the simple (single-argument) answer, which is the one you gave me.

In the current documentation, the function is mentioned in a FAQ that I guess is relevant for your use case:  
[https://docs.julialang.org/en/v1/manual/faq/#faq-nothing-1](https://docs.julialang.org/en/v1/manual/faq/#faq-nothing-1)

> [@](#):
>
> When a reference (variable, object field, or array element) is uninitialized, accessing it will immediately throw an error. This situation can be detected using the [`isdefined`](https://docs.julialang.org/en/v1/base/base/#Core.isdefined) or [`isassigned`](https://docs.julialang.org/en/v1/base/arrays/#Base.isassigned) functions.

And the docstring of the function `isdefined` includes a reference to the macro:  
[https://docs.julialang.org/en/v1/base/base/#Core.isdefined](https://docs.julialang.org/en/v1/base/base/#Core.isdefined)  
(Although it it just a terse “See also `@isdefined`”.)

I understand that you might have missed it, because it is just an indirect, not very explicit reference. But you are in the best position to suggest how it might be clearer: Add a link to `@isdefined` in that FAQ? A more explicit comment on it in the docstring of `isdefined`…?

As @simeonschaub told, if you are user of Github you may propose a modification to the documentation yourself. (There is an “Edit on Github” link a the top of the documentation pages.)

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 10, 2020, 9:37pm UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/16 "2020-07-10T21:37:51Z")

</div>

It just seems to me that the documentation is written as if the audience is those who are already experts in Julia and its ways of doing things. The examples seem to be chosen to exhibit the greatest degree of generality of the functionality of what is being documented, rather the simplest cases.  
I am not saying that all the generality should not be demonstrated, but perhaps some very basic examples also included, which does not seem part of the ‘culture’ at the moment.

---

<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:** [July 11, 2020, 6:41am UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/17 "2020-07-11T06:41:14Z")

</div>

> [@compleat](#):
>
> perhaps some very basic examples also included, which does not seem part of the ‘culture’ at the moment

It is not a matter of “culture”, but just someone doing work.

The best way to change this is by making PRs adding the examples.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 14, 2020, 8:47am UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/18 "2020-07-14T08:47:32Z")

</div>

I tried to do as you suggested, but I couldn’t even figure out how to suggest a change/addition - seems like it is only for experts. [I have contributed to Wikipedia in the past without issue, but I wouldn’t have a clue how to proceed with this. Again, it seems like (as I said) the dominant culture here tends towards ‘by experts for experts’ - I am not complaining as I am grateful that Julia is even there at all, I am just saying that as someone who does not wish to dedicate my whole life to Julia, I feel marginalised]

---

<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:** [July 14, 2020, 9:04am UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/19 "2020-07-14T09:04:54Z")

</div>

> [@compleat](#):
>
> as someone who does not wish to dedicate my whole life to Julia, I feel marginalised

It’s fine if you don’t want to contribute, no one is forcing you.

But engaging in hyperbole as an alternative is unlikely to accomplish anything — most people contribute to Julia without “dedicating their whole life” to it.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 14, 2020, 9:14am UTC](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845/20 "2020-07-14T09:14:11Z")

</div>

I’m just saying that it could be made a bit more straightforward for a non-expert to make a contribution if there were a sincere interest in engaging non-experts.

[Next page](https://discourse.julialang.org/t/test-for-existence-of-a-variable/42845.md?page=2)
