Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6730847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:27:29+00:00 2026-05-26T10:27:29+00:00

I know Javascript always passes objects by reference. But what about the other types?

  • 0

I know Javascript always passes objects by reference. But what about the other types?

number – ?
string – Immuteable, so it shouldn’t matter
object – ref
array – ?
function – ?
regexp – ?

I came to the conclusion that not all values in Javascript can be objects nor can they be pass by reference with the following code:

function SomeFunc(func) {
    var numba = 0;
    var innerFunc = function() {
       numba = func(numba);//try commenting me out and switching to next line
        //func(numba);//numba fails to increment
       return numba;
    };
   return innerFunc;
}

//this variable should persist the numba variable through closure.  Incrementing each call by 1.
var closure = SomeFunc(function(numba) {
    numba = numba + 1;
    return numba;
});

document.writeln(closure());
document.writeln(closure());
document.writeln(closure());

Because numba fails to increment, unless I return the number and updated the variable in closure…then that tells me this is not pass by reference. Am I missing something?


or take the very basic

function UpdateNumber(numba) {
    numba = 10;  
   document.writeln('in func number becomes ' + numba);
}

var numba2 = 5;
UpdateNumber(numba2);

document.writeln('back to caller number is ' + numba2);
//in func number becomes 10 
//back to caller number is 5 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:27:29+00:00Added an answer on May 26, 2026 at 10:27 am

    No. JavaScript always passes by value, not by reference.

    People often confuse the ability to modify an object in a method and have the modification be visible on the outside as being pass by reference. This is not the case. Pass by reference means modifying what the value points to is visible outside the function. For example

    var f1 = function (p1) {
      p1 = 42;
    };
    var x = {};
    f1(x);
    

    In this example if JavaScript implemented pass by reference then x === 42 would be true. However it’s not. The variable x would still have the value {}

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You know what I liked best about obtrusive javascript? You always knew what it
I do not know javascript right now but I am planning on learning it.
I've been using both in javascript ... really don't know the difference. Googling always
I believe javascript can be anywhere (almost), but I almost always see it in
I've always wanted to know what is, in a general way, the opinions about
I know zero javascript, and have one simple task: Find all text in the
I know that javascript, for example supports functions inside of functions, like so: function
I know that javascript doesn't have pointers in terms of a variable referring to
I know inline Javascript is frowned upon and with the new on-the-fly Javascript compressors
Do you know a JavaScript library that implements a generic Iterator class for collections

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.