Declaring variables in JavaScript
“Hey hot dog I know how to declare a variable! You don’t have to tell me how to do that.” Of course you do, but I just wanted to highlight the best way of declaring a few a time. Instead of writing this:
var name = 'Bobbin';
var question = 'are you my mother?';
var birthplace = 'Monkey Island';
We can do something a little more efficient than writing var every time. Why not try this on for size:
var name = 'Bobbin',
question = 'are you my mother?',
birthplace = 'Monkey Island';
Pretty, pretty, pretty good. Omit var and use a comma to separate variable names. And because we’re using 4 spaces for indentation it looks nice and pretty too. We don’t even have to wait until we’ve ended the statement to use the previously declared variables. For example:
var name = 'Bobbin',
question = 'are you my mother?',
fullQuestion = 'I\'m ' + name + ', ' + question;
I’ll shut up now.