JavaScript data types

Data types

Primitive and non-primitive data types

Dynamically typed language

JavaScript is a dynamically typed language, which means variables can hold values of different types. In JavaScript, there are several built-in data types that you can use to store and manipulate different kinds of data.

Primitive data types

Primitive data types are basic data types that are not objects and do not have any methods associated with them. Here are the commonly used primitive data types in JavaScript:

String
A string represents a sequence of characters enclosed in single or double quotes.
var greeting = 'Hello!';
Learn more about strings on MDN
Number
A number represents numeric values, including integers and floating-point numbers.
var count = 10;
var pi = 3.14;
Learn more about numbers on MDN
Boolean
A boolean represents either true or false.
var isTrue = true;
var isFalse = false;
Learn more about booleans on MDN
Null
Null represents the intentional absence of any object value.
var person = null;
Learn more about null on MDN
Undefined
Undefined represents an uninitialized variable or a missing property of an object.
var x;
var obj = {};
Learn more about undefined on MDN
Symbol
Symbol represents a unique identifier. Introduced in ECMAScript 6.
var key = Symbol('unique key');
Learn more about symbols on MDN

Non-Primitive Data Types

Non-primitive data types are objects and have properties and methods associated with them. Here are some commonly used non-primitive data types in JavaScript:

  • Object
    Represents a collection of key-value pairs.
  • Array
    Represents an ordered collection of elements.
  • Function
    Represents a reusable block of code.
  • Date
    Represents a specific moment in time.
  • RegExp
    Represents a regular expression pattern.

These non-primitive data types can store and manipulate more complex data structures and perform specific operations.

Choose the appropriate data type based on the requirements

Understanding the different data types in JavaScript is essential for writing efficient and reliable code. Each data type has its own characteristics and usage, so it's important to choose the appropriate data type based on the requirements of your program.