Log in / create account | Login with OpenID
DocForge
An Open Wiki For Software Developers

JavaScript/Data structures

From DocForge

Strings [edit]

In Javascript, strings are formed by a paired single quite (') or double quote("). Strings are literal sections of text, with escape sequences possible.

Strings may be converted from any object, by using the toString property.

The following functions are of note:

  • string.fromCharCode(...)
  • string.charAt(pos)
  • string.charCodeAt(pos)
  • string.concat(string1, string 2, ...)
  • string.indexOf(searchString, positiion), string.lastIndexOf(searchString, positiion)
  • string.match(regexp)
  • string.replace
  • string.slice (start, end), string.substring (start, end)
  • string.split (delimitor, max)
  • string.toLowerCase(), string.toUpperCase(), string.toLocaleLowerCase(), string.toLocaleUpperCase()
  • string.trim()
  • string.length


The following functions are used for converting strings to or from numbers:

  • string.toNumber ()
  • parseInt(string, radix)
  • parseFloat(string)
  • string.toString(radix)

Arrays [edit]

Defining an array can be done in the following methods:

var foo = new Array();
 
var foo = ["1", "2", "3"];

Arrays have the following properties:

length

Arrays have the following methods:

concat Joins two or more arrays, returning one array
join(delimiter) Joins elements of an array into a string, with the parameter providing the delimiter (default: ",")
pop Removes and returns last element in the array.
shift Removes and returns first element in the array.
push Adds the element to the end of the array.
unshift Adds the element to the front of the array
reverse Reverses the order of the array.
slice(begin, end) Returns a segment of the array.
splice Adds and/or removes elements from the array.

Objects [edit]

JavaScript supports prototype-based object oriented programming, meaning objects are cloned and not pre-defined using classes.

Objects can be instantiated using a very simple syntax:

var bar = {};   // An empty object
var bar = {one: 1, two: "Two"};