JavaScript22 min readBeginner

Variables, Types & Operators

let, const, primitives, operators, and the famous JavaScript type-coercion rules.

Declaring variables: const, let, var

JavaScript has three keywords for declaring variables, but in modern code you only ever use two of them.

  • `const` — declare a variable that won't be REASSIGNED. This is the default. Use it for almost everything.
  • `let` — declare a variable that you'll reassign later (e.g. inside a loop, or a counter that changes).
  • `var` — the old way. Has weird scoping rules (function-scoped, hoisted). Avoid it in new code.
javascript
const name = "Ada";
// name = "Bob"; // ERROR: Assignment to constant variable

let age = 36;
age += 1;          // fine — let allows reassignment
console.log(`${name} is ${age}`);

Note: `const` prevents REASSIGNMENT, not mutation. `const list = [1, 2, 3]; list.push(4);` works fine — you didn't reassign `list`, you mutated the array it points to.

📦
Real-life analogy — const ≠ frozen — it's just a glued-down label
`const` glues the variable name to the value, so you can't change WHICH value the name points to. But if the value is a box (object/array), you can still rearrange the box's INSIDE. To truly freeze, use Object.freeze().
After: const name; let age; const tags = [...]
name
"Ada"
age
37
tags
["js", "ts"]
Each variable name (left) is a label that POINTS TO a value stored in memory (right). Reassigning a variable doesn't move the value — it just re-points the label.

Primitive types

JavaScript has 7 primitive types — values that aren't objects:

  • `string` — text. Use single, double, or backtick quotes. Backticks support interpolation.
  • `number` — both integers and floats live here. Watch out: 0.1 + 0.2 !== 0.3 (floating-point math).
  • `boolean` — true or false.
  • `null` — explicit "no value".
  • `undefined` — "this hasn't been set yet". Default for uninitialized variables and missing object properties.
  • `bigint` — integers larger than what `number` can hold safely. Written like `9007199254740993n`.
  • `symbol` — unique, opaque values. Mostly used as object keys; you'll rarely need them directly.
javascript
const a = "ada";
const b = 36;
const c = true;
const d = null;
let   e;            // undefined
const f = 9007199254740993n;

console.log(typeof a, typeof b, typeof c, typeof d, typeof e, typeof f);

Curiously, `typeof null` returns `"object"`. That's a famous bug from the language's first days that can never be fixed without breaking the web. Just remember it.

Operators

javascript
// Arithmetic
console.log(7 + 3, 7 - 3, 7 * 3, 7 / 3, 7 % 3, 2 ** 10);

// String concatenation
console.log("hello" + " " + "world");

// Comparison
console.log(5 > 3);
console.log("a" < "b");

// Logical
console.log(true && false);   // AND
console.log(true || false);   // OR
console.log(!true);           // NOT

== vs === — the most important rule in JavaScript

JavaScript has two equality operators. `==` does "loose" equality, which converts the operands to a common type before comparing. `===` does "strict" equality, which compares without conversion.

javascript
console.log("5" == 5);    // true  (string coerced to number)
console.log("5" === 5);   // false (different types)
console.log(0 == false);  // true  (both coerced)
console.log("" == 0);     // true  (both coerced to 0)
console.log("" == false); // true (lol)
⚠ Watch out
Use === and !== always. The coercion rules of == are full of surprises and have caused untold numbers of bugs. There is no situation where you genuinely want loose equality — except `x == null`, which checks for both null and undefined in one go.

Truthy and falsy

When a non-boolean is used in a boolean context (like `if (x)`), it gets coerced to true or false. The values that coerce to false — the FALSY values — are: `false`, `0`, `0n`, `""`, `null`, `undefined`, and `NaN`. Everything else is truthy, including `"0"`, `"false"`, `[]`, and `{}`.

javascript
if ([]) console.log("an empty array is truthy!");
if ({}) console.log("an empty object is truthy!");
if (!"") console.log("an empty string is falsy");

Template literals

Backtick-quoted strings let you interpolate expressions with `${...}` and span multiple lines. Use them whenever you'd otherwise be doing `"hello " + name + "!"`.

javascript
const user = "Ada";
const greeting = `Hello, ${user.toUpperCase()}!
Welcome back.`;
console.log(greeting);