Back to all posts

JavaScript Primitive Data Types: A Foundation for Powerful Programming


JavaScript is a dynamically typed (loosely typed) programming language, which means that variables in JavaScript can hold values of different types, and the type of variable can change during the execution of a program. JavaScipt has several built-in data types, each serving a specific purpose.

Data Types Description Example
String represents textual data 'hello', "hello world!" etc
Number an integer or a floating-point number 3, 3.234, 3e-2 etc.
BigInt an integer with arbitrary precision 900719925124740999n , 1n etc.
Boolean Any of two values: true or false true and false
undefined a data type whose variable is not initialized let a;
null denotes a null value let a = null;
Symbol data type whose instances are unique and immutable let value = Symbol('hello');
Object key-value pairs of collection of data let student = { };

Number

Number represents integer and floating numbers (decimals and exponentials). For example,

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5

A number type can also be +Infinity, -Infinity, and NaN (not a number). For example,

const number1 = 3/0;
console.log(number1); // Infinity

const number2 = -3/0;
console.log(number2); // -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // NaN

BigInt

In JavaScript, Number type can only represent numbers less than (253 – 1) and more than -(253 – 1). However, if you need to use a larger number than that, you can use the BigInt data type.

BigInt number is created by appending n to the end of an integer. For example,

// BigInt value
const value1 = 900719925124740998n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // "900719925124740999n"

const value2 = 900719925124740998n;

// Error! BitInt and number cannot be added
const result2 = value2 + 1; 
console.log(result2); 

String

The string data type represents sequences of characters.

In JavaScript, strings are surrounded by quotes:

  • Single quote: 'Hello'
  • Double quote: "Hello"
  • Backticks: `Hello`

For example,

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;

Single quotes and double quotes are practically the same and you can use either of them.

Backticks are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with ${variable or expression} as shown above.

Boolean

The Boolean data type represents logical values and has two possible values: true and false. Booleans are commonly used in conditional statements and logical operations to control the flow of a program.

let isLogged = true;
let hasPermission = false;

Null

In JavaScript, null ia a special value that represents empty or unknown value. for example,

let user = null;

Undefined

The undefined data type represents value that is not assigned. If a variable is declared but that value is not assigned, then the value of that variable will be undefined .

For example,

let name;
console.log(name); // undefined

It is also possible to explicitly assign a variable value undefined. For example,

let name = undefined;
console.log(name); // undefined

Note: It is recommended not to explicitly assign undefined to a variable. Usually, null is used to assign ‘unknown’ or ’empty’ value to a variable.

Symbol

This data type was introduced in a newer version of JavaScript (from ES2015).

A value having the data type symbol can be referenced to as a symbol value. symbol is an immutable primitive value that is unique. For example,

// two symbols with the same description

const value1 = Symbol('hello');
const value2 = Symbol('hello');

Though value1 and value2 both contain 'hello', they are different as they are of the Symbol type.