AnyCompiler

Variables and Data Types

A variable is a name that points to a value. JavaScript has three ways to declare one — let, const, and the older var — and which one you choose actually matters.

let name = "Alice";
const age = 30;
let isStudent = false;

console.log(name, typeof name);
console.log(age, typeof age);
console.log(isStudent, typeof isStudent);
Output
Alice string
30 number
false boolean

let vs. const vs. var

  • const creates a variable that can't be reassigned — use this by default
  • let creates a variable that can be reassigned — use this only when the value genuinely needs to change
  • var is the old way to declare variables, from before let/const existed — avoid it in new code, it has confusing scoping rules

The core data types

typeof tells you what type a value actually is. JavaScript's core primitive types are string (text), number (both whole numbers and decimals — JavaScript doesn't separate them like Python's int/float), boolean (true or false), undefined (a variable that's been declared but not given a value), and null (an intentional absence of value).

let score;
console.log(score, typeof score);

let empty = null;
console.log(empty, typeof empty);
Output
undefined undefined
null object

Notice typeof null returns "object" — this is a famous, long-standing quirk in JavaScript (kept for backward compatibility), not a sign that null is actually an object. Treat it as a special value meaning "intentionally nothing."

Next: numbers — the arithmetic operators and how JavaScript's single number type actually works.

Practice exercise

Create a const named language set to "JavaScript" and a const named version set to the number 6. Print them together with console.log(language, version).