Sobes.tech
Middle

Tell about data types in JavaScript (null, undefined, string, number, bigint, object, symbol). How does the typeof operator work? List falsy values.

sobes.tech AI

Answer from AI

JavaScript has several fundamental data types:

  • null — a special value indicating "absence of value";
  • undefined — the value of a variable that has not been assigned a value;
  • string — a sequence of characters;
  • number — a numeric value (integer or floating-point);
  • bigint — integers of arbitrary length;
  • object — objects, arrays, functions, and other complex structures;
  • symbol — unique and immutable identifiers.

The typeof operator returns a string indicating the type of the operand, for example:

typeof 42; // "number"
typeof null; // "object" (language peculiarity)
typeof undefined; // "undefined"
typeof Symbol(); // "symbol"

It is important to remember that typeof null returns "object" due to a historical bug.

Falsy values are values that become false when converted to a boolean. These include:

  • false
  • 0
  • -0
  • 0n (BigInt zero)
  • "" (empty string)
  • null
  • undefined
  • NaN

All other values are considered truthy.