All posts by elliotbonneville

Javascript’s little-known do-while loop

Many Javascript programmers are familiar with the while loop, which executes iterates until a given condition evaluates to false. It’s a pretty handy construct and can provide the fastest loops in the Javascript world (if used properly). However, there is a drawback. If the given condition evaluates to false immediately, the while loop will not run even once. This is because the while loop’s condition is evaluated before executing the code within the loop. “Oh well,” you say, “if that’s the case, then too bad, right?” Wrong.

Introducing: the do-while loop

The do-while loop is a lesser-known cousin of the while loop that few Javascript programmers know about. Unlike the while loop, it executes its statement exactly once before evaluating its condition. The do-while loop looks like this:

do {
statement;
} while (condition);

Because the statement of a do-while loop is executed once before the condition is evaluated, you can do interesting stuff like this:

do {
// statement
} while (false);

If we were using an ordinary while loop, the above statement would fail. However, because the statement of a do-while loop is executed before the condition is evaluated, the above code runs once and then fails.

Too long; didn’t read

Functionally, the do-while loop is the equivalent of this while loop:

while(true) {
// statement

if(condition == false) {
break;
}
}

Top 8 Javascript learning tools

Javascript is a complex and multi-faceted language, and can be hard to learn properly. I’ve found quite a few invaluable tools that have helped me along my Javascript-learning journey, but some of them have been a little hard to dig up and others aren’t very well-known. To save you the pain, I’ve scrounged around and pulled together a list of what I’ve found to be the most useful Javascript learning tools. Enjoy!
Continue reading Top 8 Javascript learning tools

9 useful techniques to optimize your Javascript

Javascript is an infamously easy language to program in. However, just because something works doesn’t mean you’re doing it right. Oftentimes, programmers will write a script to provide functionality they need and then leave it as soon as it’s working. Don’t be like those guys: read this tutorial on Javascript optimization (or at least skim it!) and apply the techniques listed here in your code. Your users will thank you.
Continue reading 9 useful techniques to optimize your Javascript

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