JavaScript · Web Development

Pass by Value vs Pass by Reference in JavaScript

Type Coercion

All variables in JavaScript are basically objects. Taking into consideration type coercion or dynamic typing, the type of each variable is casted to an object. If you find it hard to believe, try to declare a variable and assign it different values, then call the typeof operator on the just created variable.

Primitive Types

The basic types such as numbers, strings, dates, and booleans are passed by value. That means if a function makes modifications to its parameters, those changes won’t be visible outside the scope of the function.

Composite Types

User defined objects, functions, and arrays are passed by reference. That is any modification we make on the variable in the local scope will be reflected in the global scope.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s