Understanding JavaScript Data Types
JavaScript is the language that brings websites and web applications to life, making them interactive and dynamic. Data types are at its core. In this article, we'll explore what data types are and how many of them exist in JavaScript. Let's break it down step by step.
What is a Data Type?
A data type defines the type of data that a variable can hold. It determines how information is stored, manipulated and understood within a program.
In JavaScript, there are two main groups of data types:
Primitive Data Types
Primitive data types represent single values. There are seven primitive data types: string, number, bigint, boolean, undefined, null and symbol. They are used to store simple values. Let's dive into each of them:
let name = "Marco";
console.log(typeof name); // Output: string
let age = 25;
let degrees = 10.5;
console.log(typeof age); // Output: number
console.log(typeof degrees); // Output: number
let largeNumber = 392939909293103023n;
console.log(typeof largeNumber); // Output: bigint
let javascriptIsCool = true;
console.log(typeof javascriptIsCool); // Output: boolean
let name;
console.log(typeof name); // Output: undefined
console.log(name); // Output: undefined
let age = null;
console.log(age); // Output: null
console.log(typeof age); Output: object
let key = Symbol("description");
console.log(typeof key); Output: symbol
Non-Primitive Data Types
Non-Primitive data types are also know as Reference data types. They don't hold a direct value. Instead, they are more complex and can hold collections of data and complex entities. There is only one non-primitive data type.
let car = { "brand": "Tesla", model: "S" };
console.log(typeof car); // Output: object
let colors = ["blue", "green", "red"];
console.log(typeof colors); // Output: object
Extras
We covered the essentials about JavaScript Data Types. However, there are some important extras to care about:
There are three more numeric values possibilities:
let result = 10 / "text";
console.log(typeof result); /// Output: number
console.log(result); // Output: NaN
let result = 2 / 0;
console.log(typeof result); // Output: number
console.log(result); // Output: Infinity
let exponential = 3e5; // is equal to 3 * 10^5
console.log(typeof exponential); // Output: number
Numbers always stored as 64-bit floating point
All JavaScript numbers are stored as 64-bit floating point values. This means that even integers are stored as floating-point values.
Integer limit - when bigint comes to the rescue
The maximum safe integer value is 2^53 - 1 (equals to 9007199254740991). This happens because, as said above, JavaScript numbers are stored as 64-bit floating point values. 53 bits of those are allocated to represent the integer part of a number. That's why the highest integer that can be represented accurately is 2^53 -1. Numbers higher than that should be represented using bigint.
Floating Point Precision
There are decimal fractions that can't be represented precisely in binary floating-point format. This can lead to rounding errors. The difference is usually very small but it's important to be aware of these.
let result = 0.3 - 0.1; // We expect 0.2
console.log(result); // Output: 0.19999999999999998
Arrays and Functions are classified as objects in JavaScript
This is why object is the only non-primitive data type.
Wrapping Up
There are two types of data types in JavaScript: primitives and non-primitives. Non-primitives are also known as Reference.
Primitives
Non-primitives
Understanding data types is essential for any JavaScript developer. So it's important to master these concepts in order to write consistent and bug-free code.
Hashtags: #javascript #webdevelopment #coding #datatypes
Connect with me on X at @CodeWithMarco - https://twitter.com/CodeWithMarco