Bird
Raised Fist0
Typescriptprogramming~10 mins

Fresh object literals vs variable assignment behavior in Typescript - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a fresh object literal assigned to obj.

Typescript
const obj = [1];
Drag options to blanks, or click blank then click option'
Aundefined
B[]
Cnull
D{}
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of curly braces {}
Assigning null or undefined instead of an object
2fill in blank
medium

Complete the code to assign obj2 to the same object as obj1.

Typescript
const obj1 = {};
const obj2 = [1];
Drag options to blanks, or click blank then click option'
Aobj1
B{}
Cnull
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a new object literal instead of the existing variable
Assigning null or undefined
3fill in blank
hard

Fix the error in the code to correctly copy properties from source to target without sharing the same object.

Typescript
const source = { a: 1 };
const target = [1];
// Now target and source are separate objects
Drag options to blanks, or click blank then click option'
A{ ...source }
Bsource
CObject.assign(source)
Dsource = {}
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning source directly instead of copying
Using Object.assign incorrectly without target object
4fill in blank
hard

Complete the code to create a new object copy with all properties from original and add a new property id with value 5.

Typescript
const original = { name: 'Alice' };
const copy = [1] , { id: 5 };
Drag options to blanks, or click blank then click option'
A{ ...original }
B+
C...
D,
Attempts:
3 left
💡 Hint
Common Mistakes
Using plus (+) to combine objects
Using spread operator incorrectly outside object literal
5fill in blank
hard

Fill all three blanks to create a fresh object result that copies data, overrides count to 10, and adds a method show that logs count.

Typescript
const data = { count: 3 };
const result = {
  [1],
  count: [2],
  show() { console.log(this.[3]); }
};
Drag options to blanks, or click blank then click option'
A...data
B10
Ccount
Ddata.count
Attempts:
3 left
💡 Hint
Common Mistakes
Using data.count inside the method instead of this.count
Not overriding the count property correctly

Practice

(1/5)
1. In TypeScript, what happens when you assign an object variable to another variable using let b = a;?
easy
A. The original object is deleted.
B. A new copy of the object is created.
C. The object is converted to a string.
D. Both variables point to the same object in memory.

Solution

  1. Step 1: Understand variable assignment with objects

    Assigning one object variable to another copies the reference, not the object itself.
  2. Step 2: Effect of reference copying

    Both variables now point to the same object in memory, so changes via one affect the other.
  3. Final Answer:

    Both variables point to the same object in memory. -> Option D
  4. Quick Check:

    Object assignment copies reference = A [OK]
Hint: Assigning objects copies references, not objects themselves [OK]
Common Mistakes:
  • Thinking a new object is created on assignment
  • Assuming variables hold independent copies
  • Confusing object assignment with primitive assignment
2. Which of the following is the correct way to create a fresh object literal in TypeScript?
easy
A. let obj = obj2;
B. let obj = Object.assign({});
C. let obj = { name: 'Alice' };
D. let obj = new Object(obj2);

Solution

  1. Step 1: Identify fresh object literal syntax

    Using curly braces {} creates a new, fresh object literal.
  2. Step 2: Check other options

    let obj = Object.assign({}); is incorrect because Object.assign({}) needs a source object to copy from; let obj = obj2; copies reference; let obj = new Object(obj2); creates an object but with a reference or unexpected behavior.
  3. Final Answer:

    let obj = { name: 'Alice' }; -> Option C
  4. Quick Check:

    Fresh object literal uses {} = A [OK]
Hint: Use curly braces {} to create fresh objects [OK]
Common Mistakes:
  • Using assignment to copy objects instead of fresh literals
  • Misusing Object.assign without source
  • Confusing new Object() with fresh literal
3. What will be the output of this TypeScript code?
let a = { x: 1 };
let b = a;
b.x = 5;
console.log(a.x);
medium
A. 1
B. 5
C. undefined
D. Error

Solution

  1. Step 1: Understand reference assignment

    Variable b points to the same object as a.
  2. Step 2: Modify property via b

    Changing b.x to 5 changes the shared object property.
  3. Step 3: Log a.x

    Since a and b reference the same object, a.x is now 5.
  4. Final Answer:

    5 -> Option B
  5. Quick Check:

    Shared reference means change visible = B [OK]
Hint: Changing one reference changes all pointing to same object [OK]
Common Mistakes:
  • Expecting original object to remain unchanged
  • Confusing object copying with reference assignment
  • Assuming primitive copy behavior for objects
4. Identify the bug in this TypeScript code snippet:
let obj1 = { val: 10 };
let obj2 = obj1;
obj2 = { val: 20 };
console.log(obj1.val);
medium
A. obj2 assignment creates a new object, obj1 remains unchanged.
B. Both obj1 and obj2 point to the new object.
C. Syntax error due to reassignment.
D. obj1.val will be 20, which is unexpected.

Solution

  1. Step 1: Analyze initial assignment

    obj2 initially points to the same object as obj1.
  2. Step 2: Reassign obj2 to a new object

    obj2 now points to a fresh object; obj1 still points to the original.
  3. Step 3: Check console output

    obj1.val remains 10 because obj1's reference did not change.
  4. Final Answer:

    obj2 assignment creates a new object, obj1 remains unchanged. -> Option A
  5. Quick Check:

    Reassignment changes reference, original unaffected = D [OK]
Hint: Reassigning variable changes reference, original object stays same [OK]
Common Mistakes:
  • Thinking obj1 changes when obj2 is reassigned
  • Confusing property change with variable reassignment
  • Expecting both variables to point to new object
5. You want to create two independent objects in TypeScript so that changing one does not affect the other. Which code snippet achieves this correctly?
const obj1 = { a: 1 };
const obj2 = ???
obj2.a = 5;
console.log(obj1.a);
hard
A. obj2 = { ...obj1 };
B. obj2 = Object.assign(obj1);
C. obj2 = obj1;
D. obj2 = obj1 as any;

Solution

  1. Step 1: Understand shallow copy with spread operator

    Using { ...obj1 } creates a fresh object with copied properties.
  2. Step 2: Check other options

    obj2 = obj1; copies reference; obj2 = Object.assign(obj1); is incorrect usage of Object.assign; obj2 = obj1 as any; is just a type assertion, no copy.
  3. Step 3: Confirm independence

    Changing obj2.a does not affect obj1.a.
  4. Final Answer:

    obj2 = { ...obj1 }; -> Option A
  5. Quick Check:

    Spread operator creates fresh object copy = C [OK]
Hint: Use spread {...obj} to copy objects independently [OK]
Common Mistakes:
  • Assigning reference instead of copying
  • Misusing Object.assign without target
  • Confusing type assertion with copying
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard