Pointers in JavaScript
Posted by Superadmin on May 01 2023 06:30:15

Pointers in JavaScript

By Priya PedamkarPriya Pedamkar
  

Pointers in JavaScript

Introduction to Pointers in JavaScript

The following article provides an outline on Pointers in JavaScript. In JavaScript, most of the things are Objects whether we consider arrays, functions etc, hence it is considered as an Object-Oriented Language. The only elements that are not objects are the primitive data types. Primitive data types are immutable, and objects are mutable elements.

What are pointers?

A pointer is a variable through which we can access another variable’s value.

The below diagram depicts how pointers work in a C Programming Language.

Pointers

References in JavaScript

Examples of Pointers in JavaScript

Given below are the examples:

Example #1

Creating an Object Literal.

Code:

<!DOCTYPE html>
<html>
<body>
<script>
var objREf = {num: 100};
function pointer(obj){
obj.num++;
}
pointer(objREf);
console.log(objREf.num);
</script>
</body>
</html>

Output:

pointers in javascript 1

Explanation:

Example #2

Code:

<!DOCTYPE html>
<html>
<body>
<h2>Refernces in JavaScript</h2>
<p id="references"></p>
<script>
var sport = {outdoor:"Basketball", indoor:"Carrom"};
document.getElementById("references").innerHTML = sport;
</script>
</body>
</html>

Output:

pointers in javascript 2

pointers in javascript 3

Explanation:

The above output is seen when we change the below line:

from:document.getElementById("references").innerHTML = sport;
to: document.getElementById("references").innerHTML = sport.outdoor;

i.e. we access the sportobject value.

Also, when we assign a variable, like the one below:

var object=new Object()

new Object() is an OBJECT, and the variable object is a pointer or a reference (more suitable term in the JavaScript World). So, from now onwards in this blog, we will call “pointers”, references.

Example #3

Creating an Object.

Code:

<!DOCTYPE html>
<html>
<body>
<p id="objDemo"></p>
<script>
var speaker = new Object();
speaker.fullName = "Mike Lewis";
speaker.topic = " How to handle Success & Failures in Life";
document.getElementById("objDemo").innerHTML =
speaker.fullName + " will be speaking on -  " + "<strong>"+speaker.topic+"</strong>";
</script>
</body>
</html>

Output:

creating an object

Explanation:

Example #4

Shared Value.

Code:

<!DOCTYPE html>
<html>
<body>
<script>
var cat = [69,69,69];
//assign-by-reference
var copycat = cat;
cat.push(0);
console.log("Var 1:", cat);
console.log("Var 2:", copycat);
</script>
</body>
</html>

Output:

shared value

Explanation:

Example #5

Reassignment of References.

Code:

<!DOCTYPE html>
<html>
<body>
<script>
var four = [4,8,12];
//assign-by-reference
var number = four;
console.log("Four",four);
console.log("Number",number);
//value is reassigned (create new reference)
number = [9,0,9];
console.log("Four",four);
console.log("Number",number);
</script>
</body>
lt;/html>

Output:

Reassignment of Refences.

Explanation:

Conclusion

In conclusion, JavaScript primitive types are always passed by value, whereas the values inside objects are passed by reference. It is important to understand references in JavaScript which can help developers to avoid common mistakes and write better executional scripts. Point to remember is that the references in JavaScript only point at contained values and not at other variables, or references.