DEV Community

Cover image for Datatypes in Js
Mahalakshmi C
Mahalakshmi C

Posted on

Datatypes in Js

Data Types in JavaScript

Data types in JavaScript define the type of value that a variable can store. JavaScript is a dynamically typed language, which means we do not need to specify the data type while declaring a variable. JavaScript automatically determines the data type based on the value assigned to the variable.

JavaScript data types are mainly divided into Primitive Data Types and Non-Primitive Data Types.

Primitive Data Types in JavaScript

  • Primitive data types are the basic data types in JavaScript that represent a single value.
  • Primitive values are immutable, which means their actual value cannot be changed after it is created. If we try to change a primitive value, JavaScript creates a new value instead of modifying the original value.

JavaScript has seven primitive data types:

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

1. String

A String is used to store text or a sequence of characters. Strings can be written using single quotes, double quotes, or backticks.

let name = "John";

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

Output:

John
Enter fullscreen mode Exit fullscreen mode

2. Number

The Number data type is used to store numeric values, including integers and decimal (floating-point) numbers.

let age = 25;
let price = 99.50;

console.log(age);
console.log(price);
Enter fullscreen mode Exit fullscreen mode

Output:

25
99.5
Enter fullscreen mode Exit fullscreen mode

3. BigInt

BigInt is used to store very large integer values that cannot be safely represented by the regular Number data type. A BigInt value is created by adding n at the end of the number.

let bigNumber = 123456789012345678901234567890n;

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

Output:

123456789012345678901234567890n
Enter fullscreen mode Exit fullscreen mode

4. Boolean

The Boolean data type has only two possible values: true and false.

let isLoggedIn = true;

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

Output:

true
Enter fullscreen mode Exit fullscreen mode

5. Undefined

A variable has the value undefined when it has been declared but no value has been assigned to it.

let name;

console.log(name);
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 the programmer explicitly wants to indicate that a variable has no value.

let selectedUser = null;

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

Output:

null
Enter fullscreen mode Exit fullscreen mode

7. Symbol

Symbol is used to create unique values.

Primitive Data Types Summary

Data Type Example Description
String "Hello" Represents text
Number 25, 10.5 Represents numbers
BigInt 123n Represents very large integers
Boolean true, false Represents true or false
Undefined undefined Variable declared without a value
Null null Represents intentional absence of a value
Symbol Symbol("id") Represents a unique value

Top comments (0)