-
Notifications
You must be signed in to change notification settings - Fork 1
What is the difference between pass by value and pass by reference in JavaScript? #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Pass by Value vs. Pass by Reference in JavaScriptWhen you're learning to code in JavaScript, you might come across the terms "pass by value" and "pass by reference." These sound a bit complicated, but they're actually all about how JavaScript handles data when you pass it into functions. Let's explore the differences between the two in a simple way. Pass by ValueIn JavaScript, some types of data are passed by value. This means that when you pass these values into a function, JavaScript makes a copy of the value. It’s like making a photocopy of a picture and giving it to someone. You keep the original, and they get the copy. Any changes they make to their copy won't affect your original. This happens with primitive data types. In JavaScript, primitive types include things like:
For example: let a = 10;
function changeValue(x) {
x = 20;
}
changeValue(a);
console.log(a); // Output: 10 In this example, Pass by ReferenceNow, pass by reference is a bit different. This happens when you’re dealing with objects or arrays. Instead of making a copy, JavaScript gives a reference, which means that any change made affects the original. Think of it like giving someone a link to a shared document online—any changes they make will show up in the document for everyone to see. For example: let myArray = [1, 2, 3];
function changeArray(arr) {
arr.push(4);
}
changeArray(myArray);
console.log(myArray); // Output: [1, 2, 3, 4] Here, Deep CloningSometimes, you want to work with a copy of an object or array without affecting the original. In such cases, you need to create a deep clone. A deep clone makes an entirely new copy of an object or array, including any nested objects or arrays. This way, changes to the copy won’t affect the original. You can create a deep clone in JavaScript using methods like let originalArray = [1, 2, { a: 3 }];
let clonedArray = JSON.parse(JSON.stringify(originalArray));
clonedArray[2].a = 42;
console.log(originalArray); // Output: [1, 2, { a: 3 }]
console.log(clonedArray); // Output: [1, 2, { a: 42 }] In this example, Key Difference
Why Is This Important?Understanding the difference helps you write better, bug-free code. If you want to make sure that a value doesn’t change, use primitives, which are passed by value. But if you need to share and modify data across different parts of your code, using objects or arrays can be really useful. For more advanced reading, it's also helpful to know about deep cloning. Sometimes, you may need to create a complete, independent copy of an object or array (not just a reference). In these cases, you can use techniques like Knowing about deep cloning will also help you avoid unexpected surprises when you want to keep your original data unchanged. It’s one of those little things that makes you a more careful and effective programmer. |
No description provided.
The text was updated successfully, but these errors were encountered: