Next page | Contents page |

Useful core objects

As well as the Global object there are several other pre-defined types in core JavaScript with useful properties and methods.


Array
  a.length, a.sort(), a.push(), a.pop(), a.reverse(), etc

Date  
  var now = new Date ();
  d.getDay (); // etc
  d.getMilliseconds (); // for differencing
  var then = new Date (millisecs); // Since 1970.0
  d.toLocaleString (); // Taking time zone into account, from your system

Math
  Math.PI, Math.abs (), Math.sqrt (), Math.sin (), etc

Number
  n.toString (), n.toPrecision (2), etc

Object
  o.toString (), o.valueOf ()

RegExp
  new RegExp (pattern, attributes) 
  // Regular Expression - there is a page about, soon

String
  s.length  // a property, not a method (unlike in Java)
  
  var s = "Some text.";
  var a = s.charAt (3);	         // "e"
  var b = s.charCodeAt (4);      // 32
  
  var c = s.concat (" ", "Some more text."); 
                // To concatenate any number of strings
  
  var d = String.fromCharCode (65); // "A".
                // NB: static function, requires type name 
				
  var e = s.indexOf ("text");  // 5
  var f = s.indexOf ("TEXT");  // -1, meaning not found
  
  var g = c.indexOf ("text", 6); 
                // Starting at index 6 - find the 2nd one
				
  var h = c.lastIndexOf ("text");	// Finds the 2nd one 
  var j = s.substr (5, 4);    // "text": 4 chars at index 5
  var k = s.substring (5, 9); // "text": chars from 5 to before 9
  var m = s.toLowerCase ();   // or s.toUpperCase ()
  
  var n = s.split (" ");	
      // Returns array of substrings, split on the separator character
Next page | Contents page |