Featured Article

Call By Value And Call By Reference In JavaScript W3Schools Explained

Adam Jul 13, 2026

When working with JavaScript, understanding the concepts of "call by value" and "call by reference" is paramount to writing efficient and bug-free code. These concepts, borrowed from other programming languages, might seem straightforward, but they can lead to confusion and surprising behavior in JavaScript. Let's dive into these topics, using w3schools as our guiding resource.

Call by Value vs Call by Reference
Call by Value vs Call by Reference

Before we delve into JavaScript's behavior, let's briefly recap these concepts. In call by value, the value of an argument is copied into the parameter of the called function. Changes made to the parameter inside the function do not affect the original argument. In call by reference, the reference (memory address) of the argument is copied into the parameter. Changes to the parameter inside the function reflect in the original argument.

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

Call by Value in JavaScript

In JavaScript, primitive types like Numbers, Strings, Booleans, and Null/Undefined are passed by value. This means when you pass these types as arguments to a function, the function receives a copy of their value, not the original.

Callbacks In JavaScript
Callbacks In JavaScript

For instance, consider the w3schools code snippet:

function changeValue(a) { a = 10; console.log(a); } var x = 5; changeValue(x);

Here, `changeValue(x)` doesn't modify the original value of `x`. The output is `10`, not `5`.

How to Create a Function in JavaScript: Step-by-Step Guide 🚀
How to Create a Function in JavaScript: Step-by-Step Guide 🚀

Call by Value and Data Types

JavaScript passes data types like Numbers and Strings by value, but it seems to break this rule with floating-point numbers. If you pass a floating-point number with a precision less than 64, JavaScript actually passes it by reference. This behavior is due to JavaScript's ingestion of the IEEE 754 standard for floating-point arithmetic.

Here's an example demonstrating this idiosyncrasy:

JavaScript calculator projet
JavaScript calculator projet

function changeFloat(a) { a.w = 25; console.log(a, a.w); } var x = new Number(3.98765432190); changeFloat(x);

The output is `{w: 25, 3.9876543219}`, demonstrating that `x.w` is a different variable than the original `x`.

Call by Reference in JavaScript

In JavaScript, objects and functions (which are special types of objects) are passed by reference. This means when you pass an object or function as an argument, the function receives a reference to the original object or function.

Student Management System In JavaScript With Source Code
Student Management System In JavaScript With Source Code

To illustrate this, consider the code from w3schools:

function changeObject(o) { o.name = "John"; console.log(o); } var person = new Object(); person.name = "Mary"; changeObject(person);

The output is `{name: "John"}` because `changeObject` alters the original object `person`.

Javascript String Methods
Javascript String Methods
Variables in JavaScript Explained Simply
Variables in JavaScript Explained Simply
JavaScript Functions Cheat Sheet Every Beginner Needs âš¡
JavaScript Functions Cheat Sheet Every Beginner Needs âš¡
a computer screen with the words css basics on it
a computer screen with the words css basics on it
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
Javascript
Javascript
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
Important Method in JavaScript
Important Method in JavaScript
JavaScript while Loop: Repeat Tasks Efficiently 🔄
JavaScript while Loop: Repeat Tasks Efficiently 🔄
different types of languages and their uses
different types of languages and their uses
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript Basics | Chapter 1 📖 | Part 2
JavaScript Basics | Chapter 1 📖 | Part 2
Javascript Read Excel
Javascript Read Excel
The Complete Guide to JavaScript Classes
The Complete Guide to JavaScript Classes
JavaScript loops by Duomly
JavaScript loops by Duomly
Javascript Add Text To Image
Javascript Add Text To Image
a poster with different types of projects and instructions for students to use in the classroom
a poster with different types of projects and instructions for students to use in the classroom
🔥 JavaScript Chapter 1 Notes 2026 📒✨
🔥 JavaScript Chapter 1 Notes 2026 📒✨
a diagram showing the different types of data
a diagram showing the different types of data

Call by Reference and Array Mutations

However, when it comes to arrays, JavaScript's behavior is a bit more complex. Modifying an array inside a function doesn't change the original array if the modification creates a new array. But if you modify elements of an existing array, those changes are reflected in the original array.

For instance, consider this w3schools code snippet:

function changeArrayElement(arr) { arr[0] = 'Hello'; } var fruits = ['Apple', 'Banana', 'Orange']; changeArrayElement(fruits);

The output is `'Hello'`, demonstrating that `fruits` was modified. But if you push a new element into the array, as in `arr.push('Hello')`, the original `fruits` array remains unchanged.

Understanding these nuances in JavaScript's call by value and call by reference behavior will help you write clearer, more predictable code. Keep exploring w3schools and other resources to delve deeper into these essential JavaScript concepts.