-
JavaScript vs. C/C++ behavior
From
Digital Man@VERT to
All on Wed May 1 14:08:54 2019
In C/C++:ã i = 0 || 2; // i == 1ãIn JavaScript:ã i = 0 || 2; // i == 2ããWhat about Java, C# or other C-like languages?ãã digital manããSynchronet "Real Fact" #91:ãCaptured chat with Wayne Bell:
http://wiki.synchro.net/history:waynebell_chatãNorco, CA WX: 70.9øF, 49.0% humidity, 6 mph ESE wind, 0.00 inches rain/24hrsã---ã þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.netã
-
From
echicken@VERT/ECBBS to
Digital Man on Wed May 1 17:46:09 2019
Re: JavaScript vs. C/C++ behaviorã By: Digital Man to All on Wed May 01 2019 14:08:54ãã DM> In C/C++:ã DM> i = 0 || 2; // i == 1ã DM> In JavaScript:ã DM> i = 0 || 2; // i == 2ãã DM> What about Java, C# or other C-like languages?ããIIRC in Java this won't work at all, as it wants both operands to be boolean. ãThis caused me some mild annoyance a while back. I think it's the same in C#.ãã---ãechickenãelectronic chicken bbs - bbs.electronicchicken.com - 416-425-5435ã þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.comã
-
From
Mortifis@VERT/ALLEYCAT to
Digital Man on Tue May 21 00:51:25 2019
In C/C++:ã > i = 0 || 2; // i == 1ã > In JavaScript:ã > i = 0 || 2; // i == 2ãã > What about Java, C# or other C-like languages?ãã > digital mamããin c-like languages (depending on how a function evokes an evaluation) if theãBoolean callback is neither 0 nor 2 it will return a 1 as represented by theãinduction of i, anything greater than 0 but less than 2 is 1) ... in JS if theãcallback is greater than the initial state (0) then it is forced to 2 ..ãrisking looking like a moron ... there wasn't much meat on the bone in questionã... either way, it's all about OS calls/callbacks ... hold it, where's my beer?ã:-)ãããããThe Granny Lane, where seasoned drivers slow down and smell the blacktop rosesãã---ã þ Synchronet þ AlleyCat! BBS - http://alleycat.synchro.net:81ã
-
From
Fort Exile@VERT to
Mortifis on Sun Jul 7 10:03:29 2019
Re: Re: JavaScript vs. C/C++ behaviorã By: Mortifis to Digital Man on Tue May 21 2019 12:51 amãã > > In JavaScript:ã > > i = 0 || 2; // i == 2ãã > the induction of i, anything greater than 0 but less than 2 is 1) ... in JSã > if the callback is greater than the initial state (0) then it is forced to 2ã > .. risking looking like a moron ... there wasn't much meat on the bone inããThe trouble with boolean operations in JavaScript is the concept of "truthy" anf "falsy" values. Javascript will always try and make a boolean comparison work, even if it is not comparing two boolean values. Thus, we wind up with "falsy" values that evaluate to FALSE when compared, and "truthy" values that evaluate to TRUE.ããThe "falsy" values are: FALSE, 0, "" '' and `` (the empty string), null, undefined, and NaN.ããThe "truthy" values are everything else, including empty objects and arrays.ããThis particular assignment does a boolean OR comparison. The first element that is "truthy" is the one that is assigned to the variable. Since 0 is always a "falsy" value, it is skipped, and we wind up with i === 2.ã---ã þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.netã
-
From
Digital Man@VERT to
Fort Exile on Sun Jul 7 13:15:32 2019
Re: Re: JavaScript vs. C/C++ behaviorã By: Fort Exile to Mortifis on Sun Jul 07 2019 10:03 amãã > Re: Re: JavaScript vs. C/C++ behaviorã > By: Mortifis to Digital Man on Tue May 21 2019 12:51 amã >ã > > > In JavaScript:ã > > > i = 0 || 2; // i == 2ã >ã > > the induction of i, anything greater than 0 but less than 2 is 1) ... inã > > JS if the callback is greater than the initial state (0) then it isã > > forced to 2 .. risking looking like a moron ... there wasn't much meat onã > > the bone inã >ã > The trouble with boolean operations in JavaScript is the concept of "truthy"ã > anf "falsy" values. Javascript will always try and make a boolean comparisonã > work, even if it is not comparing two boolean values. Thus, we wind up withã > "falsy" values that evaluate to FALSE when compared, and "truthy" valuesã > that evaluate to TRUE.ã >ã > The "falsy" values are: FALSE, 0, "" '' and `` (the empty string), null,ã > undefined, and NaN.ããNaN != false or true.ãã > The "truthy" values are everything else, including empty objects and arrays.ã >ã > This particular assignment does a boolean OR comparison. The first elementã > that is "truthy" is the one that is assigned to the variable. Since 0 isã > always a "falsy" value, it is skipped, and we wind up with i === 2.ããAnd the original implementation of JavaScript/LiveScript didn't actually behave that way, which is interesting. It was introduced as a "feature" as I suspect much of the languages weird (and cool) warts were.ãã digital manããSynchronet "Real Fact" #50:ãJAM and Squish were considered before developing Synchronet Message Base format.ãNorco, CA WX: 76.7øF, 61.0% humidity, 0 mph S wind, 0.00 inches rain/24hrsã---ã þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.netã