DEV Community

Débora Quintal
Débora Quintal

Posted on

Data Types in Javascript

Hi, everyone. I decided to start writing about my knowledge in JS to help those who are beginners in dev community.

I hope this article would be helpful to you :)

First of all... What is a variable?

A Variable is a container which stores values.
This value can be changed when we use the word 'let' to declare a variable.

We have the option to declare a variable using the word 'const', too, but this value will not be changing, so be careful.

In Javascript, we have 7 types of data:

let name = 'Debs'; // String (a sequence of text)
let age = 24; // Number
let isGraduated = true; // Boolean (this is a true or false value)
let team; // Undefined (there's no value defined)
let food = null; // Null (represents the intentional absence of any object value)
let symbol = Symbol(); // Symbol
let newObject = {}; // Object
Enter fullscreen mode Exit fullscreen mode

These are primitive data, it means that it is not mutable (except for the objects).

If you are not sure about the type of the variable you are working with, there is a way to discover it by using the command 'typeof' inside the console.log


let name = 'Debs';
console.log(typeof name); // returns string
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

jonrandy profile image
Jon Randy 🎖️ • Edited

You missed bigint.

console.log(typeof 10n)  // bigint
Enter fullscreen mode Exit fullscreen mode

JS has 7 primitive data types: string, number, boolean, null, undefined, symbol and bigint. Everything else is an object (which isn't a primitive).

Sometimes though, depending on how you define a value - the result of typeof may surprise you:

let myNum = 3
console.log(typeof myNum)  // number
console.log(myNum * 7)  // 21

let myOtherNum = new Number(3)
console.log(typeof myOtherNum )  // object
console.log(myOtherNum * 7)  // 21
Enter fullscreen mode Exit fullscreen mode

Also, be careful how you use the word 'changing' when talking about const - it is better to say a const cannot be redefined. You are perfectly free to change the properties of an object declared as a constant:

const num = 3
num = 4  // TypeError

const obj = {}
obj.num = 6  // No problem
Enter fullscreen mode Exit fullscreen mode