Skip to content
SoftwareSolutions.net
Cheat sheet

JavaScript Basics Syntax Cheat Sheet

JavaScript syntax for variables, functions, arrays, objects and common DOM tasks, with examples for the browser console.

JavaScript runs in every web browser. Open the developer tools with Ctrl+Shift+J (Windows) or Cmd+Option+J (Mac) to try these lines in the console.

Variables and types

  • const name = "Ana"; for values that do not change
  • let count = 0; for values that change
  • typeof x shows the type
  • === compares value and type

Functions and arrays

  • function add(a, b) { return a + b; }
  • const add = (a, b) => a + b; arrow function
  • [1, 2, 3].map(n => n * 2) transforms each item
  • [1, 2, 3].filter(n => n > 1) keeps matching items

Objects and the page

  • const user = { name: "Ana" }; creates an object
  • user.name reads a property
  • document.querySelector("h1") finds an element
  • element.textContent = "New" changes text