Ask Your Question
3

Is it possible to pass arguments exclusively by reference to function in JavaScript?

asked 2021-05-04 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-07-02 01:00:00 +0000

bukephalos gravatar image

No, it is not possible to pass arguments exclusively by reference to a function in JavaScript. This is because JavaScript passes primitive data types (e.g. numbers, strings, booleans) by value and objects (including arrays and functions) by reference.

When a primitive data type is passed to a function, a copy of its value is created and passed to the function. This means that any changes made to the parameter within the function are not applied to the original value.

When an object is passed to a function, a reference to the original object is passed. This means that any changes made to the parameter within the function will affect the original object.

Therefore, in JavaScript, it is not possible to pass primitive data types by reference without wrapping them in an object (e.g. passing an object containing a number instead of a number itself).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-05-04 11:00:00 +0000

Seen: 11 times

Last updated: Jul 02 '22