What are data types in JavaScript

In JavaScript, data types are categorized to define the type of data that can be stored and manipulated within the language. Understanding these data types is essential for effective programming in JavaScript.

JavaScript has several built-in data types:

  • Number: Represents both integer and floating-point numbers.
  • String: Represents a sequence of characters, enclosed in quotes.
  • Boolean: Represents a logical entity and can have two values: true or false.
  • Undefined: A variable that has been declared but has not yet been assigned a value.
  • Null: Represents the intentional absence of any object value.
  • Object: A collection of properties, where each property is defined as a key-value pair.
  • Symbol: A unique and immutable primitive value, often used as object property keys.
  • BigInt: A numeric type that can represent whole numbers larger than 253 - 1.

Here’s a simple example illustrating different data types:

// Defining variables of different data types let numberVar = 42; // Number let stringVar = "Hello, World!"; // String let booleanVar = true; // Boolean let undefinedVar; // Undefined let nullVar = null; // Null let objectVar = { // Object name: "John", age: 30 }; let symbolVar = Symbol('sym'); // Symbol let bigIntVar = BigInt(9007199254740991); // BigInt console.log(numberVar); console.log(stringVar); console.log(booleanVar); console.log(undefinedVar); console.log(nullVar); console.log(objectVar); console.log(symbolVar); console.log(bigIntVar);

JavaScript data types Number String Boolean Undefined Null Object Symbol BigInt