Start learning free

LESSON 18 OF 348 · CALCULATIONS WITH NUMBERS

Area of a Rectangle in TypeScript: Multiplying Two Variables

Lesson Read the full explanation

We created two variables to store the width and the height of a rectangle.

On line 3, use these two variables and an arithmetic operation you just learned to print the area of the rectangle to the console.

STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · CALCULATIONS WITH NUMBERS

    Area Of Rectangle in TypeScript, explained

    What the exercise asks

    The starter code stores the size of a rectangle in two variables, width and height. The task is to print the rectangle's area to the console, using an arithmetic operation from the previous lessons. The code has just two lines to begin with, and you add the third:

    let width: number = 23;
    let height: number = 17;
    

    Nothing is printed until you ask for it. Declaring variables only stores values, and console.log is what shows a value in the console.

    The idea

    The area of a rectangle is its width multiplied by its height. A rectangle 23 units wide and 17 units tall can be cut into 17 rows of 23 unit squares, and 17 rows of 23 squares is 23 * 17 squares in total. In TypeScript the multiplication operator is *, so the area is width * height.

    You don't need to write the numbers 23 and 17 anywhere in your solution. Use the variable names. The expression then works for any values stored in the variables, and the code keeps matching the data if someone changes the numbers at the top.

    Step by step

    1. Pick the operation. Area is a product, not a sum. Adding the two sides would give the half-perimeter, 40, which is not what is asked.
    2. Write the expression with the two variables: width * height.
    3. Print it. Put the expression between the parentheses of console.log. TypeScript evaluates the expression first and prints the result. You don't need to save the area in a new variable, although you could.
    let width: number = 23;
    let height: number = 17;
    
    console.log(width * height);
    

    This prints:

    391
    

    Storing the result first is equally correct, and is a good habit when you need the area again later:

    let area: number = width * height;
    console.log(area);   // 391
    

    Language notes

    • Both variables are number. The annotation : number is checked by the compiler and then erased, so it adds nothing at run time. TypeScript has no separate integer and decimal types, so the same code would work if width were 2.5. With 2.5 and 17 the result is 42.5.
    • The compiler guards the types. If one of the variables were a string, such as "23", the compiler would report width * height as an error. Arithmetic on text is not allowed in TypeScript, even though the JavaScript that is still produced would print 391, and you should convert the string with Number first.
    • Whole numbers stay exact. Multiplying two whole numbers gives an exact result as long as it stays below 9,007,199,254,740,992 (2 ** 53). For the sizes in this exercise that limit is far away.
    • Spacing is free. width*height and width * height mean the same. Spaces around operators are only for readability.

    Common mistakes

    • Using the wrong operator. width + height prints 40, and width ** height raises 23 to the power of 17, which is a huge number. Check that you used *.
    • Printing a variable instead of the expression. console.log(width) prints 23. The product has to be computed before it is printed.
    • Putting the expression in quotes. console.log("width * height") prints the words, not the area, because anything inside quotes is text.
    • Forgetting console.log. A line that only says width * height; computes 391 and discards it, so the console stays empty.
    • Typing the numbers directly. console.log(23 * 17) prints the right answer for this data, but it ignores the variables the task gave you.

    Key takeaways

    • The area of a rectangle is width * height.
    • You can pass a whole expression to console.log. It is evaluated first, then printed.
    • Use the variables, not their current values, so the code stays correct if the data changes.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free