I cannot seem to find anything similar to if let from other programming languages in JavaScript.
If I want to get the logo text of Stack Overflow I need to do
let text = document.querySelector('[class="-img _glyph"]')
if(text) {
result = text.innerText
//do some other work
}
So after declaring, I have to check for it to not be undefined first before using it. Now what would be much more logical is the following:
if let text = document.querySelector('[class="-img _glyph"]') {
result = text.innerText
//do some other work
}
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?
I found this 10 year old thread https://esdiscuss.org/topic/if-scoped-let but since there were no further responses, I don't know if there is already anything that solves this.