JavaScript Tip: Adding Property to an Object

After you’ve declared your object, however, you can still add key/value pairs to it in a couple of ways.

This is known as bracket notation. The alternative and more common syntax is dot notation. Here’s an example:

Dot notation is simpler than bracket notation; however, there are certain tasks that can only be done with bracket notation. For example, you can use a variable inside the brackets, which can’t be done using dot notation. Bracket notation also supports strings containing spaces and other characters that are invalid in dot notation.

JavaScript Tip: Creating an Object

As with arrays, there are a couple of ways to create objects, and, just like arrays, one is preferred over the other. So even though you can do this:

You can also use object literal notation:

It’s safer to use the object literal notation {} as it’s unable to be overwritten. The object literal represents a new, empty object. Continue reading

JavaScript Tip: Strict Equal (===)

A strict equal comparison performs no conversion of types. Where “” == 0 would return true for a regular equal comparison, “” === 0 would not, since an empty string does not equal zero:

JavaScript Tip: Minifying Code

Once you’ve written, tested, debugged, optimized, and finalized all of your code, it’s time to release it into the wild. This is to say: distribute the code on live sites. There’s one more step you could take before doing so: minify the code.

To minify code is to remove all of its comments and extraneous white space in order to condense the code as much as possible. Minifying a script will significantly reduce its file size, perhaps by as much as 50 percent. This in turn makes the site load faster in the browser, as there will be less data for the user to download. Continue reading

JavaScript Tip: Anonymous Functions

Anonymous functions are functions that are declared as they are run, and they have no name assigned to them. Rather than writing a detailed function, you can use an anonymous function and have it execute immediately when it is run, instead of having it reference a function elsewhere in your document.

Anonymous functions perform better than a normally defined function because there is nothing to reference; they just execute when needed. These functions are used only once; they can’t be referenced over and over. Making something an anonymous function will prevent the variable being used from slipping into the global scope.

JavaScript Tip: Is Random Really Random?

As random as random can be.

A computer cannot generate a truly random number because computation is deterministic: it follows an unbroken chain of cause and effect in which no truly random events ever occur. Instead, it uses a set of complex algorithms to generate what’s known as a pseudorandom number — a number that gives the appearance of randomness, and is good enough for any practical purpose.

This produces a number between zero and one (excluding either limit). Continue reading

JavaScript Tip: Submitting a Form by Enter key

Depending on the browser type and configuration, pressing the Enter key while in a form does not always submit the form. Sometimes, for instance, the button that submits the form resides in another frame. In that case, adding a bit of JavaScript to ensure that the Enter key sends the form data, as well, comes in handy.

All that is necessary to implement for that function is a standard key listener: Keyboard events are not part of DOM Level 1 or Level 2, but are still implemented in recent browsers.

Accessing the event differs from the usual approach (window.event in Internet Explorer; the event as the automatic parameter of the function in all other browsers). Continue reading