Featured Article

Pass By Value And Pass By Reference In Javascript W3schools Guide

Adam Jul 13, 2026

In the dynamic world of JavaScript, understanding Pass by Value and Pass by Reference is crucial for manipulating data effectively. This concept, often clouded by confusion, is clear once you grasp the nuances of each. Let's explore these concepts, using examples inspired by W3Schools exercises.

Javascript Fundamental
Javascript Fundamental

JavaScript primarily uses Pass by Value for primitive types, and Pass by Reference for objects and functions. But first, let's understand the two concepts:

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Pass by Value

In Pass by Value, a copy of the variable's value is passed to the function. This means changes in the function don't affect the original variable. Let's consider a number:

"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"

Paraguay

Numbers and strings in JavaScript are immutable, meaning you can't alter their content. Here's an example:

JavaScript study guide for fast learning
JavaScript study guide for fast learning

```javascript let num1 = 5; function changeValue(num) { num = 10; console.log(num); // Outputs: 10 } changeValue(num1); console.log(num1); // Outputs: 5 ```

Despite changing 'num' inside 'changeValue', 'num1' remains 5 because changes are applicable to the copy, not the original value.

Immutable Strings

Strings also rely on Pass by Value. Here's a string manipulation example:

Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial

```javascript let str1 = 'hello'; function changeString(str) { str = 'world'; console.log(str); // Outputs: world } changeString(str1); console.log(str1); // Outputs: hello ```

Even though we changed 'str' inside 'changeString', the original 'str1' is untouched, demonstrating the Pass by Value behavior of immutable types.

Pass by Reference

Pass by Reference does not copy the object's value; it passes the memory address of the original object. Changes made to the object within the function reflect in the original object. Consider an object:

Examination Apps in JavaScript with Source Code
Examination Apps in JavaScript with Source Code

Arrays: Mutable Objects

Arrays are objects in JavaScript. Here's an example demonstrating their mutability and Pass by Reference behavior:

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
8 Essential JavaScript Array Methods Visual Guide
8 Essential JavaScript Array Methods Visual Guide
How To Print A Page Or Section in Javascript
How To Print A Page Or Section in Javascript
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
Coding with Parvez
Coding with Parvez
the web programming poster is shown
the web programming poster is shown
JavaScript Roadmap for Beginners 2026
JavaScript Roadmap for Beginners 2026
an image of programming tricks and tips for beginners to learn in this video game
an image of programming tricks and tips for beginners to learn in this video game
an image of a web page with different types of text
an image of a web page with different types of text
Javascript Divisor Function, Arrow Function Javascript, How To Use Arrow Functions In Javascript, Javascript Scope Diagram, What Is Arrow Function In Javascript, Arrow Functions In Javascript Text, Javascript Syntax Infographic, How To Find Divisors In Javascript, Javascript Scope Explanation
Javascript Divisor Function, Arrow Function Javascript, How To Use Arrow Functions In Javascript, Javascript Scope Diagram, What Is Arrow Function In Javascript, Arrow Functions In Javascript Text, Javascript Syntax Infographic, How To Find Divisors In Javascript, Javascript Scope Explanation
JavaScript if Statement: Control Program Flow Like a Pro ✅
JavaScript if Statement: Control Program Flow Like a Pro ✅
Learn JavaScript in 30 Days
Learn JavaScript in 30 Days
Add Table Rows & Cells In Javascript
Add Table Rows & Cells In Javascript
what jaascrit can do to improve the language of web design and development
what jaascrit can do to improve the language of web design and development
The Complete Guide to JavaScript Classes
The Complete Guide to JavaScript Classes
a diagram showing the different types of data
a diagram showing the different types of data
JavaScript Assignment Operators: Simplify Your Code Efficiently ✅
JavaScript Assignment Operators: Simplify Your Code Efficiently ✅

```javascript let arr = [1, 2, 3]; function changeArray(arr) { arr.push(4); console.log(arr); // Outputs: [1, 2, 3, 4] } changeArray(arr); console.log(arr); // Outputs: [1, 2, 3, 4] ```

We pushed '4' into 'arr' inside 'changeArray', reflecting in the original 'arr'. This happens because arrays are passed by reference, allowing us to modify their content.

Objects: Pass by Reference

Basic objects also follow Pass by Reference. Here's an example:

```javascript let obj = {a: 1, b: 2}; function changeObject(obj) { obj.c = 3; console.log(obj); // Outputs: {a: 1, b: 2, c: 3} } changeObject(obj); console.log(obj); // Outputs: {a: 1, b: 2, c: 3} ```

We added a new property 'c' to 'obj' inside 'changeObject', showing up in the original 'obj', exemplifying Pass by Reference.

Understanding these concepts enables you to write efficient and predictable code. Next, you might want to explore the implications of these behaviors in asynchronous JavaScript for a deeper understanding. Happy coding!