DEV Community

Cover image for JavaScript Primitive Data Types Explained for Beginners
Megalraja
Megalraja

Posted on

JavaScript Primitive Data Types Explained for Beginners

While learning JavaScript, one of the first concepts I wanted to understand properly was data types.

JavaScript has several data types, but they can be divided into two main categories:

Primitive data types
Non-primitive data types

In this post, I'm focusing only on primitive data types.

What is a Primitive Data Type?

A primitive data type represents a single, simple value.

JavaScript has 7 primitive data types:

  1. String
  2. Number
  3. BigInt
  4. Boolean
  5. Undefined
  6. Null
  7. Symbol

Let's understand each one.

1. String

A String is used to store text.

let name = "John";
let city = "Chennai";

Strings can use:

"Double quotes"
'Single quotes'
Backticks

Example:

let message = "Hello John";

console.log(message);

Output:

Hello John

Check the type:

console.log(typeof message);

Output:

string
2. Number

The Number type is used for numbers, including integers and decimal values.

let age = 25;
let price = 99.99;
let temperature = -10;

All of these are number.

console.log(typeof age);

Output:

number

JavaScript doesn't have separate int and float types like some other programming languages.

3. BigInt

BigInt is used for very large integers that cannot be safely represented by the normal Number type.

Example:

let bigNumber = 123456789012345678901234567890n;

Notice the n at the end.

console.log(typeof bigNumber);

Output:

bigint

You can think of it as:

Number → normal-sized numbers
BigInt → very large integers

*4. Boolean *

A Boolean can have only two values:

true
false

Example:

`let isLoggedIn = true;
let isAdmin = false;

Booleans are commonly used with conditions.

if (isLoggedIn) {
console.log("Welcome John!");
}

Output:

Welcome John!`

5. Undefined

undefined usually means a variable has been declared but hasn't been given a value.

let username;

console.log(username);

Output:

undefined

Example:

let age;

console.log(typeof age);

Output:

undefined

So a simple way to remember it is:

Declared but no value → undefined

6. Null

null represents an intentional absence of a value.

Example:

let selectedUser = null;

Here, we are intentionally saying that there is currently no selected user.

One interesting JavaScript behavior is:

console.log(typeof null);

Output:

object

This is a well-known historical quirk of JavaScript.

Even though typeof null returns "object", null is considered a primitive value.

7. Symbol

Symbol is used to create unique values.

Example:

let id1 = Symbol("id");
let id2 = Symbol("id");

console.log(id1 === id2);

Output:

false

Even though both symbols have the same description, they are different unique values.

Symbols are mainly useful when unique identifiers are needed.

*Quick Cheat Sheet *
Primitive Type Example
String "John"
Number 25
BigInt 123n
Boolean true
Undefined undefined
Null null
Symbol Symbol("id")
One Important Thing

These 7 types are primitive data types in JavaScript:

String
Number
BigInt
Boolean
Undefined
Null
Symbol

Understanding these basics makes it easier to learn the next JavaScript concepts, such as:

Variables
Operators
Type conversion
Conditions
Functions
Arrays
Objects

I'm learning JavaScript step by step, and understanding the basics properly is helping me build a stronger foundation.

What's the JavaScript data type you found confusing at first?

javascript #webdevelopment #programming #frontend #coding #beginners #learningtocode

Top comments (0)