You can declare JavaScript variables one at a time like so: var myName = "Kieran";
. But did you know you can declare more than one at a time using a single var
statment?
Let's say I wanted to declare my name and my dog's name at the same time.
Instead of doing this:
var myName = "Kieran";
var dogName = "Yoko";
I could just do this:
var myName = "Kieran", dogName = "Yoko";
The only trouble is, it can cause some confusion.
It's possible to declare variables without initializing them; for example, I could declare var myName;
. That would create the variable without assigning it a value, so it would just be undefined
. I could then assign a value to it later on in the script.
That's fine, but when I've taken it a step further and written things like the following, it's confused other developers:
var myName, dogName = "Yoko";
This is because, in my opinion, it looks like I'm assigning the same value to both myName
and dogName
. In reality, I'm declaring myName
without assigning it a value (so it's undefined
) while assigning the string "Yoko"
to dogName
.
Just something to be aware of in case you see it in the wild!