Cheatography
https://cheatography.com
Javascript for Cambridge A Level
Script tag
<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
if (a>b){
// a > b
}else if (a==b){
// do when a equals to b
}else{
// everything else
}
|
Logical
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<-1), used in if |
! |
Logical not, e.g. a!=b same as !(a==b) |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets