Hello coders, This is going to be our 7th question of the front-end interview questions series you can access all the questions by clicking here

Before proceeding further we should have a idea about primitive and non-primitive data types if you are not clear about them click here before moving further.

Everything in Javascript is pass-by-value except objects, when we pass objects, the value passed is actually the reference address of it.

Pass by value

In pass by value, when a variable is passed as an argument to a function, changing the argument inside the function does not modify the original variable’s value. JavaScript copies the values of the passing variables into arguments inside the function.

Pass by value example

In this example, the add function takes a a as an argument (num). Inside the function, we add num to num. However, since we’re working with a copy of the original a variable, the original value outside the function remains unchanged.

Pass by Reference

In pass by reference, when we pass the objects/arrays (complex data types) to the function as an argument, JavaScript does not create a new space in the memory, instead, we pass the reference/address of the actual parameter, which means the function can access the original value of the variable. Thus, if we change the value of the variable inside the function, then the original value also gets changed.

Pass by value example

Here, both student and obj share the same reference, so modifying the age property inside the function updates the student object outside the function.

If you find this helpful please share with your network ✅.

Happy Coding 🙌 !