MadJS Message Board › Newbie Variable Question

Newbie Variable Question

Christopher Edwin ...
chrisedjohn
Milwaukee, WI
Post #: 1
Hey guys. I'm getting stuck on an if/then statement. I'm doing a sort of "lookup" function in a Google spreadsheet and it's doing something odd (to me). When I'm doing an "if/then" check, the check itself is setting the variable. Very frustrating. The code is below.



for (var k = 1; k < lastContactsRow ; k++) {
var currentname;
var currentemail;
currentname = contactData[k][0];
currentemail = contactData[k][1];
if(contact = currentname) {
recipient+= ','+currentemail;
}



In the " if(contact = currentname) {" line, the "contact" variable is getting set to whatever the "currentname" variable is at the time, which defeats my check.

Any input would be greatly appreciated!

-Chris
Steve K.
user 58357252
Madison, WI
Post #: 1
Hi Chris,

Your If check only contains one equal sign which is causing contact to be set to currentname. Change it to:
if (contact === currentname)

Steve
Ryan R.
user 79596502
Madison, WI
Post #: 1
Hey Chris, you answered your own question there. In the line "if (contact = currentName)" the variable "contact" is being assigned, rather than compared. You're using the assignment operator (=) when what you want is the equality comparison operator (==).

If you don't need casting, if you know that both operands will always be the same type (strings in this case) you can get some performance gains by using the "identity" operator (===) rather than the equality operator (==). The equality operator will do any needed casting, so ("0" == 0) is true after a cast to string, where ("0" === 0) is false, because a string doesn't equal a number.

Here's a performance comparison : http://jsperf.com/com...­

Happy coding!
-Ryan Rahlf
Bill B.
user 44297102
Madison, WI
Post #: 1
Just a simple feature of the language, Chris. In javascript, "=" is the assignment statement. You are using it as an equality test. In javascript, the equality test is "==".

It's a little bit tricky, because "==" returns true or false depending on whether the operands have the same value, without affecting the value of either one. "=", on the other hand, sets the first operand to the value of the second, and then returns true. So, both operators return a boolean, and the if () expression remains syntactically valid even though it's not what you want to do. The code executes without a problem.

This is not an uncommon confusion. Or so I've heard - it never happened to me when I was first learning the language. ;-)

-Bill
Christopher Edwin ...
chrisedjohn
Milwaukee, WI
Post #: 2
Thank you all! That was precisely what I was after. Insanity lessening...
Ryan R.
user 79596502
Madison, WI
Post #: 2
... "=", on the other hand, sets the first operand to the value of the second, and then returns true.

Hey Bill! I like how you worded your answer, but I have one small correction. The assignment operator doesn't return true, it returns the value of the variable which was just assigned. This is most often going to evaluate to a "truthy" value in JavaScript, but can be false on occasion. For example, the following will always evaluate to false (and the alert will never run):

if(myVar = false) { alert('it was false'); } //this alert will never run

You can see this in action by uncommenting some of the assignments in this jsfiddle: http://jsfiddle.net/p...­

Happy Coding!
-Ryan Rahlf
Bill B.
user 44297102
Madison, WI
Post #: 2
Thanks for the correction, Ryan! Always glad to patch my understanding of the language. :-)
Powered by mvnForum

Our Sponsors

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy