Logo


Data types in javascript


There are two types of data types in javascript one is primitive and the other one is none-primitive


Primitive data types in javascript


Primitive data types are copied by value not by reference. Primitive data types are undefined, null, boolean, number, bigint, string, symbol. All other values are objects.we can’t change, add, or remove properties of primitive data types. primitives data types are immutable.

In primitive data types when we copy value of one variable to another variable then their values are transfer and when we change the value of one of them, the other one will not be effected.



1let x = 123;
2let y = x;
3console.log(x) // 123
4console.log(y) // 123

In this example we have copied x to y and then check their output. Both have same values (123). we have did example with integers but you can use strings, boolean and float data types.



1let x = 123;
2let y = x;
3x = 456;
4console.log(x) // 456 
5console.log(y) // 123

Here we have copied x to y and then changed the value of x to (456). When we log both, x value is 456 and y is 123. This is becuase x is copied by value not by reference.



None-Primitive data types in javascript

None Primitive data types are objects, arrays and regular Expressions.


In none-primitive data types when we copy one variable to another variable then their reference are transfer and when we change the value of one of them, then the other one's value will be change.


1let xArray = [1, 2, 3];
2let yArray = xArray;
3
4console.log("x array : " + xArray); // x array : 1,2,3
5console.log("y array : " + yArray); // y array : 1,2,3

In this example we have copied Array x to Array y and then check their output. Both have same values.


1 let xArray = [1, 2, 3];
2let yArray = xArray;
3
4yArray.push(4);
5console.log("x array : " + xArray);  // x array : 1,2,3,4
6console.log("y array : " + yArray); // y array : 1,2,3,4

Here we have created two arrays named xArray and yArray. Now have copied array x to array y. In line 4 we have pushed value '4' to array x. Now in output array is also changed. This is becuase array x is copied by reference not by value.


Conclusion

As we have studied in the start that primitive data types are coping by value and none primitive are coping by reference. By reference means when we change in one the other will be change.


About

Moiz is a software engineer from Arid University with a passion for writing tech tutorials and doing coding. I am continously produce JavaScript and other web development technology tutorials in concepts through easy-to-understand explanations written in plain English.I have expertise in next js ,react js ,javascript,html,bootstrap and node js.

Follow him on Twitter

Hassan is a software engineer from kohat university of science and technology with a passion for writing tech tutorials and doing javascript practices on daily basis.I have expertise in next js ,react js ,javascript,html,bootstrap and node js. Problem Solving and making concpets is my strong point.

Follow him on Twitter

Tags

Click to see all tutorials tagged with:

© Copyright 2023 Pak Annonymous | Back To Homepage | Privacy Policy
Share