Log to Console
console.log(value)Prints a value to the browser or Node console.
JavaScript syntax quick reference for common tasks, from MDN Web Docs. Every item below is clearly marked as a shortcut, command, or syntax reference.
console.log(value)Prints a value to the browser or Node console.
const name = "Ada";Declares a variable that can't be reassigned.
function add(a, b) {
return a + b;
}Defines a named function.
const add = (a, b) => a + b;Defines a short function expression.
if (x > 0) {
...
} else {
...
}Runs code depending on a condition.
let count = 0;Declares a block-scoped variable that can change.
for (let i = 0; i < 10; i++) {
...
}Loops a set number of times.
for (const item of items) {
...
}Loops over the values of an array or iterable.
Prints a value to the browser or Node console.
console.log(value)Declares a variable that can't be reassigned.
const name = "Ada";Defines a named function.
function add(a, b) {
return a + b;
}Defines a short function expression.
const add = (a, b) => a + b;Runs code depending on a condition.
if (x > 0) {
...
} else {
...
}Declares a block-scoped variable that can change.
let count = 0;Loops a set number of times.
for (let i = 0; i < 10; i++) {
...
}Loops over the values of an array or iterable.
for (const item of items) {
...
}Creates an ordered list of values.
const nums = [1, 2, 3];Adds items to the end of an array.
nums.push(4);Creates a new array by transforming each item.
nums.map((n) => n * 2)Keeps only items that pass a test.
nums.filter((n) => n > 0)Creates key-value pairs.
const user = { name: "Ada", age: 36 };Inserts values into a string.
`Total: ${total}`Handles an error without crashing.
try {
...
} catch (err) {
console.error(err);
}Waits for a promise inside an async function.
async function load() {
const data = await getData();
}Requests data from a server and parses JSON.
const res = await fetch(url);
const data = await res.json();Picks one of two values in one line.
const label = ok ? "Yes" : "No";Compares value and type without conversion.
a === bReturns the first item that passes a test.
users.find((u) => u.id === id)Checks whether an array contains a value.
nums.includes(3)Returns the number of items in an array or string.
items.lengthPulls properties into variables.
const { name, age } = user;Copies or merges arrays and objects.
const merged = { ...a, ...b };Converts a JSON string to an object.
JSON.parse(text)Converts a value to a JSON string.
JSON.stringify(obj)Imports exports from another module.
import { add } from "./math.js";Finds the first element matching a CSS selector.
document.querySelector(".btn")Runs a function when an event happens.
button.addEventListener("click", handler);Adds single-line or multi-line comments.
// line comment
/* block comment */Repeats while a condition is true.
while (x < 10) {
x++;
}Combines all items into one value.
nums.reduce((sum, n) => sum + n, 0)Reads a nested property without crashing on null.
user?.address?.cityUses a default when a value is null or undefined.
const n = value ?? 0;Defines a class with a constructor.
class Dog {
constructor(name) {
this.name = name;
}
}Makes a value available to other modules.
export function add(a, b) { ... }Runs a function once after milliseconds.
setTimeout(() => { ... }, 1000);Loops over an object's keys.
for (const key in obj) {
...
}Throws an error on purpose.
throw new Error("Something went wrong");It covers 39 common JavaScript patterns and examples, including Log to Console, Declare a Constant, Define a Function, Arrow Function, If / Else.
Yes. Each reference page includes a copyable code example.
Check the linked documentation for language or runtime version requirements. Libraries, file paths, terminal quoting and environment settings can differ across systems; no blanket platform compatibility is implied.