Data Types in JavaScript

Data Types in JavaScript
Data Types in JS

Get to understand the different data types in Javascript which help in creating websites and web apps.

You will always catch the word Data Types in any programming language. This is because they are the way to classify data to let the computer know how the browser should use/ interpret/display the data. These data types help the browser understand which part of the code is a keyword (reserved words in a programming language) and which part of the code is text or number that should not be interpreted.

By the end of the article, you will understand the different data types and see how they work in a program.

Different Data Types in JavaScript

There are three main data types in JavaScript, Ie. String, number, and boolean. Other data types include Null and Undefined. These 5 data types are known as primitive data types.

Something to keep in mind is that you can only store one data type in one variable.

String

A string, just as the name says, is a string of characters. These are the characters in the alphabet that makeup text. In JavaScript, a string is enclosed between two quotation marks, which separates it from the code and other data types. We can use single quotes or double quotes for the strings.

It is important to know that you cannot use a single opening quote and a double closing quote. If you choose to use single quotes, use them for the opening and closing. Let us look at an example.

For example:

var greet = "Hello World!"

In this example, the term var is a keyword in JavaScript, meaning variable. It acts like a box that stores information. The name of that box is greet.

The equal sign assigns the string "Hello world!" to the variable greet.

Let us print the var greet to the console:

console.log(greet)

Output:

Hello World!

As you can see, the string "Hello World!" was stored in the variable greet. Hence when we print greet to the console, it displays the contents inside.

Numbers

The next data type is Numbers. Numbers are just what they sound like, they are integers, and they can represent anything that is given a number value, i.e., age and money. You can add a decimal point to the numbers but not a comma. You cannot add letters in numbers.

Numbers having different symbols from the code alphabet do not need special characters to tell them apart from the code. This means we do not require double quotes or other symbols. Instead, numbers are typed as they are.

For example:

var age = 3

As you can see, the number "3" is written without any symbols.

If we try to log age to the console:

console.log(age)

Output:

3

Boolean

The other data type is known as a boolean. Boolean/bool describes data as either true or false, meaning the data has to have only two states. Like a switch that can only be on or off.

Null and Undefined

These are other data types that represent empty variables. They have no values assigned. The difference between these two data types is that with Null, the programmer sets it intentionally, unlike Undefined, which happens when a value is not assigned to the variable.

Other Data types

Objects and Arrays are also data types. They store multiple data. A good example is a person's information which can be represented using an object.

Example:

const person = {
	name: "Jay Jeff",
	age: 100,
	profession: "Developer",
}

We have stored multiple pieces of information in one constant. A const is a variable that cannot be changed once it is assigned a value.

Example

var cars = ["Audi", "Mercedes", "VW", "Toyota", "Ford"]

Typeof()

The Typeof() keyword lets you know which data type you use in your code. The syntax for this keyword is:

Typeof()

This keyword will let you know the data type of whatever you put between the round brackets.

Let us see it in action.

Example1:

Typeof(23)

Output

number

Example2:

Typeof(true)

Output:

boolean

Example3

Typeof("Geekbits")

Output:

string

Conclusion

Booleans, numbers, and strings are the foundation for creating websites and web apps. Therefore, you need to use them accordingly, and it is essential to know how to use these data types.

Stay tuned for more JS tutorials.

That is it for this one. Hopefully, the article was helpful. If you have any queries, leave them in the comments, and we will be sure to respond to them.

Thank you for reading!!

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.