JavaScript

Wat zijn JavaScript data types?

JavaScript heeft 7 primitieve types en objects.

Home/Categorieën/JavaScript/Wat zijn JavaScript data types?

Primitive types (7)

number - 42, 3.14, Infinity, NaN string - 'hello', "world", `backticks` boolean - true, false undefined - variabele niet gedefinieerd null - intentional absence of value BigInt - 123n (hele getallen, willekeurig groot) Symbol - unique identifiers

Objects

Object, Array, Function, Date, RegExp, etc. Alles wat niet primitive is een Object (functie van typeof).

typeof operator

typeof geeft type van variabele. Aanvaard: typeof undefined === 'undefined' maar typeof null === 'object' (historic bug!)

Code Voorbeelden

JAVASCRIPTData types voorbeelden
// Primitives
const num = 42;
const str = "Hello";
const bool = true;
const nothing = undefined;
const empty = null;
const big = 123n;

// Objects
const obj = { name: "Jan" };
const arr = [1, 2, 3];
const func = () => {};
const date = new Date();

// typeof
console.log(typeof num);      // "number"
console.log(typeof str);      // "string"
console.log(typeof bool);     // "boolean"
console.log(typeof nothing);  // "undefined"
console.log(typeof empty);    // "object" (bug!)
console.log(typeof big);      // "bigint"
console.log(typeof obj);      // "object"
console.log(typeof arr);      // "object" (arrays zijn objects!)
console.log(typeof func);     // "function"

Relevante trefwoorden

data typetypeofprimitiveobject