Lesson not found
Lesson Progress: 0%
let a: number = 10;
let b: number = a;
b = 20;
console.log(a);
console.log(b);Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
Primitive types like number, string, and boolean are stored directly in the variable. When you copy a primitive into another variable, TypeScript makes a brand new separate copy. This means the two variables do not share anything, and changing one will never affect the other.
a starts with the number 10.let b: number = a copies the value 10 into a new separate box called b.b is changed to 20, only b is updated.a stays 10 because primitives are completely independent after copying.let obj1: { value: number } = { value: 10 };
let obj2 = obj1;
obj2.value = 20;
console.log(obj1.value);
console.log(obj2.value);Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
Objects are reference types in TypeScript. When you assign an object to another variable, you are not making a copy. Instead, both variables point to the same object in memory. This is like two people sharing one locker — if one person changes what is inside, the other person sees the change too.
obj1 holds an object { value: 10 } in memory.let obj2 = obj1 does not make a copy. It makes obj2 point to the exact same object.obj2.value is set to 20, the shared object is changed.console.log calls print 20 because both variables see the same changed object.let arr1: number[] = [1, 2, 3];
let arr2 = arr1;
arr2.push(4);
console.log(arr1);
console.log(arr2);Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
Arrays are also reference types in TypeScript. Just like objects, when you assign an array to another variable, both variables point to the same array in memory. Any change you make to the array through one variable will be visible through the other variable as well.
arr1 holds an array [1, 2, 3] in memory.let arr2 = arr1 makes arr2 point to the same array, not a copy.arr2.push(4) adds a new item to the shared array.console.log calls print the same thing: [1, 2, 3, 4].arr2 to be an independent copy ofarr1.let x: { color: string } = { color: "red" };
let y = x;
y = { color: "blue" };
console.log(x.color);
console.log(y.color);Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
Reassigning a variable to a new object breaks the shared reference. When you use the assignment operator = to give a variable a brand new object, that variable starts pointing to the new object instead. The other variable keeps pointing to the original object.
x and y start by pointing to the same object { color: "red" }.y = { color: "blue" } creates a new object and points y to it.x is pointing to.console.log prints red because x still points to the original object.console.log prints blue because y now points to the new object.let original: { a: number; b: number } = { a: 1, b: 2 };
let copy = { ...original };
copy.a = 99;
console.log(original.a);
console.log(copy.a);Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
To make a true independent copy of an object, you can use the spread operator .... The spread operator pulls all the properties out of the original object and places them into a new object. The new object is completely separate from the original.
original holds an object { a: 1, b: 2 }.let copy = { ...original } spreads the properties into a brand new object.copy.a is changed to 99, only the copy is affected.a stays 1 because the copy is completely independent.let nums: number[] = [10, 20, 30];
let spreadCopy = [...nums];
spreadCopy.push(40);
console.log(nums);
console.log(spreadCopy);Instructions
▲ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
The spread operator ... also works for arrays. It pulls all the elements out of the original array and places them into a new array. This gives you a true independent copy that you can modify without affecting the original.
nums holds an array [10, 20, 30].let spreadCopy = [...nums] creates a brand new array with the same numbers.spreadCopy.push(40) adds a new number only to the copy.nums stays [10, 20, 30] because the copy is completely separate.Test Incomplete