Show Menu
Cheatography

JavaScript Cheat Sheet (DRAFT) by

A basic how to cheat sheet for beginner and experienced JavaScript developers. Quickly refresh syntax and functions.

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

Basics

On Page Script
 
<script type=
"­tex­t/j­ava­scr­ipt­"
> ...
</script>
Include external JS file
<script src=
filena­me.js
></script>
Delay - 1 second timeout
setTimeout(function () {
  
}, 1000);
Functions
function addNumbers(a, b) {
return a + b; ;
}
x = addNum­bers(1, 2);
Edit DOM Element
document.getEle­men­tById(
"­ele­men­tID­"
).inne­rHTML =
"­Hello World!­"
;
Output
console.log(a);
document.write(a);
// write to the browser console 
 

For Loop

bold
for (var i = 0; i < 10; i++) {
document.write(i + ": " + i*3 + "<br />");
}
var sum = 0;
for (var i = 0; i < a.length; i++) {
sum + = a[i];
}               // parsing an array
html = "";
for (var i of custOrder) {
html += "<li>" + i + "</li>";
}
 

Loops

for (var i = 0; i < 10; i++) { docume­nt.w­rite(i + ": " + i*3 + "­<br />"); } var sum = 0; for (var i = 0; i < a.length; i++) { sum + = a[i]; }               // parsing an array html = "­"; for (var i of custOrder) { html += "­<li­>" + i + "­</l­i>"; }