In JavaScript, the type of data determines whether it’s passed by value or reference. Primitive types (like strings, numbers, booleans) are passed by value:
var myName = "Kyle";
var yourName = myName;
myName = "Frank";
console.log(myName); // Frank
console.log(yourName); // KyleEach variable holds its own copy. In contrast, objects (arrays, functions, etc.) are passed by reference:
var myAddress = { street: "123 JS Blvd" };
var yourAddress = myAddress;
myAddress.street = "456 TS Ave";
console.log(yourAddress.street); // 456 TS AveBoth variables point to the same object in memory, so changes are shared.
JavaScript supports many ways to define functions. One common form is the anonymous function expression:
var awesomeFunction = function(coolThings) {
return amazingStuff;
};In ES6, JS may infer a name for anonymous functions, but it’s only metadata — not usable as an identifier.
var awesomeFunction = function someName(coolThings) {
return amazingStuff;
};
awesomeFunction.name; // "someName"Here, someName is an internal identifier, useful for recursion or debugging.
If a function exists, it has a purpose — and that purpose deserves a name. Naming functions improves readability, debugging, and long-term maintenance. Even a simple function like x * 2 benefits from being called double or multBy2.
JavaScript offers many function forms:
function* two() {}async function three() {}x => x * 2(function(){})()class A { method() {} }{ method() {}, old: function() {} }Each form has its use case. For example, arrow functions preserve this lexically, but they’re not ideal for every situation.
Understanding the difference between value and reference, and mastering the various function forms, are essential skills for writing professional JavaScript. Naming functions clearly and choosing the right form for each task improves clarity and maintainability across your codebase.