Passing values in JS in objects is by reference

Passing in a primitive type variable like a string or a number, the value is passed in by value. Passing in an object, however, passes it in by reference.

Here is an example:

<script>
function printStudent(student){
    for(var i in student.marks){
        console.log(marks[i].subject+" s ocentka "+marks[i].score);
    }
}
var marks=[ {subject:"OOP",score:'6'} ];
var student = {name:"Dido",marks:marks};
printStudent(student);
marks[0].score=5;
printStudent(student);
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *