DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Antonio
Antonio

Posted on • Originally published at antonniotr.netlify.app

A Quick Overview Of JavaScript Data Types

In JavaScript, you can assign any type of value to any variable. It is not a problem if a variable had a value type of string and now you want to store a number in it.
There are 8 types of values, separated into two groups: primitives and references.
The object type is a reference, and the rest are primitives.

The typeof Operator

  console.log(typeof "hello"); // string
  console.log(typeof 5); // number
  console.log(typeof null); // object
  console.log(typeof undefined); // undefined
  console.log(typeof true); // boolean
  console.log(typeof Symbol("hey")); // symbol
  console.log(typeof {}); // object
  console.log(typeof 1n); // bigint
Enter fullscreen mode Exit fullscreen mode

You can use the typeof operator to see the type of each value.

String

const firstName = "Joe";
const lastName = 'Doe';
let age = `is 45 years old.`;

age = `${firstName} ${lastName} is 45 years old.`;
console.log(age); // Joe Doe is 45 years old.
Enter fullscreen mode Exit fullscreen mode

A string is a text enclosed in double-quotes, single quotes, or backticks.
The strings enclosed in backticks are called template literals. They have extra functionality because we can put an expression between ${}.

Number

const age = 45;
console.log(age + 5); // 50
Enter fullscreen mode Exit fullscreen mode

The number type is for both integers and floats. But there are 3 special numbers here:
infinity, -infinity, and, NaN.

console.log(Math.pow(2, 1024)); // Infinity
console.log(-Math.pow(2, 1024)); // -Infinity
console.log("hello" / 5; // NaN
Enter fullscreen mode Exit fullscreen mode

Infinity is bigger than any other number, and -Infinity is smaller than any other number.

NaN stands for Not a Number but if we check its type with the typeof operator we get a number. It’s the result of a wrong calculation for example if we try to divide a string with a number we will get NaN.

BigInt

const bigNum = 1n;
const  bigNum2 = BigInt("9007199254740991");
Enter fullscreen mode Exit fullscreen mode

The bigint type represents numbers bigger than 2⁡³ - 1. You can create a bigint by appending n to the end of a number, or by using the BigInt object.

Null

let number = null;
Enter fullscreen mode Exit fullscreen mode

The null type stands for no value. One thing that is worth to note is that if we check its type with the typeof operator it will return object. This is a bug in JavaScript, and now it is too late to be fixed.

Undefined

let variable;
console.log(variable); // undefined
Enter fullscreen mode Exit fullscreen mode

When a variable has not been declared with a value it has a type of undefined.

Boolean

const bool = true;
const bool2 = false;
console.log(8 < 4); // false
console.log(4 > 2); // true
Enter fullscreen mode Exit fullscreen mode

Booleans can be true or false. They are used in conditionals with operators like ==, ===, >, <, and, more.

Symbol

const sym = Symbol("hey");
console.log(sym); // Symbol(hey)
Enter fullscreen mode Exit fullscreen mode

The symbol type is used as a unique identifier. It has its own properties and each symbol value is unique. We create a symbol using the Symbol object.

Object

const object = {
  name: "Joe Doe",
  age: 45,
}
Enter fullscreen mode Exit fullscreen mode

The object type is different than any other type because it has its own properties and methods. It is also mutable, while the others are not.

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.