var x=3 VERSUS var x="3"

M

Matt

I want to know if var a = 3 and var a = "3" make any differences? So in JavaScript,
we always use var type? there is no integer type?

If I compare 3 > 2 and 3 > "2", it doesn't make any differences.

Also, in (1), I don't understand why it returns false,
but if i just do alert(a > b);, then it returns true.


<html><body>
<script type="text/javascript">
var a = 3;
var b = 2;
if (a > b) alert("Case 1: " + a > b); //false <== (1)

var x = "3";
var y = "2";
if (x > y) alert("Case 2: " + x > y); //true
</script>
</body>
</html>

any ideas? thanks!!
 
L

Lasse Reichstein Nielsen

I want to know if var a = 3 and var a = "3" make any differences?

Yes. In the first case, the variable "a" is assigned the number 3,
in the second, it is assigned the string "3". The values have different
types.

So in JavaScript, we always use var type? there is no integer type?

Javascript is a dynamically typed language. Variables don't have
types, only values. Any variable can hold any value, so there is
no way to declare the type of the variable.
If I compare 3 > 2 and 3 > "2", it doesn't make any differences.

Correct. The ">" operator on operands where one is a number, will
convert the other argument to a number and compare the numbers.
In this case the string "2" is converted to the number 2, so
the result is the same for both expressions.
Also, in (1), I don't understand why it returns false,
but if i just do alert(a > b);, then it returns true. ....
var a = 3;
var b = 2;
if (a > b) alert("Case 1: " + a > b); //false <== (1)

Notice that it alerts "false", not "Case 1: false".
The precedence of ">" is lower than "+", so you are comparing
the string "Case 1: 3" to the number 2. That attempts to
convert the former to a number. This fails, yielding NaN, which
is not larger than any number.

Try
if (a > b) alert("Case 1: " + (a > b)); // Case 1: true
var x = "3";
var y = "2";
if (x > y) alert("Case 2: " + x > y); //true

Here you compare strings, since both arguments are strings, and the
string "2" is (lexically) smaller than "Case 2: 3". Again, add
parentheses for the desired result.

/L
 
L

Lee

Matt said:
I want to know if var a = 3 and var a = "3" make any differences? So in
JavaScript,
we always use var type? there is no integer type?

If I compare 3 > 2 and 3 > "2", it doesn't make any differences.

The script engine tries to convert both operands to the same
type before comparing them. If they can both be converted to
numbers, as in these cases, they are.

Also, in (1), I don't understand why it returns false,
but if i just do alert(a > b);, then it returns true.
var a = 3;
var b = 2;
if (a > b) alert("Case 1: " + a > b); //false <== (1)


Notice that it doesn't alert "Case 1: false", but just "false".
That's because it's first evaluating "Case 1:" + a, and then
it's comparing that intermediate result (the string "Case 1: 3")
with 2. Since the intermediate result can't be converted to a
number, the comparison is performed as strings, and "Case 1: 3"
is lexically less than "2".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top