Skip to main content

Command Palette

Search for a command to run...

Pass by value vs Pass by reference

Updated
2 min read
Pass by value vs Pass by reference
P

Working as an online tutor for the Bachelor of Computer Science Degree from Goldsmiths, University of London on the Coursera platform. This is the first undergraduate degree programme available on Coursera, one of the world’s leading online education providers.

The programme has been designed to equip students to access careers in emerging technologies, providing opportunities for students to study machine learning, data science, virtual reality, game development and web programming to meet the needs of career changers in industry as well as those taking their first steps into the innovative computer science field.

In JavaScript, all function arguments are always passed by value. It means that JavaScript copies the values of the passing variables into arguments inside of the function.

Any changes that you make to the arguments inside the function does not affect the passing variables outside of the function.

So, the changes made to the arguments are not reflected outside of the function.

let a = 10;
let b = a;

a = 20;

//a and b are two INDEPENDENT Variables
console.log(`a is: ${a}`); //a is: 20 
console.log(`b is: ${b}`); //b is: 10

Understanding JavaScript Pass By Value

let numb = 10;

function increase(number){
  number++;
}

increase(numb);
increase(numb);
increase(numb);
console.log(`number is: ${numb}`); //10
let numb = 10;

let increase = numb => {
  console.log(`inside increase function argument numb is ${numb}`);  
  numb++;
  console.log(`inside increase function after increment is ${numb}`);
  }

increase(numb);
increase(numb);
console.log(`number is: ${numb}`); //10

Arrow function expressions

let square = x => x = x * x;
let y = 10;
let result = square(y);

//template literals use `` and ${}
console.log(`The value of variable y is: ${y} `); // 10 -- no change
console.log(`The result is: ${result} `); // 100

Primitive types (also known as Value types)

So, when we're copying simple primitive types, we call that passing by value because we are literally taking that whole value and saving it in another variable.


Reference types

Unlike value types, a reference type doesn't store its value directly. Instead, it stores the address where the value is being stored. In other words, a reference type contains a pointer to another memory location that holds the data.

  • Objects
  • Arrays
  • Functions

When we're doing it with more complex types, JavaScript isn't copying over the whole object, it's just copying a reference to it, so it's called passing by reference. So, if you had a complex object with thousands of properties in it or an array with millions of elements, then JavaScript isn't making copies of it in memory!

//Objects are copied by their referenc!

let a = { value: 10 };
let b = a;

a.value = 20;

console.log(`a.value is: ${a.value}`);
console.log(`b.value is: ${b.value}`);

//Both a and b are pointing to the same memory address

Call by Reference example

my codepen

528 views
N
Neha Soni5y ago

Good job

1

Introduction to Programming

Part 1 of 9

This module is focused on adding to the basic programming skill set you developed in Introduction to Programming I and giving you experience working with existing OOP code and third-party libraries.

Up next

Shallow vs Deep Copy

Deep Copy methods JSON.parse(JSON.stringify()) Service Workers postMessage() onmessage History API history.pushState(obj, title) history.state Notification API new Notification("title", {data: obj} ).data Build a custom recursive function Deep ...

More from this blog

pedbad

39 posts

Head of eLearning at the University of Cambridge Language Centre & Online Tutor for the BSc Computer Science, Goldsmiths, University of London.