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
| Keyword | Reassign? | Scope | Use |
|---|---|---|---|
const | ❌ | block { } | default choice |
let | ✅ | block | when the value changes |
var | ✅ | function | old 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 nullis"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.2is0.30000000000000004(floating point), so round money with care.Number("42")andparseInt("42px")convert text; a failed conversion givesNaN.
Naming
Use camelCase (userName, isLoggedIn) and descriptive names.
Example
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)