Data Types in JavaScript (immutable vs mutable) #javascript #wecommit100xshare
Data types are categorized into Primitive and Reference types in JavaScript. Before explaining these categories, let's look at two important terms with regards to memory that you will need to know: the Stack and Heap.
What is the Stack?
Stack is a data structure that obeys the Last In First Out (LIFO) principle. This implies that the last item to enter the stack goes out first.
Imagine a pile of books stacked up on a shelf. The last book ends up being the first to be removed. Data stored inside the stack can still be accessed easily.
What is the Heap?
Reference data are stored inside the heap. When reference data is created, the variable of the data is placed on the stack but the actual value is placed on the heap.
What are Primitve Data Types in JavaScript?
Primitive data types are immutable and are not objects because they lack properties and methods.
To determine the kind of data you are working with, use the typeof operator. The typeof operator works perfectly with all primitive data types except null.
What are Reference Data Types in JavaScript?
By default, reference data types are mutable. Reference data types consist of Functions, Arrays, and Objects.
Let's look at some examples of reference data types to help you understand better:
How to Clone Object Properties
You can clone the properties of an object using the Object. assign() method and the spread operator. With these, you can change the properties of the cloned object without changing the properties of the object from which it was cloned.
Founder @Bles Software | Driving Success as Top Seller AI Solutions | 152+ Projects Delivered | 120+ Five-Star Ratings on Fiverr | 14+ Years Experience Full Stack Dev
6moKnowing the difference between immutable and mutable data types in JavaScript can really level up your coding skills. Let's dive deeper into how these concepts impact your code efficiency!
Technical Lead at VERP | .Net, SQL Server, SQL optimizer, Angular
9moGreat!
⚡ Backend Developer (Freelance & Remote) | Web Development & Database Optimization | PHP, Node.js, MySQL, MongoDB
9moThanks for sharing