Data types describe the different kinds of data that we can work with and store in variables. In JavaScript, there are eight basic data types, which are divided into two categories: Primitive and Object (Reference) types.
JavaScript is a "dynamically typed" language. This means you don't have to declare the type of data a variable will hold, and the same variable can hold different types of data at different times.
let x; // x is currently undefined
x = 5; // x is now a number
x = "Mim"; // x is now a string
Primitive values are single pieces of data. They are immutable (cannot be changed). There are 7 primitive types in JavaScript:
Textual data wrapped in quotes.
Ex: "Hello"
Integers and decimals.
Ex: 42, 3.14
Logical values.
Ex: true, false
A variable that has not been assigned a value.
Represents an intentional "empty" or "nothing" value.
Very large integers beyond the limit of Number type.
Unique and immutable values used as object keys.
There is only one non-primitive type in JavaScript: The Object.
Objects can store collections of data and more complex entities. Common objects you will use include:
{ name: "Mim", age: 25 }[1, 2, 3, 4]new Date()When you use operators like +, JavaScript behaves differently based on the
data type:
let x = 5 + 5; // Result is 10 (Addition)
let y = "5" + 5; // Result is "55" (Concatenation)
let z = "5" + "5"; // Result is "55" (Concatenation)