Are there pointers in JavaScript? A Beginner’s Guide

Are there pointers in JavaScript? Well, if you want to know the answer, keep on reading!

This article will answer this question below.

Discover the truth about JavaScript pointers with our beginner’s guide.

Learn how references work and how they differ from pointers in other languages.

Start to enhance your coding skills today!

What is pointers in JavaScript?

JavaScript Pointers refer to a variable that stores the memory location of another variable in the field of computer science.

These pointers find widespread application in low-level programming languages such as C and C++, enabling developers to directly manipulate memory.

With pointers, developers can efficiently carry out tasks like memory allocation, deallocation, and accessing data structures.

Moreover, pointers in JavaScript refer to the concept of memory addresses and direct manipulation of memory, similar to how pointers work in low-level programming languages.

However, it’s important to note that JavaScript does not have explicit pointers like languages such as C and C++.

Instead, JavaScript uses references to work with objects and manage memory.

Does JavaScript have pointers?

JavaScript doesn’t have pointers in the same sense as languages like C or C++. However, it does have references, which are similar to pointers in some ways.

In JavaScript, objects are passed around by passing a copy of a reference. This means that when you pass an object to a function, you are passing a reference to that object, not the object itself.

This allows you to modify the contents of the object within the function.

For example, consider the following code:

var sampleObj = {a: 0};
function increment(obj) {
obj.a++;
}
increment(sampleObj);
console.log(sampleObj.a);

In this example, sampleObj is an object with a property “a” set to “0.” When we pass sampleObj to the increment() function, we are passing a reference to sampleObj, not the object itself.

Inside the function, we can use this reference to modify the contents of sampleObj.

When we log the value of sampleObj.a after calling the function, we can see that it has been incremented.

Output:

1

JavaScript pointers examples

Here are some examples of how pointers do in JavaScript:

Example 1: Create an Object Literal

let objREf = {sampledata: 10};
function pointer(obj){
obj.sampledata++;
}
pointer(objREf);
console.log(objREf.sampledata);

In this example, objRef is a reference to an object. When objRef is passed to the function pointer, that reference is copied over to obj.

Consequently, obj and objRef refer to the same thing in memory. Changing the sampledata property of obj affects the sampledata property of objRef.

Output:

11

Example 2: Variables that are an object or an array are always just pointers

const sampleobj1 = { website: 'Sample', offers: "Free sourcecodes and tutorials" };
const sampleobj2 = sampleobj1;
sampleobj2.website = "Itsourcecode";
console.log (sampleobj1);

In this example, when we assign obj1 to obj2, we are actually assigning a reference to the same object in memory. So when we change the value of the property of one object, it affects the other object as well.

Output:

{ website: 'Itsourcecode', offers: 'Free sourcecodes and tutorials' }

Example 3: Shared value

let sample = [1, 2, 3];

var copysample = sample;
sample.push(0);
console.log("let 1:", sample);
console.log("let 2:", copysample);

As you can see in this example, copysample is assigned a reference to the same array as a sample. When we modify the sample using the push method, the change is also reflected in copysample.

Both variables output [1, 2, 3, 0] when logged to the console.

Output:

let 1: [ 1, 2, 3, 0 ]
let 2: [ 1, 2, 3, 0 ]

Conclusion

In conclusion, this article discusses if there is pointers in JavaScript.

However, JavaScript does not have explicit pointers like low-level programming languages such as C and C++.

Luckily, it does have object references that behave similarly to pointers in certain aspects.

In JavaScript, objects are passed by copying a reference, which means that when you pass an object to a function, you are actually passing a reference to that object rather than the object itself.

This allows you to modify the object’s contents within the function, and the changes are reflected in the original object.

This article has provided a beginner’s guide to understand the JavaScript pointers and references.

It has explained how references work in JavaScript and highlighted their similarities and differences compared to pointers in other languages.

We are hoping that this article provides you with enough information that helps you understand the pointers in JavaScript.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment