Start learning free

LESSON 12 OF 348 · VARIABLES

Variable Reassignment in TypeScript: Changing a Stored Value

Lesson Read the full explanation

You can always change the value stored in a variable with the assignment operator(=):

// Initializing with 5:
let myVar = 5;

// Assigning the value 10:
myVar = 10;

console.log(myVar); // Output: 10

First, this code creates a variable named myVar and initializes it with 5.

Then, the code assigns 10 to myVar. Now, when myVar appears again in the code, the program will treat it as if it is 10.


Assigning the Value of One Variable to Another:

You can also assign the value of some variable to another variable using the assignment operator:

let myVar = 5;
let myNum = 10;

myVar = myNum;

console.log(myVar); // Output: 10

First, this code creates a variable named myVar and initializes it with 5 and another variable named myNum and initializes it with 10.

Then, the contents of myNum (which is 10) is assigned to the variable myVar. Now, myVar also has the value of 10.


Assignment
Follow the Coding Tutorial and let's reassign some variables.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · VARIABLES

    Variable Reassignment in TypeScript, explained

    Changing what a variable holds

    A variable declared with let can hold a different value later. You change it with the assignment operator =, written on its own line without let:

    let myVar = 5;
    myVar = 10;
    console.log(myVar);
    
    10
    

    The first line declares myVar and gives it the starting value 5. The second line reassigns it. From that point on, every use of myVar sees 10, and the old value is gone.

    Declaring is different from assigning

    Two actions that look alike have different jobs:

    • let age = 24; creates a new variable. It may happen once for each name.
    • age = 34; stores a new value in a variable that already exists. It may happen as often as you like.

    If you write let a second time for the same name, TypeScript treats it as a second declaration and refuses:

    let age: number = 24;
    let age: number = 34;
    

    The compiler reports Cannot redeclare block-scoped variable 'age'. Reassigning a name that was never declared fails too, with Cannot find name 'age'.

    Copying one variable into another

    The right side of = can be any expression, including another variable. The value is copied:

    let myVar = 5;
    let myNum = 10;
    myVar = myNum;
    myNum = 99;
    console.log(myVar, myNum);
    
    10 99
    

    After myVar = myNum, both hold 10. Changing myNum afterwards does not affect myVar, because myVar received a copy of the value, not a link to the other variable.

    Using the old value to compute the new one

    The right side is worked out before the assignment happens, so it can use the variable's current value:

    let count = 1;
    count = count + 1;
    count += 1;
    console.log(count);
    
    3
    

    Here count + 1 is calculated first, then stored back in count. The line count += 1 is a shorthand for the same thing.

    Order matters

    Statements run from top to bottom, so a variable has to be declared before you assign to it. Writing age = 5; above let age = 3; is reported as Block-scoped variable 'age' used before its declaration. Once a value is assigned, code above that line never sees it. It only affects the lines that run after it.

    Not every variable can be reassigned. A variable declared with const is fixed after its declaration, which a later lesson covers. The variables in this exercise use let, which is why reassignment is allowed.

    The type stays the same

    An annotated variable keeps its type. Assigning a value of another type is a compile error:

    let age: number = 24;
    age = "thirty";
    

    The compiler reports Type 'string' is not assignable to type 'number'. The same happens without the annotation, because TypeScript infers the type from the first value. Reassignment changes the value and keeps the type.

    The lesson's exercise

    The program prints facts about a developer, and then it prints them again "ten years later". It declares role, age and language. The task is to update role and age in the marked area, so the second set of lines is accurate, and to leave language as it is.

    1. Reassign role with role = "Senior Developer";. There is no let and no type annotation, because the variable already exists and already has its type.
    2. Reassign age to ten more than 24, which is 34.
    3. Do not touch language. He kept the same language, so the second set of lines should still say Java.
    role = "Senior Developer";
    age = 34;
    

    These two lines go between the first and second set of console.log calls. The first group prints the old values, and the second group prints 34, Senior Developer and Java. The order matters: moving the assignments above the first group would make both groups print the new values.

    Common mistakes

    • Writing let again. That declares the name a second time and is an error.
    • Adding a type annotation to the reassignment. Annotations belong to the declaration only.
    • Reassigning in the wrong place. A reassignment affects only the code that runs after it.

    Key takeaways

    • Declare once with let, then reassign with = as often as needed.
    • A reassignment replaces the old value, and code after it sees the new one.
    • Assigning one variable to another copies the value.
    • The variable keeps its type, so the new value must match it.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free