Middle
What data types are there in JavaScript? What are the major groups and how do they differ?
sobes.tech AI
Answer from AI
In JavaScript, there are two main data types: primitives and objects.
Primitive types:
undefined— the default value for undeclared variables.null— a special value indicating the absence of a value.boolean— logical valuestrueorfalse.number— numbers (integers and floating-point).bigint— integers of arbitrary length.string— sequences of text.symbol— unique and immutable identifiers.
Objects:
- Complex data structures that can contain properties and methods.
- Include arrays, functions, dates, regular expressions, and user-defined objects.
The difference is that primitives are stored and passed by value, while objects are by reference. Primitives are immutable, objects are mutable.
Example:
let a = 5; // number
let b = 'hello'; // string
let c = { name: 'John' }; // object
// Primitives are copied by value
let d = a;
d = 10; // a remains 5
// Objects are copied by reference
let e = c;
e.name = 'Jane'; // c.name also changes to 'Jane'