overview edit doc new doc remove

Jan 26, 2018

Javascript

Basics

Syntax

Comments

// one line

/*
  multi
  line
  comment
*/

Datatypes

Variables can't start with a number. You can define any kind of datatype with var:

var myName = "Tom";

Mathematical functions

MATH.E
MATH.PI
MATH.log()
MATH.sqrt()

if else

if ( ) { 

  .. 

} else if ( ) { 

  .. 

}

Ternary Operator assigns a value based on a condition as: "(condition) ? iftrue : ifFalse":

var canIVote = (age >= 18) ? true : false;

Loops

Switch

switch (age) {

  case 5 :
    document.write ("Go to Kindergarten<br />");
    break;

  case 7 :
    document.write ("Go to 1st Grade<br />");
    break;

  default :
    document.write ("Subtract 5 from your age<br />");

}
Do while
do {

  var guess = prompt ("Guess a number between 1 and 20");

} while (guess != 15)
For
for (j = 0; j <= 20; j++) {

  if ((j % 2) == 0) {
    continue;

  }

  if (j == 15) {

    break;

  }

}

for (k in customer) {

  document.write (customer[k], "<br />");

}

Converting types

Convert strings to numbers:

document.write ("Converted String : ", Number("3.14"), "<br />");

Random numbers

document.write ("Random # (1-10) = ", Math.floor ((Math.random() * 10) + 1), "<br/>");

Strings

A string is a bunch of individual characters.

var randStr = "A long " + "string that " + "goes on and on";

Get the length of a string:

document.write ("String Length : ", randStr.length + "<br/>");

Slice and substr:

document.write (randStr.slice(19, 23) + "<br/>");
document.write (randStr.slice(19) + "<br/>");
document.write (randStr.substr(19, 4) + "<br/>");

Replace a string:

document.write (randStr.replace("and on", "forever") + "<br/>");

Get characters at index:

document.write ("At Index 2 : ", randStr.charAt (2) + "<br/>");

Split:

var randStrArray = randStr.split (" ");

Trim:

randStr = randStr.trim ();

Converting to uppercase or to lowercase:

document.write (randStr.toUpperCase() + "<br/>");
document.write (randStr.toLowerCase() + "<br/>");

Styling a string:

var strToStyle = "Random String";

document.write ("Big : ", strToStyle.big(), "<br />");
document.write ("Bold : ", strToStyle.bold(), "<br />");
document.write ("Font Color : ", strToStyle.fontcolor("blue"), "<br />");
document.write ("Font Size : ", strToStyle.fontsize("8em"), "<br />");
document.write ("Italics : ", strToStyle.italics(), "<br />");
document.write ("Google : ", strToStyle.link("http://google.com"), "<br />");
document.write ("Small : ", strToStyle.small(), "<br />");
document.write ("Strike : ", strToStyle.strike(), "<br />");
document.write ("Sub : ", strToStyle.sub(), "<br />");
document.write ("Sup : ", strToStyle.sup(), "<br />");

Arrays

Arrays have variable sizes and can contain multiple types in JS.

var tomSmith = ["Tom Smith", "123 Main", 120.50];

Add an item:

tomSmith[3] = "tsmith@aol.com";

Delete an index:

delete tomSmith[3];

Sort an array:

tomSmith.sort ();

Concat strings:

var combinedArray = numbers.concat (tomSmith);

Push and pop:

tomSmith.pop ();

tomSmith.push ("555-1212", "US");

Shift the first item:

tomSmith.shift ();

Date

var curDate = new Date ();

document.write ("Date : ", curDate.getDate (), "<br />");
document.write ("Month : ", curDate.getMonth (), "<br />");
document.write ("Day : ", curDate.getDay (), "<br />");
document.write ("Year : ", curDate.getFullYear (), "<br />");
document.write ("Time : ", curDate.getHours (), ":", curDate.getMinutes (),
  ":", curDate.getSeconds (), ":", curDate.getMilliseconds (), "<br />");

Custom date object

var myBD = new Date ("December 21, 2015");

Functions

Local variables defined in functions can't be accessed outside of the function.

function inArray (arrayToCheck, value) {

  ..

}

function times2 (num) {

  var var2 = 2;

  return num * var2;

}

Exception handling

var custArray = ["Tom", "Bob", "Sally", "Sue"];

var getCust = function (index) {

  if (index > custArray.length) {

    throw new RangeError ("Index must be >= 0 and <= " + custArray.length );

  } else {

    return custArray[index];

  }

}

try {

  document.write("Customer : ", getCust(5), "<br />");

} catch (ex) {

  if (ex instanceof RangeError) {

    document.write(ex.message + "<br />");

  }

}

Object Orientated

var object = function ( <parameters> ) {

  var self = {};
  self.<property> = <property>;

  self.<function> = function () {

    ..

  }

  var <function> = self.<function> ();

  return self;
}

To create an object do:

new object(<parameters>);
1