# Test expression for instance of variable

**URL:** <https://discourse.julialang.org/t/test-expression-for-instance-of-variable/110740>\
**Category:** New to Julia\
**Created:** [February 25, 2024, 2:58pm UTC](https://discourse.julialang.org/t/test-expression-for-instance-of-variable/110740 "2024-02-25T14:58:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 25, 2024, 2:58pm UTC](https://discourse.julialang.org/t/test-expression-for-instance-of-variable/110740/1 "2024-02-25T14:58:50Z")

</div>

Is there a way of testing a symbolic expression to see if it contains a given variable other than seeing if the derivative is zero. For example test `(r*sin(th))^2` to see if it contains `th`. Is there a way of looking at the expression tree to do this?

---

<div class="post-metadata">

**Author:** ![savq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/savq/32/22063_2.png) [@savq](https://discourse.julialang.org/u/savq)\
**Post date:** [March 3, 2024, 6:37am UTC](https://discourse.julialang.org/t/test-expression-for-instance-of-variable/110740/2 "2024-03-03T06:37:31Z")

</div>

I don’t know if you found an answer for Symbolics specifically, but here’s how I’d do it for a plain `Expr`:

```julia-repl
julia> begin
           check_var(expr::Expr, var::Symbol) = any(sub_expr -> check_var(sub_expr, var), expr.args)
           check_var(candidate::Symbol, var::Symbol) = candidate === var
           check_var(token, _) = false # ignore numbers, strings, etc
       end
check_var (generic function with 3 methods)

julia> check_var(
           :((r*sin(th))^2),
           :th
       )
true

```
