Coding Newbie

JavaScript Data Types

Introducing JavaScript Data Types: The Fundamentals

Like any other programming language, JavaScript as well has its data types. What are JavaScript data types and what is their importance?

If you are unsure what JavaScript is and you are interested to read more about it first, you definitely should read an article What Is JavaScript? Complete Beginners Guide.

different data type on keyboard

What are actually Data Types?

A data type defines the type, the kind of data that is stored within a declared variable.

It is important to understand different data types, so you are aware of characteristics of each type, because this can have an impact on the code later on.

What are JavaScript Data Types?

All data types in JavaScript can be divided into two main groups:

  • Primitive data types
  • Non-primitive data types

Primitive JavaScript Data Types

The main characteristic of primitive data type is the ability to hold or store only one value at a time.

There are five primitive data types:

  • String
  • Number
  • Boolean
  • Undefined
  • Null

String Data Type

Strings represent collections of characters and symbols, whether numeric or alphabetical, surrounded by single or double quotes.

This represents the way to store letters, words, and phrases:

let string  = "Hello";
let string2 = "This is also a string";
let string3 = "And this is a string number 3"

A string can also be declared using inbuilt JavaScript function String() :

let stringName = String("Creating a string using an inbuilt JS function");

Numbers Data Type

Numbers data type is the obvious data type. It can be an integer, or a decimal number, even an exponential number.

These variables are used for mathematical operations later in the code, or as counters.

let number  = 100; //integer
let decimal = 10.5; //decimal number
let exp = 10e5; //exponential number

Please keep in mind that numbers are declared without using quotes. If quotes are used, the variable data type will be a string, not a number.

let numberVariable = 100; //a number
// however...
let otherVariable = "100"; //this is a string, not a number

When it comes to number data type, JavaScript has one interesting functionality.

JavaScript will never return a fatal result of mathematical operations, which can crash the code.

This is because JavaScript has two additional, specific numeric values. These are Infinity and NaN (Not A Number). That makes, for example, dividing by zero possible in JavaScript, as this will not break the program, but will return Infinity as a result.

let newNumber = 1 / 0;
// this operation will result in INFINITY
// and that will have a number data type

NaN represents an error in computing, an undefined mathematical operation.

let newNumber = "a string" / 5;
// here as a result we will get NaN
// and it will also have a number data type

Infinity and NaN ensure we always get some result of our operations, and the code will not break.

Boolean Data Type

Boolean is an interesting data type, as it only has two possible results: true and false.

This is a logical data type, used to store yes or no states:

let isSelected = true;
let isChecked = false;

Boolean data type does not always need to be declared. It can be a result of an logical expression, which can be useful as a trigger for later code or some function, especially as a part of IF statements.

let a = 5;
let b = 2;
alert(a > b);
// a in fact has a greater value than b, which will return true as a result

Undefined Data Type

Undefined is a special data type, as it can have only one value – undefined.

It is mostly the case when the variable has no declared value.

let a
// the "a" variable is only declared here, without a designated value
// this means that "a" variable is not defined, and if we use it anywhere in our code
// it will result in a value UNDEFINED

Null Data Type

Null is another special data type, as it only refers to one value – null. This is a value that represents nothing, a not existing value.

It is not zero, a number 0, or an empty string ‘ ‘. The difference is that null is a non-value data type:

let a = 0; // this is not a null data type, it is in fact a number, zero
let b = ''; // this is a string, empty, but still a string
let c = null; // and this is NULL data type, non-existing, unknown, emptiness

Difference between null and undefined is that undefined is the result of not defining a variable. But with null, you have to intentionally declare it. So we use null when we do not wish our variable to have a value at all.

Non-primitive Data Types

Non-primitive data types are complex data types. Unlike primitive data types, which can hold only one value, non-primitive data types represent collections of data. With these collections, we can group multiple data elements (and multiple primitive data types) into one package.

The non-primitive data types are:

  • Objects
  • Arrays
  • Functions

To be more precise, arrays and functions belong to object data type, as arrays and functions are in fact objects. However, if we check functions type in JavaScript, the code will return “type of function”. But arrays and functions are objects essentially.

Conclusion

In JavaScript, data types refer to the various sorts of data that you will be working with and saving in variables. It’s critical that you understand each of these data types since otherwise, data might be saved in an incorrect format, causing problems in your code later when it gets more complex.

There are five basic, or primitive, types of data. The five most basic types of data are Strings, Numbers, Boolean, Undefined, and Null.

Besides primitive data types, there are also more complex, non-primitive data types. These are Objects, Arrays, and Functions.

Maybe it is a good way to start learning Vanilla JavaScript first, as this will be a great foundation for further learning of JavaScript frameworks. Learning about Vanilla JavaScript is available in the post What is Vanilla JavaScript and should you learn it first.

%d bloggers like this: