javascript
  1. javascript-data-types

JavaScript Data Types

JavaScript is a dynamic, loosely-typed programming language that supports various data types. Data types are used to represent various values in a program. There are mainly two types of data types in JavaScript:

  1. Primitive Data Types
  2. Reference Data Types

We will discuss both types in detail below.

Primitive Data Types

Primitive data types are basic data types that are not objects and do not have methods. Primitive data types are immutable, meaning they cannot be modified.

Syntax

The six primitive data types are:

  • String: represents a sequence of characters. Example: "Hello, World!"
  • Number: represents a numerical value. Example: 42
  • Boolean: represents true or false values. Example: true
  • undefined: represents an undefined value. Example: let x;
  • null: represents a null value. Example: let y = null;
  • Symbol: represents a unique identifier. Example: const mySymbol = Symbol();

Example

let name = "John";
let age = 30;
let isStudent = true;
let employeeID; // undefined
let totalAmount = null;
let mySymbol = Symbol("mySymbol");

Output

name // John
age // 30
isStudent // true
employeeID // undefined
totalAmount // null
mySymbol // Symbol(mySymbol)

Explanation

  • Strings are usually surrounded by quotes (single or double)
  • Numbers can be integers or floating-point numbers
  • Booleans are either true or false
  • undefined is used to declare a variable without assigning a value
  • null represents an intentional absence of any object
  • Symbol values are created by calling the Symbol() function. A symbol value can be used as an object property.

Use

Primitive data types are used to represent simple values in a program. They are used in almost all JavaScript programs.

Important Points

  • Primitive data types are immutable and cannot be modified.
  • Assigning a new value to a primitive data type replaces the original value.
  • null and undefined are similar but have different meanings: undefined means a variable has been declared but has not been assigned a value, while null means a variable has been assigned the value null.

Reference Data Types

Reference data types are data types that are objects. Objects in JavaScript are key-value pairs. Reference data types are mutable, meaning they can be modified.

Syntax

Some common reference data types in JavaScript are:

  • Array: represents a collection of values. Example: const myArray = ["apple", "orange", "banana"]
  • Object: represents a collection of properties. Example: const person = {name: "John", age: 30}
  • Function: represents a function. Example: function add(a, b) { return a + b; }

Example

const myArray = ["apple", "orange", "banana"];
const person = {name: "John", age: 30};
function add(a, b) { return a + b; }

Output

myArray // ["apple", "orange", "banana"]
person // {name: "John", age: 30}
add // function add(a, b) { return a + b; }

Explanation

  • Arrays are ordered collections of data, where each element can be of any type
  • Objects are collections of key-value pairs, where the keys are strings and the values can be of any type
  • Functions are objects that can be called and can return a value

Use

Reference data types are used to represent complex values in a program.

Important Points

  • Reference data types are mutable and can be modified.
  • Objects and arrays are referenced values. When assigned to a variable, the variable points to the memory location of the object or array, not to the object or array itself. Therefore, changing the variable also changes the original object or array.
  • Functions are first-class objects in JavaScript and can be passed as arguments and returned as values.
Published on: