JavaScript Basics (Syntax, Types, Control Flow)

Context: FIT2102_MOC · the imperative half of JavaScript, kept deliberately small so JavaScript Functions as Values can replace most of it · same role for JS as Python Basics (Syntax, Types, Control Flow) plays for Python Problem it solves: bind values, branch, and loop in JavaScript — and know which of those constructs the unit wants you to stop using. Course notes: JavaScript intro.

Quick Revision

  • 🎯 Trigger: any JS snippet ➔ const by default, let only where mutation is genuinely needed, === never ==.
  • ⚡ Key Constraint: const freezes the binding, not the valueconst a = [1,2]; a[0] = 9 is legal; a = [9,2] is not.

🔧 Minimal Working Example

const z = 1;                       // constant (immutable binding) at global scope
 
/**
 * define a function with two parameters, print, and return the result
 */
function myFunction(x, y) {
  let t = x + y;                   // let -> mutable
  t += z;                          // += adds the right-hand expression to t
  const result = t;                // semicolons optional, but catch errors
  console.log("hello world");      // prints to the console
  return result;                   // returns to the caller
}
myFunction(1, 2);                  // logs "hello world", returns 4

Expected output: hello world on the console; return value 4 ().

  • Bindingsconst immutable binding (default choice) · let mutable. Reassigning a const throws a TypeError at run time (Assignment to constant variable.) — not a compile-time or lint-time error, so an untested branch can still ship it. var is the legacy third form: function-scoped, hoisted, silently redeclarable ➔ never use it.
  • Branch, three surfacesif (c) { … } else { … } · early return (if (x >= y) return x; return y;) · conditional expression x >= y ? x : y.
  • Loopswhile (cond) { … } · for (let i = 1; i <= n; i++) { … }. Both are on the unit’s discouraged list ➔ Programming Paradigms.
  • Truthinesswhile (n) terminates at zero because Boolean(0) === false. Concise, and a trap when 0 is a legitimate value.
  • Arraysconst tutors = ['tim', 'michael', 'yan'] · tutors.length · tutors[1] (0-based). The reference is immutable, the referenced array is nottutors[1] = 'mic' succeeds under const.
  • Comments// line · /** … */ JSDoc block above a function (the deck’s house style for documenting parameters and return).

🔀 Operator reference

GroupOperatorsGotcha
arithmetic+ - * / · x % y (modulo)
equalityx == y / x != y loose · x === y / x !== y strictloose comparison may type-convert; use strict whenever the types should already match
logicala && b and · a || b orshort-circuiting
bitwisea & b · a | b⚠ single character — a typo away from the logical form
unaryi++ post-inc · ++i pre-inc · i-- · --i · !x notpost returns the old value then updates; pre updates then returns
in-placex += e · also -= *= /= |= &=x += e adds the result of e to x
ternary<cond> ? <true result> : <false result>an expression, so it can be returned or passed

✍️ Practice

⚠️ Common Mistakes

  • 💡 const on an array or object is not deep immutability ➔ it forbids rebinding the name, not mutating what the name points at. This is the single most-quoted JS surprise.
  • 💡 == where === was meant ➔ loose equality type-converts, so 0 == '0' and '' == 0 are true. Default to ===/!== and only relax it deliberately.
  • 💡 Truthiness guards break on 0while (n) and n ? a : b treat a legitimate 0 as “done”/“false”. Fine for sumTo, wrong for a count that may genuinely be zero.
  • 💡 i++ vs ++i inside an expression ➔ post-increment yields the value before updating; mixing it into an accumulation (sum += n--) is exactly where off-by-one bugs hide.