1. Home
  2. Computing & Technology
  3. JavaScript

Functions

These small functions perform tasks that you may find useful.
Alternative Ways to Define Functions
There is more than one way to define a function in Javascript - in fact there are three. Each works slightly differently and can result in more effective code in the right circumstances.
Optional Function Arguments
Javascript provides a completely different way of making function arguments optional from the way that most programming languages do it. Javascript's way gives you the maximum possible flexibility.
Adding Javascript from Javascript
When you need to link additional external Javascripts into the head of your page from within your Javascript processing then you need this function that will use the DOM to add the script properly into the head of your page.
Encoding Web Addresses
Some characters are either invalid in web addresses or have a special meaning. Where you need to use any of these characters with their regular meaning the address needs to be encoded. JavaScript supply two functions for encoding web addresses to make this task much easier.
Addslashes and StripSlashes
Javascript doesn't provide an equivalent to the addslashes() and stripslashes() functions that are so useful in PHP. We can quickly fix this oversight by defining them ourselves.
Targetting External Links to New Window
If you want off site links to open in a new window then you can either add a target attribute manually to each link or use this Javascript function that automatically ads them to all of the external links on the page for you.
Formatting Numbers
Would you like to be able to display numbers with thousands separators and your choice of decimal point, currency symbol and negative indicator? Well now you can using my formatNumber function.
Numbers to Words
When you finish a numerical calculation and displaying the answer in numbers just wont do then perhaps converting the number into words is what you need.
Returning Multiple Values
A function only allows for one value to be returned when it finishes. Here's how to get around that limitation and return multiple values.
Add Digits Together
There are a few situations where you may want to add up all the digits in a number to reduce it to a single digit. Calculating a check digit value and numerology are two totally different situations where this function will be useful.
Numeric Test
A quick way to test that numeric input fields actually contain a number.
Converting IP Addresses
While the dotted notation for IP addresses is the most common one that people use, computers actually treat them as single numbers. We can use JavaScript to convert to and from other versions of IP addresses.
Hex to Decimal and Decimal to Hex
Convert etween decimal and hexadecimal with these simple functions. Very useful if you randomly generate a number and need the hex equivalent.
Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Controls and Widgets
  5. Functions

©2009 About.com, a part of The New York Times Company.

All rights reserved.