In modern JavaScript variables are declared with let (the value can be changed) and const (a constant — cannot be reassigned). The type is determined by the value and checked with typeof.
- Store a value under a clear name and reuse it.
- Fix immutable constants, protecting them from being overwritten.
let score = 10
score = 25
console.log(score)Output:
25let allows changing the value: the variable keeps the last assigned one.
const age = 21
console.log(typeof age)Output:
numbertypeof returns the name of the value type; for numbers it is the string "number".
Key rules:
- ✓let — the value changes; const — a constant, reassignment is forbidden.
- ✓The type is determined by the value and checked with the typeof operator.
Common mistakes:
- !Reassigning const throws a TypeError.
- !typeof null returns "object" — a historical quirk.
Template strings in backtick quotes inject values via a dollar sign and braces. Strings are immutable: methods (toUpperCase, split) return new values without changing the original string.
- Build strings with value injection instead of concatenation with +.
- Split a string into an array of parts with split.
const lang = "JavaScript"
console.log(`I am learning ${lang}!`)Output:
I am learning JavaScript!Inside the braces after the dollar sign the variable value is injected, producing a single string.
console.log("a,b,c".split(","))Output:
[ 'a', 'b', 'c' ]split divides the string by a separator (a comma) and returns an array of parts.
console.log("javascript".toUpperCase())Output:
JAVASCRIPTThe toUpperCase method returns a new string without changing the original.
Key rules:
- ✓Template strings use backtick quotes and inject values with a dollar sign and braces.
- ✓Strings are immutable: methods return new values.
Common mistakes:
- !split returns an array, not a new string.
Conversion can be explicit (Number("42")) or implicit: if a string takes part in +, concatenation happens. The empty string "", 0, null, undefined and NaN are “falsy”, while an empty array [] is truthy.
- Turn values into the needed type explicitly with Number, String, Boolean.
- Understand when + adds numbers and when it glues strings together.
console.log(Number("42") + 1)Output:
43Number("42") turns the string into the number 42, then 42 + 1 = 43.
console.log("2" + 2)Output:
22When a string takes part in +, string gluing happens: "2" + 2 → "22".
console.log(Boolean(""))
console.log(Boolean([]))Output:
false
trueThe empty string is falsy (false), while the empty array is truthy (true).
Key rules:
- ✓Explicit conversion: Number(), String(), Boolean().
- ✓falsy: "", 0, null, undefined, NaN; the array [] is truthy.
Common mistakes:
- !"2" + 2 gives "22", while 2 + 2 is 4: watch the operand types.
=== compares both value and type (no coercion), while == coerces types: so "2" == 2 is true, but "2" === 2 is false. The ternary condition ? a : b returns one of two values, ?? takes the right side only on null/undefined, and ?. safely accesses nested fields.
- Compare values reliably without unexpected type coercion.
- Express a condition briefly (ternary) and set a default value (??).
console.log("2" == 2)Output:
true== coerces types, so it considers the string "2" and the number 2 equal.
console.log("2" === 2)Output:
false=== compares type and value: the number 2 is not equal to the string "2".
const age = 20
console.log(age >= 18 ? "Adult" : "Young")Output:
AdultIf the condition is true, the value before the colon is taken, otherwise the one after.
let nickname = null
console.log(nickname ?? "Guest")Output:
Guest?? takes the right side when the left one is null or undefined.
Key rules:
- ✓Prefer ===; == coerces types and can be tricky.
- ✓A ternary is a shortened if/else; ?? — a default value for null/undefined.
Common mistakes:
- !?? cannot be mixed with || without parentheses — that is a syntax error.
An arrow function (a, b) => a + b returns the expression automatically; with curly braces {} an explicit return is needed. The rest parameter ...rest collects all remaining arguments into an array.
- Write short functions and pass them into array methods.
- Collect an arbitrary number of arguments via rest.
const add = (a, b) => a + b
console.log(add(2, 3))Output:
5A one-line body without {} returns the expression automatically: 2 + 3 = 5.
const f = (a, ...rest) => rest
console.log(f(1, 2, 3))Output:
[ 2, 3 ]The first argument (1) goes into a, the rest into the array rest → [2, 3].
Key rules:
- ✓Without {} an arrow function returns the expression automatically.
- ✓...rest collects the remaining arguments into an array.
Common mistakes:
- !With braces {} you must write return, otherwise undefined is returned.
An array is an ordered collection of values. push adds to the end, length is the size, includes — a presence check, map — a new array of the same length, filter — a filtered array, and the spread ... copies elements into a new array.
- Store and iterate over sets of same-typed data.
- Transform collections: map — per element, filter — per condition, without mutating the source array.
const nums = [1, 2, 3]
console.log(nums.map(n => n * 2))Output:
[ 2, 4, 6 ]map calls the function for each element and returns a new array with the results.
console.log([1, 2, 3, 4].filter(n => n % 2 === 0))Output:
[ 2, 4 ]filter keeps only the elements for which the condition is true (even ones).
const a = [1, 2]
const b = [...a, 3]
console.log(b)Output:
[ 1, 2, 3 ]...a spreads the array into elements, to which 3 is added.
Key rules:
- ✓push — to the end, length — the size, includes — presence.
- ✓map and filter do not change the source array; push does.
Common mistakes:
- !map returns a new array of the same length as the source.
A classic for goes by index, for...of goes by array values, and forEach is an array method for simply calling a function over each element. while repeats while the condition is true.
- Repeat an action a fixed number of times (for) or while a condition holds (while).
- Simply iterate over array values with for...of and forEach.
let sum = 0
for (let i = 1; i <= 3; i++) {
sum += i
}
console.log(sum)Output:
61, 2, 3 are added to sum in turn → 6.
let s = ""
for (const n of [3, 1, 2]) s += n
console.log(s)Output:
312for...of takes each value in turn: 3, then 1, then 2 → "312".
[1, 2, 3].forEach(n => console.log(n))Output:
1
2
3forEach calls the function for each element of the array.
Key rules:
- ✓for...of — by values, preferable to a simple for for beginners.
- ✓forEach — a simple iteration; use map when you need to transform and return a new array.
Common mistakes:
- !for...of gives no index; if you need one — use for or array.entries().
An object stores key: value pairs. Properties are accessed with a dot (user.name) or with brackets (user["name"]). Destructuring const { name } = user “pulls” a property into a variable.
- Group related data under one name.
- Quickly extract the needed fields via destructuring.
const user = { name: "Anna", age: 25 }
console.log(user.name)Output:
Annauser.name reads the name property of the object → "Anna".
const obj = { x: 5, y: 9 }
const { x } = obj
console.log(x)Output:
5const { x } = obj puts the value of obj.x into the variable x.
Key rules:
- ✓Access properties with a dot or brackets.
- ✓const { name } = obj — destructuring, extracts a property into a variable.
Common mistakes:
- !If a property is absent, access returns undefined (not an error).