JavaScript Quick Tips #2

Concatenating when Logging without the Plus Sign

You can use the comma in place of the plus sign when concatenation is needed.

Give the Comma a Try When Logging
1
2
3
var stuff = 'my stuff';
//the comma acts like the plus to concatenate your strings
console.log('some text:', stuff);

Attaching Properties to Non-Object Objects

Usually when you deal with an array you normally do not attach properties to it, but you are allow to do such a thing.

Attaching Property to an Array
1
2
3
4
var myArray = [1, 2, 3];
myArray.aKey = "myValue";
//outputs "myValue"
console.log(myArray.akey);

You can also do the same thing for a function and other non-object objects.

Now Attaching Property to a Function
1
2
3
var myFunction = function() {};
//outputs "myValue"
myFunction.aKey = "myValue";

The reason you can do this is because everything in JavaScript is treated like an object. You may want to do this to include meta data on your arrays, functions or other variable types that are not object objects, but do use your discretion when a object is more fitting for the task.