Magento 2 Experten — Hyvä Theme, Tailwind CSS & SEO aus einer Hand ›

Variables and Data Types in JavaScript

Variables and Data Types in JavaScript

~12 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026

Now let's create our household budget's first data – along the way, we'll learn how JavaScript declares variables and which data types it comes with built in.

let, const, and var

JavaScript has three keywords for declaring variables. For new code, a clear rule applies:

  • const – the DEFAULT. The identifier can't be reassigned after assignment. ALWAYS use const unless you already know the value needs to change.
  • let – only when reassignment is actually needed (e.g. a loop counter).
  • var – the OLD form, the only one before 2015. Has confusing function-scope behavior instead of block scope (details in chapter 15) and should NOT be used in new code.
const balance = 1500; // fixed - cannot be reassigned
let transactionCount = 0; // will change

transactionCount = 1; // allowed, since 'let'
balance = 2000;    // Error: Assignment to constant variable.

Achtung: const doesn't make the VALUE immutable – only the ASSIGNMENT itself is locked. An object or array declared with const can still be mutated internally (more on this in chapters 10 and 12).

Primitive data types

JavaScript has seven primitive (elementary, not further decomposable) data types. The first three matter most in everyday use:

  • string – text, in single quotes, double quotes, or backticks: 'Rent', "Rent", `Rent`.
  • number – ANY number, integer or decimal – unlike many other languages, JavaScript has NO separate integer type: 42, -850, 19.99.
  • boolean – truth value: true or false.
  • undefined – the automatic value of a declared but not-yet-assigned variable.
  • null – a deliberately set "no value" state, as opposed to undefined.
  • bigint – for integers beyond number's safe limit (123n), rarely needed in practice.
  • symbol – unique, non-colliding values, mostly for internal object keys, also rare in everyday use.

Checking a value's type: typeof

console.log(typeof 'Rent');     // 'string'
console.log(typeof 42);         // 'number'
console.log(typeof true);       // 'boolean'
console.log(typeof undefined);  // 'undefined'
console.log(typeof null);       // 'object' - a historic bug, uncorrectable for decades for compatibility reasons!

Achtung: typeof null returns 'object' – that's WRONG, but a bug from JavaScript's very first release that was never fixed for backwards compatibility. Always check for null explicitly with value === null instead.

undefined vs. null: the difference that's often confused

ValueMeaning
undefinedSet AUTOMATICALLY by JavaScript: a declared but unassigned variable, a non-existent object key, a function without return.
nullSet DELIBERATELY by you to express "intentionally no value" – e.g. a transaction with no assigned category: category: null.

The budget app's first transaction

With this foundation, let's create our project's first piece of data – a single transaction, for now as loose variables (in chapter 12 we'll combine them into an object):

src/index.js
console.log('Household budget app started');

const description = 'January salary';
const amount = 2400;
const isIncome = true;
const note = null; // no note recorded yet

console.log(description, amount, isIncome, note);
console.log(typeof description, typeof amount, typeof isIncome);