Primitive Data Types in JavaScript

Computers can do some incredibly complex tasks, but they all rely on a handful of simple building blocks. In JavaScript, these building blocks are called primitive data types. They are the basic structures that JavaScript uses to represent information.

There are five primitive data types: number, string, boolean, null, and undefined.

Technically, JavaScript also has two additional primitive datatypes that are a bit more advanced and were introduced later: BigInt and Symbol. I've mentioned these for the sake of completeness, but we won't delve into them right now, as they involve more advanced applications.

1. Number

Numbers are the backbone of computation, and JavaScript provides a comprehensive way of handling them.

The number data type in JavaScript encompasses both whole numbers and decimals.

For instance:

let age = 25; // whole number

let temperature = 98.6; // decimal number

Besides regular numbers, there are so-called “special numeric values” which also belong to this data type: Infinity, -Infinity and NaN.

I've explained these special numbers in more detail in another article, but let's touch on them briefly here.

Infinity

When you perform a numeric operation and the result is larger than the largest number that can be represented, JavaScript returns a special value called Infinity.

1 / 0; // Infinity

Number.MAX_VALUE * 2; // Infinity

-Infinity

Similarly, when the absolute value of a negative number is bigger than the largest negative number that JavaScript can handle, JavaScript will return a value of -Infinity.

-1 / 0; // -Infinity

Number.MIN_VALUE * 2; // -Infinity

NaN

It stands for Not a Number. Zero divided by zero does not have a defined value, so JavaScript returns NaN.

This value also arises if you attempt to divide infinity by infinity, take the square root of a negative number, or use arithmetic operators with non-numeric operands that cannot be converted to numbers.

0 / 0; // NaN

Infinity / Infinity; // NaN

Math.sqrt(-1); //NaN

2. String

Strings are used to represent text in JavaScript.

They can be really short, even just one character, or they can be as lengthy as you want them to be. They can include all kinds of characters: letters, numbers, symbols, and spaces. They're super flexible and let you manipulate text in lots of different ways.

They're enclosed within single quotes (' ') or double quotes (" ").

let message = "Hello, world!";

let name = 'Alice';

Remember that you can use any type of quote, but they must match. If you start with a single quote, you must end with a single quote, and if you start with a double quote, you must end with a double quote.

Quotes must match

Quotes must match


At times, you might also need to include a double or single quote mark within a string.

Since strings can be enclosed in either single or double quotes, you can put the whole string in single quotes if you want to use double quotes within it.

Similarly, if you want to use single quotes in the string, you can wrap the string in double quotes.

title = "Molly's Special Offers";

You can also use backticks. They're like advanced quotes that offer extended functionality. With backticks, you can include variables and expressions in a string by enclosing them in ${...}, like this:

let name = "John";

console.log(`Hello, ${name}!`); // Hello, John!

The expression within ${...} gets calculated, and its outcome becomes a portion of the string. We can insert anything there: a variable such as "name" or a math operation like 1 + 2, or even something more complex.

Also, it's important to note that strings should always be written on a single line.

Strings should be written on a single line

Strings should be written on a single line


3. Boolean

Booleans represent two states: true or false. They're used for making decisions and controlling the flow of a program. For example:

let isReady = true;

let isLoggedIn = false;

Booleans are incredibly valuable because they allow your program to perform different actions based on different conditions.

Boolean values also come as a result of comparisons

let isGreater = 4 > 1;

This data type does have two very popular uses:

  • Booleans are used when the value can only be true/false. You could also think of these values as on/off or 0/1: true is like on or 1, while false is like off or 0.
  • Booleans are used when your code can take more than one path. They help to run different codes in different circumstances. The path the code takes depends on a test or condition.

4. The “null” value

It’s just a special value which represents “nothing” or “empty”.

let age = null;

The code above states that age is unknown.

5. The “undefined” value

The meaning of undefined is “value is not assigned”.

If a variable is declared, but not assigned, then its value is undefined:

let age;

console.log(age); // shows "undefined"

Technically, it is possible to explicitly assign undefined to a variable:

let age = 100;

//Change the value to undefined
age = undefined;

console.log(age); // "undefined"

But it's not recommended to do that. Normally, one uses null to assign an “empty” or “unknown” value to a variable, while undefined is reserved as a default initial value for unassigned things.

Summary

We have discussed five primitive data types:

  • Number: For numeric values, including integers and decimals.
  • String: For textual information, enclosed in quotes or backticks.
  • Boolean: For true or false values, essential for decision-making.
  • null: Representing intentional absence of value.
  • undefined: Indicating an unassigned value.

These data types are the basic building blocks that help you represent and manipulate information in your programs. They form the foundation upon which more complex structures and operations are built.

Reinforce what you’re learning

Once you've read the article, you understand how things work, but that's not the same as being able to use it by yourself.

My aim isn't just for you to comprehend what's taught in the article, but for you to feel confident using it on your own once you've grasped it.

To help you do that, I've put together exercises. These will assist you in practicing what you've learned and identifying any areas where you might have some gaps in your understanding.

Solve These Exercises

Get my free, weekly JavaScript tutorials

Want to improve your JavaScript fluency?

Every week, I send a new full-length JavaScript article to thousands of developers. Learn about asynchronous programming, closures, and best practices — as well as general tips for software engineers.

Join today, and level up your JavaScript every Sunday!

Thank you, Taha, for your amazing newsletter. I’m really benefiting from the valuable insights and tips you share.

- Remi Egwuda