Javascript for Cambridge A Level
<script type="text/javascript">
...
</script>
// One line comment
/*
Mult-line comment
*/
function addNumbers(a, b){
return a+b;
}
<button onclick="functionName()">click me</button>
...
<script>
function functionName(){
// Write what will happen after button is clicked here
}
</script>
var a; |
create new variable
|
var a = 10; |
create a new variable and assign value
|
var b = "something"; |
create a new variable and assign string
|
var c = [1, 12, 31, 45] |
create a new array with 4 elements in it
|
c[0] |
access item 0 (first item) in array c
|
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
|
document.getElementById("xxx").innerHTML=a; |
Element ID "xxx" content replaced with the message
|
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
|
// 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++;
}
// If
if (a>b){
// a > b
}else if (a==b){
// do when a equals to b
}else{
// everything else
}
|
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)
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.