Web Development · Module 5 · Lesson 11 min read

Variables & Data Types

JavaScript adds behaviour

HTML is structure, CSS is style, and JavaScript is behaviour: clicks, data, animations, API calls. It runs in every browser, and on servers with Node.js.

Declaring variables

KeywordReassign?ScopeUse
const❌block { }default choice
let✅blockwhen the value changes
var✅functionold style; avoid

const stops reassignment, not mutation: a const array can still be .push()ed.

Types

  • Primitives: string, number (integers and decimals together), boolean, null (deliberately empty), undefined (not set yet), bigint, symbol.
  • Objects: {}, arrays [], functions, dates…
  • typeof "hi" is "string". ⚠️ typeof null is "object", a famous historical bug.

Strings

  • Template literals: `Hello, ${name}!`. Easy interpolation and multi-line text.
  • Useful methods: .length, .toUpperCase(), .includes(), .split(), .trim(), .slice().

Numbers

  • 0.1 + 0.2 is 0.30000000000000004 (floating point), so round money with care.
  • Number("42") and parseInt("42px") convert text; a failed conversion gives NaN.

Naming

Use camelCase (userName, isLoggedIn) and descriptive names.

Example

JavaScript
const name = "Asha";
let score = 10;
score += 5;

const user = { name, score, isAdmin: false };
const tags = ["js", "web"];
tags.push("dev");                 // allowed: mutating, not reassigning

console.log(`Hello, ${name}! Score: ${score}`);   // Hello, Asha! Score: 15
console.log(typeof score, typeof user, typeof null);   // number object object
console.log(0.1 + 0.2);                           // 0.30000000000000004
console.log(Number("42") + 1, Number("abc"));     // 43 NaN
console.log(tags);                                // ["js", "web", "dev"]
// ▶️ Runs in the code editor (JavaScript mode)