Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26922

Re: puzzled by example code using the logical OR

From Joao Rodrigues <groups_jr-1@yahoo.com.br>
Newsgroups comp.lang.javascript
Subject Re: puzzled by example code using the logical OR
Date 2015-02-23 23:26 -0300
Organization Aioe.org NNTP Server
Message-ID <mcgngj$b1t$1@speranza.aioe.org> (permalink)
References <088f6cf2-dbfe-4633-8c69-0bed2328a067@googlegroups.com> <mcgmh2$99b$1@speranza.aioe.org>

Show all headers | View raw


On 02/23/2015 11:09 PM, Joao Rodrigues wrote:
> On 02/23/2015 04:20 PM, JeffG wrote:
>> 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.
>>
>
> According to the ES 5.1, §11.11 (Binary Logical Operators), "The value
> produced by a && or || operator is not necessarily of type Boolean. The
> value produced will always be the value of one of the two operand
> expressions".
> <http://ecma-international.org/ecma-262/5.1/#sec-11.11>
>
> Also, as described in ES 5.1, §9.2 (ToBoolean), there are seven "falsy"
> values: false, +0, -0, "", NaN, null, and undefined. All other values
> are "truthy".
> <http://ecma-international.org/ecma-262/5.1/#sec-9.2>
>
> So, AppSpace || {} is evaluated as follows:
>
> If ToBoolean(AppSpace) is true, return AppSpace; otherwise, return {}
> (an empty object).
>
> But it is a tad efficient to use:

Better: a little bit more efficient.

>
> var AppSpace;
> if (!AppSpace) {
>    AppSpace = {};
> }
>
> Or simply:
>
> var AppSpace;
> if (!AppSpace) AppSpace = {};
>

Sorry, it should be:

if (typeof AppSpace == "undefined") {
   var AppSpace = {};
}

>
> The logical operators may be described in the following table:
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators>
>
>
>


-- 
Joao Rodrigues

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

puzzled by example code using the logical OR JeffG <jeffgutsell@fuse.net> - 2015-02-23 11:20 -0800
  Re: puzzled by example code using the logical OR "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-02-23 21:57 +0100
    Re: puzzled by example code using the logical OR Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-02-23 21:57 +0000
      Re: puzzled by example code using the logical OR Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-23 22:10 +0000
        Re: puzzled by example code using the logical OR Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-23 23:39 +0100
      Re: puzzled by example code using the logical OR Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-23 23:12 +0100
  Re: puzzled by example code using the logical OR Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-02-23 23:09 -0300
    Re: puzzled by example code using the logical OR Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-02-23 23:26 -0300

csiph-web