Pass by value
What you'll learn:
- What "pass by value" means
- Which kinds of values JavaScript passes by value
- Why changing a number inside a function does not change the original
When you pass a primitive value — a number, string, or boolean — JavaScript hands the function a copy. Whatever the function does to that copy, the original variable outside stays the same.
Primitives are: number, string, boolean, undefined, null.
Pass by reference
What you'll learn:
- What "pass by reference" means
- Which kinds of values JavaScript passes by reference
- Why pushing to an array inside a function changes the original
When you pass an object or an array, JavaScript hands the function a reference — a direct pointer to the same spot in memory. If the function modifies the object or array, the original is modified too.
Try it: Read the code and predict what each console.log will print. Then run it and check.