Test for existence of a variable

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. Sorry but I saw those examples and they were of no help to me.

1 Like

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


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.



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

I don’t know what you mean by ampersand functions. You might be interested in the Noteworthy Differences section of the manual, where some differences from MATLAB are highlighted.

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

Do you mean macros?


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?

If you think it should be mentioned at a specific point in the manual, a PR 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.

1 Like

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.

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.


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!

1 Like

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

In the current documentation, the function is mentioned in a FAQ that I guess is relevant for your use case:

And the docstring of the function isdefined includes a reference to the macro:
(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.)


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.

1 Like

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.


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]

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.

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.