JS
JavaScript Master Cheat Sheet
A compact, styled reference that covers basics → advanced topics. Paste into a WordPress Custom HTML block.
Basics
Include scripts, comments, and simple examples.
<!-- Inline -->
<script>
// JS goes here
console.log('Hello world');
</script>
<!-- External file -->
<script src="app.js"></script>
Variables & Types
Use let/const for block scope; var is function-scoped.
// Declaration examples
var legacy = 'hello';
let counter = 0;
const PI = 3.14159;
// Common types
let name = "Jane"; // string
let isActive = true; // boolean
let scores = [10, 20, 30]; // array
let user = { id: 1, name: 'J' }; // object
Arrays
Common methods for manipulating arrays.
const fruits = ["Banana","Apple","Pear"];
fruits.push("Orange"); // add
fruits.pop(); // remove last
fruits.includes("Apple"); // true
const part = fruits.slice(1,3); // shallow copy portion
fruits.splice(2,1, "Kiwi"); // remove & replace
Operators
Arithmetic, comparison, logical.
5 + 3; // 8
10 % 3; // 1 remainder
x === y; // strict equality
x !== y; // not equal
a && b; // logical AND
a || b; // logical OR
!flag; // logical NOT
Functions
Classic, arrow, default params, IIFE.
// Classic
function greet(name){ return `Hello ${name}`; }
// Arrow
const add = (a,b) => a + b;
// Default param
function welcome(user = 'Guest'){ console.log(user); }
// IIFE (immediately-invoked)
(function(){ console.log('Init'); })();
Loops
for, while, for…of, break/continue.
// for
for(let i=0;i<5;i++){ console.log(i); }
// while
let n=3; while(n--){console.log(n);}
// for...of for arrays
for(const v of [1,2,3]) { console.log(v); }
// break & continue
for(let i=0;i<5;i++){
if(i===2) continue;
if(i===4) break;
}
If / Else
Conditional logic with ternary shortcuts.
if(age >= 18){
console.log('Adult');
} else if(age >= 13){
console.log('Teen');
} else {
console.log('Child');
}
// ternary
const status = age >= 18 ? 'Adult' : 'Minor';
Strings
Common methods & escape sequences.
const s = "JavaScript";
s.toLowerCase();
s.toUpperCase();
s.includes("Script");
s.replace("Java","ECMA");
s.slice(0,4); // "Java"
// escapes: \n, \t, \', \"
Regular Expressions
Patterns, flags, common usage.
// flags: i (ignore), g (global), m (multiline)
const rx = /\\d+/g;
"abc123".match(rx); // ["123"]
/^[A-Z]/.test("Hello"); // true if starts with capital
Numbers & Math
Parsing, Math helper methods.
Number("42"); // 42
parseInt("10px"); // 10
parseFloat("3.14"); // 3.14
Math.random(); // 0..1
Math.round(2.7); // 3
Math.max(1,5,3); // 5
Math.sqrt(16); // 4
Dates
Create and manipulate dates.
const now = new Date();
const custom = new Date(2025, 11, 25); // year, monthIndex(0-11), day
now.getFullYear();
now.getMonth(); // 0-11
now.getTime(); // ms since 1970
DOM (Document)
Select elements, update content, create nodes.
// Select
const el = document.getElementById('title');
const firstBtn = document.querySelector('.btn');
// Update
el.textContent = 'New Title';
el.innerHTML = '<strong>Bold</strong>';
// Create / append
const div = document.createElement('div');
div.textContent = 'Hi';
document.body.appendChild(div);
Browser & Window
Window methods & properties.
alert('Hi');
console.log(window.location.href);
setTimeout(()=>console.log('later'), 1000);
localStorage.setItem('k','v');
Events
Listening patterns and common event names.
// Click
button.addEventListener('click', (e) => {
console.log('clicked', e.target);
});
// Keyboard
window.addEventListener('keydown', (e) => {
if(e.key === 'Enter') console.log('enter pressed');
});
Errors & Debugging
try/catch and useful console tools.
try {
mightFail();
} catch(err) {
console.error('Error:', err.message);
} finally {
console.log('cleanup');
}
// console methods: log, warn, error, table, group
Tip: Use the copy buttons to quickly paste examples into your editor.