Support Online
Skip to main content

Data Types in JavaScript

Introduction

Data types are used in programming languages to classify a particular type of data.
For example, a number and a string are different data types and are handled differently by JavaScript.

The type of data you use is important because it determines what values ​​you can assign to it and what you can do with it.
So, in order to manipulate variables in JavaScript, it is very important to understand the data type of each variable.

In this guide, we will examine how data types work in JavaScript and the important data types found in the language itself.
This will not be a comprehensive review of data types; but it will help you become familiar with the options you will encounter in JavaScript.

Dynamic Types

Data types in JavaScript are dynamic.
In other words, type checking is done at runtime, not at compile time.
Python's data types are likewise dynamic.

In dynamically typed languages, a variable with the same name can be used to hold different data types.

For example, the variable t defined with the let keyword (note: let limits the variable to a specific scope) can be assigned to hold different data types.
It can even be just defined and left without being valued.

let t = 16; // t bir number (sayı)
let t = "Tarık"; // t bir string (metin)
let t = true; // t bir Boolean (mantıksal değer)
let t; // t undefined (tanımlı ama değeri yok)

As seen in the example above, the variable t can be set to any data type available in JavaScript.
The data type does not need to be explicitly specified before use.

Numbers

There is only one number type in JavaScript.
There is no separate type for integers and floating-point numbers.

Therefore, numbers can be written with or without decimal places in JavaScript:

let num1 = 53;
let num2 = 53.00;

In both cases, the data type is number and it does not matter whether the number contains decimal places or not.

In JavaScript, exponential notation can be used to abbreviate very large or very small numbers.
As in the examples below:

let num3 = 647e8; // 64700000000
let num4 = 647e-8; // 0.00000647

In JavaScript, numbers are considered exact (up to 15 digits).
16. After the digit the numbers start rounding:

let num5 = 999999999999999; // 999999999999999 olarak kalır
let num6 = 9999999999999999; // 10000000000000000 olarak yuvarlanır

In addition to representing numbers, JavaScript's number type also has three special symbolic values:

  • Infinity — Numeric value representing positive infinity
  • -Infinity — Numeric value representing negative infinity
  • NaN — Abbreviation for “Not a Number”; that is, it represents a value that is not a number

In operations that go beyond the maximum range of numbers that can be used in JavaScript, the result is returned as Infinity or -Infinity.
These values ​​may also arise from undefined operations (for example, division by zero).

let num7 = 5 / 0; // Infinity döner
let num8 = -5 / 0; // -Infinity döner

Technically, when a number exceeds 1.797693134862315E+308 (the upper limit in JavaScript), the result is Infinity.

Similarly, when the number falls below -1.797693134862316E+308, the result is -Infinity.

Infinity value can also be used in loops:

while (num9 != Infinity) {
// Buradaki kod num9 = Infinity olana kadar çalışır
}

NaN is displayed for values that are not a valid (legal) number.
If you try to perform a mathematical operation on a number and a non-numeric value, the result returns NaN.

Like the example below:

let x = 20 / "Köpekbalığı"; // x'in değeri NaN olur

The number 20 cannot be divided by the string "Köpekbalığı" because "Köpekbalığı" cannot be evaluated as a numeric value.
Therefore, the return value of variable x is NaN.

However, if a string can be evaluated as a numeric value, JavaScript can perform this mathematical operation:

let y = 20 / "5"; // y'nin değeri 4 olur

In the above example, since the string "5" can be treated as a numeric value in JavaScript, it is treated as a regular number with the division operator /.

However, if a variable is assigned the value NaN and that variable is used in an operation, the result will still be NaN even if the other operand is a valid number:

let a = NaN;
let b = 37;
let c = a + b; // c'nin değeri NaN olur

There is only one number data type in JavaScript.
When working with numbers, any number you enter is automatically evaluated as number type.

You do not need to specify the data type separately as in static typed languages ​​because JavaScript is a dynamically typed language.

Strings (Texts)

String is a sequential form of one or more characters (letters, numbers, symbols).
They are very useful because they represent textual data.

In JavaScript, strings are defined within single quotes (') or double quotes (").
So to create a string you need to enclose the string in quotes:

let singleQuotes = 'Bu, tek tırnak içinde bir stringdir.';
let doubleQuotes = 'Bu, çift tırnak içinde bir stringdir.';

You can choose to use single quotes or double quotes, but whichever you choose you must be consistent throughout the program.

“Hello, World!” program is a classic example of how a string can be used in computer programming. But we will adapt it to "Welcome to GenixNode Docs.." The characters that make up the statement Welcome to GenixNode Docs.. in alert() below are a string.

<!DOCTYPE HTML>
<html>
<head>
<script>
function rabisuFunction() {
alert("GenixNode Docs'a Hoşgeldiniz..");
}
</script>
</head>
<body>
<p><button onclick="rabisuFunction()">Bana Tıkla</button></p>
</body>
</html>

When we run the code and press the Click Me button, a pop-up appears with the following output:

Output
GenixNode Docs'a Hoşgeldiniz..

Like other data types, we can store strings in variables:

let hw = "GenixNode Docs'a Hoşgeldiniz..";

And by calling the variable, we can represent the string in alert():

...
<script>
let hw = "GenixNode Docs'a Hoşgeldiniz..";
function rabisuFunction() {
alert(hw);
}
</script>
...
Output
GenixNode Docs'a Hoşgeldiniz..

We can perform many operations on strings in our programs.
Thanks to these operations, we can obtain the results we want by manipulating strings.

Strings are important because they are used to convey information to the user,
They also enable the user to send information to the program.

Boolean

The Boolean data type can only take one of two values: true or false.
Booleans represent truth values ​​that correspond to branches of logic in mathematics.
They are used in computer science to direct the operation of algorithms.

Many operations in mathematics return true or false:

  • Greater

    • 500 > 100 → true
    • 1 > 5 → false
  • Small

    • 200 < 400 → true
    • 4 < 2 → false
  • Equals

    • 5 == 5 → true
    • 500 == 400 → false

Like other data types, we can store a Boolean value in a variable:

let myBool = 5 > 8; // false

Since the number 5 is not greater than 8, the value of the variable myBool is false.

As you write more programs in JavaScript, you will gain a better understanding of how Booleans work.
You'll see how the true or false values ​​returned from different functions and operations can change the flow of the program.

Arrays

Array can hold multiple values in a single variable.
This way, you can put a list of values ​​into an array and loop over it.

Each element in an array is called an element.
Array elements are accessed using index number.

Just as strings are defined within quotes, arrays are defined by enclosing square brackets [ ].

For example, an array of strings looks like this:

// Balık isimlerini içeren bir dizi tanımlıyoruz
let baliklar = ["köpekbalığı", "mürekkep balığı", "palyaço balığı", "yılan balığı"];

When you print it to the console, you get the following output:

["köpekbalığı", "mürekkep balığı", "palyaço balığı", "yılan balığı"]

Arrays are a very flexible data type because they can be modified; that is, the values ​​of the elements within them can be added, subtracted and changed.

Objects

In JavaScript, the object data type can hold multiple values as key:value (name:value) pairs. Thanks to this structure, it is very easy to store and access data.

When defining objects, curly brackets { } are used and each property is written in the logic anahtar:değer.

It is generally preferred to keep interrelated information. For example, a user's identification information (such as ID, name, e-mail) can be collected under a single object.

Below is a fully crafted JavaScript object:

let balik = {ad: "Ahmet", soyad: "Köpekbalığı", renk: "mavi", konum: "okyanus"};

Alternatively, instead of writing objects on a single line, we can write them line by line. This method increases readability considerably, especially if there are a lot of features. Additionally, leaving a space after the : sign is more pleasing to the eye.

let balik = {
ad: "Ahmet",
soyad: "Köpekbalığı",
renk: "mavi",
konum: "okyanus"
};

The balik object we created in the examples above had 4 properties: name, lastname, color and location.

Each feature was written as anahtar: değer and these pairs were separated by a comma.

In short:

  • name → “Ahmet”
  • surname → “Shark”
  • color → “blue”
  • location → “ocean”

This is the basic logic in objects: property name on the left, value of that property on the right. Thanks to this structure, we can keep the data organized and accessible.

When you write a program, you work with more than one data type. However, operations are generally performed on the same data type. In other words, mathematical operations are performed with numbers, and fragmentation or combination operations are performed with texts (strings).

Sometimes an operator can also be used with different data types. For example, the + operator both adds numbers and adds text side by side. But when different types are involved, things can get complicated.

For example, when you concatenate a number and a string with +, the number behaves like a string and the result is just adding together. Additionally, the order of data types affects the result obtained.

For example, if we define a variable like this, JavaScript interprets each element as a string:

let o = "Okyanus" + 5 + 3;

If you call the o variable we created in this example, JavaScript will return you the following value:

Output
Okyanus53

But if you start with numbers first, JavaScript adds those numbers first. Then, when the program reaches the string "Okyanus", it converts the resulting sum to the string and combines it with it.

let o = 6 + 7 "Okyanus" ;
Output
13Okyanus

Because of such unexpected results, it is generally safer to perform your operations within a single data type. So if you're working with numbers, do only math operations; if you're dealing with strings, do only text operations.

One of the interesting things about JavaScript is that it doesn't throw errors when you mix different data types. While some other programming languages ​​give warnings immediately, JavaScript tries to interpret the situation internally.

This provides both flexibility and can sometimes lead to annoying surprise results.

Result

Now you have a clearer idea of the basic data types you can use in JavaScript.

You will encounter these data types frequently in your future projects, and each will be very important in its own place. In short, knowing which data type to use where is a great advantage in your JavaScript learning journey.