Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

JavaScript is a programming language used to make web pages interactive and to build many kinds of apps. This cheat sheet covers the essentials students need when reading, writing, and debugging beginner JavaScript code. It gives quick reminders for syntax, common patterns, and rules that are easy to forget.

Students can use it as a printable reference while practicing coding problems or building small projects.

The core ideas include storing values in variables, choosing the right data type, and using functions to organize reusable code. Conditionals let a program make decisions, while loops repeat actions efficiently. Arrays and objects store groups of data, and DOM methods let JavaScript change content on a web page.

Careful attention to braces, parentheses, equality checks, and variable scope helps prevent many common errors.

Key Facts

  • Use let for variables that can change, const for variables that should not be reassigned, and avoid var in modern JavaScript.
  • Common data types include string, number, boolean, undefined, null, object, and array.
  • A function can be declared as function add(a, b) { return a + b; } and called with add(3, 4).
  • Use if (condition) { code } else { otherCode } to run different code based on a true or false condition.
  • A for loop often uses the pattern for (let i = 0; i < count; i++) { code } to repeat code a set number of times.
  • Arrays use zero-based indexing, so the first item in colors is colors[0], not colors[1].
  • Objects store key-value pairs, such as let user = { name: 'Ava', score: 10 }; and user.score gives 10.
  • Use document.querySelector('#id') to select an element and element.textContent = 'Hello' to change its displayed text.

Vocabulary

Variable
A named storage location that holds a value your program can use or update.
Data type
A category of value, such as number, string, boolean, array, object, null, or undefined.
Function
A reusable block of code that can take inputs, perform steps, and return a result.
Conditional
A control structure that runs code only when a condition is true or chooses between different code paths.
Loop
A structure that repeats a block of code while a condition is true or for a set number of times.
DOM
The Document Object Model, which represents a web page so JavaScript can read and change its elements.

Common Mistakes to Avoid

  • Using = instead of === in a condition is wrong because = assigns a value, while === compares both value and type.
  • Forgetting that arrays start at index 0 is wrong because array[1] gives the second item, not the first item.
  • Leaving out braces, parentheses, or semicolons carelessly can cause syntax errors or make code run in an unexpected way.
  • Changing a const variable is wrong because const prevents reassignment after the variable is created.
  • Using a variable outside its scope is wrong because variables declared inside a block or function may not exist elsewhere.

Practice Questions

  1. 1 What value is stored in total after this code runs: let total = 5; total = total + 7;
  2. 2 An array is defined as let scores = [82, 91, 77, 88]; what is the value of scores[2]?
  3. 3 Write a function named doubleNumber that takes one number and returns the number multiplied by 2.
  4. 4 Explain why a programmer might use a function instead of copying and pasting the same code many times.