DEV Community

ABISHEK M
ABISHEK M

Posted on

Primitive Data Types in JavaScript

What is a Data Type?

A data type tells us what kind of value we are working with in a program. It helps JavaScript understand whether a value is text, a number, a true/false value, or something else.

For example:

let name = "Abishek";
let age = 25;
let isStudent = true;
Enter fullscreen mode Exit fullscreen mode

Here, "Abishek" is a String, 25 is a Number, and true is a Boolean.

Types of Data Types in JavaScript

JavaScript data types are mainly divided into two categories:

  1. Primitive Data Types
  2. Non-Primitive Data Types

Primitive data types represent a single value, while non-primitive data types are used to store more complex data.

What are Primitive Data Types?

Primitive data types are the basic data types in JavaScript. They represent a single value and are not objects.

JavaScript has 7 primitive data types:

  • String
  • Number
  • BigInt
  • Boolean
  • Undefined
  • Null
  • Symbol

Primitive data types are used to store simple values such as text, numbers, and true/false values.

Primitive Data Types are Immutable

All primitive data types in JavaScript are immutable. This means their value cannot be changed after it is created.

For example:

let name = "Abishek";

name[0] = "X";

console.log(name);
Enter fullscreen mode Exit fullscreen mode

Output:

Abishek
Enter fullscreen mode Exit fullscreen mode

The string cannot be changed directly. However, we can assign a completely new value to the variable:

let name = "Abishek";

name = "Kumar";

console.log(name);
Enter fullscreen mode Exit fullscreen mode

Here, the original "Abishek" value was not changed. The variable was simply assigned a new value.

1. String

A String is used to store text or a sequence of characters.

let name = "Abishek";
let message = "Hello World";
let msg = `Hi`;
Enter fullscreen mode Exit fullscreen mode

Strings can be written using single quotes, double quotes, or backticks.

2. Number

The Number data type is used to store numerical values, including integers and decimal numbers.

let age = 25;
let price = 99.50;
Enter fullscreen mode Exit fullscreen mode

3. BigInt

BigInt is used to store very large integer values that cannot be safely represented by the normal Number type.

let bigNumber = 12345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

The n at the end indicates that the value is a BigInt.

4. Boolean

A Boolean has only two possible values: true or false. It is commonly used for conditions and decision-making.

let isLoggedIn = true;
let isCompleted = false;
Enter fullscreen mode Exit fullscreen mode

5. Undefined

Undefined means a variable has been declared but no value has been assigned to it.

let username;

console.log(username);
Enter fullscreen mode Exit fullscreen mode

Output:

undefined
Enter fullscreen mode Exit fullscreen mode

6. Null

Null represents the intentional absence of a value. It is used when we want to indicate that a variable currently has no value.

let selectedUser = null;
Enter fullscreen mode Exit fullscreen mode

Here, null means there is currently no selected user.

7. Symbol

Symbol is used to create unique values. It is mainly useful when we need unique property keys in objects.

let id = Symbol("id");
Enter fullscreen mode Exit fullscreen mode

Every Symbol is unique, even if two Symbols have the same description.

Primitive data types are the basic building blocks of JavaScript. JavaScript has seven primitive data types: String, Number, BigInt, Boolean, Undefined, Null, and Symbol.

All primitive values are immutable, which means their values cannot be modified after they are created. Understanding these data types is important because they are used throughout JavaScript programs to store and work with different kinds of values.

Top comments (0)