Explain passed by value and passed by reference.

bookmark

JavaScript provides two different categories of datatypes Primitive and Objects

Primitive Datatypes: Number, Boolean, String, Null and Undefined.

Objects: Arrays, functions, plain objects and more Anything except primitive are objects.

NOTE: All primitive data types in JavaScript is passed by value 

Pass by value: 

In this, the function is called by passing the value directly as an argument. So, any changes that’s made inside a function won’t affect the actual value.

The parameters passed as arguments are mutated (Creation of own copy). So, any changes made inside the function is made to a copied value but not for the original one.

Example: 

<script>

   let a = 1;

   let change = (val) => 

 {

      val = 2

   }

   change(a);

   document.write(a);

</script>