menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

How Values...
source image

Medium

1M

read

58

img
dot

Image Credit: Medium

How Values Get Passed in JavaScript Functions

  • In JavaScript, primitive values like numbers, strings, and booleans are copied when passed into functions.
  • The function works with a copied version of the primitive, leaving the original value unchanged.
  • Passing primitives to functions ensures that changes made inside the function do not affect the original value.
  • JavaScript sets aside new memory space for copies of primitives during function calls.
  • Primitives provide predictability as their values don't change unless intentionally reassigned.
  • On the other hand, objects and arrays are passed by reference in JavaScript functions.
  • When objects or arrays are passed, both the original variable and the function parameter share the same underlying object.
  • Changes made to objects or arrays inside a function reflect outside because they operate on the same shared memory location.
  • Passing objects or arrays into functions can lead to unexpected changes if not handled carefully.
  • While passing by reference may introduce surprises, it also allows JavaScript to handle complex data structures efficiently.

Read Full Article

like

3 Likes

For uninterrupted reading, download the app