Photo by Juanjo Jaramillo on Unsplash
Data Types in JavaScript
Let's dive deeper into JavaScript data types, exploring their characteristics, usage, and additional details.
Table of contents
JavaScript data types can be divided into two main categories:
Primitive
Object.
Primitive Data Types
There are a total 7 primitive data types in JavaScript
Number: Represents both integer and floating-point numbers. JavaScript uses a 64-bit floating-point representation for numbers.
let count = 12; let cost = 145.62; let age = 42;
String: A string is a sequence of characters. It is mainly used to define the texts.
let firstName = 'Alexander' let iceCream = 'Strawberry'
Boolean: Boolean is a logical type that can be "True" or "False". It's used for making decisions.
let isCustomer = True let isOverdue = False
Undefined: Value taken by a variable that is not defined. It's an empty value. A variable is declared but not assigned a value.
let customer; console.log(customer) // Output: undefined
Null: It is also an empty value treated as a falsy value.
let orders = null; console.log(customer) // Output: null
Symbol: It is introduced in ES2015 (or ES6). A symbol represents a unique value and it can not be changed.
let symbol1 = Symbol('description'); let symbol2 = Symbol('description'); console.log(symbol1 === symbol2); // Output: false
BigInt: BigInt is introduced in ES2020. It can hold larger integers than the type Number can hold.
let bigInt = BigInt(9808797867887687574567890);
Javascript has dynamic typing. You don't have to manually define the data type of the value stored in a variable. Javascript does it for you internally. This means you don't have to assign the type such as String or Number to the value.
Also, In Javascript, it's the value that has the type, not the variable. Variable just stores the value that has the type.
let customer = "Julia";
console.log(customer) //Output: "Julia"
customer = True
console.log(customer) //Output: True
Conclusion
JavaScript has a rich set of data types, each serving a different purpose. Understanding these types and how to use them effectively is crucial for writing robust and efficient JavaScript code.