Skip to main content
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes and Ezra Parker and Dan Wilson and John Mason and Jason Dean and Luis Majano and Mark Mandel and Brian Kotek and Wil Genovese and Rob Brooks-Bilson and Andy Matthews and Simeon Bateman and Ray Camden and Chris Rockett and Joe Bernard and Dan Skaggs and Byron Raines and Barney Boisvert and Simon Free and Steve 'Cutter' Blades and Seth Bienek and Katie Bienek and Jeff Coughlin
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes Ezra Parker Dan Wilson John Mason Jason Dean Luis Majano Mark Mandel Brian Kotek Wil Genovese Rob Brooks-Bilson Andy Matthews Simeon Bateman Ray Camden Chris Rockett Joe Bernard Dan Skaggs Byron Raines Barney Boisvert Simon Free Steve 'Cutter' Blades Seth Bienek Katie Bienek Jeff Coughlin

Javascript Short-Hand IF Within Object Notation

By
Published in Comments (2)

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.

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel
Managed hosting services provided by:
xByte Cloud Logo