Pillars of JavaScript: Data and Structure Types.

JS is a dynamically typed language, and this can be confusing if you are coming from a statically typed language like C, C++, or Java.

Look at the following example in Java.

int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D'; // Character
boolean myBool = true; // Boolean
String myText = "Hello"; // String

In this example, we know in advance the data type that can be assigned in a variable, and we can only use those types when we want to reassign the value in that variable.

In JS, the data type is determined by the value assigned, and we can reassign all kinds of values in the same variable.

var someValue = 42;    // foo is now a number
someValue = 'foo'; // foo is now a string
someValue = true; // foo is now a boolean
someValue = {}; // foo is now an Object
someValue = [1,2,3,4]; // foo is now an Array

In this example, we can assign to ‘someValue’ any kind of values, and dynamically JS changes the data type in the variable.

To see the data type in JS, we can use the operator typeof, and this operator returns a string with the name of the type.

var someValue = 42;    // foo is now a number
console.log(typeof someValue); // returns "number"
someValue = 'foo'; // foo is now a string
console.log(typeof someValue); // returns "string"
someValue = true; // foo is now a boolean
console.log(typeof someValue); // returns "boolean"
someValue = {}; // foo is now an Object
console.log(typeof someValue); // returns "object"
someValue = [1,2,3,4]; // foo is now an Array
console.log(typeof someValue); // returns "object"

In modern JS, we can divide data types into Primitives and Non-Primitives. A Primitives is a value that is not an object and has no methods, and we can find seven primitive data types.

string — Used for text values.

number — Used for numeric values.

boolean — Used for logic values (true and false).

null — Used to represent no value. null is a special case of primitive and represents an Object with no value.

undefined — A variable that never gets an assigned value.

symbol — A unique value that’s not equal to any other value.

bigint — A big numeric value.

Non-Primitives are Objects, and we can say that the main Non-Primitives types are.

Object — This is a special non-data structural value.

function — This is a particular object value, and if we use typeof, we get a “function” string.

It is essential to know Primitives and Non-Primitives types to understand how JS converts our variable types dynamically in runtime when we assign or not a value in our code.

Well, I hope this post can be handy, and don’t forget to comment if you have questions or reach me on Twitter. Bye :)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store