Understanding passing JS objects and return values

I’m studying some JS code and compared to other languages it’s not making any sense to me. First, there’s a date object that’s declared and that I understand,

$ns.constant = {
    date: {}, /* Input date */

Then there’s functions like this one below where a date is passed to a function. Here’s what I don’t understand. In other languages, when you pass a variable to a function, that function creates a new variable that’s local to the function and the function acts upon that local variable.

This function returns date.julian. But it also adds other dates like date.j2000 and date.1950. In my understanding of other languages, those aren’t being returned because they’re the date object local to the function. It would seem to me that in order to put those values into the date constant you’d have to return the entire date object the function created and not just date.julian. Or, is js just seeing that date being passed and somehow knows that it’s the date constant?

Is this function modifying the date constant, an object that’s local to the function or what?


$ns.julian.calc = function (date) {
    var centuries;
    var year;
    var month;
    var b = 0;
    var c;
    var e;

    year = date.year + 4800;
    if (date.year < 0) {
        year += 1;

    month = date.month;
    if (month <= 2) {
        month += 12;
        year -= 1;
    e = Math.floor ((306 * (month + 1)) / 10);

    centuries = Math.floor (year / 100);

    if (date.year <= 1582) {
        if (date.year == 1582) {
            if (date.month < 10) {
                b = -38;
            if (date.month > 10 || date.day >= 15) {
                b = Math.floor ((centuries / 4) - centuries);
        else { b = -38; } 
    } else {
        b = Math.floor ((centuries / 4) - centuries);
    c = Math.floor ((36525 * year) / 100);

    date.julianDate = b + c + e + date.day - 32167.5;

    date.julianTime = (3600.0 * date.hours + 60.0 * date.minutes + date.seconds) / 86400.0;

    date.julian = date.julianDate + date.julianTime;

    date.j2000 = 2000.0 + (date.julian - $const.j2000) / 365.25;
    date.b1950 = 1950.0 + (date.julian - $const.b1950) / 365.25;
    date.j1900 = 1900.0 + (date.julian - $const.j1900) / 365.25;

    return date.julian;


It appears that you’re passing a date “object” to your function and then adding things to it, which will make the original date passed to that function update as well, not just local to the function.

In all languages, not just javascript, objects and heap references are passed by reference, while stack variables are passed by value (a copy local to the function). An object, in any language, will be passed by reference and whatever you change on that object will also make the original object change.

That’s because the “date” that you’re passing is a pointer or reference to a heap location in memory where you’re updating things, so when you later, outside of that function, use “date” again it’s still pointing to the same heap address where you updated things. Changing the values on a heap reference will reflect in anything that’s pointing to the same reference.

You might also be confused with how immutability works since you declared “date” as a constant, but you assigned that date an object. What that means is that the reference to that heap location (date) can’t change, so you couldn’t i.e. do date = {} since you can’t reassign a new address to that pointer, however the values on that object can still change, they are not immutable, so you can do something like date.example = 'example'. I’m not aware of a data structure in JS that’s recursively immutable unless you use something like https://immutable-js.com/.