Skip to main content

Primitive Types

In JavaScript, a primitive type is a data type that represents a single value. We have talked about the six primitive data types in JavaScript in the last section. In this section, we will be talking about the primitive types in more detail.

Number

The Number data type represents numeric values. It can be an integer or a floating-point number. Here are some examples of numbers:

let age = 30; // Integer
let price = 9.99; // Floating-point number

You can perform arithmetic operations on numbers, such as addition, subtraction, multiplication, and division. Here is an example:

operations.js
let x = 10;
let y = 5;
let sum = x + y; // Addition
let difference = x - y; // Subtraction
let product = x * y; // Multiplication
let quotient = x / y; // Division

String

The String data type represents textual data enclosed in single or double quotes. Here are some examples of strings:

let name = 'John Doe';
let message = "Hello, World!";

You can concatenate strings using the + operator. Here is an example:

concatenation.js
let firstName = 'John';
let lastName = 'Doe';
let fullName = firstName + ' ' + lastName;

You can also use template literals to interpolate variables in strings. Here is an example:

template-literals.js
let name = 'John Doe';
let greeting = `Hello, ${name}!`;

Even more you can perform indexing and slicing on strings. Here is an example:

indexing.js
let message = 'Hello, World!';
let firstCharacter = message[0]; // H
let lastCharacter = message[message.length - 1]; // !
let substring = message.slice(7); // World!

Since we can think of strings as an array of characters, we can use the array notation to get the characters of a string like this:

array-notation.js
let message = 'Hello, World!';
let firstCharacter = message[0]; // H
let lastCharacter = message[message.length - 1]; // !

Boolean

The Boolean data type represents logical values. It can have two values: true or false. Here are some examples of booleans:

let isTrue = true;
let isFalse = false;

You can use comparison operators to compare values and get a boolean result. Here is an example:

comparison.js
let x = 10;
let y = 5;
let isEqual = x === y; // false
let isGreater = x > y; // true

Undefined

The Undefined data type represents an undefined value. It is the default value of a variable that has not been assigned a value. Here is an example:

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

Null

The Null data type represents a null value. It is used to represent the absence of a value. Here is an example:

let x = null;
console.log(x); // null

Null is the only data type in JavaScript that has only one value: null. Typically, you would use null to represent an empty value or to reset a variable. Typeof null returns object, which is a bug in JavaScript.

Symbol

The Symbol data type represents a unique value. It was introduced in ECMAScript 6 (ES6) and is used to create unique identifiers for object properties. Here is an example:

let id = Symbol('id');

Symbols are unique and immutable, meaning that they cannot be changed once they are created. They are often used as property keys in objects to prevent naming conflicts.

In the next section, we will talk about the non-primitive data types in JavaScript. Let's keep going 🚀

Made with ❤️ by Fasakin Henry