Recently I was working with some JavaScript code and I found this:
<script type="text/javascript">
window.localStorage&&window.localStorage.clear();
</script>
I did research around the web but didn’t find anything about how actually that ‘&&’ operator works if isn’t in a control statement.
Does anyone know how it works?
That’s the same as:
The
&&short-circuits as soon as it sees afalse(or “falsy”) value.So, if
window.localStorageisfalse(or “falsy”), it stops. If it’s true, it continues and runswindow.localStorage.clear(). The return value is ignored.