✨? JavaScript Data Types_ 27 Quick Questions You Need To Master
✨? JavaScript Data Types_ 27 Quick Questions You Need To Master
15 1
This type determines what operations can be performed on the value and how it's
stored 🗳️.
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 1/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
So, there are 7 primitive data types: string, number, boolean, undefined, null,
symbol, bigint.
Stack: Stores static data (data with a fixed size). Since primitive values are
immutable and won't change, they will be stored here.
Heap: Stores object values. The heap's size can change during the code execution
to accommodate object value changes.
GIF
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 3/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
This includes: object literals ( { name: "Fatou" } ), functions ( function sayHi(){} ), arrays
( [1, 2, 3] ), etc.
👉 When a variable contains a non-primitive data type, it doesn't hold the actual
object value.
House != Address
const house = {
floors: 2,
gifts: [],
color: "blue"
}
house.gifts.push("Red dress")
house.color = "red"
In the following example, the code modifies the original array since it is passed to the
function as an address:
function addUser(arr){
const randomId = "123"
arr.push({ id: randomId } );
return arr;
}
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 4/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
console.log(newUsers); // [{ id: ... }]
console.log(users); // [{ id: ... }] (since users was modified)
GIF
💡 Note: This method doesn't work for null because typeof null returns object
😅. To safely check if value is an object, you need to do value != null && typeof
value === "object"
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 5/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
BigInt was introduced for integers outside this range. Any integer can be converted
to a bigint by appending n to it or using BigInt(...)
💡 Tip: You can get the min/max safe integers with Number.MIN_SAFE_INTEGER and
Number.MAX_SAFE_INTEGER .
undefined conveys a value that hasn't been defined yet. It's like you just bought a
new house, and someone asks about the swimming pool 🏊♂️. You haven't even
thought about it yet — it's undefined!
null conveys a value that has been set to "empty". It's like you bought the house
and decided there wouldn't be a swimming pool (since you're too broke for it 😅).
💡 Note: Don't rely on that distinction in your code. Just consider any value null
or undefined as empty.
For example, the object me has the properties name , age , etc.
const me = {
name: "Fatou",
age: 30,
job: "Software Engineer"
}
const obj = {
"first name": "Fatou"
};
console.log(obj.first name); // Doesn't work
console.log(obj["first name"]); // "Fatou"
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 7/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
That function can only be accessed through the object like this:
const me = {
name: "Ndeye Fatou Diop",
sayHi(){
console.log(`Hi, my name is ${this.name}`)
},
sayHelloWorld(){
console.log("Hello World")
}
}
💡 Note: Be careful with the object in the sayHi function. If the function was
this
called like this, const fn = me.sayHi; fn(); , this != me . You can learn more about
this rule here.
👉 Coercion happens.
It is when JavaScript implicitly converts values from one data type to another.
In the example above, JavaScript will convert 9 to the string "9" and return "8" +
"9" => "89" .
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 8/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
const result = "9" - 5; // returns 4 since "9" is coerced into the number `9`
const sum = true + false; // returns 1 since true is coerced into 1 and false into 0
💡 Notes: There are coercion rules, but you don't need to remember them all.
Instead, avoid comparing apples and oranges and invalid operations 😅.
So, every falsy value will be coerced to false when JavaScript expects a boolean
value (see Question #12).
Examples:
// value is falsy
if(value) {
// Not reachable since `value` will be coerced into `false`
}
const name = value && "myValueHere"; // `name` will be equal to `value` since `value
false
0 , +0 , -0 , 0n , NaN
empty string: "" , '' , etc.
null
undefined
document.all : the only falsy object
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 9/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(!!1); // true
console.log(!!0); // false
In fact, == will return true if the two values are equal after JavaScript coercion kicks
in.
For example, 1 == true returns true because JavaScript will coerce 1 into a boolean.
Since 1 is truthy (see Question #13), it will be equivalent to true.
Conversely, 1 === true will return false since the types differ.
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 10/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
Unfortunately, numbers like 0.1 and 0.2 cannot be represented precisely in this
standard, leading to a loss of precision.
Since those references (i.e., addresses) are different, the comparison returns false.
GIF
Let's say we have the following object, and we want to access the name property:
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 11/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
const me = {
name: "Ndeye Fatou Diop",
job: "Software Engineer",
"way of life": "Coding"
}
We can do:
me.name
me["name"]
💡 Note: For properties with spaces like "way of life" , we can only use me["way of
life"] .
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 12/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
// Option 3: Use forEach
arr.forEach(x => {
console.log(x)
})
For an object
const obj = {
name: "Fatou",
age: 30
}
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 13/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
For example,
const car = {
wheels: 4
};
const bmw = {
brand: "BMW"
};
function Car(brand){
this.brand = brand
}
const bmw = new Car("BMW"); // bmw.[[Prototype]] has the following shape: { construc
const car = {
wheels: 4
};
const bmw = Object.create(car); // bmw.[[Prototype]] is equal to car
bmw.brand = "BMW";
When you try to access a method or property on an object, JavaScript will first
examine the object.
When no property/method can be found, JavaScript will look into the prototype
object.
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 14/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
1. The property/method exists on the prototype object => JavaScript will return it
2. The prototype method is undefined => JavaScript will return undefined
3. The prototype method is not undefined => Inheritance kicks in. Since the
prototype is an object, it has its own prototype. So JavaScript will look into the
prototype of the prototype. It will continue doing so until it finds the
property/method or the prototype becomes undefined.
In the example below, bmw object does not have the wheels property, so the one on
car will be returned.
const car = {
wheels: 4
};
const bmw = {
brand: "BMW",
print(){
console.log(`My brand is ${this.brand} and I have ${this.wheels} wheels`);
}
};
💡 Note: If you want to learn more about prototypal inheritance? Check this great
post from @lydiahallie.
obj is undefined
obj.value is present by undefined
value is present in the prototype chain (see Question #21)
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 15/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
We can try value in obj , but this breaks when obj is undefined or null.
null N/A
undefined N/A
number Number
string String
boolean Boolean
bigint BigInt
symbol Symbol
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 16/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
When we do name.trim() , name is a primitive value and doesn't have methods.
JavaScript will then:
let age;
var name;
const me = {
name: "Ndeye Fatou Diop"
}
let age = me.age; // `age` is undefined because it doesn't exist in the object
Let's say I have an array arr1 , and I want to create a copy of that array arr2 .
I can't do const arr2 = arr1 since the arrays will point to the same object, and any
modification to arr2 will affect arr1 (see Question #3).
This will make sure the two arrays point to different objects in memory.
This works fine when the arrays contain primitive values like const arr1 = [1, 2, 3] ,
but what if the arrays contain objects like const arr1 = [ { name: "Fatou"}] ?
Because arr2 still contains a list of references that are the same as in arr1 . So, any
modification will affect the objects these references point to.
A deep copy is when we create new references for every object, so any
modification does not affect the original objects.
// Option #1: This will only work when every value can be serialized
const arr2 = JSON.parse(JSON.stringify(arr1))
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 18/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
GIF
In fact, when we have the following code, typeof name is equivalent to typeof "Ndeye
Fatou Diop" which returns "string".
As a result, since variables declared with var and let can have their values changed,
their types can also change.
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 19/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
x = 45;
console.log(typeof x); // logs "number"
👋 Before you go
Do your career a favor. Join DEV. (The website you're on right now)
It takes one minute, it's free, and is worth it for your career.
Get started
Sentry PROMOTED
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 20/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
Try Sentry
Hi 👋🏽, I am a self-taught Senior Front-End Engineer. I share tips to help overwhelmed junior
frontend developers build the confidence and expertise they need to reach the next level 😻🥳.
LOCATION
Asnieres-Sur-Seine, France
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 21/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
WORK
Senior Frontend Engineer
JOINED
Jul 26, 2020
React: 5 Small (Yet Easily Fixable) Mistakes Junior Frontend Developers Make With React State
#react #webdev #javascript #beginners
Platform.sh PROMOTED
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 22/23
6/10/24, 1:13 PM ✨🧱 JavaScript Data Types: 27 Quick Questions You Need To Master - DEV Community
Show me more
https://dev.to/_ndeyefatoudiop/javascript-data-types-27-quick-questions-you-need-to-master-1828?ref=dailydev 23/23