Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Pass by Value and Pass by Reference in Javascript
Next article icon

Pass by Value and Pass by Reference in Javascript

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

JavaScript handles variables in different ways when passing them to functions. Variables in JavaScript can either be passed by value or passed by reference, depending on the type of data they hold.

"Pass by Value" in JavaScript

When a variable is passed by value, a copy of the actual value is passed to the function. Any changes made to the parameter inside the function do not affect the original variable.

In JavaScript, primitive data types are passed by value. These include:

  • Numbers
  • Strings
  • Booleans
  • Undefined
  • Null
  • Symbol
  • BigInt
JavaScript
let n = 10;  function modify(x) {     x = 20;     console.log("Inside function: ", x); }  modify(n); console.log("Outside function: ", n);  

Output
Inside function:  20 Outside function:  10 

In this example

  • The value of n (10) is copied to x.
  • Modifying x inside the function does not affect n because x is a separate copy.

"Pass by Reference" in JavaScript

When a variable is passed by reference, a reference to the actual data is passed. This means changes made to the parameter inside the function affect the original variable.

In JavaScript, non-primitive data types (such as objects and arrays) are passed by reference.

JavaScript
let obj = { name: "Ravi" };  function modify(o) {     o.name = "Arun";     console.log("Inside function: ", o.name); }  modify(obj); console.log("Outside function: ", obj.name); 

Output
Inside function:  Arun Outside function:  Arun 

In this example the obj reference is passed to the function, allowing changes to the original object.

Difference between Pass by Value and Pass by Reference

FeaturePass by ValuePass by Reference
Applies ToPrimitive data typesNon-primitive data types (objects, arrays)
Data CopyCreates a copy of the actual valuePasses a reference to the original data
Effect of ChangesChanges inside the function do not affect the original variableChanges inside the function affect the original variable
Use CasesNumbers, strings, booleans, etc.Arrays, objects, functions

Important Clarification: Does JavaScript Truly Support "Pass by Reference"?

JavaScript does not have "true pass by reference." Instead:

  • Primitive types (like numbers, strings, booleans) are always passed by value—a copy of the value is passed.
  • For objects and arrays, JavaScript passes the reference value, which is essentially a pointer to the memory location of the data. This allows modifications to the object or array but does not allow reassigning the original variable within the function.
JavaScript
let obj = { name: "Ravi" };  function reassignReference(o) {     // Reassigning the reference     o = { name: "Arun" };      console.log("Inside function: ", o.name);  }  reassignReference(obj); console.log("Outside function: ", obj.name); 

Output
Inside function:  Arun Outside function:  Ravi 

In this example

  • The function receives a reference to the original object.
  • However, reassigning o creates a new object, leaving the original object untouched.

Thus, JavaScript passes reference values for objects but not "by reference" as in languages like C++.

Common Pitfalls

1. Unexpected Side Effects

When working with objects or arrays, be cautious of unintended changes to the original data.

JavaScript
let config = { theme: "dark" };  function updateTheme(c) {     c.theme = "light"; }  updateTheme(config); console.log(config.theme);  

2. Cloning to Avoid Reference Issues

To avoid modifying the original object, create a shallow or deep copy.

Shallow Copy

JavaScript
let original = { name: "Ravi" }; let copy = { ...original }; copy.name = "Arun"; console.log(original.name); 

Output
Ravi 

Deep Copy

JavaScript
let original = { name: "Ravi", details: { age: 25 } }; let deepCopy = JSON.parse(JSON.stringify(original)); deepCopy.details.age = 30; console.log(original.details.age); 

Output
25 

Next Article
Pass by Value and Pass by Reference in Javascript

A

AStream26
Improve
Article Tags :
  • Technical Scripter
  • JavaScript
  • Web Technologies
  • Technical Scripter 2020
  • javascript-basics

Similar Reads

    Primitive and Reference value in JavaScript
    In JavaScript, a variable may store two types of values, Primitive values or Reference values. This article will describe and help to compare both these types of values. Primitive value: JavaScript provides six types of primitive values that include Number, String, Boolean, Undefined, Symbol, and Bi
    2 min read
    Reference and Copy Variables in JavaScript
    In this article, we will talk about pass-by-value and pass-by-reference in JavaScript. JavaScript always passes by value, but in an array or object, the value is a reference to it, so you can 'change' the data. JavaScript has 5 primitive data types that are passed by value, they are Boolean, NULL, u
    5 min read
    Call by Value Vs Call by Reference in JavaScript
    Call by Value: In this method, values of actual parameters are copied to the function’s formal parameters, and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in the actual parameters of the caller. Suppose there is a varia
    3 min read
    Set the Value of an Input Field in JavaScript
    We will learn how you can set the value of an input field in JavaScript. We will set user input to the placeholder value of the input field using JavaScript.Below are the approaches to set the value of an input field in JavaScript:Table of ContentBy using the innerHTML propertyBy using setAttribute
    2 min read
    Global and Local variables in JavaScript
    In JavaScript, understanding the difference between global and local variables is important for writing clean, maintainable, and error-free code. Variables can be declared with different scopes, affecting where and how they can be accessed. Global VariablesGlobal variables in JavaScript are those de
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences