Closed Thread Icon

Topic awaiting preservation: True vs. False (1 or 0) (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=23811" title="Pages that link to Topic awaiting preservation: True vs. False (1 or 0) (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: True vs. False (1 or 0) <span class="small">(Page 1 of 1)</span>\

 
Karl
Bipolar (III) Inmate

From: Phoenix
Insane since: Jul 2001

posted posted 10-26-2004 18:10

This continues to be a confusing subject. Is 1 typically a true value or a false value?

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 10-26-2004 18:46

In Javascript, 0 is false. Any non-zero value is true.

: . . DHTML Slice Puzzle : . . .

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-26-2004 18:46

0 is typically a false value
everything else is not false, thus interpreted as true

[edit] Nice COMBO x2 [/edit]



(Edited by poi on 10-26-2004 18:48)

WebShaman
Maniac (V) Mad Scientist

From: Happy Hunting Grounds...
Insane since: Mar 2001

posted posted 10-26-2004 19:00

Unless one is using negative values...*bleh*

Had that with the IBM skits of the Supercomputer that I worked on. That really played with mah head.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-26-2004 21:01

Just so you know the deal. Technically in most languages 1 and 0 does not equal true or false. These are different datatypes. Numbers can be any of several different numeric datatypes (long, double, int, etc..) where true/false is a boolean datatype (either true or false)

If the results of something gives a 1 or 0 for the result you should be testing for 0 or 1

if ( blah == 0 )

Javascript is pretty loose with it's typecasting so when blah = 0 it allows you to do something like

if ( blah == false )

though that's kind of a bad habit. You're better off getting in the habit of testing against the correct type.



.:[ Never resist a perfect moment ]:.

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 10-26-2004 21:39

Most languages (Javascript included) treat 0 as false, and anything else (though it is usually represented by either -1 (in an integer, all bits are a 1) or 1 (just the last bit is a 1)) as true.

That is why you can do something like for(i=10; i--; ), and get 10 iterations: on the last iteration, i becomes 0, and then the loop exits.

"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

(Edited by Iron Wallaby on 10-26-2004 21:43)

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 10-27-2004 06:45

PHP is among these, so if you set a function to return 1 on a sucessful execution or 0 in the case of an error you can test for 'if(!$return)' to make sure it's not false, or not 0 in this case. I believe some of the other languages like Java and Delphi require specific boolean datatypes to test for boolean conditions (true or false)


Justice 4 Pat Richard

Ray Norrish
Nervous Wreck (II) Inmate

From:
Insane since: Sep 2004

posted posted 10-29-2004 01:39

I don`t bother to use boolean in JS as it doesn't seem to work like I expect it to I program mainly in Delphi or SQL.
In JS , then I just started using an integer of zero or one and said bollocks to the silly JS interpretation of boolean.

Delphi is simply specifying a boolean datatype and that can only be true of false - which is the right way - makes complete sense and all operations on the result work as expected.

The SQL variations of NULL, 0 and '' are far more interesting

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 10-29-2004 01:40

Wait... does JS actually have a Boolean type? o_O

I thought it had Reals, Strings, Functions, and Objects...

"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

Ray Norrish
Nervous Wreck (II) Inmate

From:
Insane since: Sep 2004

posted posted 10-29-2004 01:55

It has true and false conditions, but after weird behaviour (come home after working in Delphi or SQL) and struggle with JS and several Beers, it was the easy way out to use 0 and 1

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 11-03-2004 01:25

I have noticed something recently using string.indexOf('blah') - if i simply do this:

code:
var string = 'my blah';

if ( string.indexOf( 'blah' ) ) alert( 'yes' );

The alert wont trigger, neither will it trigger if i do:

code:
var string = 'my blah';

if ( string.indexOf( 'blah' ) == true ) alert( 'yes' );

Instead I found it worked as expected if I did:

code:
var string = 'my blah';

if ( string.indexOf( 'blah' ) > -1 ) alert( 'yes' );

Confused the hell out of me, so if 1 is true and 0 is false, why is 0 true and -1 false in this instance?

SCRWD//MULTIMEDIA PORTAL

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 11-03-2004 01:50

smonkey: the string.indexOf() method returns the ... index of the substring given in parameter, NOT simply a boolean saying if the substring is in the string. If the substring is present, the value returned can be in the range [ 0, string.length-1 ], otherwise it'll be -1.

Hebedee
Paranoid (IV) Inmate

From: Maryland, USA
Insane since: Jan 2001

posted posted 11-03-2004 06:41

Right, if functions return values other than booleans to signify that that have been completed, you have to cater your conditions to the datatype returned.

TwoD
Nervous Wreck (II) Inmate

From: Sweden
Insane since: Aug 2004

posted posted 11-03-2004 09:14

smonkey: what you're looking for is

code:
var string = 'my Blah';

if (/blah/i.test(string)) ) alert( 'yes' );



test() is a Regular expression method and simply returns true if the regexp is found.
the flag "i" tells it to ignore case so "Blah"=="blah".
you can also set the flag "g" to make it search for more than one occurance of the regexp but
that is often only useful for the string.replace(regExp,string) method.

/TwoD

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 11-03-2004 19:18

I feel such a n00b

I am familiar with the test and other regexp methods, but for some reason I generally avoid using them for such trivialities. Thanks for the insight Poi - slowly you are begining to improve my opinion of the french populus

SCRWD//MULTIMEDIA PORTAL

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 11-03-2004 20:43

smonkey: You're welcome. Who knows, some day we'll probably eat frog legs together

BillyRayPreachersSon
Bipolar (III) Inmate

From: London
Insane since: Jul 2004

posted posted 11-05-2004 13:32
quote:
Karl said:

This continues to be a confusing subject. Is 1 typically a true value or a false
value


To give my two-pence worth (although my worth is hopefully more than that )...

1 is neither true nor false. 1 is 1, true is true, and false is false. Comparing the number 1 to the Boolean value true is like comparing apples to pears, or coke to pepsi (the latter, IMHO, should never be done ).

Having said that, you can, in most languages (JS included), represent a value of one type as another type, usually by typecasting it. For example, in JS, you can do this:

code:
alert(Boolean(1));


Which will show you "true". Changing the number to "-21" (for example), also shows true. So to answer the question

- so to paraphrase your question:

Does the numerical value 1, when typecast as a Boolean value, equal true or false?

We can safely say that when typecast, it equals true.

Having said all of that, the following will always show 0:

code:
alert(Number(false));


and the following will always show 1:

code:
alert(Number(true));


Hope that doesn't confuse things too much

Dan

(Edited by BillyRayPreachersSon on 11-05-2004 13:34)

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 11-05-2004 14:53

That is a good and succinct way of saying it. I do, however, think it is worth mentioning that since the two are directly interchangeable, there is so little of a distinction between the two as to be negligable.



"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 11-05-2004 15:04
quote:
Iron Wallaby said:
there is so little of a distinction between the two as to be negligable.

38789! hum... I mean (new Date()) ! sorry... I mean ("anything goes") ! Actually I mean TRUE.

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 11-05-2004 18:02
code:
t = 0;
while(position += velocity += acelleration) ++t;



Will give how long it takes for something to hit the ground, assuming integer math. I still hold to my statement.

"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 11-06-2004 00:25

Iron Wallaby, consider that numbers and strings in JS are also directly interchangeable. Doesn't make them the same =)

There's an internal conversion that goes on.


 

BillyRayPreachersSon
Bipolar (III) Inmate

From: London
Insane since: Jul 2004

posted posted 11-06-2004 01:08

Numbers and strings are not always interchangeable. Consider the trap that I always fall into - having strings representing unit values for CSS properties. For example:

code:
myObj.style.top = baseValue + newOffset + 'px';



If either baseValue or newOffset are strings, then the result will be that the two "numbers" will be concatenated, rather than added. This can make for very quick animation sometimes

Dan

Iron Wallaby
Paranoid (IV) Inmate

From: USA
Insane since: May 2004

posted posted 11-06-2004 20:57

I'm not saying ALL variables can be considered interchangeable! Just Booleans and other values.

If something is not 0, then it is true. Therefore, true can be considered as any statement that is not equivalent to the number 0.

"Any sufficiently advanced technology is indistinguishable from magic." -- Arthur C. Clarke
"Any sufficiently arcane magic is indistinguishable from technology." -- P. David Lebling

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 11-07-2004 20:55

What you are talking about is an agreed up convention.

Keep in mind that JavaScript, Java, C++, and a whole host of other languages were derived from C. C in turn was derived from B which was an assembly language macro processor.

When Thompson and Richie were designing C they made a decision to have as few built-in data types as possible. (This was different from the language trend at the time which was for the language to intrinsically define as much of the world as possible). They felt that this decision (along with others such as not defining I/O in the language) would help to make the language more portable to other machines. There are languages such as FORTRAN, PL/I, COBOL, Algol, Bliss, Jovial, etc. which differentiate between Boolean and other types. This convention has its advantages and dissadvantages.

As a consequence there was no Boolean type defined in C. But there was still a need to perform Boolean kinds of operations so they made the decision to "fold" the operations for Boolean into the operations used for integers. The convention was made that any integer could be treated like a Boolean by placing the integer in the same syntactic position that a Boolean might be placed and if the integer were 0 the Boolean would test to FALSE otherwise it would test to TRUE. An example of this is that originally there were no && and || operators. You used & and | (the bitwise operators) to perform all Boolean tests. However, early on they realized that there needed to be a difference between Boolean and bit-wise tests so the two Boolean operators were added.

The point that poi and slime are trying to make is that languages like JavaScript blur the lines between what is an integer, a string, or a Boolean. It is the programmers job to keep them straight. You differentiate between an integer and a string or an integer and a Boolean by the way you use them. For example, the indexOf function returns an integer. You should interpret the integer to be the position of a sub-string inside another string. Because of the conventions of the language you may interpret this integer to be an argument to a Boolean test, but as has been demonstrated here that can become confusing.

I try to follow a policy of treating an integer like an integer, a Boolean like a Boolean and a string like a string. The fact that the language let's me treat them alike is convenient for conversion. But to avoid my own confusion I avoid statements like if (indexOf('...')), even if it does do what I want it to. I want it obvious to me or anyone else that the return value of that function is an integer so I always write if (indexOf('...') != 0) to emphasis the fact that the function is returning an integer.

[note]The above example ignores that fact that indexOf returns -1 to indicate that the sub-string was not found. I am assuming a situation in which I want to do something specific when the sub-string is found in the 0th position of the string and will do somethin else for all otehr cases. In this example, I [i]could[/] write if (indexOf('...')) but won't.[/note]

I have always felt that thinking and talking about these kinds of nuances is the difference between excellent programmers and those who are not as good.

-- not necessarily stoned... just beautiful.


(Edited by hyperbole on 11-07-2004 20:58)

« BackwardsOnwards »

Show Forum Drop Down Menu