title: JavaScript: if let / if-scoped let
body: <p>I cannot seem to find anything similar to <code>if let</code> from other programming languages in JavaScript.</p>
  <p>If I want to get the logo text of Stack Overflow I need to do</p>
  <pre><code>let text = document.querySelector('[class=&quot;-img _glyph&quot;]')
  if(text) {
      result = text.innerText
      //do some other work
  }
  </code></pre>
  <p>So after declaring, I have to check for it to not be <code>undefined</code> first before using it. Now what would be much more logical is the following:</p>
  <pre><code>if let text = document.querySelector('[class=&quot;-img _glyph&quot;]') {
      result = text.innerText
      //do some other work
  }
  </code></pre>
  <p>which however doesn't work in JavaScript. Is there another syntax that I can use to avoid having to use the extra line just for the undefined-check?</p>
  <p>I found this 10 year old thread <a href="https://esdiscuss.org/topic/if-scoped-let" rel="nofollow noreferrer">https://esdiscuss.org/topic/if-scoped-let</a> but since there were no further responses, I don't know if there is already anything that solves this.</p>