- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Describe pass by value and pass by reference in JavaScript?
Pass by value
In pass by value, a function is called by directly passing the value of the variable as the argument. Changing the argument inside the function doesn’t affect the variable passed from outside the function. Javascript always pass by value so changing the value of the variable never changes the underlying primitive (String or number).
In the following example, variable 'a' has assigned value 1. But inside function 'change' it got assigned with value 2. Since javascript is always a pass by value, the displayed output will be '1' but not '2'.
Example
<html> <body> <script> let a = 1; let change = (val) => { val = 2 } change(a); document.write(a); </script> </body> </html>
Output
1
Pass by reference
There are some instances that address is passed instead of arguments to call a function. At that time, changing the value inside the function affect the variable passed from outside the function. This is called a pass by reference. In javascript mostly arrays and objects follow pass by reference.
In the following example an object named 'a' is declared outside the function 'change'. Here one should heed that variable 'a' got mutated but not assigned with value 2, as shown in example 2. A pass by reference takes place when mutation has occurred.
Example-1
<html> <body> <script> let a = {num:1}; let change = (val) => { val.num = 2 } change(a); document.write(JSON.stringify(a)); </script> </body> </html>
output
{"num":2}
In the following example instead of mutation, variable 'a' got assigned with value 2. So pass by value takes place and there will be no affect on outside variable.
Example-2
<html> <body> <script> let a = {num : 1}; let change = (val) => { val = {num :2}; } change(a); document.write(JSON.stringify(a)); </script> </body> </html>
output
{"num":1}
- Related Articles
- Is JavaScript a pass-by-reference or pass-by-value language?
- Pass by reference vs Pass by Value in java
- What is Pass By Reference and Pass By Value in PHP?
- Differences between pass by value and pass by reference in C++
- Is java pass by reference or pass by value?
- Pass by reference vs value in Python
- Which one is better in between pass by value or pass by reference in C++?
- Pass an integer by reference in Java
- What is the difference between pass by value and reference parameters in C#?
- What is pass by reference in C language?
- How to pass an array by reference in C++
- How to pass arguments by reference in Python function?
- Why do we pass a Pointer by Reference in C++?
- How to pass arguments by reference in a Python function?
- How do you pass objects by reference in PHP 5?
