BleepingComputer.com: Deciphering if statement syntax in Javascript

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Deciphering if statement syntax in Javascript

#1 User is offline   Ray Parrish 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 91
  • Joined: 30-October 08
  • Gender:Male
  • Location:Cottage Grove, Oregon

Posted 12 November 2008 - 09:38 PM

Hello,

I'm reading on the W3C site about scripting, and they are calling the below code a window handler. The my_onload function has no code in it right now, but that's not the part I'm having trouble with.

The if statement in the last line of the script is what has me stymied. They had the tcl code for this too, and the tcl's if statement looked more familiar to me as it tested for if (win != "") which I understand to mean if the value of win is not equal to the empty string then execute.

Could someone explain to me in plain english what that short hand if statement is doing in this javascript? I'm not at all familiar with that syntax. I was also wondering why they don't have the trailing () pair on the call to the function in the if statement. I've always seen it with them, even if no values are being passed in.

<script type="text/javascript">
	  function my_onload() {
		 . . .
	  }

	  var win = window.open("some/other/URI");
	  if (win) win.onload = my_onload;
</SCRIPT>


Thanks for any help you can be. Later, Ray Parrish

#2 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 13 November 2008 - 08:37 AM

Used that way, it isa function reference, not an actual call to use the function. You are essentially overriding a standard function witha function of your own, so if during execution, there is a call to function 'win.onload', it is going to use 'my_onload' instead.

I think I am explaining that correctly.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#3 User is offline   Ray Parrish 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 91
  • Joined: 30-October 08
  • Gender:Male
  • Location:Cottage Grove, Oregon

Posted 13 November 2008 - 06:15 PM

Thank you for that, but it still doesn't help understand the if statement...

Is the if condition testing for the variable to be populated with data or is it testing to see if the variable is empty?

OH! Is it still possible to test if ( var != condition ) in javascript? This form is much more readable to me.

#4 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 13 November 2008 - 07:05 PM

Quote

OH! Is it still possible to test if ( var != condition ) in javascript? This form is much more readable to me.


If statements evaluate to true or false. If the condition is true, then whatever is inside the if statement is executed. If the condition is false, then whatever is inside the if block is executed. What this is evaluating in this case is if (true), that is, if win != null, which would be a false condition. So if there is not a window assigned to variable win, then the if statement will evaluate to false. Presumably, if the statement evaluates to false, then somewhere else in the application would be code to handle what is presumably an error.

Taking this a step further. In binary, a false==0, and true==1, so all 'if' statements (and all control structures) evaluate to zero or one.

so, if (1+1==2) evaluates to one, because the condition is true.
if(1>5) evaluates to zero, because the condition is false.

Does that make it any clearer?
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#5 User is offline   Ray Parrish 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 91
  • Joined: 30-October 08
  • Gender:Male
  • Location:Cottage Grove, Oregon

Posted 13 November 2008 - 08:16 PM

Thanks! That clears it up for me. I think I'll stick to explicitly testing for var == null so it's easier to follow what's going on.

Later, Ray Parrish

#6 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 13 November 2008 - 09:04 PM

The only problem with that is that it might not necessarily have null for a value. It might just be random crap in memory. Just something to think about. :thumbsup:
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users