Search This Blog

2023/04/20

Javascript:Primitive & Non Primitive Data Types

In Node.js, like in most programming languages, data types can be classified
into two categories primitive and non-primitive.

Primitive Data Types:

Primitive data types are the most basic data types in a programming language.
They are immutable (unchangeable) and are passed by value rather than by
reference. In Node.js, there are six primitive data types:

1) Boolean: represents a logical value of true or false.
2) Number: represents numeric values, including integers, floats,
and NaN (Not a Number).
3) String: represents a sequence of characters.
4) Null: represents a deliberate non-value or absence of an object value.
5) Undefined: represents the absence of a value or uninitialized variable.
6) Symbol: represents a unique identifier.

Non-primitive Data Types:

Non-primitive data types are also known as reference data types. They are
mutable (changeable) and are passed by reference rather than by value.

In Node.js, there are three non-primitive data types:

1) Object: represents a collection of related data and/or functionality.
2) Array: represents an ordered collection of data.
3) Function: represents a reusable block of code that performs a specific
task.

Its worth noting that some sources might consider null and undefined as
non-primitive data types since they are both objects in JavaScript. However,
in Node.js they are considered primitive data types.


Immutable:

In programming, an immutable type is a type of data that cannot be changed
once it is created.Any operation on an immutable type creates a new value
rather than modifying the original value.


Any operation on a string returns a new string with the desired change.

Code:
let greeting = "Hello, World!"; // create a string variable
// create a new string with all uppercase characters
let newGreeting = greeting.toUpperCase();
console.log(greeting); // output: "Hello, World!"
console.log(newGreeting); // output: "HELLO, WORLD!"

Explanation:
Original variable greeting remain unchanged

Non-immutable:

Non-immutable data types are data types that can be changed after they are
created. Operations on non-immutable data types modify the original value
rather than creating a new value. In other words, non-immutable data types
are mutable.

Code:
let numbers = [1, 2, 3, 4, 5]; // create an array
numbers.push(6); // add a new element to the end of the array
console.log(numbers); // output: [1, 2, 3, 4, 5, 6]

Explanation:
Notice that the original array variable numbers has been modified,
rather than creating a new array with the desired change.


Value Type Storage in Memory :

Value types are been stored on the Stack in our memory. The stack is simply a
stack of data that has a “LIFO” data structure.LIFO stands for Last In First Out.
It doesn’t have much space (compare to other data structures) but due to how it
works,it is very fast to access.


When storing a value type in memory, it adds an element to the top of the stack
with the value of the newly created variable. When creating a new variable and
assigned the first one to the new one, it adds a new element on top of the
stack with the value of the new variable

Code:
var name = "Maya";
var newName = name;

Explanation:The first variable — name gets into the stack with the value
of the variable.Then, the newName gets into the stack in a new memory
location with the value of the variable name.

Reference Types:

Reference types are been stored on the Heap. Heap has no order of how to store
the data. You can think of it as it stores the data randomly, where each of
the data has its own address. It is slower to access but has much more space
since it handles more complex variables.

When storing a reference type in memory, it adds a new element to the top of
the stack when its value is a pointer/reference to the address of the object
that has been stored on the heap.

Code:
var Person = {name: "Maya", age: "29"}
var newPerson = Person;

Explanation:A new element gets into the stack with a value of the
pointer/reference to the Person object that has been stored on the
heap.Here person & newPerson share same memory address so change in
\ value of one will reflect in other.

No comments:

Post a Comment