Back to blog

Understand the Different Data Types in Programming (JavaScript)

Hello HaWkers! Jeff Bruchado here.

This is the fifth article in our series that will cover our journey to Learn Programming from Scratch.

Well, no wasting time, let's get started!

Within the universe of programming, we have different Data Types available to use in our daily lives as Programmers.

This article aims to clarify the different data types available in Programming, specifically in JavaScript, which is the language we chose to study here in this article series.

Well, first of all:

You need to understand that JavaScript is a dynamically typed programming language, which means that a variable's type can change during code execution.

This differs from statically typed programming languages, where a variable's type is defined at the time of declaration and cannot be changed afterwards.

Knowing this, we can start with the different types:

String

Strings are a sequence of characters used to represent text.

Strings are delimited by single quotes (' ') or double quotes (" ").

Here's an example of how to declare a string in JavaScript:

let single = 'Hello, world!';
let double = "Hello, world!";

As you can see, and as I explained in the article about variables, the type declaration is directly related to the variable assignment, so to assign String values to a variable you can do as exemplified above.

Number

The Number type is a data type that can represent an integer (1, 2, 20, 30, 35...) or a floating-point number (1.2, 1.5, 2.5, 10.20, 20.50, 35.50...).

JavaScript doesn't have a separate data type for integers and floating-point numbers, so both are represented as numbers.

Here's an example of how to declare a number:

let int = 42;
let float = 3.14;

BigInt

BigInt is a data type that represents integers of arbitrary precision.

But Jeff, what does that mean?!

Basically, they refer to a data type used in computing to represent integers without limiting their size by the number of bits in a computer word.

Normally, the precision of integers in computers is limited by the word size (for example, 32 bits or 64 bits), which defines an upper and lower limit for the values that can be represented. In contrast, arbitrary precision integers don't have a fixed size limit.

They can represent extremely large (or small, if negative) integers that exceed the limits of standard integer data types. This is useful in various applications, such as cryptography, computational mathematics, and whenever operations with very large numbers are needed.

And this representation is done by the value n, where n is an integer followed by an n.

Here's an example of how to declare a BigInt:

let bigInt = 123456789012345678901234567890n;

Boolean

The Boolean type is a data type that can assume two values: true or false.

Boolean values are often used in control flow structures (The famous If...else, you'll receive an article about this soon.) to test whether a condition is true or false.

And I'll tell you in advance: Understanding this type is extremely important, because we use them all the time in our lives as Programmers.

Below is an example of how to declare a boolean value:

let isNight = true;
let isMorning = false;

Null

The Null type is a data type that represents the absence of a value.

It's used when a value is expected, but not available. In other words, it's simply an emptiness that doesn't exist.

Below is an example of how to declare a null value:

let empty = null;

Undefined

The Undefined type is a data type that is automatically assigned to a variable that hasn't been initialized (meaning no value has been assigned).

Here I need to open a parenthesis for the following problem:

Reading the concept above you might understand the undefined type and the null type as being the same. However, they are different.

Both types represent "no value".

However, the main difference between them is that undefined means that a variable was declared, but doesn't yet have any value assigned to it.

In other words, undefined is the default value for variables that are declared but not initialized.

In contrast, null is a value that represents the intentional absence of any object value. And it's used to indicate or assign "nothing" or "no value" to a variable.

Well, but let's look at an example of how to declare a variable with undefined value:

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

As shown above, to declare a variable with undefined value, you simply declare the variable without assigning any value to it.

Symbol

The Symbol data type is a unique and immutable data type used to create unique identifiers.

Symbols are created using the Symbol() function.

And to be honest, they are rarely used, but I brought this type here for you to know they exist.

Below is an example of declaring a Symbol:

let symbol1 = Symbol('hello');
let symbol2 = Symbol('hello');
console.log(symbol1 === symbol2); // false

Object

Well, in Programming and JavaScript, besides primitive data types, there's also the Object data type, which is a more generic type that represents any type of object.

Basically, objects are composed of key: value pairs and can contain any data type, including other objects.

For example:

let obj = {
  id: 1,
  key: 'value',
  num: 42,
  isHawk: true,
  metadata: {
    createdBy: 'Jeff',
    updatedBy: 'John',
  },
};

In other words, it's through Objects that you'll be able to bring more complex abstractions relative to the real world.

With them, you'll be able to concentrate much more information within a single variable.

Now that I've shown you how objects work, next I'll show you how to access these values through Object keys.

Accessing object keys

To access the values of object keys, you can use dot notation or bracket notation.

Next, I'll show you examples of how to access the keys of the obj object we defined above:

Dot notation

First, we have the most common way to access these keys, dot notation. And this is the most common way to access object keys.

To do this, just type the object name, followed by a dot and then the key name:

console.log(obj.id); // 1
console.log(obj.key); // 'value'
console.log(obj.num); // 42
console.log(obj.isHawk); // true
console.log(obj.metadata.createdBy); // 'Jeff'
console.log(obj.metadata.updatedBy); // 'John'

Bracket notation

In contrast, the second option is to use bracket notation, it's useful when the key is a variable or a string that may not be a valid JavaScript identifier.

To use bracket notation, type the object name, followed by brackets and the key in quotes:

console.log(obj['id']); // 1
console.log(obj['key']); // 'value'
console.log(obj['num']); // 42
console.log(obj['isHawk']); // true
console.log(obj['metadata']['createdBy']); // 'Jeff'
console.log(obj['metadata']['updatedBy']); // 'John'

Both notations are considered "correct", and both have their applicability, bracket notation is especially used when you don't know the key value, or when that key value is defined dynamically.

But between the two options, if you have the possibility to choose one or the other, always prefer to use Dot Notation as it's more concise and keeps the code cleaner.

Conclusion

Well HaWker, as you can see, this is quite a dense article, and extremely important.

This is probably one of the most important ones I've sent you.

And with this, you understood that there are different data types and each applies in its correct context.

It's extremely important that you can understand these concepts to progress in the world of Programming.

So if you didn't understand something, or if you have any questions, please! Reply to this email with your question, so that I can help you in the best way.

And remember, if you want full access to this material from the article sequence you'll receive, with practical exercises for you to study the theory and immediately start putting it into practice, click here on this link.

PS: Click on it only if you really want to accelerate.

After all, over time you'll receive everything here, completely free. What I offer you in that link is an acceleration and greater organization with everything in one place.

If it interests you, go ahead!

I hope this article helped you better understand these different Data Types, and that you understood their importance in our lives as Programmers.

And remember, I'm here to help you every step of the way!

Awesome HaWker! 👏🏻

We've reached the end of our fifth article. 😮‍💨

And if you made it this far, please reply to this email with "I managed to learn about the different data types". This way I'll know that this article series is being useful for you.

Only a true HaWker makes it to the end.

Congratulations!

I'm happy to have you here.

See you soon, let's go! 🦅

- Jeff Bruchado

Congratulations on making it this far, HaWker! 🎉

Now that you've learned about the different data types in JavaScript programming, how about testing your knowledge? Take the quiz below and see how much you absorbed from our article.

It's a great way to reinforce learning and prepare for the next steps in your programming journey.

Click to access the quiz.

Good luck and have fun!

Comments (0)

This article has no comments yet 😢. Be the first! 🚀🦅

Add comments