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.

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.

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.

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`.

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:

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.

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`.




















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.