How to check for an undefined variable in Javascript

There are a number of ways to check if a variable is undefined, and not all of these methods are created equal. Some of them work half of the time; some of them are just plain wrong, and one method stands above all others in usefulness. In this (somewhat lengthy) article, you’ll find out which is which– and why! If you just want the “right” way to check if a variable is undefined, scroll right to the bottom, where I show you the proper method to do this sort of check.
Continue reading How to check for an undefined variable in Javascript

Improving on Javascript’s random numbers

Javascript’s lack of a good random numbers function is something that has always bothered me. No, I’m not talking about an extremely complex seedable psuedo-random number generator that optionally generates numbers from static interference in the Earth’s atmosphere or radio waves or whatever “real” random number generators do (see random.org for that). I’m simply talking about random numbers that are actually useful– e.g. random numbers between two other numbers, or what are called ranged random numbers.
Continue reading Improving on Javascript’s random numbers

On Javascript string operations (a tutorial)

There are a lot of things you can do to a String, and usually about fifty different ways to achieve your goal (without even going into RegEx). In order to keep this tutorial/quick reference to a readable length, I’ll only be commenting on a few of the most useful ones, and ignoring RegEx completely. Here are the Javascript string functions we’ll be taking a look at, listed in no particular order.

  • String.indexOf() and String.lastIndexOf()
  • String.split();
  • String.charAt();
  • String.substr();

Using only these four functions, you can achieve greatness! Or at least do what needs to get done without using RegEx, which usually has pretty much the same amount of personal satisfaction.

Continue reading On Javascript string operations (a tutorial)

The Power of Objects– Javascript Object Tutorial

Warning: this tutorial assumes you know the basics of working with Javascript and HTML. If you’re familiar with Javascript syntax already, you can probably skip this first bit. If not, read on!

So, what exactly are Objects?

Objects are an extremely powerful feature of Javascript that are often overlooked. They allow you to do many things in a few lines of code that would otherwise be very messy to implement. Basically put, an Object in Javascript is a variable that allows you to store other variables in it. A variable stored within an object is known as a ‘property’. You can store any variable you want on an object; you can even store functions on an object!

Continue reading The Power of Objects– Javascript Object Tutorial