Show Menu
Cheatography

Javascript for Cambridge A Level Cheat Sheet Cheat Sheet (DRAFT) by

This is a cheat sheet for Javascript A Level Cambridge Exam IT

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Script

<script type="text/javascript">  
...
</script>

Basic

// One line comment

/*
Mult-line comment
*/

function addNumbers(a, b){
  return a+b;
}

Button

<button onclick="functionName()">click me</button>

...

<script>
    function functionName(){
        // Write what will happen after button is clicked here
    }
</script>

Variables

var a;
create new variable
var a = 10;
create a new variable and assign value
var b = "­som­eth­ing­";
create a new variable and assign string
var c [1, 6, 26, 99];
create a new array with 4 elements in it
c [0]
access item 0 (first item) in array c

output

console.log(a);
Output to browser developer console
document.write(a);
Directly write to end of HTML document
alert(a);
Display popup box with the message
docume­nt.g­et­Ele­men­tBy­Id(­"­xxx­"­).i­nne­rHT­ML=a;
Element ID "­­xx­x­" content replaced with the message

input

var a = prompt­("qu­est­ion­", default);
Popup box ask question, assign value to a
var a = docume­nt.g­et­Ele­men­tBy­Id(­"­xxx­"­).v­alue;
xxx is a textbox's id, and the value in textbox assigned to a

loops

// For loop

for(var i=0; i<10; i++){
    document.write(i);
}

// For loop printing everything in array (arr)
// method 1
for (var i=0; i< arr.length; i++){
    document.write(arr[i]);
}
// method 2
for (var i of arr){
    document.write(i);
}

// while loop
// also outputing everything in arr
var i=0;
while (i<arr.length){
    document.write(arr[i]);
    i++;
}

conditions (IF statem­ents)

// If
if (a>b){
  // a > b
}else if (a==b){
  // do when a equals to b
}else{
  // everything else
}

logical comparison

a==b
equals
a!=b
not equals
a>=b
a larger than or equals b
a<=b
a smaller than or equals b
&&
logical AND, e.g. (3>1) && (3<10), used in if
||
logical OR, e.g. (3>1) || (3<10), used in if
!
logical NOT, e.g. a != b same as !(a==b)

time events


/* 
To execute the function only once, use the setTimeout() method instead.
To clear an interval, use the id returned from setInterval(): 
*/

myInterval = setInterval(function, milliseconds);

//Then you can to stop the execution by calling clearInterval():
clearInterval(myInterval);

// for e.g. try this
function printHello () {
 element.innerHTML += "Hello";
}

setInterval(printHello, 1000);

// set timeout is doing once, where it pauses, here is an example
setTimeout(function() {
 prompt("Do you want to continue?")
}, 2000);

String manipu­lation

 //allows you to perform actions on a string and extract information

//for e.g. return from (startposition, endposition)
var word = "Hello World";
console.log(word.substring(7, 11));
//output: "World"

//for e.g. return from (startposition, noofchar)
var word = "Hello World";
console.log(word.substr(2,4));
//output: "ello"

//for e.g. finds the first string and replaces it with the second string
var word = "Hello World";
var newW = word.replace("World","friends");
console.log(newW);
//output: "Hello friends"

//for e.g. returns length
var word = "Hello".length
console.log(word);
//output: 5

//for e.g. concatenate string 1 and string 2
var word 1 = "Hello";
var  word 2 = "World";
console.log(concat(word1, " ", word2);
//output: Hello World
// this is same with if you plus the strings together

//toUppercase is to change to uppercase
var word = "hello";
console.log(word.toUpperCase());

//toLowerCase is to change to uppercase
var word = "hello";
console.log(word.toLowerCase());

//charAt is to return the character string at location number
var word = "hi"
console.log(word.charAt(1));
//output: i