JavaScript chapters

Chapter 16 of 17

JavaScript Syntax Cheatsheet

Try this in our JavaScript Compiler →

Everything in one place

Use this chapter as a quick reference once you've worked through the rest of the course. It collects every core construct of JavaScript into runnable, copy-pasteable snippets you can scan whenever you forget the exact syntax.

Hello world & variables

```javascript
console.log("Hello, World!");

let age = 25; const name = "Aman"; let isStudent = true; console.log(name, age, isStudent); ```

Data types & operators

```javascript
let n = 42;          // number
let s = "hello";     // string
let b = true;        // boolean
let arr = [1, 2, 3]; // array (object)
let obj = { x: 1 };  // object
let nothing = null;  // null

let a = 10, b = 3; console.log(a + b, a - b, a * b, a / b, a % b); console.log(a > b, a === b, a !== b); console.log(a > 5 && b < 5); ```

Conditionals & loops

```javascript
const score = 72;
if (score >= 90) {
  console.log("A");
} else if (score >= 60) {
  console.log("Pass");
} else {
  console.log("Fail");
}

for (let i = 0; i < 5; i++) { console.log("i =", i); }

let n = 0; while (n < 3) { n++; } ```

Functions & arrays

```javascript
function add(a, b) {
  return a + b;
}
const square = x => x * x;
console.log(add(2, 3), square(5));

const nums = [10, 20, 30]; nums.push(40); const doubled = nums.map(n => n * 2); const sum = nums.reduce((t, n) => t + n, 0); console.log(doubled, sum); ```

Strings & objects

```javascript
const name = "PlayLearn";
console.log(name.length);
console.log(name.toUpperCase());
console.log(name.includes("Learn"));
const greet = `Hello, ${name}!`;

const user = { name: "Aman", age: 20, greet() { return "Hi " + this.name; } }; console.log(user.greet()); ```

Errors, modules & I/O

```javascript
try {
  const data = JSON.parse("not json");
} catch (err) {
  console.error("Parse failed:", err.message);
}

// math.js export function add(a, b) { return a + b; }

// main.js import { add } from "./math.js"; console.log(add(2, 3));

// Browser const name = prompt("Your name?"); alert("Hello, " + name);

// Node.js import fs from "node:fs"; const text = fs.readFileSync("notes.txt", "utf8"); ```

How to use this page

Bookmark it. When you start a new project, open this cheatsheet alongside your editor and use it as a syntax lookup. Once a construct becomes second nature you'll stop needing the reference for it — and that's exactly when you know you've mastered that part of JavaScript.

Try it yourself

JavaScript Compiler
Output
Code runs in a sandboxed preview inside your browser — your code is saved locally for next time.