Jump to content

Boolean


shreyaskudav

Recommended Posts

what are you having trouble understanding? a boolean is a value of either true or false. (yes/no, on/off).

Link to comment
Share on other sites

Are you referring to Boolean values (true or false) or the JavaScript Boolean object? FWIW, I never use the Boolean object. It's very counter-intuitive, I find. YMMV.

Link to comment
Share on other sites

JavaScript Boolean object
here's some additional infohttps://developer.mozilla.org/en/JavaScript...Objects/BooleanI've never really found myself using it, preferring to just use true/false when necessary. Same thing for array's and objects; I just use the literal values [] and {} respectively as opposed to new instances. Reading the MDN docs seems to lead me to think that the return type of Boolean may not be as consistent/expected given my set knowledge of what typically constitutes 'truthy' statements vs false one's; so I think I'll stick with my reserved words. :)
Link to comment
Share on other sites

I think it exists for completeness. Everything else in JavaScript (ECMAScript) is an object, so I can only assume the designers wanted a Boolean object too.But it's annoying. You have to use it a special way. What I mean is this:

var myBoolean = new Boolean(false);

You would think that the following test would push us into Part 2, but it puts us in Part 1

if (myBoolean) {   // Part 1}else {   // Part 2}

That's because a Boolean object always evaluates to TRUE!!!!To get the "correct" value, you need to examine myBoolean.valueOf(), which IMO is stupid looking and not very intuitive. Seriously, who uses this thing??? Blecch.If the whole point is to convert a value into true or false, use this old programmers trick:

var myValue = "Hello";var myBoolean = !!myValue;

Tricks like that exist for optimizing memory usage. But in a modern environment with automatic memory allocation and garbage collection, it's almost never useful.

Link to comment
Share on other sites

...If the whole point is to convert a value into true or false, use this old programmers trick:
var myValue = "Hello";var myBoolean = !!myValue;

is it necessary to declare it explicitly ?wouldn't if(myValue) show its implicit Boolean value ?
Link to comment
Share on other sites

wouldn't if(myValue) show its implicit Boolean value ?
Yes.Here's more explanation than you probably want.Let's say you're working in a very old-fashioned environment with very little memory. Like a 64K Commodore from 1980 or something. Imagine you have a string that 500 characters long. Out of 64K, that's a big chunk. Now let's say you're going to pass it to the second parameter of a function like this:
function myfunc (myVal, myBool) {   if (myBool) {	  // do something cool with myVal   }}var someVal = 25;var someString = "abcd"; // but it could actually be 500 charactersvar myResult = myfunc (someVal, someString);

Get the picture? myfunc doesn't actually do anything with someString. It just wants to know if someString has a value or not. In an old-fashioned environment, this would be wasteful, because passing a 500 character string to myFunc would create a COPY of the 500-character string. So now we've wasted 501 bytes when all we needed was 1 byte. Converting someString to a Boolean before passing it to myfunc would save 500 bytes. (That's not 100% true, but true enough for this conversation.)We say that JavaScript passes data by value (which would create a copy) but it doesn't. What it really does is pass an address in memory to myFunc. As long as myFunc does not change the value of the original string, JavaScript does not create a copy. Why should it? It's the same value, so it uses the original in both contexts. JavaScript only writes data into the location addressed by the new variable (the one inside the function) when the value changes. This way, the value of the original variable doesn't change.The point is, you can copy a 10,000 character string in JavaScript without really copying it. So if you pass a "copy" around without changing it and treat it like a Boolean, it's no big deal. Not until you modify the copy in some way.

Link to comment
Share on other sites

Yes.Here's more explanation than you probably want....
nope, never - your insights are always appreciated. (Deirdre is a lucky girl !)it's probably the sort of stuff that is ignored by programmers of bloatware.i think i get the gist of it, what ever didn't go over my head, but i can always come back to it; with any luck this thread will be the go-to thread when someone googles "Boolean".
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...