Javascript Will Assign And Test A Variable In The Same Statement

Posted March 31, 2007 at 6:51 PM by Ben Nadel

Tags: Javascript / DHTML

I just came across this really awesome Javascript "shorthand". It's always great to feel like you know a language very well and then come across something totally new. Usually, when I write Javascript, I am used to assigning variable values and then checking to see if they exists before I use them:

  • // Get the page header.
  • var objHeader = document.getElementById( "header" );
  •  
  • // Check to see if the header exists.
  • if (objHeader){
  •  
  • ... more code here ...
  •  
  • }

But, I just found out that through the beauty of variable assignment and short-circuit evaluation, the above two statements can actually be combined into a single IF statement. Sweet ass!

  • <!-- HTLM header / span. -->
  • <div id="header"><span>Title Span</span></div>
  •  
  • <script type="text/javascript">
  •  
  • // Define the variables as null.
  • var objHeader = null;
  • var objHeaderSpan = null;
  •  
  •  
  • // In this IF statement, assign the variables and
  • // check to see if they result in valid objects.
  • if (
  • (objHeader = document.getElementById( "header" )) &&
  • (objHeaderSpan = objHeader.childNodes[ 0 ])
  • ){
  •  
  • // If we have gotten this far, then we have assigned
  • // DOM elements to both objHeader and objHeaderSpan
  • // and we KNOW that they exist. Aler inner HTML.
  • alert( objHeaderSpan.innerHTML );
  •  
  • }
  •  
  • </script>

Notice that within the IF statement above, I am assigning AND testing the existence of both the Header and the Span within it. And, through the amazing beauty of short circuit evaluation, we can be sure that if the Header does not exist and results in a NULL, then the IF statement will fail and the AND'd part (setting the header span) will never get executed.

This is waaay awesome.



Reader Comments

Mar 31, 2007 at 7:42 PM // reply »
24 Comments

Yes, yes...it's very nice. I love tricks lke that.


Apr 1, 2007 at 12:55 AM // reply »
2 Comments

Somewhat related:

Instead of:

var target = null;

if(event.target) {
// DOM2 event property
target = event.target;
} else {
// IE proprietary event property
target = event.srcElement;
}

Do:

var target = event.target || event.srcElement;

You can do this with && too:

var message = ieStopsSucking && "Hallelujah!" || "Awwww, poo!";


Apr 1, 2007 at 3:50 AM // reply »
120 Comments

Most C-style languages have always allowed this but I generally advise against it as it confuses the heck out of people who don't realize you can do this. Especially when you do what you have done and use an implicit test of the resulting value (although of course with a boolean result you specifically do *not* want to test it against true/false explicitly!).

You'll find you can also do things like:

a = b = c = d = 0;

which sets all four variables to zero. Again, I'd say avoid this as it is cryptic and unclear.


Apr 1, 2007 at 9:35 AM // reply »
11,314 Comments

@Aaron,

Yeah, that is a cool one. I do like using that, especially for things that are different from browser to browser (like event models).

@Sean,

I have to say that I agree with that. I am always one who pushes for readability / maintainability over neat shorthands. However, creating a DOM node pointer and then checking to make sure it exists feels so standard at this point that it is something I might want to consider using.

This doesn't work in CF (I just checked).


Apr 1, 2007 at 11:01 AM // reply »
1 Comments

Lots of languages have this ability Some consider it confusing, Flex Builder actually issues a warning for it but compiles it anyway, but in some languages it's actually become a common idiom for certain situations, especially used in while loops. PHP comes to mind, where you often see this form used in processing recordsets returned from a database query:

while($rec = $stmt->fetchRow()) {
// process data...
}

Personally I don't find it so terribly confusing, but it does take a sharp eye and I prefer to use it sparingly. My rule of thumb for such things is, "imagine yourself having to look at this code two years from now."


Apr 7, 2007 at 6:14 PM // reply »
172 Comments

A couple other, similar, somewhat-cryptic time savers:

var a = b || c;

...sets a to b if b is truthy, otherwise it sets it to c.

var a = b && c;

...sets a to c if b is truthy, otherwise it sets it to b.


Apr 9, 2007 at 8:50 AM // reply »
11,314 Comments

@Steve,

The OR'ing I knew about, but I didn't realize that AND'ing would work that way. Very cool. If neither B or C are truthy, does A still get set to C or does it get null?


Apr 9, 2007 at 10:08 AM // reply »
2 Comments

@Ben: It seems (through some experimentation on the Firebug console) that if neither b or c are truthy, then a gets set to b.

var b = null;
var c = false;

var a = b && c // a = null

var b = true;
var c = false;

var a = b && c // a = false


Apr 9, 2007 at 10:18 AM // reply »
11,314 Comments

Awesome. Thanks for testing that. Good to know.


Feb 22, 2008 at 5:54 AM // reply »
2 Comments

I have tried to lay this different, to broke it into CSS

avascript:
document.body.onload = function(){
var objHeader = document.getElementById("header");
objHeader.outerHTML+='<script>window.onresize=document.getElementById("main").style.width="772px";</script>';
}

<!--[if IE]>
<script>
var objmain = document.getElementById("main");
function updatesize(){ var bodyw = window.document.body.offsetWidth; if(bodyw <= 790) objmain.style.width="772px"; else if(bodyw >= 1016) objmain.style.width="996px"; else objmain.style.width="100%"; }
updatesize(); window.onresize = updatesize;
</script>
<![endif]-->

but it dosen't work :-(

Any thoughs ?


Aug 12, 2011 at 1:21 PM // reply »
1 Comments

I realize this is a 4 year old post, but please note that using this will create GLOBAL variables.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 20, 2013 at 1:09 AM
The Beauty Of The jQuery Each() Method
my html code : <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="nss.js"> ... read »
Jun 19, 2013 at 11:31 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Ben, bunch to learn indeed, but thats fun part : ) ... read »
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
Jun 19, 2013 at 9:38 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Alesei, Glad you like it. Even after working with AngularJS for months, I still get a bunch of unexpected, "$digest is already in progress". So hard to debug sometimes! ... read »
Jun 19, 2013 at 9:36 PM
Working With Inherited Collections In AngularJS
@Mike, The relationship of $scope values is definitely an interesting thing! But it's not simple - it really forces you to understand prototypal inheritance, which is not at all a simple topic! Gla ... read »
Jun 19, 2013 at 9:35 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Joe, Oh, super interesting! I had only thought to url-encode the signature; but I think that's because the S3 docs actually have a special NOTE telling you to do so. It would have never occurred t ... read »
Jun 19, 2013 at 9:32 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Richard, Glad you like! Hopefully I'll have some more interesting stuff coming. This morning, I blogged a bit more about generating the pre-signed, query string authenticated URLs; but, then deeme ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools