• Home
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • React
      • Introduction to React
      • React Developer
    • Python
      • Introduction to Python
      • Python Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Reading Grounds

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds
  • Featured on DanielLaunches

Courses

  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

Lesson 2

Lesson not found

Primitive vs Reference Types

Lesson Progress: 0%

Lesson Progress: 0%
Primitive vs Reference Types
Lesson Incomplete
Lesson not found
Code Example
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.
  • The code editor is below.
  • The left side of the code editor is where you type your input code.
  • The right side of the code editor is where you see the output of your code.
  • Practice by typing the code example above, in the left side of the code editor below.
  • Then click the "Run Code" button, to run the code.
  • You will see the output of your code in the output section.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

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.

  • The variable a starts with the number 10.
  • The line let b: number = a copies the value 10 into a new separate box called b.
  • When b is changed to 20, only b is updated.
  • The variable a stays 10 because primitives are completely independent after copying.
  • This is called "copy by value" because the actual value is copied, not a reference to it.
Code Example
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.
  • The code editor is below.
  • The left side of the code editor is where you type your input code.
  • The right side of the code editor is where you see the output of your code.
  • Practice by typing the code example above, in the left side of the code editor below.
  • Then click the "Run Code" button, to run the code.
  • You will see the output of your code in the output section.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

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.

  • The variable obj1 holds an object { value: 10 } in memory.
  • The line let obj2 = obj1 does not make a copy. It makes obj2 point to the exact same object.
  • When obj2.value is set to 20, the shared object is changed.
  • Both console.log calls print 20 because both variables see the same changed object.
  • This is called "copy by reference" because only the memory address (reference) is copied, not the actual data.
Code Example
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.
  • The code editor is below.
  • The left side of the code editor is where you type your input code.
  • The right side of the code editor is where you see the output of your code.
  • Practice by typing the code example above, in the left side of the code editor below.
  • Then click the "Run Code" button, to run the code.
  • You will see the output of your code in the output section.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

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.

  • The variable arr1 holds an array [1, 2, 3] in memory.
  • The line let arr2 = arr1 makes arr2 point to the same array, not a copy.
  • Using arr2.push(4) adds a new item to the shared array.
  • Both console.log calls print the same thing: [1, 2, 3, 4].
  • This can cause surprising bugs if you expect arr2 to be an independent copy ofarr1.
Code Example
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.
  • The code editor is below.
  • The left side of the code editor is where you type your input code.
  • The right side of the code editor is where you see the output of your code.
  • Practice by typing the code example above, in the left side of the code editor below.
  • Then click the "Run Code" button, to run the code.
  • You will see the output of your code in the output section.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

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.

  • Both x and y start by pointing to the same object { color: "red" }.
  • The line y = { color: "blue" } creates a new object and points y to it.
  • This does not change the object that x is pointing to.
  • The first console.log prints red because x still points to the original object.
  • The second console.log prints blue because y now points to the new object.
Code Example
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.
  • The code editor is below.
  • The left side of the code editor is where you type your input code.
  • The right side of the code editor is where you see the output of your code.
  • Practice by typing the code example above, in the left side of the code editor below.
  • Then click the "Run Code" button, to run the code.
  • You will see the output of your code in the output section.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

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.

  • The variable original holds an object { a: 1, b: 2 }.
  • The line let copy = { ...original } spreads the properties into a brand new object.
  • When copy.a is changed to 99, only the copy is affected.
  • The original object's a stays 1 because the copy is completely independent.
  • This is a safe way to duplicate objects without accidentally sharing data between variables.
Code Example
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.
  • The code editor is below.
  • The left side of the code editor is where you type your input code.
  • The right side of the code editor is where you see the output of your code.
  • Practice by typing the code example above, in the left side of the code editor below.
  • Then click the "Run Code" button, to run the code.
  • You will see the output of your code in the output section.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

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.

  • The variable nums holds an array [10, 20, 30].
  • The line let spreadCopy = [...nums] creates a brand new array with the same numbers.
  • Using spreadCopy.push(40) adds a new number only to the copy.
  • The original nums stays [10, 20, 30] because the copy is completely separate.
  • Using spread is the standard way to safely duplicate arrays and avoid reference bugs.

Test Incomplete

What happens when you copy a primitive type like number into another variable in TypeScript?

Question #

1/15

Score

0/0 - 0.0 %