Javascript Scripting Tutorial: Built in Functions

In this article, I’ll list all of the built in functions available to you with Javascript. While not everything will be covered, I will cover every function that is used on a regular basis. I’m still deciding whether I’ll cover Javascript Graphics or AJAX next. Leave comments below to help me understand. On to the functions.

Javascript Array Functions

In previous articles I described how to create Arrays, but if you forgot here is how you create arrays:

  • var a = new array() // Creates an empty array
  • var b = new array(8) // Creates an array with 8 element cells
  • var c = [1, 2, “turtle”, “number”, true]; // Creates an array with a bunch of random values

Javascript Array Properties and Functions

  • b.length // A property that would return the value of 8, because of the array declaration above
  • c.concat(3,4) // The array c now has the values 1, 2,  “turtle”, “number”, true, 3, 4
  • c.join(“: “) // Returns a string with each element separated by a colon and a space. 1: 2: number: true
  • c.slice(0, 2) // Returns an array which contains all elements between the first value you pass to the second value. In this example you would be returned [1, 2, number]
  • c.sort() // Returns the array elements in alphabetical order. This example returns [1, 2, number, true, turtle]
  • c.toString() // Converts the array into a string and then returns the string.

Javascript Date Functions

There are numerous date functions. I’ll cover the most commonly used here.

  • a = new Date(); // Creates a new date object and assigns the current time to it
  • document.write(a.getTime()); : Returns a millisecond representation of a date. ex. 1270487133410
  • document.write(a.getDay()); : Returns the day of the week
  • document.write(a.getFullYear()); : Returns the year of the date
  • document.write(a.getHours()); : Returns the hours of the set date
  • document.write(a.getMinutes()); : Returns the minutes of the set date
  • a.setTime(); : Creates a millisecond representation of a date. ex. 1270487133410
  • a.setDay(); : Sets the day of the week
  • a.setFullYear(); : Sets the year of the date
  • a.setHours(); : Sets the hours of the set date
  • a.setMinutes(); : Sets the minutes of the set date

Javascript Math Functions

Here are a few of the most commonly used Javascript Math Functions

  • isNaN(variableToTest) : Returns true if the variable is a number and false otherwise
  • Math.abs(variableToTest) : Performs an absolute value calculation
  • Math.ceil(variableToTest) : Rounds a number up
  • Math.floor(variableToTest) : Rounds a number down
  • Math.max(x, y) : Returns the larger of the two variables it is passed
  • Math.min(x, y) : Returns the smaller of the two variables it is passed
  • Math.random(): Returns a random number
  • parseFloat(string) : Converts a string into a float number
  • parseInt(string) : Converts a string into an int number

Javascript String Functions

Here are a few of the most used string functions.

  • stringVariable.length : Returns the number of characters in a string
  • stringVariable.charAt(x) : Returns the character at the position sent to this function
  • stringVariable.concat(x1, x2) : Adds one or more values to the end of the string
  • stringVariable.indexOf(wordYourLookingFor, whereToStartSearch) : Searches the string for a chosen word or character
  • stringVariable.match(regexp) : Look for a specific regular expression in a string. See my Javascript Regular Expression tutorial if you don’t know what they are.
  • stringVariable.replace(regexp, replacementText) : Search for a regular expression match and then replace the match with the replacement text given
  • stringVariable.slice(startPoint, endPoint) : Extracts a string from the beginning point to the ending point given
  • stringVariable.split(delimiter, maxLength) : Breaks a string into a series of cells, by separating the words by the chosen delimiter. ex. “1,2,3,4”.split(“,”) would return [“1″,”2″,”3″,”4”]. You have the option of defining a maximum number of elements in the array that is created.

Javascript Document Functions

  • document.getElementsByName(idName) : Provides you with access to a node on the web page by passing it the id Name.
  • document.write() : Appends text or variable values to the web page
  • document.writeln() : Appends text or variable values to the web page, followed by a newline

Javascript Event Handlers

  • onclick : Performs an action when the user clicks on a node in a document
  • ondblclick : Performs an action when the user double clicks on a node in a document
  • onkeydown : Performs an action when the user presses a key
  • onkeypress : Performs an action when the user presses and releases a key
  • onkeyup : Performs an action when the user releases a key
  • onmousedown : Performs an action when the user presses the mouse button
  • onmousemove : Performs an action when the user moves the mouse
  • onmouseout : Performs an action when the user moves the mouse off of an element
  • onmouseover : Performs an action when the user moves their mouse over an element
  • onmouseup : Performs an action when the user releases a mouse button

Javascript Node Functions

  • node.appendChild(nodeName) : Inserts a new node after the node specified.
  • node.insertBefore(nodeName) : Insert a new node in front of the node you specified.
  • node.removeChild(nodeName) : Remove the child node of the node you specified
  • node.replaceChild(nodeName) : Replace a child node with the new one you are specifying

That is basically all of the commonly used prebuilt functions available to you in Javascript. You also basically also know just about all there is to know about Javascript as well. I’m going to cover AJAX, which allows you to communicate with the web server without reloading the web page. Also, look forward to a Javascript Graphics Tutorial.

As always, leave your questions or comments below.

Till next time…

3 Responses to “Javascript Scripting Tutorial: Built in Functions”

  1. Oscar Manos says:

    I’m thankful I’m not the solitary one who feels this way. I attempt to pen interesting posts with information, but I can’t be one of those blogs that’s purely informative, even if I do adhere to a couple themes. I as well favour scanning blogs with that personalised component.

  2. benard says:

    this is a great tutorial..i have managed to improve my javascript base knowledge upto another because of this tutarial..please continue with your good work…Thanks very much.

Trackbacks/Pingbacks

  1. Tweets that mention Javascript Scripting Tutorial Built in Functions | new think tank -- Topsy.com - [...] This post was mentioned on Twitter by Derek Banas. Derek Banas said: Published: Javascript Scripting Tutorial: Built in Functions…

Leave a Reply

Your email address will not be published.

Google+