A quick lookup for JavaScript language features grouped by category. Each entry covers what it does and how to use it, so you can find answers without leaving the platform.
Operator What it does Example +Addition (also concatenates strings) 3 + 2 → 5-Subtraction 3 - 2 → 1*Multiplication 3 * 2 → 6/Division 6 / 2 → 3%Remainder (modulo) 7 % 3 → 1**Exponentiation 2 ** 3 → 8++Increment by 1 x++ or ++x--Decrement by 1 x-- or --x
Operator What it does Notes ===Strict equality — value and type Prefer over == !==Strict inequality Prefer over != ==Loose equality — type-coerces Avoid !=Loose inequality Avoid <Less than — >Greater than — <=Less than or equal — >=Greater than or equal —
Operator What it does Example &&AND — true if both sides are truthy isOpen && hasStock||OR — true if either side is truthy isGuest || isAdmin!NOT — inverts truthiness !isLoading??Nullish coalescing — right side if left is null/undefined name ?? 'Guest'
Operator Equivalent to x = yassign y to x x += yx = x + yx -= yx = x - yx *= yx = x * yx /= yx = x / yx %= yx = x % yx **= yx = x ** y
The ternary operator is a compact if/else expression:
const label = count === 1 ? ' item ' : ' items ' ;
// If count is 1, label is 'item'; otherwise 'items'
const discount = isMember ? 0.1 : 0 ;
condition ? valueIfTrue : valueIfFalse
All array methods are called on an array instance. Methods that accept a callback receive (element, index, array) — use only what you need.
Method What it does Returns forEach(fn)Runs fn for each element — no return value undefinedmap(fn)Transforms each element with fn New array filter(fn)Keeps elements where fn returns true New array find(fn)First element where fn returns true Element or undefined findIndex(fn)Index of first match Number or -1 reduce(fn, initial)Accumulates elements to a single value Accumulated value some(fn)true if any element passes fnBoolean every(fn)true if all elements pass fnBoolean includes(value)true if value is in the arrayBoolean indexOf(value)Index of first occurrence Number or -1 push(item)Adds item to the end — mutates New length pop()Removes and returns the last element — mutates Removed element shift()Removes and returns the first element — mutates Removed element unshift(item)Adds item to the beginning — mutates New length splice(start, n)Removes n elements at start — mutates Removed elements slice(start, end)Copies from start up to (not including) end New array sort(fn)Sorts in place — mutates Same array reverse()Reverses in place — mutates Same array join(separator)Joins all elements into a string String flat(depth)Flattens nested arrays New array flatMap(fn)map then flat(1)New array
const prices = [ 12 , 5 , 8 , 20 , 3 ];
// map — apply a transformation
const withTax = prices . map ( p => p * 1.1 );
// filter — keep items that pass the test
const affordable = prices . filter ( p => p < 10 );
// find — first item that matches
const firstExpensive = prices . find ( p => p > 15 ); // 20
// reduce — sum all values
const total = prices . reduce ( ( sum , p ) => sum + p , 0 ); // 48
push, pop, splice, sort, and reverse mutate the original array. map, filter, slice, and reduce return a new array and leave the original unchanged.
These methods and properties are available on element objects returned by query methods. document refers to the global document object.
Method What it does Returns document.querySelector(selector)First element matching a CSS selector Element or null document.querySelectorAll(selector)All elements matching a CSS selector NodeList (array-like) document.getElementById(id)Element with the given id Element or null document.getElementsByClassName(name)Elements with the given class HTMLCollection
Method / Property What it does document.createElement(tag)Creates a new element — not yet in the DOM element.append(node)Adds a node or text string as the last child element.prepend(node)Adds a node or text string as the first child element.remove()Removes the element from the DOM parent.removeChild(child)Removes a specific child node element.cloneNode(deep)Copies the element; deep: true copies children too
Property / Method What it does Notes element.textContentGets or sets all text content Safe — no HTML parsing element.innerHTMLGets or sets HTML string content Parses HTML — avoid with user input element.setAttribute(name, value)Sets an attribute — element.getAttribute(name)Gets an attribute value — element.removeAttribute(name)Removes an attribute — element.datasetObject of all data-* attributes data-user-id → dataset.userId
Method / Property What it does element.classList.add(name)Adds a class element.classList.remove(name)Removes a class element.classList.toggle(name)Adds if absent, removes if present element.classList.contains(name)Returns true if the class is present element.classList.replace(old, new)Swaps one class for another element.style.propertyGets or sets an inline style — use camelCase (backgroundColor)
const btn = document . querySelector ( ' #submit-btn ' );
btn . textContent = ' Save changes ' ;
btn . classList . add ( ' is-loading ' );
const li = document . createElement ( ' li ' );
li . textContent = ' New item ' ;
document . querySelector ( ' ul ' ) . append ( li );
// <div data-tour-id="42" data-price="89">
const card = document . querySelector ( ' [data-tour-id] ' );
console . log ( card . dataset . tourId ); // '42'
console . log ( card . dataset . price ); // '89'
querySelectorAll returns a NodeList — use forEach to iterate, or spread into an array with [...nodeList] to access full array methods.
Attach events with element.addEventListener(type, handler). Remove with element.removeEventListener(type, handler) using the same function reference.
Event Fires when clickUser clicks the element dblclickUser double-clicks the element mouseenterPointer moves onto the element (does not bubble) mouseleavePointer moves off the element (does not bubble) mouseoverPointer moves onto the element or a descendant (bubbles) mouseoutPointer moves off the element or a descendant (bubbles) mousemovePointer moves over the element contextmenuUser right-clicks the element
Event Fires when Useful properties keydownKey is pressed — fires repeatedly while held event.key, event.code, event.ctrlKeykeyupKey is released event.key, event.code
keypress is deprecated — use keydown instead.
Event Fires when submitForm is submitted inputValue changes as the user types changeValue is committed (on blur or selection change) focusElement receives focus blurElement loses focus resetForm is reset
Event Fires when DOMContentLoadedHTML is parsed and the DOM is ready (before images/styles load) loadPage and all resources (images, styles) have loaded resizeBrowser window is resized scrollUser scrolls the page or an element
document . querySelector ( ' #menu-btn ' ) . addEventListener ( ' click ' , () => {
document . querySelector ( ' #nav ' ) . classList . toggle ( ' is-open ' );
// Form submit with preventDefault
document . querySelector ( ' form ' ) . addEventListener ( ' submit ' , ( event ) => {
const name = event . target . elements [ ' name ' ] . value ;
console . log ( ' Submitted: ' , name );
document . addEventListener ( ' keydown ' , ( event ) => {
if ( event . key === ' Escape ' ) {
// Run after DOM is ready
document . addEventListener ( ' DOMContentLoaded ' , () => {
Use mouseenter/mouseleave for hover effects on a single element. Use mouseover/mouseout only when you need to detect movement over child elements too.