Wednesday, August 6, 2008

= == === ??? (WBP Notes)

=
Assigns values

"red" = 2;
"blue" = 1;

alert(1);
//we get red
alert(2);
//we get blue

==
is used to compare values in a condition
does this equal this!

if (1 == "red"){
alert("This is true, we assigned 1 to red");
}

===
is used to make sure that this is exactly true, no ifs and or buts. This better be true!
Checks the input and datatype!
so no matter what 1 has to be 1.
if (1 === 1){
}

}

1 comment:

kateF said...

umm...why are you assigning numbers to strings??? you have the word red in double quotes...that says red is a string, not a variable. if you want a variable named red to equal 1, you'd write:

var red = 1;

remember that you assign the value written on the RIGHT to the variable name on the LEFT.

== compares values and ignores data types.

=== compares both values and data types.

so if you had:

var red = 1;
var white = "1";

red == white would be true. they both contain the same value. 1.

red === white would be false because although they hold the same value, red has a data type of number and white has a data type of string. they are not the same, so the comparison would be false.