Cheatography
https://cheatography.com
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
|
create new variable |
|
create a new variable and assign value |
|
create a new variable and assign string |
|
create a new array with 4 elements in it |
|
access item 0 (first item) in array c |
|
output
|
Output to browser developer console |
|
Directly write to end of HTML document |
|
Display popup box with the message |
document.getElementById("xxx").innerHTML=a;
|
Element ID "xxx" content replaced with the message |
|
input
var a = prompt("question", default);
|
Popup box ask question, assign value to a |
var a = document.getElementById("xxx").value;
|
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 statements)
// If
if (a>b){
// a > b
}else if (a==b){
// do when a equals to b
}else{
// everything else
}
|
|
logical comparison
|
equals |
|
not equals |
|
a larger than or equals 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 manipulation
//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
|
|