Topic: About logical operator

This script


<html>
<body>

<script type="text/javascript">

var a =10;
var b = 20;


result = (a && b);
document.write(result);



</script>
</body>
</html>

will have the output: 20?

Why is that? && is a logical operator "AND". Why did that script resulted to 20? And when I assign  text string such as dog or cat to variables a and b, it gives no result, but when I use true and false, the result always shows the value of the last variable used. for example:

<script type="text/javascript">

var a =true;
var b = false;


result = (a && b);
document.write(result);



</script>

Output will be : false

Same with logical operator "or" (  ||  ). Hope someone can help me understand this. I'm just a newbie with regards to Jscript.

Re: About logical operator

I'm not sure if you are using "&&" and "||" the way they are intended to be used in javascript. As far as I know, they are intended to be used as part of a conditional statement, like "if" or a "while" loop....

if (var1 <1 || var1 > 10) for example.

If you are wanting to combine the two variables, for example, 10+20, or "cat" + "dog" you'll need to use "+". Combining booleans will give you their value (false = 0, true = 1).

---

I will point out that:
-- if you try to use "20 && 10", the result will always be the last variable ("10")
-- if you use " "cat && "dog" " (you need "" around the text, because they are strings) you'll get the last variable ("dog")
-- if you use "true && false", you'll get false if any one of the variables are false (order doesn't matter) and true otherwise.