4 views
How do reference types differ from primitive types?
Asked at
AI answer
In JavaScript, primitive values (for example, number, boolean, string, null, undefined, bigint, and symbol) store the value itself. When assigned or passed to a function, the value is copied: changing one variable does not change the other.
Objects, arrays, and functions are reference values. When assigned, a reference to the same object is copied, so changes to its properties are visible through both variables. However, the variable itself is not “passed by reference”: reassigning a function parameter does not reassign the original variable.
let a = 5;
let b = a;
b++; // a remains 5
const x = { count: 1 };
const y = x;
y.count++; // x.count is now 2
Also asked at СБЕР
- Have you worked with useLayoutEffect and how does it differ from useEffect?
- [name] asked: why did you decide to change your job?
- What is Event Loop in JavaScript?
- What is Virtual DOM and how does it work in React?
- Please tell us about your experience: what tasks do you solve, what is your area of responsibility, what technologies have you worked with, and how are the team processes organized?
Similar questions
- What is the main difference between an object and a primitive? How do a const object and a const string behave when you try to change them?
- After creating a copy of the user using a shallow copy and replacing a field, what is now stored in the variable instead of the previous reference?
- What are reference data types, and why does the runtime need them?
- Analyze the code: an object is passed to a function, and then the parameter is assigned a new value inside the function. Will the original object change?
- Tell me about collections in Java: what types are there, and how do they differ?