JavaScript Data Types


JavaScript has 8 Datatypes

1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object

The Object Datatype

The object data type can contain:

1. An object
2. An array
3. A date

Examples

// Numbers:
let length = 16;
let weight = 7.5;

// Strings:
let color = "Yellow";
let lastName = "Johnson";

// Booleans
let x = true;
let y = false;

// Object:
const person = {firstName:"John", lastName:"Doe"};

// Array object:
const cars = ["Saab", "Volvo", "BMW"];

// Date object:
const date = new Date("2022-03-25");

Note : A JavaScript variable can hold any type of data.


The Concept of Data Types

In programming, understanding data types is crucial.

In order to work with variables, it's essential to have some knowledge about their type.

Data types are crucial for a computer to solve this problem securely :

let x = 16 + "Volvo";

Is it logical to include 'Volvo' with the number sixteen? Will this action lead to an error, or will it yield a result?

In JavaScript, the provided code will be understood like this:

let x = "16" + "Volvo";

Note : When adding a number and a string, JavaScript will treat the number as a string.

JavaScript reads expressions in order, starting from the left. Using various orders can lead to distinct outcomes:

In the initial example, JavaScript sees 16 and 4 as numbers, but when it encounters "Volvo", it changes how it understands things.

In the second example, when the initial item is a string, all following items are also considered as strings.


JavaScript Types are Dynamic

JavaScript uses dynamic types, which means a single variable can store various types of data:


JavaScript Strings

A string, also known as a text string, is basically a bunch of characters put together, just like when we say "John Doe."

Text is put inside quotes. You can use either single or double quotes:

You're allowed to include quotation marks within a text, as long as they differ from the ones enclosing the text itself:

You will learn more about strings later in this tutorial.


JavaScript Numbers

All numbers used in JavaScript are kept as decimal numbers (floating point).

You can write numbers in two ways: with decimals or without decimals.


Exponential Notation

You can express really big or really small numbers using scientific (exponential) notation in this way:

Note : Most programming languages have many number types:

Whole numbers (integers):
byte (8-bit), short (16-bit), int (32-bit), long (64-bit)

Real numbers (floating-point):
float (32-bit), double (64-bit).

Javascript numbers are always one type:
double (64-bit floating point).

You will learn more about numbers later in this tutorial.


JavaScript BigInt

JavaScript numbers are kept in a special way using 64 bits, and they are not whole numbers.

JavaScript BigInt is a fresh kind of data (introduced in ES2020) that you can employ to save really large whole numbers, which cannot be held in a typical JavaScript Number.

You will learn more about BigInt later in this tutorial.


JavaScript Booleans

Booleans can only be one of two things: either true or false.

Booleans are commonly employed in conditional checks.

You will learn more about booleans later in this tutorial.


JavaScript Arrays

In HTML, you can create JavaScript arrays using square brackets.

Array items are separated by commas.

The code below makes a list called "cars" with three car names inside it:

In arrays, we start counting from zero. So, the first item is at index [0], the second is at index [1], and it continues like this.

In this tutorial, you'll discover additional information about arrays.


JavaScript Objects

In HTML, when we talk about JavaScript objects, we represent them using curly braces like this: {}.

In HTML, we describe object properties by using name-value pairs separated by commas.

In the previous example, there is an object, which is a person. This person has four things that describe them: their first name, last name, age, and the color of their eyes.

You will learn more about objects later in this tutorial.


The typeof Operator

You can use JavaScript to figure out what type a variable is by using the typeof operator.

The typeof operator tells you what kind of thing a variable or something you're looking at is.

You will learn more about typeof later in this tutorial.


Undefined

In JavaScript, when a variable doesn't have a value assigned to it, it is considered as having the value undefined. This means that the variable doesn't hold any specific data. The type of such a variable is also undefined.

You can make any variable empty by setting its value to undefined. The variable's type will also become undefined.


Empty Values

A value that is empty has no connection to undefined.

An empty string can be seen as both acceptable and possessing a specific category.