Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Understanding && Date: Fri, 16 Apr 2021 11:00:19 +0100 Organization: A noiseless patient Spider Lines: 30 Message-ID: <878s5ift1o.fsf@bsb.me.uk> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="bcf9a544088c93a28385d943b82a88e7"; logging-data="13801"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Iz5V+QLFpLLR/PVgMUT/usx4dy4oVsVM=" Cancel-Lock: sha1:mcP8PElGKEU5PXoO/EvpbOjjdCg= sha1:MzXa+Zf2MS61w3oLlEhx6C5NnXA= X-BSB-Auth: 1.672ff4eabf8721ea7cf3.20210416110019BST.878s5ift1o.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:39950 Andrew Poulos writes: > I've been looking at moveable.js > > when I noticed this (fragment of) code: > > on("resize", ({ target, width, height, dist, delta, clientX, clientY }) => { > delta[0] && (target.style.width = `${width}px`); > }) > > I don't understand what the double ampersand in > delta[0] && (target.style.width = `${width}px`); > is doing. > > Does it mean that if delta[0] is falsey then the width of target is > set? That is if (!delta[0]) target.style.width = `${width}px`; ? It's the other way round. In x && y, only if x is "truthy" must y be evaluated. When x is false, the whole expression must be false and the language guarantees that the right-hand operand won't be evaluated. The same (with the appropriate switch between truth and falsehood) is true for ||. There is also the much newer ?? operator that evaluates and returns the right-hand operand only when the left operand is null or undefined (sometimes called "nullish"). Such operators are often said to have "short circuit" evaluation. -- Ben.