Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: puzzled by example code using the logical OR Date: Mon, 23 Feb 2015 21:57:55 +0000 Organization: A noiseless patient Spider Lines: 31 Message-ID: <87sidwi93w.fsf@bsb.me.uk> References: <088f6cf2-dbfe-4633-8c69-0bed2328a067@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="16301"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//4LxBwzOtueZiiUqNfC64kcKoIoNvmDI=" Cancel-Lock: sha1:UbWmVLGrQgIaMeaP2zaJrriYYOE= sha1:kp8czmpsdtU2K3ECu7v6VcDOcfI= X-BSB-Auth: 1.ed7e48220217b9158a28.20150223215755GMT.87sidwi93w.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:26913 "Evertjan." writes: > JeffG wrote on 23 feb 2015 in > comp.lang.javascript: > >> I've been looking at a discussion of creating objects that > are >> containers of all related functions and vars. An example > first step is >> as follows: ar AppSpace = AppSpace || {}; >> I don't see the purpose of "||". I would appreciate an > explanation since >> I cannot get a response from the author. I thought the > logical OR only >> returns true or false based on comparing two values or > expressions. > > Incorrect. That only happens if || compares boolean values. > > AppSpace = AppSpace || {}; > > AppSpace is set to AppSpace [so effectivly remains so], > unless AppSpace returns false or empty or 0 or undefined, > then AppSpace is set to {}, an empty object. It's false, the empty string, 0 (both +0 and -0), NaNs, undefined and null that are considered false. (It's common not to care about -0 and NaNs, but null matters a bit more.) -- Ben.