O'Reilly logo

Eloquent JavaScript by Marijn Haverbeke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Techniques

Now that we have a rather good idea of what JavaScript functions are and how they function, we will look at some considerations that come into play when designing and writing them.

Avoiding Repetition

The reason functions were invented is to reuse pieces of code. Programs typically need to perform the same operation (such as exponentiation) multiple times, and when you repeat the full code needed to perform the operation every time you need it, your program is going to be a lot longer.

Not only will it be longer, but it will also be more boring to read and more likely to contain errors. For example, the power function we defined does not work with negative exponents. If you find out that those are also needed, you’d have to update all ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required