Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with:

Javascript Short-Hand IF Within Object Notation

By Ben Nadel on

This isn't really a big thing here. For the first time ever, I tried using the Javascript IF statement short hand inside of an array call. I had no reason to think that this wasn't possible, it's just kind of cool to see it work:

  • <script type="text/javascript">
  •  
  • // Create an array of friends.
  • var arrFriends = new Array(
  • "Molly",
  • "Sarah",
  • "Dave",
  • "Luke"
  • );
  •  
  • // Get the day of the week.
  • var intDayOfWeek = (new Date()).getDay();
  •  
  • // Figure out which friend to call based on
  • // the given day of the week. Call Luke on saturday.
  • // For every other day, call Molly (my girlfriend).
  • var strFriendToCall = arrFriends[ ( intDayOfWeek == 6 ) ? 3 : 0 ];
  •  
  • </script>

When I am figuring out which index of the Friends array to call, I determine the value based on the day of the week. It's an IF statement inside of object notation. Again, nothing ground-breaking. Just neat.



Reader Comments

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
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.