Click to See Complete Forum and Search --> : How to do conditional statements


geuis
04-01-2003, 02:39 PM
I need to make an if statement that will basically say 'if aVar is anything except "" then....'

if (aVar <> "")
{
}

Not sure what the javascript conditional for <> is.

AdamGundry
04-01-2003, 02:41 PM
The test for an inequality is !=, so you would use this:

if (aVar != "")
{
}

Adam

Jona
04-01-2003, 02:42 PM
var word = document.forms[0].elements[0].value;
if(word=="What?"){alert("You said"+word);}


And you'd have a form:

<form>
<input type=text>
</form>

khaki
04-01-2003, 02:42 PM
!=

doesn't make sense to me either! (lol)
;) k

Jona
04-01-2003, 04:35 PM
Khaki, I always think of "!" as the word, "not" and "=" as the word equal. So if(variableName not equal to "string"){ continue... See what I mean? I often times also use if(!variableName=="string").

cgraz
04-01-2003, 05:09 PM
just a quick clarification. I usually do as jona says when coding (new to javascript, more of a php coder). So when I see != I actually say does not equal outloud to myself (talking to yourself actually helps, as crazy as it may sound). ! being not (or does not) and = being equal. However, do keep in mind that one equal signs means you're assigning a value, two equal signs means is equal to. For example:var test = "5";

if(test = 6) {
document.write("true");
} else {
document.write("false");
}This statement will always print the word "true" because you assigned 6 to variable test within the if statement. However if you want to check if variable test equals 6, you must use two equal signs.if(test == 6) {
document.write("true");
} else {
document.write("false");
}Cgraz

khaki
04-02-2003, 10:37 AM
Yeah guys... thanks - but I knew the operator (I was actually answering the question :eek: ).
I was just commenting that I don't get it (being a VB/ASP girl) because != is an alien way of writing it for those of us accustomed to <> (and thus... geuis' question!).
Ignore me on that... I was just pooh-poohing the Javascript operators (since I really dislike them :rolleyes: ).

But I do have a question which is a result of something that Jona wrote.

Jona indicates that the following code conditionally tests one value against another:

if(!variableName=="string")

...so... Is that correct (as written)?
Is ! all by itself equal to a "NOT" operator? Such as VB equiv:

if (NOT variableName = "string")

... and if so...
then != is the equiv of !var == ...
?
(ugh... that dinky little ! can be tough to spot when degugging. See... some of my dislike for these operators is based in reality too. lol)

And... is it a preferance as to which way it is written... or is there situations where one way should be used over the other?

Do my questions make sense :rolleyes: ?
(they do to me.. but... :rolleyes: )

;) k

havik
04-02-2003, 11:22 AM
I guess the other conditional statements are assumed to be known since they are common sense. Just in case, you never know, someone else might browse this thread and see it.

less than: <
less than or equal to: <=
greater than: >
greater than or equal to: >=

and the already mentioned ones
equal to: ==
not equal to: !=

You also use these for multiple statements
(statement1) && (statement2) ; the AND
(statement1) || (statement2) ; the OR

Havik

Ice3T
04-02-2003, 11:57 AM
I always wondered does this exist:
x!==y

Jona
04-02-2003, 11:57 AM
Khaki, yes it's the same thing. The following two if statements are equivalent:

if(!document.forms[0].myTextField.value=="string")

if(document.forms[0].myTextField.value!="string")

The situation in which either is used does not matter. My preference is to use the if(!var=="string") format, to help me remember that == is used to test equality, and = is used to set equality. But sometimes I prefer to use != instead. I just use different ones depending on how it looks. :) I like my code to look "rock-hard" if you know what I mean... Using the least spaces, comments, and lines of code as possible.

Jona
04-02-2003, 11:59 AM
Originally posted by Ice3T
I always wondered does this exist:
x!==y

I believe it does... But I forget what it does, because I rarely use it.

Ice3T
04-02-2003, 11:59 AM
no no...
not
if(!document.forms[0].myTextField.value=="string")
or
if(document.forms[0].myTextField.value!="string")
this:
if(document.forms[0].myTextField.value!=="string")

khaki
04-02-2003, 12:45 PM
okay...
I was doing fine with this additional info...
(nice job of explaining Jona! "rock-hard" code... interestingly put).
But now Ice has confused me.

...so... I Googled:
( ;) k )

==
This is the equal operator and returns a boolean true if both the operands are equal.
JavaScript will attempt to convert different data types to the same type in order to
make the comparison. Assuming 'a' to be 2 and 'b' to be 4, the following examples
will return a value of true:

a == 2
a == "2"
2 == '2'

!=
This is the not equal operator which returns a Boolean true if both the operands are
not equal.
Javascript attempts to convert different data types to the same type before
making the comparison. The following examples return a Boolean true:

a != b
a != 4
a != "2"

===
This is the strict equal operator and only returns a Boolean true if both the operands
are equal and of the same type.
These next examples return true:

a === 2
b === 4

!==
This is the strict not equal operator and only returns a value of true if both the operands
are not equal and/or not of the same type.
The following examples return a Boolean true:

a !== b
a !== "2"
4 !== '4'


source: http://www.mitchman.com/Quick_Reference/JavaScript/comparison_operators.html

Jona
04-02-2003, 01:47 PM
The following examples return a Boolean true:

a != b
a != 4
a != "2"


The last one will return a Boolean false. According to your other statements, a is equal to 2. The number 2 can be in quotes or out of quotes, but is still a number. a==2 is true; a!=2 is false.

jeffmott
04-02-2003, 02:16 PM
Jona
I often times also use if(!variableName=="string")I'm curious how you manage to pull that off.
alert("hello" != "world"); // returns true, as expected
alert(!"hello" == "world"); /* returns false, !"hello" is first converted to a
false value, and false is not equal to "world" */

Jona
04-02-2003, 02:36 PM
Oh yah, I normally use it for Boolean values only, rather than string comparison. I don't use alert(!"hello" == "string"). I use if(!document.formName.checkBoxName.checked) for if it's not checked and stuff like that. I normally use if("hello" == "world){ do something... } else { do something else } instead of using if("hello" != "world"). I just use the else, since I normally need to use the if as well.

jeffmott
04-02-2003, 02:42 PM
I normally use it for Boolean values onlyIt's a very important point to make that this would not work for anything except boolean values. The example you had given used strings, which would not work. So var1 != var2 cannot be universally rewritten as !var1 == var2.

Jona
04-02-2003, 02:47 PM
Yah... I think that's what I meant... But I've never encountered an error using an if.

khaki
04-02-2003, 03:22 PM
jeffmott wrote:
var1 != var2 cannot be universally rewritten as !var1 == var2So...
!var1 == var2 can only be used if var1 & var2 are integers...
and otherwise it should be written as var1 != var2?
(you guys are playing ping-pong with this and it's making my head spin! lol)

And lastly...
is it even necessary to ever have to write it as !var1 == var2...
or is it perfectly safe and logical to always use var1 != var2?

(seriously... I'm very confused now. Any chance someone with VB skills can provide a side-by-side so that I can understand how Javascript compares to what I already know?)

see... this is why i can't wrap my head around javascript. Every stinkin question comes equipped with a complete set of even more stinkin questions. yuck!

(hey geuis... did you ever suspect that this stupid != contained so much ambiguity? It's insane! lol)

crazy by choice... insane because of javascript...
k

Jona
04-02-2003, 03:26 PM
Okay. != can be used any time. It compares strings, integers and variables. ! just compares variables (variables that could also be objects).

khaki
04-02-2003, 03:36 PM
uh-oh... i'm not done yet :rolleyes: ...

when you say:
"! just compares variables (variables that could also be objects)"...
the variables can be anything?

so... (dead-horse beating alert)...

Assuming varRed=red and varBlue=blue, and cat and dog are strings:

varRed != varBlue (true)
!varRed == varBlue (true)
cat != dog (true)
!cat == dog (not allowed)

? ? ?
:rolleyes: k

Jona
04-02-2003, 03:46 PM
Right!

khaki
04-02-2003, 03:58 PM
I DON'T EVEN KNOW WHAT I'M TALKING ABOUT!

(just kidding. Thanks)
;) k

havik
04-02-2003, 04:14 PM
var cat = "cat";
var dog = "dog";

alert(!cat == dog)

this returns false, so isn't it allowed afterall?

Havik

Jona
04-02-2003, 04:27 PM
Let's get this down to complete correction:

varRed = "red"
varBlue = "blue"
varRed != varBlue (true)
!varRed == varBlue (false)
cat != dog (true)
!cat == dog (false)

khaki
04-02-2003, 04:45 PM
well...
I'll be using it within a conditional statement, so whether it works with an alert box is for the rest of you to fight over.

(yikes... are there really that many web developers that spend all their time coding for alert box applications? lol)

;) k

jeffmott
04-02-2003, 05:10 PM
So...
!var1 == var2 can only be used if var1 & var2 are integers...No, only if var1 and var2 are boolean values.

is it even necessary to ever have to write it as !var1 == var2...No.

Any chance someone with VB skills can provide a side-by-side so that I can understand how Javascript compares to what I already know?It'd be quite difficult to put the entire language side-by-side, but the parts associated with conditionals (that are different) are:
VB | JS
|
And | &&
Or | ||
Not | !
= | ==
<> | !=

!varRed == varBlue (true)This is actually false. !varRed will evaluate to false, and false is not equal to blue.

alert(!cat == dog)
this returns false, so isn't it allowed afterall?No, because you're saying it would be equivalent to cat != dog, which would return true (because obviously they're not equal). So your example of returning false is really not the return value you would want.

I'll be using it within a conditional statement, so whether it works with an alert box is for the rest of you to fight overThe alert box is there mearly so we can see what the result of the expression would be. The logic would still work exactly the same in a conditional statement.